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

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

Issue 396033002: Support "always allow" for runtime script execution (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactoring, minor changes 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/browser/extensions/active_script_controller.h"
9 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" 10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
10 #include "chrome/browser/extensions/extension_action.h" 11 #include "chrome/browser/extensions/extension_action.h"
11 #include "chrome/browser/extensions/extension_action_manager.h" 12 #include "chrome/browser/extensions/extension_action_manager.h"
12 #include "chrome/browser/extensions/extension_service.h" 13 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_tab_util.h" 14 #include "chrome/browser/extensions/extension_tab_util.h"
14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h" 16 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_window.h" 17 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/chrome_pages.h" 18 #include "chrome/browser/ui/chrome_pages.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" 19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/extensions/extension_constants.h" 20 #include "chrome/common/extensions/extension_constants.h"
20 #include "chrome/common/extensions/manifest_url_handler.h" 21 #include "chrome/common/extensions/manifest_url_handler.h"
21 #include "chrome/common/pref_names.h" 22 #include "chrome/common/pref_names.h"
22 #include "chrome/common/url_constants.h" 23 #include "chrome/common/url_constants.h"
23 #include "content/public/browser/web_contents.h" 24 #include "content/public/browser/web_contents.h"
24 #include "extensions/browser/extension_prefs.h" 25 #include "extensions/browser/extension_prefs.h"
25 #include "extensions/browser/extension_system.h" 26 #include "extensions/browser/extension_system.h"
26 #include "extensions/browser/management_policy.h" 27 #include "extensions/browser/management_policy.h"
27 #include "extensions/browser/uninstall_reason.h" 28 #include "extensions/browser/uninstall_reason.h"
28 #include "extensions/common/extension.h" 29 #include "extensions/common/extension.h"
30 #include "extensions/common/manifest_constants.h"
29 #include "grit/chromium_strings.h" 31 #include "grit/chromium_strings.h"
30 #include "grit/generated_resources.h" 32 #include "grit/generated_resources.h"
31 #include "ui/base/l10n/l10n_util.h" 33 #include "ui/base/l10n/l10n_util.h"
32 34
33 using content::OpenURLParams; 35 using content::OpenURLParams;
34 using content::Referrer; 36 using content::Referrer;
35 using content::WebContents; 37 using content::WebContents;
36 using extensions::Extension; 38 using extensions::Extension;
37 39
38 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, 40 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 return false; 74 return false;
73 75
74 if (command_id == CONFIGURE) { 76 if (command_id == CONFIGURE) {
75 return 77 return
76 extensions::ManifestURL::GetOptionsPage(extension).spec().length() > 0; 78 extensions::ManifestURL::GetOptionsPage(extension).spec().length() > 0;
77 } else if (command_id == NAME) { 79 } else if (command_id == NAME) {
78 // The NAME links to the Homepage URL. If the extension doesn't have a 80 // The NAME links to the Homepage URL. If the extension doesn't have a
79 // homepage, we just disable this menu item. 81 // homepage, we just disable this menu item.
80 return extensions::ManifestURL::GetHomepageURL(extension).is_valid(); 82 return extensions::ManifestURL::GetHomepageURL(extension).is_valid();
81 } else if (command_id == INSPECT_POPUP) { 83 } else if (command_id == INSPECT_POPUP) {
82 WebContents* web_contents = 84 WebContents* web_contents = GetActiveWebContents();
83 browser_->tab_strip_model()->GetActiveWebContents();
84 if (!web_contents) 85 if (!web_contents)
85 return false; 86 return false;
86 87
87 return extension_action_ && 88 return extension_action_ &&
88 extension_action_->HasPopup(SessionID::IdForTab(web_contents)); 89 extension_action_->HasPopup(SessionID::IdForTab(web_contents));
89 } else if (command_id == UNINSTALL) { 90 } else if (command_id == UNINSTALL) {
90 // Some extension types can not be uninstalled. 91 // Some extension types can not be uninstalled.
91 return extensions::ExtensionSystem::Get( 92 return extensions::ExtensionSystem::Get(
92 profile_)->management_policy()->UserMayModifySettings(extension, NULL); 93 profile_)->management_policy()->UserMayModifySettings(extension, NULL);
93 } 94 }
(...skipping 12 matching lines...) Expand all
106 return; 107 return;
107 108
108 switch (command_id) { 109 switch (command_id) {
109 case NAME: { 110 case NAME: {
110 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), 111 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension),
111 Referrer(), NEW_FOREGROUND_TAB, 112 Referrer(), NEW_FOREGROUND_TAB,
112 content::PAGE_TRANSITION_LINK, false); 113 content::PAGE_TRANSITION_LINK, false);
113 browser_->OpenURL(params); 114 browser_->OpenURL(params);
114 break; 115 break;
115 } 116 }
117 case ALWAYS_RUN: {
118 WebContents* web_contents = GetActiveWebContents();
119 if (web_contents) {
120 extensions::ActiveScriptController::GetForWebContents(web_contents)
121 ->AlwaysRunOnVisibleHost(extension);
122 }
123 break;
124 }
116 case CONFIGURE: 125 case CONFIGURE:
117 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); 126 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty());
118 extensions::ExtensionTabUtil::OpenOptionsPage(extension, browser_); 127 extensions::ExtensionTabUtil::OpenOptionsPage(extension, browser_);
119 break; 128 break;
120 case HIDE: { 129 case HIDE: {
121 extensions::ExtensionActionAPI::SetBrowserActionVisibility( 130 extensions::ExtensionActionAPI::SetBrowserActionVisibility(
122 extensions::ExtensionPrefs::Get(profile_), extension->id(), false); 131 extensions::ExtensionPrefs::Get(profile_), extension->id(), false);
123 break; 132 break;
124 } 133 }
125 case UNINSTALL: { 134 case UNINSTALL: {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 extension_action_ = extension_action_manager->GetBrowserAction(*extension); 179 extension_action_ = extension_action_manager->GetBrowserAction(*extension);
171 if (!extension_action_) 180 if (!extension_action_)
172 extension_action_ = extension_action_manager->GetPageAction(*extension); 181 extension_action_ = extension_action_manager->GetPageAction(*extension);
173 182
174 std::string extension_name = extension->name(); 183 std::string extension_name = extension->name();
175 // Ampersands need to be escaped to avoid being treated like 184 // Ampersands need to be escaped to avoid being treated like
176 // mnemonics in the menu. 185 // mnemonics in the menu.
177 base::ReplaceChars(extension_name, "&", "&&", &extension_name); 186 base::ReplaceChars(extension_name, "&", "&&", &extension_name);
178 AddItem(NAME, base::UTF8ToUTF16(extension_name)); 187 AddItem(NAME, base::UTF8ToUTF16(extension_name));
179 AddSeparator(ui::NORMAL_SEPARATOR); 188 AddSeparator(ui::NORMAL_SEPARATOR);
189
190 // Add the "Always Allow" item for adding persisted permissions for script
191 // injections if there is an active action for this extension. Note that this
192 // will add it to *all* extension action context menus, not just the one
193 // attached to the script injection request icon, but that's okay.
194 WebContents* web_contents = GetActiveWebContents();
195 if (web_contents &&
196 extensions::ActiveScriptController::GetForWebContents(web_contents)
197 ->HasActiveScriptAction(extension)) {
198 AddItemWithStringId(ALWAYS_RUN, IDS_EXTENSIONS_ALWAYS_RUN);
199 }
200
180 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); 201 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM);
181 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); 202 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL));
182 if (extension_action_manager->GetBrowserAction(*extension)) 203 if (extension_action_manager->GetBrowserAction(*extension))
183 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); 204 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON);
184 AddSeparator(ui::NORMAL_SEPARATOR); 205 AddSeparator(ui::NORMAL_SEPARATOR);
185 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); 206 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION);
186 } 207 }
187 208
188 const Extension* ExtensionContextMenuModel::GetExtension() const { 209 const Extension* ExtensionContextMenuModel::GetExtension() const {
189 ExtensionService* extension_service = 210 ExtensionService* extension_service =
190 extensions::ExtensionSystem::Get(profile_)->extension_service(); 211 extensions::ExtensionSystem::Get(profile_)->extension_service();
191 return extension_service->GetExtensionById(extension_id_, false); 212 return extension_service->GetExtensionById(extension_id_, false);
192 } 213 }
214
215 content::WebContents* ExtensionContextMenuModel::GetActiveWebContents() const {
216 return browser_->tab_strip_model()->GetActiveWebContents();
217 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698