File: web_app_menu_model.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (225 lines) | stat: -rw-r--r-- 8,648 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/web_applications/web_app_menu_model.h"

#include "base/feature_list.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/media/router/media_router_feature.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/extensions/extensions_container.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/common/chrome_features.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/omnibox/browser/location_bar_model.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "ui/base/accelerators/menu_label_accelerator_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/image_model.h"
#include "ui/base/ui_base_features.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/strings/grit/chromeos_strings.h"
#include "chromeos/ui/frame/desks/move_to_desks_menu_delegate.h"
#include "chromeos/ui/frame/desks/move_to_desks_menu_model.h"
#include "ui/views/widget/widget.h"
#endif

namespace {

bool ShouldAllowOpenInChrome(Browser* browser) {
  // Isolated Web Apps shouldn't be opened in Chrome.
  const bool is_isolated_web_app =
      browser->app_controller() &&
      browser->app_controller()->IsIsolatedWebApp();
  // Web Apps with enabled prevent close shouldn't be opened in Chrome.
  const bool prevent_close_enabled =
      browser->app_controller() &&
      browser->app_controller()->IsPreventCloseEnabled();
  return !is_isolated_web_app && !prevent_close_enabled;
}

}  // namespace

constexpr int WebAppMenuModel::kUninstallAppCommandId;
constexpr int WebAppMenuModel::kExtensionsMenuCommandId;

WebAppMenuModel::WebAppMenuModel(ui::AcceleratorProvider* provider,
                                 Browser* browser)
    : AppMenuModel(provider, browser) {}

WebAppMenuModel::~WebAppMenuModel() = default;

bool WebAppMenuModel::IsCommandIdEnabled(int command_id) const {
  switch (command_id) {
    case kUninstallAppCommandId:
      return browser()->app_controller()->CanUserUninstall();
    case kExtensionsMenuCommandId:
      return base::FeatureList::IsEnabled(
                 features::kDesktopPWAsElidedExtensionsMenu) &&
             browser()->window()->GetExtensionsContainer() &&
             browser()
                 ->window()
                 ->GetExtensionsContainer()
                 ->HasAnyExtensions() &&
             // Extensions are not supported inside Isolated Web Apps.
             !browser()->app_controller()->IsIsolatedWebApp();
    case IDC_OPEN_IN_CHROME: {
      return ShouldAllowOpenInChrome(browser());
    }
#if BUILDFLAG(IS_CHROMEOS)
    case chromeos::MoveToDesksMenuModel::kMenuCommandId:
      return chromeos::MoveToDesksMenuDelegate::ShouldShowMoveToDesksMenu(
          browser()->window()->GetNativeWindow());
#endif
    default:
      return AppMenuModel::IsCommandIdEnabled(command_id);
  }
}

bool WebAppMenuModel::IsCommandIdVisible(int command_id) const {
  switch (command_id) {
    case IDC_OPEN_IN_CHROME:
      return ShouldAllowOpenInChrome(browser());
#if BUILDFLAG(IS_CHROMEOS)
    case chromeos::MoveToDesksMenuModel::kMenuCommandId:
      return chromeos::MoveToDesksMenuDelegate::ShouldShowMoveToDesksMenu(
          browser()->window()->GetNativeWindow());
#endif
    default:
      return AppMenuModel::IsCommandIdVisible(command_id);
  }
}

void WebAppMenuModel::ExecuteCommand(int command_id, int event_flags) {
  switch (command_id) {
    case kUninstallAppCommandId:
      LogMenuAction(MENU_ACTION_UNINSTALL_APP);
      browser()->app_controller()->Uninstall(
          webapps::WebappUninstallSource::kAppMenu);
      break;
    case kExtensionsMenuCommandId:
      browser()->window()->GetExtensionsContainer()->ToggleExtensionsMenu();
      break;
    case IDC_OPEN_IN_CHROME:
      if (ShouldAllowOpenInChrome(browser())) {
        AppMenuModel::ExecuteCommand(command_id, event_flags);
      }
      break;
    default:
      AppMenuModel::ExecuteCommand(command_id, event_flags);
      break;
  }
}

void WebAppMenuModel::Build() {
  AddItemWithStringId(IDC_WEB_APP_MENU_APP_INFO,
                      IDS_APP_CONTEXT_MENU_SHOW_INFO);
  size_t app_info_index = GetItemCount() - 1;

  CHECK(browser());
  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();

  bool is_isolated_web_app = browser()->app_controller() &&
                             browser()->app_controller()->IsIsolatedWebApp();

  if (web_contents) {
    std::u16string display_text =
        web_app::AppBrowserController::FormatUrlOrigin(
            web_contents->GetVisibleURL());

    // For Isolated Web Apps the origin's host name is a non-human-readable
    // string of characters, so instead of displaying the origin, the short name
    // of the app will be displayed.
    if (is_isolated_web_app) {
      std::u16string short_name =
          browser()->app_controller()->GetAppShortName();
      // For Isolated Web Apps, |GetAppShortName()| must be non-empty.
      display_text = short_name;
    }
    SetMinorText(app_info_index, display_text);
  }

  SetIcon(app_info_index,
          ui::ImageModel::FromVectorIcon(
              browser()->GetFeatures().location_bar_model()->GetVectorIcon()));

  AddSeparator(ui::NORMAL_SEPARATOR);

  if (IsCommandIdEnabled(kExtensionsMenuCommandId)) {
    AddItemWithStringId(kExtensionsMenuCommandId, IDS_SHOW_EXTENSIONS);
    AddSeparator(ui::NORMAL_SEPARATOR);
  }

  if (browser()->app_controller() &&
      browser()->app_controller()->has_tab_strip() &&
      !browser()->app_controller()->ShouldHideNewTabButton()) {
    AddItemWithStringId(IDC_NEW_TAB, IDS_NEW_TAB);
  }
  AddItemWithStringId(IDC_COPY_URL, IDS_COPY_URL);

  AddItemWithStringId(IDC_OPEN_IN_CHROME, IDS_OPEN_IN_CHROME);

#if BUILDFLAG(IS_CHROMEOS)
  if (chromeos::MoveToDesksMenuDelegate::ShouldShowMoveToDesksMenu(
          browser()->window()->GetNativeWindow())) {
    AddSeparator(ui::NORMAL_SEPARATOR);
    move_to_desks_submenu_ = std::make_unique<chromeos::MoveToDesksMenuModel>(
        std::make_unique<chromeos::MoveToDesksMenuDelegate>(
            views::Widget::GetWidgetForNativeWindow(
                browser()->window()->GetNativeWindow())));
    AddSubMenuWithStringId(chromeos::MoveToDesksMenuModel::kMenuCommandId,
                           IDS_MOVE_TO_DESKS_MENU,
                           move_to_desks_submenu_.get());
  }
#endif

// Chrome OS's app list is prominent enough to not need a separate uninstall
// option in the app menu.
#if !BUILDFLAG(IS_CHROMEOS)
  DCHECK(browser()->app_controller());
  if (browser()->app_controller()->IsInstalled()) {
    AddSeparator(ui::NORMAL_SEPARATOR);
    AddItem(kUninstallAppCommandId,
            l10n_util::GetStringFUTF16(
                IDS_UNINSTALL_FROM_OS_LAUNCH_SURFACE,
                ui::EscapeMenuLabelAmpersands(
                    browser()->app_controller()->GetAppShortName())));
  }
#endif  // !BUILDFLAG(IS_CHROMEOS)
  AddSeparator(ui::NORMAL_SEPARATOR);
  CreateZoomMenu();
  AddSeparator(ui::NORMAL_SEPARATOR);
  AddItemWithStringId(IDC_PRINT, IDS_PRINT);
  CreateFindAndEditSubMenu();

  if (media_router::MediaRouterEnabled(browser()->profile())) {
    AddItemWithStringId(IDC_ROUTE_MEDIA, IDS_MEDIA_ROUTER_MENU_ITEM_TITLE);
  }

  SetCommandIcon(this, kExtensionsMenuCommandId,
                 vector_icons::kExtensionChromeRefreshIcon);
  SetCommandIcon(this, kUninstallAppCommandId, kTrashCanRefreshIcon);
  SetCommandIcon(this, IDC_NEW_TAB, kNewTabRefreshIcon);
  SetCommandIcon(this, IDC_COPY_URL, kLinkChromeRefreshIcon);
  SetCommandIcon(this, IDC_OPEN_IN_CHROME, kBrowserLogoIcon);
  SetCommandIcon(this, IDC_ZOOM_MENU, kZoomInIcon);
  SetCommandIcon(this, IDC_PRINT, kPrintMenuIcon);
  SetCommandIcon(this, IDC_FIND_AND_EDIT_MENU, kSearchMenuIcon);
  SetCommandIcon(this, IDC_ROUTE_MEDIA, kCastChromeRefreshIcon);
}