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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_context_menu_model.h"
#include "base/macros.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/extensions/context_menu_matcher.h"
#include "chrome/browser/extensions/extension_action.h"
#include "chrome/browser/extensions/extension_action_manager.h"
#include "chrome/browser/extensions/extension_action_runner.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/extensions/extension_uninstall_dialog.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/extensions/menu_manager.h"
#include "chrome/browser/extensions/scripting_permissions_modifier.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/session_tab_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/theme_resources.h"
#include "components/grit/components_scaled_resources.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/context_menu_params.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/management_policy.h"
#include "extensions/browser/uninstall_reason.h"
#include "extensions/common/extension.h"
#include "extensions/common/feature_switch.h"
#include "extensions/common/manifest_handlers/options_page_info.h"
#include "extensions/common/manifest_url_handlers.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/vector_icons_public.h"
namespace extensions {
namespace {
// Returns true if the given |item| is of the given |type|.
bool MenuItemMatchesAction(ExtensionContextMenuModel::ActionType type,
const MenuItem* item) {
if (type == ExtensionContextMenuModel::NO_ACTION)
return false;
const MenuItem::ContextList& contexts = item->contexts();
if (contexts.Contains(MenuItem::ALL))
return true;
if (contexts.Contains(MenuItem::PAGE_ACTION) &&
(type == ExtensionContextMenuModel::PAGE_ACTION))
return true;
if (contexts.Contains(MenuItem::BROWSER_ACTION) &&
(type == ExtensionContextMenuModel::BROWSER_ACTION))
return true;
return false;
}
// Returns the id for the visibility command for the given |extension|, or -1
// if none should be shown.
int GetVisibilityStringId(
Profile* profile,
const Extension* extension,
ExtensionContextMenuModel::ButtonVisibility button_visibility) {
DCHECK(profile);
int string_id = -1;
if (!FeatureSwitch::extension_action_redesign()->IsEnabled()) {
// Without the toolbar redesign, we only show the visibility toggle for
// browser actions, and only give the option to hide.
if (ExtensionActionManager::Get(profile)->GetBrowserAction(*extension)) {
string_id = IDS_EXTENSIONS_HIDE_BUTTON;
}
} else {
// With the redesign, we display "show" or "hide" based on the icon's
// visibility, and can have "transitively shown" buttons that are shown
// only while the button has a popup or menu visible.
switch (button_visibility) {
case (ExtensionContextMenuModel::VISIBLE):
string_id = IDS_EXTENSIONS_HIDE_BUTTON_IN_MENU;
break;
case (ExtensionContextMenuModel::TRANSITIVELY_VISIBLE):
string_id = IDS_EXTENSIONS_KEEP_BUTTON_IN_TOOLBAR;
break;
case (ExtensionContextMenuModel::OVERFLOWED):
string_id = IDS_EXTENSIONS_SHOW_BUTTON_IN_TOOLBAR;
break;
}
}
return string_id;
}
// Returns true if the given |extension| is required to remain installed by
// policy.
bool IsExtensionRequiredByPolicy(const Extension* extension,
Profile* profile) {
ManagementPolicy* policy = ExtensionSystem::Get(profile)->management_policy();
return !policy->UserMayModifySettings(extension, nullptr) ||
policy->MustRemainInstalled(extension, nullptr);
}
// A stub for the uninstall dialog.
// TODO(devlin): Ideally, we would just have the uninstall dialog take a
// base::Callback, but that's a bunch of churn.
class UninstallDialogHelper : public ExtensionUninstallDialog::Delegate {
public:
// Kicks off the asynchronous process to confirm and uninstall the given
// |extension|.
static void UninstallExtension(Browser* browser, const Extension* extension) {
UninstallDialogHelper* helper = new UninstallDialogHelper();
helper->BeginUninstall(browser, extension);
}
private:
// This class handles its own lifetime.
UninstallDialogHelper() {}
~UninstallDialogHelper() override {}
void BeginUninstall(Browser* browser, const Extension* extension) {
uninstall_dialog_.reset(ExtensionUninstallDialog::Create(
browser->profile(), browser->window()->GetNativeWindow(), this));
uninstall_dialog_->ConfirmUninstall(extension,
UNINSTALL_REASON_USER_INITIATED,
UNINSTALL_SOURCE_TOOLBAR_CONTEXT_MENU);
}
// ExtensionUninstallDialog::Delegate:
void OnExtensionUninstallDialogClosed(bool did_start_uninstall,
const base::string16& error) override {
delete this;
}
std::unique_ptr<ExtensionUninstallDialog> uninstall_dialog_;
DISALLOW_COPY_AND_ASSIGN(UninstallDialogHelper);
};
} // namespace
ExtensionContextMenuModel::ExtensionContextMenuModel(
const Extension* extension,
Browser* browser,
ButtonVisibility button_visibility,
PopupDelegate* delegate)
: SimpleMenuModel(this),
extension_id_(extension->id()),
is_component_(Manifest::IsComponentLocation(extension->location())),
browser_(browser),
profile_(browser->profile()),
delegate_(delegate),
action_type_(NO_ACTION),
button_visibility_(button_visibility) {
InitMenu(extension, button_visibility);
}
bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const {
const Extension* extension = GetExtension();
if (!extension)
return false;
if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST)
return extension_items_->IsCommandIdChecked(command_id);
if (command_id == PAGE_ACCESS_RUN_ON_CLICK ||
command_id == PAGE_ACCESS_RUN_ON_SITE ||
command_id == PAGE_ACCESS_RUN_ON_ALL_SITES) {
content::WebContents* web_contents = GetActiveWebContents();
return web_contents &&
GetCurrentPageAccess(extension, web_contents) == command_id;
}
return false;
}
bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const {
const Extension* extension = GetExtension();
if (!extension)
return false;
if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
return extension_items_->IsCommandIdEnabled(command_id);
}
switch (command_id) {
case NAME:
// The NAME links to the Homepage URL. If the extension doesn't have a
// homepage, we just disable this menu item. We also disable for component
// extensions, because it doesn't make sense to link to a webstore page or
// chrome://extensions.
return ManifestURL::GetHomepageURL(extension).is_valid() &&
!is_component_;
case CONFIGURE:
return OptionsPageInfo::HasOptionsPage(extension);
case INSPECT_POPUP: {
content::WebContents* web_contents = GetActiveWebContents();
return web_contents && extension_action_ &&
extension_action_->HasPopup(
SessionTabHelper::IdForTab(web_contents));
}
case UNINSTALL:
return !IsExtensionRequiredByPolicy(extension, profile_);
// The following, if they are present, are always enabled.
case TOGGLE_VISIBILITY:
case MANAGE:
case PAGE_ACCESS_SUBMENU:
case PAGE_ACCESS_RUN_ON_CLICK:
case PAGE_ACCESS_RUN_ON_SITE:
case PAGE_ACCESS_RUN_ON_ALL_SITES:
return true;
default:
NOTREACHED() << "Unknown command" << command_id;
}
return true;
}
void ExtensionContextMenuModel::ExecuteCommand(int command_id,
int event_flags) {
const Extension* extension = GetExtension();
if (!extension)
return;
if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
DCHECK(extension_items_);
extension_items_->ExecuteCommand(command_id, GetActiveWebContents(),
nullptr, content::ContextMenuParams());
return;
}
switch (command_id) {
case NAME: {
content::OpenURLParams params(ManifestURL::GetHomepageURL(extension),
content::Referrer(),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui::PAGE_TRANSITION_LINK, false);
browser_->OpenURL(params);
break;
}
case CONFIGURE:
DCHECK(OptionsPageInfo::HasOptionsPage(extension));
ExtensionTabUtil::OpenOptionsPage(extension, browser_);
break;
case TOGGLE_VISIBILITY: {
bool currently_visible = button_visibility_ == VISIBLE;
// Without the toolbar redesign turned on, action visibility refers to
// any action presence in the toolbar, independent of whether the action
// is visible or overflowed. So any action present is considered visible.
if (!FeatureSwitch::extension_action_redesign()->IsEnabled())
currently_visible = true;
ToolbarActionsModel::Get(browser_->profile())
->SetActionVisibility(extension->id(), !currently_visible);
break;
}
case UNINSTALL: {
UninstallDialogHelper::UninstallExtension(browser_, extension);
break;
}
case MANAGE: {
chrome::ShowExtensions(browser_, extension->id());
break;
}
case INSPECT_POPUP: {
delegate_->InspectPopup();
break;
}
case PAGE_ACCESS_RUN_ON_CLICK:
case PAGE_ACCESS_RUN_ON_SITE:
case PAGE_ACCESS_RUN_ON_ALL_SITES:
HandlePageAccessCommand(command_id, extension);
break;
default:
NOTREACHED() << "Unknown option";
break;
}
}
ExtensionContextMenuModel::~ExtensionContextMenuModel() {}
void ExtensionContextMenuModel::InitMenu(const Extension* extension,
ButtonVisibility button_visibility) {
DCHECK(extension);
extension_action_ =
ExtensionActionManager::Get(profile_)->GetExtensionAction(*extension);
if (extension_action_) {
action_type_ = extension_action_->action_type() == ActionInfo::TYPE_PAGE
? PAGE_ACTION
: BROWSER_ACTION;
}
extension_items_.reset(new ContextMenuMatcher(
profile_, this, this, base::Bind(MenuItemMatchesAction, action_type_)));
std::string extension_name = extension->name();
// Ampersands need to be escaped to avoid being treated like
// mnemonics in the menu.
base::ReplaceChars(extension_name, "&", "&&", &extension_name);
AddItem(NAME, base::UTF8ToUTF16(extension_name));
AppendExtensionItems();
AddSeparator(ui::NORMAL_SEPARATOR);
CreatePageAccessSubmenu(extension);
if (!is_component_ || OptionsPageInfo::HasOptionsPage(extension))
AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM);
if (!is_component_) {
bool is_required_by_policy =
IsExtensionRequiredByPolicy(extension, profile_);
int message_id = is_required_by_policy ?
IDS_EXTENSIONS_INSTALLED_BY_ADMIN : IDS_EXTENSIONS_UNINSTALL;
AddItem(UNINSTALL, l10n_util::GetStringUTF16(message_id));
if (is_required_by_policy) {
int uninstall_index = GetIndexOfCommandId(UNINSTALL);
SetIcon(uninstall_index,
gfx::Image(gfx::CreateVectorIcon(gfx::VectorIconId::BUSINESS, 16,
gfx::kChromeIconGrey)));
}
}
// Add a toggle visibility (show/hide) if the extension icon is shown on the
// toolbar.
int visibility_string_id =
GetVisibilityStringId(profile_, extension, button_visibility);
if (visibility_string_id != -1)
AddItemWithStringId(TOGGLE_VISIBILITY, visibility_string_id);
if (!is_component_) {
AddSeparator(ui::NORMAL_SEPARATOR);
AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION);
}
const ActionInfo* action_info = ActionInfo::GetPageActionInfo(extension);
if (!action_info)
action_info = ActionInfo::GetBrowserActionInfo(extension);
if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) &&
delegate_ && !is_component_ && action_info && !action_info->synthesized) {
AddSeparator(ui::NORMAL_SEPARATOR);
AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP);
}
}
const Extension* ExtensionContextMenuModel::GetExtension() const {
return ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID(
extension_id_);
}
void ExtensionContextMenuModel::AppendExtensionItems() {
MenuManager* menu_manager = MenuManager::Get(profile_);
if (!menu_manager || // Null in unit tests
!menu_manager->MenuItems(MenuItem::ExtensionKey(extension_id_)))
return;
AddSeparator(ui::NORMAL_SEPARATOR);
int index = 0;
extension_items_->AppendExtensionItems(MenuItem::ExtensionKey(extension_id_),
base::string16(), &index,
true); // is_action_menu
}
ExtensionContextMenuModel::MenuEntries
ExtensionContextMenuModel::GetCurrentPageAccess(
const Extension* extension,
content::WebContents* web_contents) const {
ScriptingPermissionsModifier modifier(profile_, extension);
DCHECK(modifier.HasAffectedExtension());
if (modifier.IsAllowedOnAllUrls())
return PAGE_ACCESS_RUN_ON_ALL_SITES;
if (modifier.HasGrantedHostPermission(
GetActiveWebContents()->GetLastCommittedURL()))
return PAGE_ACCESS_RUN_ON_SITE;
return PAGE_ACCESS_RUN_ON_CLICK;
}
void ExtensionContextMenuModel::CreatePageAccessSubmenu(
const Extension* extension) {
content::WebContents* web_contents = GetActiveWebContents();
if (!web_contents ||
!ScriptingPermissionsModifier(profile_, extension)
.HasAffectedExtension()) {
return;
}
page_access_submenu_.reset(new ui::SimpleMenuModel(this));
const int kRadioGroup = 0;
page_access_submenu_->AddRadioItemWithStringId(
PAGE_ACCESS_RUN_ON_CLICK,
IDS_EXTENSIONS_CONTEXT_MENU_PAGE_ACCESS_RUN_ON_CLICK, kRadioGroup);
page_access_submenu_->AddRadioItem(
PAGE_ACCESS_RUN_ON_SITE,
l10n_util::GetStringFUTF16(
IDS_EXTENSIONS_CONTEXT_MENU_PAGE_ACCESS_RUN_ON_SITE,
base::UTF8ToUTF16(
web_contents->GetLastCommittedURL().GetOrigin().spec())),
kRadioGroup);
page_access_submenu_->AddRadioItemWithStringId(
PAGE_ACCESS_RUN_ON_ALL_SITES,
IDS_EXTENSIONS_CONTEXT_MENU_PAGE_ACCESS_RUN_ON_ALL_SITES, kRadioGroup);
AddSubMenuWithStringId(PAGE_ACCESS_SUBMENU,
IDS_EXTENSIONS_CONTEXT_MENU_PAGE_ACCESS,
page_access_submenu_.get());
}
void ExtensionContextMenuModel::HandlePageAccessCommand(
int command_id,
const Extension* extension) const {
content::WebContents* web_contents = GetActiveWebContents();
if (!web_contents)
return;
MenuEntries current_access = GetCurrentPageAccess(extension, web_contents);
if (command_id == current_access)
return;
const GURL& url = web_contents->GetLastCommittedURL();
ScriptingPermissionsModifier modifier(profile_, extension);
DCHECK(modifier.HasAffectedExtension());
switch (command_id) {
case PAGE_ACCESS_RUN_ON_CLICK:
if (current_access == PAGE_ACCESS_RUN_ON_ALL_SITES)
modifier.SetAllowedOnAllUrls(false);
if (modifier.HasGrantedHostPermission(url))
modifier.RemoveGrantedHostPermission(url);
break;
case PAGE_ACCESS_RUN_ON_SITE:
if (current_access == PAGE_ACCESS_RUN_ON_ALL_SITES)
modifier.SetAllowedOnAllUrls(false);
if (!modifier.HasGrantedHostPermission(url))
modifier.GrantHostPermission(url);
break;
case PAGE_ACCESS_RUN_ON_ALL_SITES:
modifier.SetAllowedOnAllUrls(true);
break;
default:
NOTREACHED();
}
if (command_id == PAGE_ACCESS_RUN_ON_SITE ||
command_id == PAGE_ACCESS_RUN_ON_ALL_SITES) {
ExtensionActionRunner* runner =
ExtensionActionRunner::GetForWebContents(web_contents);
if (runner && runner->WantsToRun(extension))
runner->RunBlockedActions(extension);
}
}
content::WebContents* ExtensionContextMenuModel::GetActiveWebContents() const {
return browser_->tab_strip_model()->GetActiveWebContents();
}
} // namespace extensions
|