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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/page_action_controller.h"
9 #include "base/lazy_instance.h"
10 #include "base/metrics/histogram.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"
14 #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"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/sessions/session_id.h"
19 #include "content/public/browser/invalidate_type.h"
20 #include "content/public/browser/navigation_details.h"
21 #include "content/public/browser/web_contents.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/common/extension_set.h"
25 namespace extensions
{
27 // Keeps track of the profiles for which we've sent UMA statistics.
28 base::LazyInstance
<std::set
<Profile
*> > g_reported_for_profiles
=
29 LAZY_INSTANCE_INITIALIZER
;
31 PageActionController::PageActionController(content::WebContents
* web_contents
)
32 : content::WebContentsObserver(web_contents
) {}
34 PageActionController::~PageActionController() {}
36 std::vector
<ExtensionAction
*> PageActionController::GetCurrentActions() const {
37 ExtensionRegistry
* registry
= GetExtensionRegistry();
39 return std::vector
<ExtensionAction
*>();
41 // Accumulate the list of all page actions to display.
42 std::vector
<ExtensionAction
*> current_actions
;
44 ExtensionActionManager
* extension_action_manager
=
45 ExtensionActionManager::Get(GetProfile());
47 const ExtensionSet
& enabled_set
= registry
->enabled_extensions();
48 for (ExtensionSet::const_iterator i
= enabled_set
.begin();
49 i
!= enabled_set
.end();
51 ExtensionAction
* action
=
52 extension_action_manager
->GetPageAction(*i
->get());
54 current_actions
.push_back(action
);
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());
63 return current_actions
;
66 LocationBarController::Action
PageActionController::OnClicked(
67 const std::string
& extension_id
, int mouse_button
) {
68 ExtensionRegistry
* registry
= GetExtensionRegistry();
72 const Extension
* extension
=
73 registry
->enabled_extensions().GetByID(extension_id
);
75 ExtensionAction
* page_action
=
76 ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension
);
78 int tab_id
= ExtensionTabUtil::GetTabId(web_contents());
80 extensions::TabHelper::FromWebContents(web_contents())->
81 active_tab_permission_granter()->GrantIfRequested(extension
);
83 switch (mouse_button
) {
86 if (page_action
->HasPopup(tab_id
))
87 return ACTION_SHOW_POPUP
;
89 ExtensionActionAPI::PageActionExecuted(
90 GetProfile(), *page_action
, tab_id
,
91 web_contents()->GetURL().spec(), mouse_button
);
95 return extension
->ShowConfigureContextMenus() ?
96 ACTION_SHOW_CONTEXT_MENU
: ACTION_NONE
;
102 void PageActionController::NotifyChange() {
103 web_contents()->NotifyNavigationStateChanged(
104 content::INVALIDATE_TYPE_PAGE_ACTIONS
);
107 void PageActionController::DidNavigateMainFrame(
108 const content::LoadCommittedDetails
& details
,
109 const content::FrameNavigateParams
& params
) {
110 if (details
.is_in_page
)
113 const std::vector
<ExtensionAction
*> current_actions
= GetCurrentActions();
115 if (current_actions
.empty())
118 for (size_t i
= 0; i
< current_actions
.size(); ++i
) {
119 current_actions
[i
]->ClearAllValuesForTab(
120 SessionID::IdForTab(web_contents()));
126 Profile
* PageActionController::GetProfile() const {
127 content::WebContents
* web_contents
= this->web_contents();
129 return Profile::FromBrowserContext(web_contents
->GetBrowserContext());
134 ExtensionRegistry
* PageActionController::GetExtensionRegistry() const {
135 Profile
* profile
= this->GetProfile();
136 return profile
? ExtensionRegistry::Get(profile
) : NULL
;
139 } // namespace extensions