#include "chrome/browser/ui/toolbar/app_menu_model.h"
#include <algorithm>
#include <cmath>
#include <memory>
#include <optional>
#include "base/command_line.h"
#include "base/debug/debugging_buildflags.h"
#include "base/debug/profiler.h"
#include "base/functional/bind.h"
#include "base/i18n/number_formatting.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/media/router/media_router_feature.h"
#include "chrome/browser/prefs/incognito_mode_prefs.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/search/background/ntp_custom_background_service_factory.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/sharing_hub/sharing_hub_features.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/sync/sync_ui_util.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/global_error/global_error.h"
#include "chrome/browser/ui/global_error/global_error_service.h"
#include "chrome/browser/ui/global_error/global_error_service_factory.h"
#include "chrome/browser/ui/hats/trust_safety_sentiment_service_factory.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/lens/lens_overlay_controller.h"
#include "chrome/browser/ui/managed_ui.h"
#include "chrome/browser/ui/profiles/profile_colors_util.h"
#include "chrome/browser/ui/profiles/profile_view_utils.h"
#include "chrome/browser/ui/safety_hub/menu_notification_service_factory.h"
#include "chrome/browser/ui/safety_hub/safety_hub_constants.h"
#include "chrome/browser/ui/safety_hub/safety_hub_hats_service.h"
#include "chrome/browser/ui/safety_hub/safety_hub_hats_service_factory.h"
#include "chrome/browser/ui/startup/default_browser_prompt/default_browser_prompt_manager.h"
#include "chrome/browser/ui/tabs/organization/tab_organization_service_factory.h"
#include "chrome/browser/ui/tabs/organization/tab_organization_utils.h"
#include "chrome/browser/ui/tabs/recent_tabs_sub_menu_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/toolbar/app_menu_icon_controller.h"
#include "chrome/browser/ui/toolbar/bookmark_sub_menu_model.h"
#include "chrome/browser/ui/toolbar/chrome_labs/chrome_labs_model.h"
#include "chrome/browser/ui/toolbar/chrome_labs/chrome_labs_prefs.h"
#include "chrome/browser/ui/toolbar/chrome_labs/chrome_labs_utils.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/side_panel/companion/companion_utils.h"
#include "chrome/browser/ui/web_applications/web_app_dialog_utils.h"
#include "chrome/browser/ui/web_applications/web_app_launch_utils.h"
#include "chrome/browser/ui/webui/side_panel/customize_chrome/customize_chrome_page_handler.h"
#include "chrome/browser/ui/webui/whats_new/whats_new_util.h"
#include "chrome/browser/upgrade_detector/upgrade_detector.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_tab_helper.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/theme_resources.h"
#include "components/bookmarks/common/bookmark_pref_names.h"
#include "components/dom_distiller/content/browser/distillable_page_utils.h"
#include "components/dom_distiller/content/browser/uma_helper.h"
#include "components/dom_distiller/core/dom_distiller_features.h"
#include "components/dom_distiller/core/url_utils.h"
#include "components/feature_engagement/public/event_constants.h"
#include "components/omnibox/browser/vector_icons.h"
#include "components/password_manager/content/common/web_ui_constants.h"
#include "components/password_manager/core/common/password_manager_features.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/prefs/pref_member.h"
#include "components/prefs/pref_service.h"
#include "components/profile_metrics/browser_profile_type.h"
#include "components/saved_tab_groups/features.h"
#include "components/signin/public/base/signin_metrics.h"
#include "components/signin/public/base/signin_pref_names.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "components/webapps/browser/banners/app_banner_manager.h"
#include "components/webapps/browser/banners/install_banner_config.h"
#include "components/webapps/browser/banners/installable_web_app_check_result.h"
#include "components/webapps/browser/banners/web_app_banner_data.h"
#include "components/webapps/common/web_app_id.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/profiling.h"
#include "content/public/common/url_constants.h"
#include "media/base/media_switches.h"
#include "ui/base/accelerators/menu_label_accelerator_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/button_menu_item_model.h"
#include "ui/base/models/image_model.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/text_elider.h"
#if BUILDFLAG(GOOGLE_CHROME_BRANDING) || BUILDFLAG(IS_CHROMEOS_ASH)
#include "base/feature_list.h"
#endif
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#include "chrome/browser/ui/lens/lens_overlay_entry_point_controller.h"
#include "components/lens/lens_features.h"
#endif
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/policy/system_features_disable_list_policy_handler.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "ui/display/screen.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "base/win/shortcut.h"
#include "base/win/windows_version.h"
#include "content/public/browser/gpu_data_manager.h"
#endif
UserMetricsAction;
WebContents;
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kProfileMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kProfileOpenGuestItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kBookmarksMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kTabGroupsMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kDownloadsMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kHistoryMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kExtensionsMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kMoreToolsMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kIncognitoMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel,
kPasswordAndAutofillMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kPasswordManagerMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kShowLensOverlay);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kShowSearchCompanion);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kSaveAndShareMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kCastTitleItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kInstallAppItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel, kCreateShortcutItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(AppMenuModel,
kSetBrowserAsDefaultMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(ToolsMenuModel, kPerformanceMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(ToolsMenuModel, kChromeLabsMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(ToolsMenuModel, kReadingModeMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(ExtensionsMenuModel,
kManageExtensionsMenuItem);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(ExtensionsMenuModel,
kVisitChromeWebStoreMenuItem);
namespace {
void AddItemWithStringIdAndVectorIcon(ui::SimpleMenuModel* model,
int command_id,
int string_id,
const gfx::VectorIcon& vector_icon) { … }
void AddSubMenuWithStringIdAndVectorIcon(ui::SimpleMenuModel* model,
int command_id,
int string_id,
ui::MenuModel* sub_menu,
const gfx::VectorIcon& vector_icon) { … }
std::u16string GetUpgradeDialogTitleText() { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
std::u16string GetLacrosDataMigrationMenuItemName() {
return l10n_util::GetStringUTF16(IDS_LACROS_DATA_MIGRATION_RELAUNCH);
}
#endif
std::u16string GetInstallPWALabel(const Browser* browser) { … }
ui::ImageModel GetInstallPWAIcon(Browser* browser) { … }
std::u16string GetOpenPWALabel(const Browser* browser) { … }
std::u16string GetSyncSectionTitle(Profile* profile,
signin::IdentityManager* identity_manager) { … }
class ProfileSubMenuModel : public ui::SimpleMenuModel,
public ui::SimpleMenuModel::Delegate { … };
ProfileSubMenuModel::ProfileSubMenuModel(
ui::SimpleMenuModel::Delegate* delegate,
Profile* profile,
const ui::ColorProvider* color_provider)
: … { … }
bool ProfileSubMenuModel::IsCommandIdChecked(int command_id) const { … }
bool ProfileSubMenuModel::IsCommandIdEnabled(int command_id) const { … }
bool ProfileSubMenuModel::GetAcceleratorForCommandId(
int command_id,
ui::Accelerator* accelerator) const { … }
void ProfileSubMenuModel::ExecuteCommand(int command_id, int event_flags) { … }
int ProfileSubMenuModel::GetAndIncrementNextMenuID() { … }
bool ProfileSubMenuModel::BuildSyncSection() { … }
class PasswordsAndAutofillSubMenuModel : public ui::SimpleMenuModel { … };
PasswordsAndAutofillSubMenuModel::PasswordsAndAutofillSubMenuModel(
ui::SimpleMenuModel::Delegate* delegate)
: … { … }
class FindAndEditSubMenuModel : public ui::SimpleMenuModel { … };
FindAndEditSubMenuModel::FindAndEditSubMenuModel(
ui::SimpleMenuModel::Delegate* delegate)
: … { … }
class SaveAndShareSubMenuModel : public ui::SimpleMenuModel { … };
SaveAndShareSubMenuModel::SaveAndShareSubMenuModel(
ui::SimpleMenuModel::Delegate* delegate,
Browser* browser)
: … { … }
}
void SetCommandIcon(ui::SimpleMenuModel* model,
int command_id,
const gfx::VectorIcon& vector_icon) { … }
void LogWrenchMenuAction(AppMenuAction action_id) { … }
class HelpMenuModel : public ui::SimpleMenuModel { … };
ToolsMenuModel::ToolsMenuModel(ui::SimpleMenuModel::Delegate* delegate,
Browser* browser)
: … { … }
ToolsMenuModel::~ToolsMenuModel() = default;
void ToolsMenuModel::Build(Browser* browser) { … }
ExtensionsMenuModel::ExtensionsMenuModel(
ui::SimpleMenuModel::Delegate* delegate,
Browser* browser)
: … { … }
ExtensionsMenuModel::~ExtensionsMenuModel() = default;
void ExtensionsMenuModel::Build(Browser* browser) { … }
AppMenuModel::AppMenuModel(ui::AcceleratorProvider* provider,
Browser* browser,
AppMenuIconController* app_menu_icon_controller,
AlertMenuItem alert_item)
: … { … }
AppMenuModel::~AppMenuModel() = default;
void AppMenuModel::SetHighlightedIdentifier(
ui::ElementIdentifier highlighted_menu_identifier) { … }
void AppMenuModel::Init() { … }
bool AppMenuModel::DoesCommandIdDismissMenu(int command_id) const { … }
void AppMenuModel::ExecuteCommand(int command_id, int event_flags) { … }
void AppMenuModel::LogSafetyHubInteractionMetrics(
safety_hub::SafetyHubModuleType sh_module,
int event_flags) { … }
void AppMenuModel::LogMenuMetrics(int command_id) { … }
bool AppMenuModel::IsCommandIdChecked(int command_id) const { … }
bool AppMenuModel::IsCommandIdEnabled(int command_id) const { … }
bool AppMenuModel::IsCommandIdAlerted(int command_id) const { … }
bool AppMenuModel::IsElementIdAlerted(ui::ElementIdentifier element_id) const { … }
bool AppMenuModel::GetAcceleratorForCommandId(
int command_id,
ui::Accelerator* accelerator) const { … }
void AppMenuModel::LogMenuAction(AppMenuAction action_id) { … }
void AppMenuModel::Build() { … }
void AppMenuModel::CreateCutCopyPasteMenu() { … }
void AppMenuModel::CreateFindAndEditSubMenu() { … }
void AppMenuModel::CreateZoomMenu() { … }
bool AppMenuModel::AddGlobalErrorMenuItems() { … }
bool AppMenuModel::AddDefaultBrowserMenuItems() { … }
bool AppMenuModel::AddSafetyHubMenuItem() { … }
#if BUILDFLAG(IS_CHROMEOS)
void AppMenuModel::UpdateSettingsItemState() {
bool is_disabled =
policy::SystemFeaturesDisableListPolicyHandler::IsSystemFeatureDisabled(
policy::SystemFeature::kBrowserSettings,
g_browser_process->local_state());
std::optional<size_t> index = GetIndexOfCommandId(IDC_OPTIONS);
if (index.has_value()) {
SetEnabledAt(index.value(), !is_disabled);
}
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
index = GetIndexOfCommandId(IDC_HELP_MENU);
if (index.has_value()) {
ui::SimpleMenuModel* help_menu =
static_cast<ui::SimpleMenuModel*>(GetSubmenuModelAt(index.value()));
index = help_menu->GetIndexOfCommandId(IDC_ABOUT);
if (index.has_value()) {
help_menu->SetEnabledAt(index.value(), !is_disabled);
}
}
#else
index = GetIndexOfCommandId(IDC_ABOUT);
if (index.has_value()) {
SetEnabledAt(index.value(), !is_disabled);
}
#endif
}
#endif