OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/page_action_controller.h" | 5 #include "chrome/browser/extensions/page_action_controller.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "chrome/browser/extensions/api/extension_action/extension_action_api.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" | 12 #include "chrome/browser/extensions/extension_action.h" |
14 #include "chrome/browser/extensions/extension_action_manager.h" | 13 #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" | 14 #include "chrome/browser/extensions/tab_helper.h" |
17 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/sessions/session_id.h" | 16 #include "chrome/browser/sessions/session_id.h" |
19 #include "content/public/browser/invalidate_type.h" | |
20 #include "content/public/browser/navigation_details.h" | 17 #include "content/public/browser/navigation_details.h" |
21 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
22 #include "extensions/browser/extension_registry.h" | 19 #include "extensions/browser/extension_registry.h" |
23 #include "extensions/common/extension_set.h" | 20 #include "extensions/common/extension_set.h" |
24 | 21 |
25 namespace extensions { | 22 namespace extensions { |
26 | 23 |
27 // Keeps track of the profiles for which we've sent UMA statistics. | 24 // Keeps track of the profiles for which we've sent UMA statistics. |
28 base::LazyInstance<std::set<Profile*> > g_reported_for_profiles = | 25 base::LazyInstance<std::set<Profile*> > g_reported_for_profiles = |
29 LAZY_INSTANCE_INITIALIZER; | 26 LAZY_INSTANCE_INITIALIZER; |
30 | 27 |
31 PageActionController::PageActionController(content::WebContents* web_contents) | 28 PageActionController::PageActionController(content::WebContents* web_contents) |
32 : content::WebContentsObserver(web_contents) {} | 29 : web_contents_(web_contents) { |
30 } | |
33 | 31 |
34 PageActionController::~PageActionController() {} | 32 PageActionController::~PageActionController() { |
33 } | |
35 | 34 |
36 std::vector<ExtensionAction*> PageActionController::GetCurrentActions() const { | 35 ExtensionAction* PageActionController::GetActionForExtension( |
37 ExtensionRegistry* registry = GetExtensionRegistry(); | 36 const Extension* extension) { |
38 if (!registry) | 37 return ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension); |
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(GetProfile()); | |
46 | |
47 const ExtensionSet& enabled_set = registry->enabled_extensions(); | |
48 for (ExtensionSet::const_iterator i = enabled_set.begin(); | |
49 i != enabled_set.end(); | |
50 ++i) { | |
51 ExtensionAction* action = | |
52 extension_action_manager->GetPageAction(*i->get()); | |
53 if (action) | |
54 current_actions.push_back(action); | |
55 } | |
56 | |
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()); | |
61 } | |
62 | |
63 return current_actions; | |
64 } | 38 } |
65 | 39 |
66 LocationBarController::Action PageActionController::OnClicked( | 40 LocationBarController::Action PageActionController::OnClicked( |
67 const std::string& extension_id, int mouse_button) { | 41 const Extension* extension) { |
68 ExtensionRegistry* registry = GetExtensionRegistry(); | |
69 if (!registry) | |
70 return ACTION_NONE; | |
71 | |
72 const Extension* extension = | |
73 registry->enabled_extensions().GetByID(extension_id); | |
74 CHECK(extension); | |
75 ExtensionAction* page_action = | 42 ExtensionAction* page_action = |
76 ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension); | 43 ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension); |
77 CHECK(page_action); | 44 CHECK(page_action); |
78 int tab_id = ExtensionTabUtil::GetTabId(web_contents()); | |
79 | 45 |
80 extensions::TabHelper::FromWebContents(web_contents())-> | 46 int tab_id = SessionID::IdForTab(web_contents_); |
47 TabHelper::FromWebContents(web_contents_)-> | |
81 active_tab_permission_granter()->GrantIfRequested(extension); | 48 active_tab_permission_granter()->GrantIfRequested(extension); |
82 | 49 |
83 switch (mouse_button) { | 50 if (page_action->HasPopup(tab_id)) |
84 case 1: // left | 51 return LocationBarController::ACTION_SHOW_POPUP; |
85 case 2: // middle | |
86 if (page_action->HasPopup(tab_id)) | |
87 return ACTION_SHOW_POPUP; | |
88 | 52 |
89 ExtensionActionAPI::PageActionExecuted( | 53 ExtensionActionAPI::PageActionExecuted( |
90 GetProfile(), *page_action, tab_id, | 54 web_contents_->GetBrowserContext(), |
91 web_contents()->GetURL().spec(), mouse_button); | 55 *page_action, |
92 return ACTION_NONE; | 56 tab_id, |
57 web_contents_->GetLastCommittedURL().spec(), | |
58 1 /* Button indication. We only ever pass left-click. */); | |
93 | 59 |
94 case 3: // right | 60 return LocationBarController::ACTION_NONE; |
95 return extension->ShowConfigureContextMenus() ? | 61 } |
96 ACTION_SHOW_CONTEXT_MENU : ACTION_NONE; | 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 } | |
97 } | 77 } |
98 | 78 |
99 return ACTION_NONE; | 79 Profile* profile = GetProfile(); |
80 // Report the number of page actions for this profile, if we haven't already. | |
not at google - send to devlin
2014/05/08 23:33:19
this is so broken. can you TODO-ify to fix this (s
Devlin
2014/05/09 00:15:32
Done.
| |
81 if (!g_reported_for_profiles.Get().count(profile)) { | |
82 UMA_HISTOGRAM_COUNTS_100("PageActionController.ExtensionsWithPageActions", | |
83 num_current_actions); | |
84 g_reported_for_profiles.Get().insert(profile); | |
85 } | |
86 | |
87 LocationBarController::NotifyChange(web_contents_); | |
100 } | 88 } |
101 | 89 |
102 void PageActionController::NotifyChange() { | 90 Profile* PageActionController::GetProfile() { |
103 web_contents()->NotifyNavigationStateChanged( | 91 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
104 content::INVALIDATE_TYPE_PAGE_ACTIONS); | |
105 } | |
106 | |
107 void PageActionController::DidNavigateMainFrame( | |
108 const content::LoadCommittedDetails& details, | |
109 const content::FrameNavigateParams& params) { | |
110 if (details.is_in_page) | |
111 return; | |
112 | |
113 const std::vector<ExtensionAction*> current_actions = GetCurrentActions(); | |
114 | |
115 if (current_actions.empty()) | |
116 return; | |
117 | |
118 for (size_t i = 0; i < current_actions.size(); ++i) { | |
119 current_actions[i]->ClearAllValuesForTab( | |
120 SessionID::IdForTab(web_contents())); | |
121 } | |
122 | |
123 NotifyChange(); | |
124 } | |
125 | |
126 Profile* PageActionController::GetProfile() const { | |
127 content::WebContents* web_contents = this->web_contents(); | |
128 if (web_contents) | |
129 return Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
130 | |
131 return NULL; | |
132 } | |
133 | |
134 ExtensionRegistry* PageActionController::GetExtensionRegistry() const { | |
135 Profile* profile = this->GetProfile(); | |
136 return profile ? ExtensionRegistry::Get(profile) : NULL; | |
137 } | 92 } |
138 | 93 |
139 } // namespace extensions | 94 } // namespace extensions |
OLD | NEW |