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"
7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
10 #include "chrome/browser/extensions/component_loader.h"
11 #include "chrome/browser/extensions/extension_action.h"
12 #include "chrome/browser/extensions/extension_action_manager.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_tab_util.h"
15 #include "chrome/browser/extensions/tab_helper.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/sessions/session_id.h"
18 #include "content/public/browser/invalidate_type.h"
19 #include "content/public/browser/navigation_details.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/web_contents.h"
22 #include "extensions/browser/extension_system.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 ExtensionService
* service
= GetExtensionService();
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(profile());
47 for (ExtensionSet::const_iterator i
= service
->extensions()->begin();
48 i
!= service
->extensions()->end(); ++i
) {
49 ExtensionAction
* action
=
50 extension_action_manager
->GetPageAction(*i
->get());
52 current_actions
.push_back(action
);
55 if (!g_reported_for_profiles
.Get().count(profile())) {
56 UMA_HISTOGRAM_COUNTS_100("PageActionController.ExtensionsWithPageActions",
57 current_actions
.size());
58 g_reported_for_profiles
.Get().insert(profile());
61 return current_actions
;
64 LocationBarController::Action
PageActionController::OnClicked(
65 const std::string
& extension_id
, int mouse_button
) {
66 ExtensionService
* service
= GetExtensionService();
70 const Extension
* extension
= service
->extensions()->GetByID(extension_id
);
72 ExtensionAction
* page_action
=
73 ExtensionActionManager::Get(profile())->GetPageAction(*extension
);
75 int tab_id
= ExtensionTabUtil::GetTabId(web_contents());
77 extensions::TabHelper::FromWebContents(web_contents())->
78 active_tab_permission_granter()->GrantIfRequested(extension
);
80 switch (mouse_button
) {
83 if (page_action
->HasPopup(tab_id
))
84 return ACTION_SHOW_POPUP
;
86 ExtensionActionAPI::PageActionExecuted(
87 profile(), *page_action
, tab_id
,
88 web_contents()->GetURL().spec(), mouse_button
);
92 return extension
->ShowConfigureContextMenus() ?
93 ACTION_SHOW_CONTEXT_MENU
: ACTION_NONE
;
99 void PageActionController::NotifyChange() {
100 web_contents()->NotifyNavigationStateChanged(
101 content::INVALIDATE_TYPE_PAGE_ACTIONS
);
104 void PageActionController::DidNavigateMainFrame(
105 const content::LoadCommittedDetails
& details
,
106 const content::FrameNavigateParams
& params
) {
107 if (details
.is_in_page
)
110 const std::vector
<ExtensionAction
*> current_actions
= GetCurrentActions();
112 if (current_actions
.empty())
115 for (size_t i
= 0; i
< current_actions
.size(); ++i
) {
116 current_actions
[i
]->ClearAllValuesForTab(
117 SessionID::IdForTab(web_contents()));
123 Profile
* PageActionController::profile() const {
124 content::WebContents
* web_contents
= this->web_contents();
126 return Profile::FromBrowserContext(web_contents
->GetBrowserContext());
131 ExtensionService
* PageActionController::GetExtensionService() const {
132 Profile
* profile
= this->profile();
134 return ExtensionSystem::Get(profile
)->extension_service();
139 } // namespace extensions