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

Side by Side Diff: chrome/browser/extensions/api/extension_action/extension_action_api.cc

Issue 1804123003: [Extensions] Refactor extension action execution (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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/api/extension_action/extension_action_api.h" 5 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 if (GetBrowserActionVisibility(extension_id) == visible) 163 if (GetBrowserActionVisibility(extension_id) == visible)
164 return; 164 return;
165 165
166 GetExtensionPrefs()->UpdateExtensionPref(extension_id, 166 GetExtensionPrefs()->UpdateExtensionPref(extension_id,
167 kBrowserActionVisible, 167 kBrowserActionVisible,
168 new base::FundamentalValue(visible)); 168 new base::FundamentalValue(visible));
169 FOR_EACH_OBSERVER(Observer, observers_, OnExtensionActionVisibilityChanged( 169 FOR_EACH_OBSERVER(Observer, observers_, OnExtensionActionVisibilityChanged(
170 extension_id, visible)); 170 extension_id, visible));
171 } 171 }
172 172
173 ExtensionAction::ShowAction ExtensionActionAPI::ExecuteExtensionAction(
Devlin 2016/03/16 13:21:16 Pretty much just moved to ExtensionActionRunner::R
174 const Extension* extension,
175 Browser* browser,
176 bool grant_active_tab_permissions) {
177 content::WebContents* web_contents =
178 browser->tab_strip_model()->GetActiveWebContents();
179 if (!web_contents)
180 return ExtensionAction::ACTION_NONE;
181
182 int tab_id = SessionTabHelper::IdForTab(web_contents);
183
184 ExtensionActionRunner* action_runner =
185 ExtensionActionRunner::GetForWebContents(web_contents);
186 bool has_pending_scripts = false;
187 if (action_runner && action_runner->WantsToRun(extension))
188 has_pending_scripts = true;
189
190 // Grant active tab if appropriate.
191 if (grant_active_tab_permissions) {
192 TabHelper::FromWebContents(web_contents)->active_tab_permission_granter()->
193 GrantIfRequested(extension);
194 }
195
196 // If this was a request to run a script, it will have been run once active
197 // tab was granted. Return without executing the action, since we should only
198 // run pending scripts OR the extension action, not both.
199 if (has_pending_scripts)
200 return ExtensionAction::ACTION_NONE;
201
202 ExtensionAction* extension_action =
203 ExtensionActionManager::Get(browser_context_)->GetExtensionAction(
204 *extension);
205
206 // Anything that gets here should have a page or browser action.
207 DCHECK(extension_action);
208 if (!extension_action->GetIsVisible(tab_id))
209 return ExtensionAction::ACTION_NONE;
210
211 if (extension_action->HasPopup(tab_id))
212 return ExtensionAction::ACTION_SHOW_POPUP;
213
214 ExtensionActionExecuted(*extension_action, web_contents);
215 return ExtensionAction::ACTION_NONE;
216 }
217
218 bool ExtensionActionAPI::ShowExtensionActionPopup( 173 bool ExtensionActionAPI::ShowExtensionActionPopup(
219 const Extension* extension, 174 const Extension* extension,
220 Browser* browser, 175 Browser* browser,
221 bool grant_active_tab_permissions) { 176 bool grant_active_tab_permissions) {
222 ExtensionAction* extension_action = 177 ExtensionAction* extension_action =
223 ExtensionActionManager::Get(browser_context_)->GetExtensionAction( 178 ExtensionActionManager::Get(browser_context_)->GetExtensionAction(
224 *extension); 179 *extension);
225 if (!extension_action) 180 if (!extension_action)
226 return false; 181 return false;
227 182
(...skipping 11 matching lines...) Expand all
239 194
240 ToolbarActionsBar* toolbar_actions_bar = 195 ToolbarActionsBar* toolbar_actions_bar =
241 browser->window()->GetToolbarActionsBar(); 196 browser->window()->GetToolbarActionsBar();
242 // ToolbarActionsBar could be null if, e.g., this is a popup window with no 197 // ToolbarActionsBar could be null if, e.g., this is a popup window with no
243 // toolbar. 198 // toolbar.
244 return toolbar_actions_bar && 199 return toolbar_actions_bar &&
245 toolbar_actions_bar->ShowToolbarActionPopup( 200 toolbar_actions_bar->ShowToolbarActionPopup(
246 extension->id(), grant_active_tab_permissions); 201 extension->id(), grant_active_tab_permissions);
247 } 202 }
248 203
249 bool ExtensionActionAPI::PageActionWantsToRun(
250 const Extension* extension,
251 content::WebContents* web_contents) {
252 ExtensionAction* page_action =
253 ExtensionActionManager::Get(browser_context_)->GetPageAction(*extension);
254 return page_action &&
255 page_action->GetIsVisible(SessionTabHelper::IdForTab(web_contents));
256 }
257
258 bool ExtensionActionAPI::HasBeenBlocked(const Extension* extension,
259 content::WebContents* web_contents) {
260 ExtensionActionRunner* action_runner =
261 ExtensionActionRunner::GetForWebContents(web_contents);
262 return action_runner && action_runner->WantsToRun(extension);
263 }
264
265 void ExtensionActionAPI::NotifyChange(ExtensionAction* extension_action, 204 void ExtensionActionAPI::NotifyChange(ExtensionAction* extension_action,
266 content::WebContents* web_contents, 205 content::WebContents* web_contents,
267 content::BrowserContext* context) { 206 content::BrowserContext* context) {
268 FOR_EACH_OBSERVER( 207 FOR_EACH_OBSERVER(
269 Observer, 208 Observer,
270 observers_, 209 observers_,
271 OnExtensionActionUpdated(extension_action, web_contents, context)); 210 OnExtensionActionUpdated(extension_action, web_contents, context));
272 211
273 if (extension_action->action_type() == ActionInfo::TYPE_PAGE) 212 if (extension_action->action_type() == ActionInfo::TYPE_PAGE)
274 NotifyPageActionsChanged(web_contents); 213 NotifyPageActionsChanged(web_contents);
275 } 214 }
276 215
216 void ExtensionActionAPI::DispatchExtensionActionClicked(
Devlin 2016/03/16 13:21:16 Copy-paste from ExtensionActionExecuted() below
217 const ExtensionAction& extension_action,
218 WebContents* web_contents) {
219 events::HistogramValue histogram_value = events::UNKNOWN;
220 const char* event_name = NULL;
221 switch (extension_action.action_type()) {
222 case ActionInfo::TYPE_BROWSER:
223 histogram_value = events::BROWSER_ACTION_ON_CLICKED;
224 event_name = "browserAction.onClicked";
225 break;
226 case ActionInfo::TYPE_PAGE:
227 histogram_value = events::PAGE_ACTION_ON_CLICKED;
228 event_name = "pageAction.onClicked";
229 break;
230 case ActionInfo::TYPE_SYSTEM_INDICATOR:
231 // The System Indicator handles its own clicks.
232 NOTREACHED();
233 break;
234 }
235
236 if (event_name) {
237 scoped_ptr<base::ListValue> args(new base::ListValue());
238 args->Append(
239 ExtensionTabUtil::CreateTabObject(web_contents)->ToValue().release());
240
241 DispatchEventToExtension(web_contents->GetBrowserContext(),
242 extension_action.extension_id(), histogram_value,
243 event_name, std::move(args));
244 }
245 }
246
277 void ExtensionActionAPI::ClearAllValuesForTab( 247 void ExtensionActionAPI::ClearAllValuesForTab(
278 content::WebContents* web_contents) { 248 content::WebContents* web_contents) {
279 DCHECK(web_contents); 249 DCHECK(web_contents);
280 int tab_id = SessionTabHelper::IdForTab(web_contents); 250 int tab_id = SessionTabHelper::IdForTab(web_contents);
281 content::BrowserContext* browser_context = web_contents->GetBrowserContext(); 251 content::BrowserContext* browser_context = web_contents->GetBrowserContext();
282 const ExtensionSet& enabled_extensions = 252 const ExtensionSet& enabled_extensions =
283 ExtensionRegistry::Get(browser_context_)->enabled_extensions(); 253 ExtensionRegistry::Get(browser_context_)->enabled_extensions();
284 ExtensionActionManager* action_manager = 254 ExtensionActionManager* action_manager =
285 ExtensionActionManager::Get(browser_context_); 255 ExtensionActionManager::Get(browser_context_);
286 256
(...skipping 27 matching lines...) Expand all
314 return; 284 return;
315 285
316 scoped_ptr<Event> event( 286 scoped_ptr<Event> event(
317 new Event(histogram_value, event_name, std::move(event_args))); 287 new Event(histogram_value, event_name, std::move(event_args)));
318 event->restrict_to_browser_context = context; 288 event->restrict_to_browser_context = context;
319 event->user_gesture = EventRouter::USER_GESTURE_ENABLED; 289 event->user_gesture = EventRouter::USER_GESTURE_ENABLED;
320 EventRouter::Get(context) 290 EventRouter::Get(context)
321 ->DispatchEventToExtension(extension_id, std::move(event)); 291 ->DispatchEventToExtension(extension_id, std::move(event));
322 } 292 }
323 293
324 void ExtensionActionAPI::ExtensionActionExecuted(
325 const ExtensionAction& extension_action,
326 WebContents* web_contents) {
327 events::HistogramValue histogram_value = events::UNKNOWN;
328 const char* event_name = NULL;
329 switch (extension_action.action_type()) {
330 case ActionInfo::TYPE_BROWSER:
331 histogram_value = events::BROWSER_ACTION_ON_CLICKED;
332 event_name = "browserAction.onClicked";
333 break;
334 case ActionInfo::TYPE_PAGE:
335 histogram_value = events::PAGE_ACTION_ON_CLICKED;
336 event_name = "pageAction.onClicked";
337 break;
338 case ActionInfo::TYPE_SYSTEM_INDICATOR:
339 // The System Indicator handles its own clicks.
340 NOTREACHED();
341 break;
342 }
343
344 if (event_name) {
345 scoped_ptr<base::ListValue> args(new base::ListValue());
346 args->Append(
347 ExtensionTabUtil::CreateTabObject(web_contents)->ToValue().release());
348
349 DispatchEventToExtension(web_contents->GetBrowserContext(),
350 extension_action.extension_id(), histogram_value,
351 event_name, std::move(args));
352 }
353 }
354
355 void ExtensionActionAPI::NotifyPageActionsChanged( 294 void ExtensionActionAPI::NotifyPageActionsChanged(
356 content::WebContents* web_contents) { 295 content::WebContents* web_contents) {
357 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); 296 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
358 if (!browser) 297 if (!browser)
359 return; 298 return;
360 LocationBar* location_bar = 299 LocationBar* location_bar =
361 browser->window() ? browser->window()->GetLocationBar() : NULL; 300 browser->window() ? browser->window()->GetLocationBar() : NULL;
362 if (!location_bar) 301 if (!location_bar)
363 return; 302 return;
364 location_bar->UpdatePageActions(); 303 location_bar->UpdatePageActions();
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 if (host->extension_host_type() != VIEW_TYPE_EXTENSION_POPUP || 632 if (host->extension_host_type() != VIEW_TYPE_EXTENSION_POPUP ||
694 host->extension()->id() != extension_->id()) 633 host->extension()->id() != extension_->id())
695 return; 634 return;
696 635
697 SendResponse(true); 636 SendResponse(true);
698 response_sent_ = true; 637 response_sent_ = true;
699 registrar_.RemoveAll(); 638 registrar_.RemoveAll();
700 } 639 }
701 640
702 } // namespace extensions 641 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698