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/extension_action.h"
13 #include "chrome/browser/extensions/extension_action_manager.h"
14 #include "chrome/browser/extensions/tab_helper.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/sessions/session_id.h"
17 #include "content/public/browser/navigation_details.h"
18 #include "content/public/browser/web_contents.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/common/extension_set.h"
22 namespace extensions {
24 // Keeps track of the profiles for which we've sent UMA statistics.
25 base::LazyInstance<std::set<Profile*> > g_reported_for_profiles =
26 LAZY_INSTANCE_INITIALIZER;
28 PageActionController::PageActionController(content::WebContents* web_contents)
29 : web_contents_(web_contents) {
32 PageActionController::~PageActionController() {
35 ExtensionAction* PageActionController::GetActionForExtension(
36 const Extension* extension) {
37 return ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension);
40 ExtensionAction::ShowAction PageActionController::OnClicked(
41 const Extension* extension) {
42 ExtensionAction* page_action =
43 ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension);
46 int tab_id = SessionID::IdForTab(web_contents_);
47 TabHelper::FromWebContents(web_contents_)->
48 active_tab_permission_granter()->GrantIfRequested(extension);
50 if (page_action->HasPopup(tab_id))
51 return ExtensionAction::ACTION_SHOW_POPUP;
53 ExtensionActionAPI::PageActionExecuted(
54 web_contents_->GetBrowserContext(),
57 web_contents_->GetLastCommittedURL().spec(),
58 1 /* Button indication. We only ever pass left-click. */);
60 return ExtensionAction::ACTION_NONE;
63 void PageActionController::OnNavigated() {
64 const ExtensionSet& extensions =
65 ExtensionRegistry::Get(web_contents_->GetBrowserContext())
66 ->enabled_extensions();
67 int tab_id = SessionID::IdForTab(web_contents_);
68 size_t num_current_actions = 0u;
69 for (ExtensionSet::const_iterator iter = extensions.begin();
70 iter != extensions.end();
72 ExtensionAction* action = GetActionForExtension(*iter);
74 action->ClearAllValuesForTab(tab_id);
75 ++num_current_actions;
79 Profile* profile = GetProfile();
80 // Report the number of page actions for this profile, if we haven't already.
81 // TODO(rdevlin.cronin): This is wrong. Instead, it should record the number
82 // of page actions displayed per page.
83 if (!g_reported_for_profiles.Get().count(profile)) {
84 UMA_HISTOGRAM_COUNTS_100("PageActionController.ExtensionsWithPageActions",
86 g_reported_for_profiles.Get().insert(profile);
89 LocationBarController::NotifyChange(web_contents_);
92 Profile* PageActionController::GetProfile() {
93 return Profile::FromBrowserContext(web_contents_->GetBrowserContext());
96 } // namespace extensions