Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: chrome/browser/extensions/extension_context_menu_model.cc

Issue 476873002: Make hiding an extension action cause it to go to the overflow menu (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/extension_context_menu_model.h" 5 #include "chrome/browser/extensions/extension_context_menu_model.h"
6 6
7 #include "base/prefs/pref_service.h" 7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h" 9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/extensions/active_script_controller.h" 10 #include "chrome/browser/extensions/active_script_controller.h"
(...skipping 15 matching lines...) Expand all
26 #include "chrome/common/pref_names.h" 26 #include "chrome/common/pref_names.h"
27 #include "chrome/common/url_constants.h" 27 #include "chrome/common/url_constants.h"
28 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
29 #include "content/public/common/context_menu_params.h" 29 #include "content/public/common/context_menu_params.h"
30 #include "extensions/browser/extension_prefs.h" 30 #include "extensions/browser/extension_prefs.h"
31 #include "extensions/browser/extension_registry.h" 31 #include "extensions/browser/extension_registry.h"
32 #include "extensions/browser/extension_system.h" 32 #include "extensions/browser/extension_system.h"
33 #include "extensions/browser/management_policy.h" 33 #include "extensions/browser/management_policy.h"
34 #include "extensions/browser/uninstall_reason.h" 34 #include "extensions/browser/uninstall_reason.h"
35 #include "extensions/common/extension.h" 35 #include "extensions/common/extension.h"
36 #include "extensions/common/feature_switch.h"
36 #include "grit/chromium_strings.h" 37 #include "grit/chromium_strings.h"
37 #include "grit/generated_resources.h" 38 #include "grit/generated_resources.h"
38 #include "ui/base/l10n/l10n_util.h" 39 #include "ui/base/l10n/l10n_util.h"
39 40
40 using content::OpenURLParams; 41 using content::OpenURLParams;
41 using content::Referrer; 42 using content::Referrer;
42 using content::WebContents; 43 using content::WebContents;
43 using extensions::Extension; 44 using extensions::Extension;
45 using extensions::ExtensionActionAPI;
46 using extensions::ExtensionPrefs;
44 using extensions::MenuItem; 47 using extensions::MenuItem;
45 using extensions::MenuManager; 48 using extensions::MenuManager;
46 49
47 namespace { 50 namespace {
48 51
49 // Returns true if the given |item| is of the given |type|. 52 // Returns true if the given |item| is of the given |type|.
50 bool MenuItemMatchesAction(ExtensionContextMenuModel::ActionType type, 53 bool MenuItemMatchesAction(ExtensionContextMenuModel::ActionType type,
51 const MenuItem* item) { 54 const MenuItem* item) {
52 if (type == ExtensionContextMenuModel::NO_ACTION) 55 if (type == ExtensionContextMenuModel::NO_ACTION)
53 return false; 56 return false;
54 57
55 const MenuItem::ContextList& contexts = item->contexts(); 58 const MenuItem::ContextList& contexts = item->contexts();
56 59
57 if (contexts.Contains(MenuItem::ALL)) 60 if (contexts.Contains(MenuItem::ALL))
58 return true; 61 return true;
59 if (contexts.Contains(MenuItem::PAGE_ACTION) && 62 if (contexts.Contains(MenuItem::PAGE_ACTION) &&
60 (type == ExtensionContextMenuModel::PAGE_ACTION)) 63 (type == ExtensionContextMenuModel::PAGE_ACTION))
61 return true; 64 return true;
62 if (contexts.Contains(MenuItem::BROWSER_ACTION) && 65 if (contexts.Contains(MenuItem::BROWSER_ACTION) &&
63 (type == ExtensionContextMenuModel::BROWSER_ACTION)) 66 (type == ExtensionContextMenuModel::BROWSER_ACTION))
64 return true; 67 return true;
65 68
66 return false; 69 return false;
67 } 70 }
68 71
72 // Returns the id for the visibility command for the given |extension|, or -1
73 // if none should be shown.
74 int GetVisibilityStringId(Profile* profile, const Extension* extension) {
75 DCHECK(profile);
76 int string_id = -1;
77 if (!extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()) {
78 // Without the toolbar redesign, we only show the visibility toggle for
79 // browser actions, and only give the option to hide.
80 if (extensions::ExtensionActionManager::Get(profile)->GetBrowserAction(
81 *extension)) {
82 string_id = IDS_EXTENSIONS_HIDE_BUTTON;
83 }
84 } else {
85 // With the redesign, we display "show" or "hide" based on the icon's
86 // visibility.
87 bool visible = ExtensionActionAPI::GetBrowserActionVisibility(
88 ExtensionPrefs::Get(profile), extension->id());
89 string_id =
90 visible ? IDS_EXTENSIONS_HIDE_BUTTON : IDS_EXTENSIONS_SHOW_BUTTON;
91 }
92 return string_id;
93 }
94
69 } // namespace 95 } // namespace
70 96
71 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, 97 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension,
72 Browser* browser, 98 Browser* browser,
73 PopupDelegate* delegate) 99 PopupDelegate* delegate)
74 : SimpleMenuModel(this), 100 : SimpleMenuModel(this),
75 extension_id_(extension->id()), 101 extension_id_(extension->id()),
76 browser_(browser), 102 browser_(browser),
77 profile_(browser->profile()), 103 profile_(browser->profile()),
78 delegate_(delegate), 104 delegate_(delegate),
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 if (web_contents) { 196 if (web_contents) {
171 extensions::ActiveScriptController::GetForWebContents(web_contents) 197 extensions::ActiveScriptController::GetForWebContents(web_contents)
172 ->AlwaysRunOnVisibleOrigin(extension); 198 ->AlwaysRunOnVisibleOrigin(extension);
173 } 199 }
174 break; 200 break;
175 } 201 }
176 case CONFIGURE: 202 case CONFIGURE:
177 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); 203 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty());
178 extensions::ExtensionTabUtil::OpenOptionsPage(extension, browser_); 204 extensions::ExtensionTabUtil::OpenOptionsPage(extension, browser_);
179 break; 205 break;
180 case HIDE: { 206 case TOGGLE_VISIBILITY: {
181 extensions::ExtensionActionAPI::SetBrowserActionVisibility( 207 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
182 extensions::ExtensionPrefs::Get(profile_), extension->id(), false); 208 bool visible = ExtensionActionAPI::GetBrowserActionVisibility(
209 prefs, extension->id());
210 ExtensionActionAPI::SetBrowserActionVisibility(
211 prefs, extension->id(), !visible);
183 break; 212 break;
184 } 213 }
185 case UNINSTALL: { 214 case UNINSTALL: {
186 AddRef(); // Balanced in Accepted() and Canceled() 215 AddRef(); // Balanced in Accepted() and Canceled()
187 extension_uninstall_dialog_.reset( 216 extension_uninstall_dialog_.reset(
188 extensions::ExtensionUninstallDialog::Create( 217 extensions::ExtensionUninstallDialog::Create(
189 profile_, browser_->window()->GetNativeWindow(), this)); 218 profile_, browser_->window()->GetNativeWindow(), this));
190 extension_uninstall_dialog_->ConfirmUninstall(extension); 219 extension_uninstall_dialog_->ConfirmUninstall(extension);
191 break; 220 break;
192 } 221 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 // attached to the script injection request icon, but that's okay. 282 // attached to the script injection request icon, but that's okay.
254 WebContents* web_contents = GetActiveWebContents(); 283 WebContents* web_contents = GetActiveWebContents();
255 if (web_contents && 284 if (web_contents &&
256 extensions::ActiveScriptController::GetForWebContents(web_contents) 285 extensions::ActiveScriptController::GetForWebContents(web_contents)
257 ->HasActiveScriptAction(extension)) { 286 ->HasActiveScriptAction(extension)) {
258 AddItemWithStringId(ALWAYS_RUN, IDS_EXTENSIONS_ALWAYS_RUN); 287 AddItemWithStringId(ALWAYS_RUN, IDS_EXTENSIONS_ALWAYS_RUN);
259 } 288 }
260 289
261 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); 290 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM);
262 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); 291 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL));
263 if (extension_action_manager->GetBrowserAction(*extension)) 292
264 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); 293 // Add a toggle visibility (show/hide) if the extension icon is shown on the
294 // toolbar.
295 int visibility_string_id = GetVisibilityStringId(profile_, extension);
296 if (visibility_string_id != -1)
297 AddItemWithStringId(TOGGLE_VISIBILITY, visibility_string_id);
298
265 AddSeparator(ui::NORMAL_SEPARATOR); 299 AddSeparator(ui::NORMAL_SEPARATOR);
266 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); 300 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION);
267 } 301 }
268 302
269 const Extension* ExtensionContextMenuModel::GetExtension() const { 303 const Extension* ExtensionContextMenuModel::GetExtension() const {
270 return extensions::ExtensionRegistry::Get(profile_) 304 return extensions::ExtensionRegistry::Get(profile_)
271 ->enabled_extensions() 305 ->enabled_extensions()
272 .GetByID(extension_id_); 306 .GetByID(extension_id_);
273 } 307 }
274 308
(...skipping 10 matching lines...) Expand all
285 extension_items_count_ = 0; 319 extension_items_count_ = 0;
286 extension_items_->AppendExtensionItems(MenuItem::ExtensionKey(extension_id_), 320 extension_items_->AppendExtensionItems(MenuItem::ExtensionKey(extension_id_),
287 base::string16(), 321 base::string16(),
288 &extension_items_count_, 322 &extension_items_count_,
289 true); // is_action_menu 323 true); // is_action_menu
290 } 324 }
291 325
292 content::WebContents* ExtensionContextMenuModel::GetActiveWebContents() const { 326 content::WebContents* ExtensionContextMenuModel::GetActiveWebContents() const {
293 return browser_->tab_strip_model()->GetActiveWebContents(); 327 return browser_->tab_strip_model()->GetActiveWebContents();
294 } 328 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698