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 : LocationBarControllerProvider(web_contents) { |
| 30 } |
33 | 31 |
34 PageActionController::~PageActionController() {} | 32 PageActionController::~PageActionController() { |
| 33 } |
35 | 34 |
36 std::vector<ExtensionAction*> PageActionController::GetCurrentActions() const { | 35 std::vector<ExtensionAction*> PageActionController::GetCurrentActions() { |
37 ExtensionRegistry* registry = GetExtensionRegistry(); | |
38 if (!registry) | |
39 return std::vector<ExtensionAction*>(); | |
40 | |
41 // Accumulate the list of all page actions to display. | 36 // Accumulate the list of all page actions to display. |
42 std::vector<ExtensionAction*> current_actions; | 37 std::vector<ExtensionAction*> current_actions; |
43 | 38 |
44 ExtensionActionManager* extension_action_manager = | 39 ExtensionActionManager* extension_action_manager = |
45 ExtensionActionManager::Get(GetProfile()); | 40 ExtensionActionManager::Get(GetProfile()); |
46 | 41 |
47 const ExtensionSet& enabled_set = registry->enabled_extensions(); | 42 const ExtensionSet& enabled_set = |
48 for (ExtensionSet::const_iterator i = enabled_set.begin(); | 43 GetExtensionRegistry()->enabled_extensions(); |
49 i != enabled_set.end(); | 44 for (ExtensionSet::const_iterator iter = enabled_set.begin(); |
50 ++i) { | 45 iter != enabled_set.end(); |
| 46 ++iter) { |
51 ExtensionAction* action = | 47 ExtensionAction* action = |
52 extension_action_manager->GetPageAction(*i->get()); | 48 extension_action_manager->GetPageAction(*iter->get()); |
53 if (action) | 49 if (action) |
54 current_actions.push_back(action); | 50 current_actions.push_back(action); |
55 } | 51 } |
56 | 52 |
57 if (!g_reported_for_profiles.Get().count(GetProfile())) { | 53 if (!g_reported_for_profiles.Get().count(GetProfile())) { |
58 UMA_HISTOGRAM_COUNTS_100("PageActionController.ExtensionsWithPageActions", | 54 UMA_HISTOGRAM_COUNTS_100("PageActionController.ExtensionsWithPageActions", |
59 current_actions.size()); | 55 current_actions.size()); |
60 g_reported_for_profiles.Get().insert(GetProfile()); | 56 g_reported_for_profiles.Get().insert(GetProfile()); |
61 } | 57 } |
62 | 58 |
63 return current_actions; | 59 return current_actions; |
64 } | 60 } |
65 | 61 |
66 LocationBarController::Action PageActionController::OnClicked( | 62 LocationBarController::Action PageActionController::OnClicked( |
67 const std::string& extension_id, int mouse_button) { | 63 const Extension* extension, |
68 ExtensionRegistry* registry = GetExtensionRegistry(); | 64 LocationBarController::MouseButton button) { |
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 = | 65 ExtensionAction* page_action = |
76 ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension); | 66 ExtensionActionManager::Get(GetProfile())->GetPageAction(*extension); |
77 CHECK(page_action); | 67 CHECK(page_action); |
78 int tab_id = ExtensionTabUtil::GetTabId(web_contents()); | 68 int tab_id = SessionID::IdForTab(web_contents()); |
79 | 69 |
80 extensions::TabHelper::FromWebContents(web_contents())-> | 70 extensions::TabHelper::FromWebContents(web_contents())-> |
81 active_tab_permission_granter()->GrantIfRequested(extension); | 71 active_tab_permission_granter()->GrantIfRequested(extension); |
82 | 72 |
83 switch (mouse_button) { | 73 switch (button) { |
84 case 1: // left | 74 case LocationBarController::MOUSE_BUTTON_LEFT: |
85 case 2: // middle | 75 case LocationBarController::MOUSE_BUTTON_MIDDLE: |
86 if (page_action->HasPopup(tab_id)) | 76 if (page_action->HasPopup(tab_id)) |
87 return ACTION_SHOW_POPUP; | 77 return LocationBarController::ACTION_SHOW_POPUP; |
88 | 78 |
89 ExtensionActionAPI::PageActionExecuted( | 79 ExtensionActionAPI::PageActionExecuted( |
90 GetProfile(), *page_action, tab_id, | 80 web_contents()->GetBrowserContext(), |
91 web_contents()->GetURL().spec(), mouse_button); | 81 *page_action, |
92 return ACTION_NONE; | 82 tab_id, |
| 83 web_contents()->GetLastCommittedURL().spec(), |
| 84 button); |
| 85 return LocationBarController::ACTION_NONE; |
93 | 86 |
94 case 3: // right | 87 case LocationBarController::MOUSE_BUTTON_RIGHT: |
95 return extension->ShowConfigureContextMenus() ? | 88 return extension->ShowConfigureContextMenus() ? |
96 ACTION_SHOW_CONTEXT_MENU : ACTION_NONE; | 89 LocationBarController::ACTION_SHOW_CONTEXT_MENU : |
| 90 LocationBarController::ACTION_NONE; |
97 } | 91 } |
98 | 92 |
99 return ACTION_NONE; | 93 return LocationBarController::ACTION_NONE; |
100 } | 94 } |
101 | 95 |
102 void PageActionController::NotifyChange() { | 96 void PageActionController::NavigatedMainFrame( |
103 web_contents()->NotifyNavigationStateChanged( | |
104 content::INVALIDATE_TYPE_PAGE_ACTIONS); | |
105 } | |
106 | |
107 void PageActionController::DidNavigateMainFrame( | |
108 const content::LoadCommittedDetails& details, | 97 const content::LoadCommittedDetails& details, |
109 const content::FrameNavigateParams& params) { | 98 const content::FrameNavigateParams& params) { |
110 if (details.is_in_page) | 99 if (details.is_in_page) |
111 return; | 100 return; |
112 | 101 |
113 const std::vector<ExtensionAction*> current_actions = GetCurrentActions(); | 102 const std::vector<ExtensionAction*> current_actions = GetCurrentActions(); |
114 | 103 |
115 if (current_actions.empty()) | 104 if (current_actions.empty()) |
116 return; | 105 return; |
117 | 106 |
118 for (size_t i = 0; i < current_actions.size(); ++i) { | 107 int tab_id = SessionID::IdForTab(web_contents()); |
119 current_actions[i]->ClearAllValuesForTab( | 108 for (size_t i = 0; i < current_actions.size(); ++i) |
120 SessionID::IdForTab(web_contents())); | 109 current_actions[i]->ClearAllValuesForTab(tab_id); |
121 } | |
122 | 110 |
123 NotifyChange(); | 111 NotifyChange(); |
124 } | 112 } |
125 | 113 |
126 Profile* PageActionController::GetProfile() const { | 114 Profile* PageActionController::GetProfile() { |
127 content::WebContents* web_contents = this->web_contents(); | 115 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
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 } | 116 } |
138 | 117 |
139 } // namespace extensions | 118 } // namespace extensions |
OLD | NEW |