File: standalone_browser_extension_app_context_menu.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (209 lines) | stat: -rw-r--r-- 8,477 bytes parent folder | download
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
// Copyright 2021 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/ash/shelf/standalone_browser_extension_app_context_menu.h"

#include <memory>
#include <utility>

#include "ash/public/cpp/app_menu_constants.h"
#include "ash/public/cpp/shelf_model.h"
#include "ash/public/cpp/shelf_types.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/extension_apps_utils.h"
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#include "chrome/browser/ash/app_list/app_list_client_impl.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller_util.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_prefs.h"
#include "chrome/browser/ui/ash/shelf/standalone_browser_extension_app_shelf_item_controller.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/webui/ash/settings/app_management/app_management_uma.h"
#include "chrome/grit/generated_resources.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "ui/base/models/image_model.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/vector_icons.h"

namespace {

// Create an appropriately sized ImageModel for a menu item icon.
ui::ImageModel GetMenuItemIcon(const gfx::VectorIcon& icon) {
  return ui::ImageModel::FromVectorIcon(
      icon,
      /*color_id=*/ui::kColorAshSystemUIMenuIcon, ash::kAppContextMenuIconSize);
}

}  // namespace

StandaloneBrowserExtensionAppContextMenu::
    StandaloneBrowserExtensionAppContextMenu(const std::string& app_id,
                                             Source source)
    : app_id_(app_id), source_(source) {}
StandaloneBrowserExtensionAppContextMenu::
    ~StandaloneBrowserExtensionAppContextMenu() = default;

void StandaloneBrowserExtensionAppContextMenu::GetMenuModel(
    GetMenuModelCallback callback) {
  // Always invoke the callback asynchronously to avoid re-entrancy.
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(&StandaloneBrowserExtensionAppContextMenu::OnGetMenuModel,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void StandaloneBrowserExtensionAppContextMenu::ExecuteCommand(int command_id,
                                                              int event_flags) {
  ash::ShelfModel* model = ash::ShelfModel::Get();
  const int item_index = model->ItemIndexByAppID(app_id_);
  const bool item_in_shelf = item_index >= 0;

  switch (static_cast<ash::CommandId>(command_id)) {
    case ash::SWAP_WITH_NEXT:
      if (!item_in_shelf)
        return;
      model->Swap(item_index, /*with_next=*/true);
      return;
    case ash::SWAP_WITH_PREVIOUS:
      if (!item_in_shelf)
        return;
      model->Swap(item_index, /*with_next=*/false);
      return;
    case ash::TOGGLE_PIN:
      if (item_in_shelf && model->IsAppPinned(app_id_)) {
        UnpinAppWithIDFromShelf(app_id_);
      } else {
        PinAppWithIDToShelf(app_id_);
      }
      return;
    case ash::MENU_CLOSE: {
      // There can only be a single active ash profile when Lacros is running.
      apps::AppServiceProxy* proxy =
          apps::AppServiceProxyFactory::GetForProfile(
              ProfileManager::GetPrimaryUserProfile());
      proxy->StopApp(app_id_);
      return;
    }

    case ash::UNINSTALL: {
      apps::UninstallSource uninstall_source =
          (source_ == Source::kShelf) ? apps::UninstallSource::kShelf
                                      : apps::UninstallSource::kAppList;
      aura::Window* window =
          (source_ == Source::kShelf)
              ? AppListClientImpl::GetInstance()->GetAppListWindow()
              : nullptr;
      apps::AppServiceProxy* proxy =
          apps::AppServiceProxyFactory::GetForProfile(
              ProfileManager::GetPrimaryUserProfile());
      proxy->Uninstall(app_id_, uninstall_source, /*parent_window=*/window);
      return;
    }
    case ash::SHOW_APP_INFO: {
      ash::settings::AppManagementEntryPoint entry =
          (source_ == Source::kShelf) ? ash::settings::AppManagementEntryPoint::
                                            kShelfContextMenuAppInfoChromeApp
                                      : ash::settings::AppManagementEntryPoint::
                                            kAppListContextMenuAppInfoChromeApp;
      chrome::ShowAppManagementPage(
          ProfileManager::GetPrimaryUserProfile(),
          apps::GetEscapedAppId(app_id_,
                                apps::AppType::kStandaloneBrowserChromeApp),
          entry);
      return;
    }
    default:
      return;
  }
}

void StandaloneBrowserExtensionAppContextMenu::OnGetMenuModel(
    GetMenuModelCallback callback) {
  bool allow_uninstall = false;
  bool allow_app_info = false;
  apps::AppServiceProxy* proxy = apps::AppServiceProxyFactory::GetForProfile(
      ProfileManager::GetPrimaryUserProfile());
  proxy->AppRegistryCache().ForOneApp(
      app_id_,
      [&allow_app_info, &allow_uninstall](const apps::AppUpdate& update) {
        allow_app_info = update.ShowInManagement().value_or(false);
        allow_uninstall = update.AllowUninstall().value_or(false);
      });

  bool allow_pin_unpin =
      GetPinnableForAppID(app_id_, ProfileManager::GetPrimaryUserProfile()) ==
      AppListControllerDelegate::PIN_EDITABLE;

  auto menu_model = std::make_unique<ui::SimpleMenuModel>(this);

  ash::ShelfModel* model = ash::ShelfModel::Get();
  const int item_index = model->ItemIndexByAppID(app_id_);
  const bool item_in_shelf = item_index >= 0;

  // Only show commands to reorder shelf items when ChromeVox or SwitchAccess
  // are enabled and the item is in the shelf.
  ash::AccessibilityManager* manager = ash::AccessibilityManager::Get();
  bool show_swap = manager &&
                   (manager->IsSpokenFeedbackEnabled() ||
                    manager->IsSwitchAccessEnabled()) &&
                   item_in_shelf;
  if (show_swap) {
    if (model->CanSwap(item_index, /*with_next=*/true)) {
      menu_model->AddItemWithStringId(ash::SWAP_WITH_NEXT,
                                      IDS_SHELF_CONTEXT_MENU_SWAP_WITH_NEXT);
    }
    if (model->CanSwap(item_index, /*with_next=*/false)) {
      menu_model->AddItemWithStringId(
          ash::SWAP_WITH_PREVIOUS, IDS_SHELF_CONTEXT_MENU_SWAP_WITH_PREVIOUS);
    }
  }

  if (allow_pin_unpin) {
    // This context menu is used by both the shelf and app list. We choose to
    // use the app list IDS string since it's clearer.
    bool currently_pinned = item_in_shelf && model->IsAppPinned(app_id_);
    const gfx::VectorIcon& icon =
        currently_pinned ? views::kUnpinIcon : views::kPinIcon;
    int ids = currently_pinned ? IDS_APP_LIST_CONTEXT_MENU_UNPIN
                               : IDS_APP_LIST_CONTEXT_MENU_PIN;

    menu_model->AddItemWithStringIdAndIcon(ash::TOGGLE_PIN, ids,
                                           GetMenuItemIcon(icon));
  }

  // Add a close menu item if there is at least one open window.
  if (item_in_shelf) {
    ash::ShelfItemDelegate* existing_delegate =
        ash::ShelfModel::Get()->GetShelfItemDelegate(ash::ShelfID(app_id_));
    StandaloneBrowserExtensionAppShelfItemController* controller =
        static_cast<StandaloneBrowserExtensionAppShelfItemController*>(
            existing_delegate);
    if (controller->WindowCount() >= 1) {
      menu_model->AddItemWithStringIdAndIcon(
          ash::MENU_CLOSE, IDS_SHELF_CONTEXT_MENU_CLOSE,
          GetMenuItemIcon(views::kCloseIcon));
    }
  }

  // Add an uninstall menu item.
  if (allow_uninstall) {
    menu_model->AddItemWithStringIdAndIcon(
        ash::UNINSTALL, IDS_APP_LIST_UNINSTALL_ITEM,
        GetMenuItemIcon(views::kUninstallIcon));
  }

  // Add a show app info menu item.
  if (allow_app_info) {
    menu_model->AddItemWithStringIdAndIcon(ash::SHOW_APP_INFO,
                                           IDS_APP_CONTEXT_MENU_SHOW_INFO,
                                           GetMenuItemIcon(views::kInfoIcon));
  }

  // TODO(https://crbug.com/1225848): Custom chrome app context menu items.

  std::move(callback).Run(std::move(menu_model));
}