Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/page_action_controller.h" | 5 #include "chrome/browser/extensions/page_action_controller.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 11 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 12 #include "chrome/browser/extensions/component_loader.h" | |
| 13 #include "chrome/browser/extensions/extension_action.h" | 12 #include "chrome/browser/extensions/extension_action.h" |
| 14 #include "chrome/browser/extensions/extension_action_manager.h" | 13 #include "chrome/browser/extensions/extension_action_manager.h" |
| 15 #include "chrome/browser/extensions/extension_tab_util.h" | |
| 16 #include "chrome/browser/extensions/tab_helper.h" | 14 #include "chrome/browser/extensions/tab_helper.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/sessions/session_id.h" | 16 #include "chrome/browser/sessions/session_id.h" |
| 19 #include "content/public/browser/invalidate_type.h" | |
| 20 #include "content/public/browser/navigation_details.h" | 17 #include "content/public/browser/navigation_details.h" |
| 21 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 22 #include "extensions/browser/extension_registry.h" | 19 #include "extensions/browser/extension_registry.h" |
| 23 #include "extensions/common/extension_set.h" | 20 #include "extensions/common/extension_set.h" |
| 24 | 21 |
| 25 namespace extensions { | 22 namespace extensions { |
| 26 | 23 |
| 27 // Keeps track of the profiles for which we've sent UMA statistics. | 24 // Keeps track of the profiles for which we've sent UMA statistics. |
| 28 base::LazyInstance<std::set<Profile*> > g_reported_for_profiles = | 25 base::LazyInstance<std::set<Profile*> > g_reported_for_profiles = |
| 29 LAZY_INSTANCE_INITIALIZER; | 26 LAZY_INSTANCE_INITIALIZER; |
| 30 | 27 |
| 31 PageActionController::PageActionController(content::WebContents* web_contents) | 28 PageActionController::PageActionController(content::WebContents* web_contents) |
| 32 : content::WebContentsObserver(web_contents) {} | 29 : web_contents_(web_contents) { |
| 30 } | |
| 33 | 31 |
| 34 PageActionController::~PageActionController() {} | 32 PageActionController::~PageActionController() { |
| 33 } | |
| 35 | 34 |
| 36 std::vector<ExtensionAction*> PageActionController::GetCurrentActions() const { | 35 ExtensionAction* PageActionController::GetActionForExtension( |
| 37 ExtensionRegistry* registry = GetExtensionRegistry(); | 36 const Extension* extension) { |
| 38 if (!registry) | 37 ExtensionAction* action = |
| 39 return std::vector<ExtensionAction*>(); | 38 ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension); |
|
not at google - send to devlin
2014/05/08 20:47:09
I would expect ExtensionActionManager to do its ow
Devlin
2014/05/08 23:01:00
Was for clearing values below, but refactored to n
| |
| 40 | 39 if (action) |
| 41 // Accumulate the list of all page actions to display. | 40 current_actions_.push_back(action); |
| 42 std::vector<ExtensionAction*> current_actions; | 41 return action; |
| 43 | |
| 44 ExtensionActionManager* extension_action_manager = | |
| 45 ExtensionActionManager::Get(GetProfile()); | |
| 46 | |
| 47 const ExtensionSet& enabled_set = registry->enabled_extensions(); | |
| 48 for (ExtensionSet::const_iterator i = enabled_set.begin(); | |
| 49 i != enabled_set.end(); | |
| 50 ++i) { | |
| 51 ExtensionAction* action = | |
| 52 extension_action_manager->GetPageAction(*i->get()); | |
| 53 if (action) | |
| 54 current_actions.push_back(action); | |
| 55 } | |
| 56 | |
| 57 if (!g_reported_for_profiles.Get().count(GetProfile())) { | |
| 58 UMA_HISTOGRAM_COUNTS_100("PageActionController.ExtensionsWithPageActions", | |
| 59 current_actions.size()); | |
| 60 g_reported_for_profiles.Get().insert(GetProfile()); | |
| 61 } | |
| 62 | |
| 63 return current_actions; | |
| 64 } | 42 } |
| 65 | 43 |
| 66 LocationBarController::Action PageActionController::OnClicked( | 44 LocationBarController::Action PageActionController::OnClicked( |
| 67 const std::string& extension_id, int mouse_button) { | 45 const Extension* extension) { |
| 68 ExtensionRegistry* registry = GetExtensionRegistry(); | |
| 69 if (!registry) | |
| 70 return ACTION_NONE; | |
| 71 | |
| 72 const Extension* extension = | |
| 73 registry->enabled_extensions().GetByID(extension_id); | |
| 74 CHECK(extension); | |
| 75 ExtensionAction* page_action = | 46 ExtensionAction* page_action = |
| 76 ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension); | 47 ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension); |
| 77 CHECK(page_action); | 48 if (!page_action) |
| 78 int tab_id = ExtensionTabUtil::GetTabId(web_contents()); | 49 return LocationBarController::ACTION_NONE; |
| 79 | 50 |
| 80 extensions::TabHelper::FromWebContents(web_contents())-> | 51 int tab_id = SessionID::IdForTab(web_contents_); |
| 52 TabHelper::FromWebContents(web_contents_)-> | |
| 81 active_tab_permission_granter()->GrantIfRequested(extension); | 53 active_tab_permission_granter()->GrantIfRequested(extension); |
| 82 | 54 |
| 83 switch (mouse_button) { | 55 if (page_action->HasPopup(tab_id)) |
| 84 case 1: // left | 56 return LocationBarController::ACTION_SHOW_POPUP; |
| 85 case 2: // middle | |
| 86 if (page_action->HasPopup(tab_id)) | |
| 87 return ACTION_SHOW_POPUP; | |
| 88 | 57 |
| 89 ExtensionActionAPI::PageActionExecuted( | 58 ExtensionActionAPI::PageActionExecuted( |
| 90 GetProfile(), *page_action, tab_id, | 59 web_contents_->GetBrowserContext(), |
| 91 web_contents()->GetURL().spec(), mouse_button); | 60 *page_action, |
| 92 return ACTION_NONE; | 61 tab_id, |
| 62 web_contents_->GetLastCommittedURL().spec(), | |
| 63 1 /* Button indication. We only ever pass left-click. */); | |
| 93 | 64 |
| 94 case 3: // right | 65 return LocationBarController::ACTION_NONE; |
| 95 return extension->ShowConfigureContextMenus() ? | 66 } |
| 96 ACTION_SHOW_CONTEXT_MENU : ACTION_NONE; | 67 |
| 68 void PageActionController::OnNavigated() { | |
| 69 Profile* profile = GetProfile(); | |
| 70 // Report the number of page actions for this profile, if we haven't already. | |
| 71 if (!g_reported_for_profiles.Get().count(profile)) { | |
| 72 UMA_HISTOGRAM_COUNTS_100("PageActionController.ExtensionsWithPageActions", | |
| 73 current_actions_.size()); | |
| 74 g_reported_for_profiles.Get().insert(profile); | |
| 97 } | 75 } |
| 98 | 76 |
| 99 return ACTION_NONE; | 77 if (current_actions_.empty()) |
| 78 return; | |
| 79 | |
| 80 int tab_id = SessionID::IdForTab(web_contents_); | |
| 81 for (std::vector<ExtensionAction*>::const_iterator iter = | |
| 82 current_actions_.begin(); | |
| 83 iter != current_actions_.end(); | |
| 84 ++iter) { | |
| 85 (*iter)->ClearAllValuesForTab(tab_id); | |
| 86 } | |
| 87 | |
| 88 LocationBarController::NotifyChange(web_contents_); | |
| 100 } | 89 } |
| 101 | 90 |
| 102 void PageActionController::NotifyChange() { | 91 Profile* PageActionController::GetProfile() { |
| 103 web_contents()->NotifyNavigationStateChanged( | 92 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 104 content::INVALIDATE_TYPE_PAGE_ACTIONS); | |
| 105 } | |
| 106 | |
| 107 void PageActionController::DidNavigateMainFrame( | |
| 108 const content::LoadCommittedDetails& details, | |
| 109 const content::FrameNavigateParams& params) { | |
| 110 if (details.is_in_page) | |
| 111 return; | |
| 112 | |
| 113 const std::vector<ExtensionAction*> current_actions = GetCurrentActions(); | |
| 114 | |
| 115 if (current_actions.empty()) | |
| 116 return; | |
| 117 | |
| 118 for (size_t i = 0; i < current_actions.size(); ++i) { | |
| 119 current_actions[i]->ClearAllValuesForTab( | |
| 120 SessionID::IdForTab(web_contents())); | |
| 121 } | |
| 122 | |
| 123 NotifyChange(); | |
| 124 } | |
| 125 | |
| 126 Profile* PageActionController::GetProfile() const { | |
| 127 content::WebContents* web_contents = this->web_contents(); | |
| 128 if (web_contents) | |
| 129 return Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 130 | |
| 131 return NULL; | |
| 132 } | |
| 133 | |
| 134 ExtensionRegistry* PageActionController::GetExtensionRegistry() const { | |
| 135 Profile* profile = this->GetProfile(); | |
| 136 return profile ? ExtensionRegistry::Get(profile) : NULL; | |
| 137 } | 93 } |
| 138 | 94 |
| 139 } // namespace extensions | 95 } // namespace extensions |
| OLD | NEW |