Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / page_action_controller.cc
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 "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"
24
25 namespace extensions {
26
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;
30
31 PageActionController::PageActionController(content::WebContents* web_contents)
32     : content::WebContentsObserver(web_contents) {}
33
34 PageActionController::~PageActionController() {}
35
36 std::vector<ExtensionAction*> PageActionController::GetCurrentActions() const {
37   ExtensionService* service = GetExtensionService();
38   if (!service)
39     return std::vector<ExtensionAction*>();
40
41   // Accumulate the list of all page actions to display.
42   std::vector<ExtensionAction*> current_actions;
43
44   ExtensionActionManager* extension_action_manager =
45       ExtensionActionManager::Get(profile());
46
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());
51     if (action)
52       current_actions.push_back(action);
53   }
54
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());
59   }
60
61   return current_actions;
62 }
63
64 LocationBarController::Action PageActionController::OnClicked(
65     const std::string& extension_id, int mouse_button) {
66   ExtensionService* service = GetExtensionService();
67   if (!service)
68     return ACTION_NONE;
69
70   const Extension* extension = service->extensions()->GetByID(extension_id);
71   CHECK(extension);
72   ExtensionAction* page_action =
73       ExtensionActionManager::Get(profile())->GetPageAction(*extension);
74   CHECK(page_action);
75   int tab_id = ExtensionTabUtil::GetTabId(web_contents());
76
77   extensions::TabHelper::FromWebContents(web_contents())->
78       active_tab_permission_granter()->GrantIfRequested(extension);
79
80   switch (mouse_button) {
81     case 1:  // left
82     case 2:  // middle
83       if (page_action->HasPopup(tab_id))
84         return ACTION_SHOW_POPUP;
85
86       ExtensionActionAPI::PageActionExecuted(
87           profile(), *page_action, tab_id,
88           web_contents()->GetURL().spec(), mouse_button);
89       return ACTION_NONE;
90
91     case 3:  // right
92       return extension->ShowConfigureContextMenus() ?
93           ACTION_SHOW_CONTEXT_MENU : ACTION_NONE;
94   }
95
96   return ACTION_NONE;
97 }
98
99 void PageActionController::NotifyChange() {
100   web_contents()->NotifyNavigationStateChanged(
101       content::INVALIDATE_TYPE_PAGE_ACTIONS);
102 }
103
104 void PageActionController::DidNavigateMainFrame(
105     const content::LoadCommittedDetails& details,
106     const content::FrameNavigateParams& params) {
107   if (details.is_in_page)
108     return;
109
110   const std::vector<ExtensionAction*> current_actions = GetCurrentActions();
111
112   if (current_actions.empty())
113     return;
114
115   for (size_t i = 0; i < current_actions.size(); ++i) {
116     current_actions[i]->ClearAllValuesForTab(
117         SessionID::IdForTab(web_contents()));
118   }
119
120   NotifyChange();
121 }
122
123 Profile* PageActionController::profile() const {
124   content::WebContents* web_contents = this->web_contents();
125   if (web_contents)
126     return Profile::FromBrowserContext(web_contents->GetBrowserContext());
127
128   return NULL;
129 }
130
131 ExtensionService* PageActionController::GetExtensionService() const {
132   Profile* profile = this->profile();
133   if (profile)
134     return ExtensionSystem::Get(profile)->extension_service();
135
136   return NULL;
137 }
138
139 }  // namespace extensions