Home | History | Annotate | Download | only in extensions
      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.
      4 
      5 #include "chrome/browser/extensions/page_action_controller.h"
      6 
      7 #include <set>
      8 
      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"
     21 
     22 namespace extensions {
     23 
     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;
     27 
     28 PageActionController::PageActionController(content::WebContents* web_contents)
     29     : web_contents_(web_contents) {
     30 }
     31 
     32 PageActionController::~PageActionController() {
     33 }
     34 
     35 ExtensionAction* PageActionController::GetActionForExtension(
     36     const Extension* extension) {
     37   return ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension);
     38 }
     39 
     40 LocationBarController::Action PageActionController::OnClicked(
     41     const Extension* extension) {
     42   ExtensionAction* page_action =
     43       ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension);
     44   CHECK(page_action);
     45 
     46   int tab_id = SessionID::IdForTab(web_contents_);
     47   TabHelper::FromWebContents(web_contents_)->
     48       active_tab_permission_granter()->GrantIfRequested(extension);
     49 
     50   if (page_action->HasPopup(tab_id))
     51     return LocationBarController::ACTION_SHOW_POPUP;
     52 
     53   ExtensionActionAPI::PageActionExecuted(
     54       web_contents_->GetBrowserContext(),
     55       *page_action,
     56       tab_id,
     57       web_contents_->GetLastCommittedURL().spec(),
     58       1 /* Button indication. We only ever pass left-click. */);
     59 
     60   return LocationBarController::ACTION_NONE;
     61 }
     62 
     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();
     71        ++iter) {
     72     ExtensionAction* action = GetActionForExtension(*iter);
     73     if (action) {
     74       action->ClearAllValuesForTab(tab_id);
     75       ++num_current_actions;
     76     }
     77   }
     78 
     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",
     85                              num_current_actions);
     86     g_reported_for_profiles.Get().insert(profile);
     87   }
     88 
     89   LocationBarController::NotifyChange(web_contents_);
     90 }
     91 
     92 Profile* PageActionController::GetProfile() {
     93   return Profile::FromBrowserContext(web_contents_->GetBrowserContext());
     94 }
     95 
     96 }  // namespace extensions
     97