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/ui/toolbar/action_box_button_controller.h" | 5 #include "chrome/browser/ui/toolbar/action_box_button_controller.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/app/chrome_command_ids.h" | |
11 #include "chrome/browser/extensions/api/page_launcher/page_launcher_api.h" | |
10 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/extension_system.h" | 13 #include "chrome/browser/extensions/extension_system.h" |
12 #include "chrome/browser/intents/web_intents_registry_factory.h" | |
13 #include "chrome/browser/intents/web_intents_registry.h" | |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_commands.h" | 16 #include "chrome/browser/ui/browser_commands.h" |
17 #include "chrome/browser/ui/browser_tabstrip.h" | 17 #include "chrome/browser/ui/browser_tabstrip.h" |
18 #include "chrome/browser/ui/toolbar/action_box_menu_model.h" | 18 #include "chrome/browser/ui/toolbar/action_box_menu_model.h" |
19 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
20 #include "chrome/common/extensions/api/extension_action/action_info.h" | |
20 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
21 #include "chrome/common/extensions/extension_set.h" | 22 #include "chrome/common/extensions/extension_set.h" |
22 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
23 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
24 #include "content/public/browser/user_metrics.h" | 25 #include "content/public/browser/user_metrics.h" |
25 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
26 #include "content/public/browser/web_intents_dispatcher.h" | |
27 #include "grit/generated_resources.h" | |
28 #include "webkit/glue/web_intent_data.h" | |
29 #include "webkit/glue/webkit_glue.h" | |
30 | |
31 namespace { | |
32 | |
33 // This indicates we need to send UMA data about the number of | |
34 // "Share with X" commands shown in the menu after user tried to | |
35 // find share extensions from web store or the first use of action | |
36 // box after browser starts. | |
37 static bool send_uma_share_command_count = true; | |
38 | |
39 // Share intents get command IDs that are beyond the maximal valid command ID | |
40 // (0xDFFF) so that they are not confused with actual commands that appear in | |
41 // the menu. Extensions get a reserved block of commands after share handlers. | |
42 // For more details see: chrome/app/chrome_command_ids.h | |
43 const int kMaxShareItemsToShow = 20; // TODO(skare): Show extras in submenu. | |
44 enum ActionBoxLocalCommandIds { | |
45 CWS_FIND_SHARE_INTENTS_COMMAND = 0xE000, | |
46 SHARE_COMMAND_FIRST, | |
47 SHARE_COMMAND_LAST = | |
48 SHARE_COMMAND_FIRST + kMaxShareItemsToShow - 1, | |
49 EXTENSION_COMMAND_FIRST | |
50 }; | |
51 | |
52 const char kShareIntentAction[] = "http://webintents.org/share"; | |
53 const char kShareIntentMimeType[] = "text/uri-list"; | |
54 | |
55 } // namespace | |
56 | 27 |
57 using content::UserMetricsAction; | 28 using content::UserMetricsAction; |
29 using content::WebContents; | |
30 using extensions::ActionInfo; | |
31 | |
32 void ActionBoxButtonController::Delegate::ShowMenu( | |
33 scoped_ptr<ActionBoxMenuModel> menu_model) { | |
34 } | |
58 | 35 |
59 ActionBoxButtonController::ActionBoxButtonController(Browser* browser, | 36 ActionBoxButtonController::ActionBoxButtonController(Browser* browser, |
60 Delegate* delegate) | 37 Delegate* delegate) |
61 : browser_(browser), | 38 : browser_(browser), |
62 delegate_(delegate), | 39 delegate_(delegate), |
63 next_extension_command_id_(EXTENSION_COMMAND_FIRST) { | 40 next_command_id_(0) { |
64 DCHECK(browser_); | 41 DCHECK(browser_); |
65 DCHECK(delegate_); | 42 DCHECK(delegate_); |
66 registrar_.Add(this, | 43 registrar_.Add(this, |
67 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 44 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
68 content::Source<Profile>(browser->profile())); | 45 content::Source<Profile>(browser->profile())); |
69 } | 46 } |
70 | 47 |
71 ActionBoxButtonController::~ActionBoxButtonController() {} | 48 ActionBoxButtonController::~ActionBoxButtonController() {} |
72 | 49 |
73 void ActionBoxButtonController::OnButtonClicked() { | 50 void ActionBoxButtonController::OnButtonClicked() { |
74 // Build a menu model and display the menu. | 51 // Build a menu model and display the menu. |
75 scoped_ptr<ActionBoxMenuModel> menu_model( | 52 scoped_ptr<ActionBoxMenuModel> menu_model( |
76 new ActionBoxMenuModel(browser_, this)); | 53 new ActionBoxMenuModel(browser_, this)); |
77 | 54 |
78 // Add share intent triggers and a link to the web store. | 55 const ExtensionSet* extensions = |
79 // Web Intents are not currently supported in Incognito mode. | |
80 ExtensionService* extension_service = | |
81 extensions::ExtensionSystem::Get(browser_->profile())-> | 56 extensions::ExtensionSystem::Get(browser_->profile())-> |
82 extension_service(); | 57 extension_service()->extensions(); |
83 if (!browser_->profile()->IsOffTheRecord()) { | 58 for (ExtensionSet::const_iterator iter = extensions->begin(); |
84 int next_share_intent_command_id = SHARE_COMMAND_FIRST; | 59 iter != extensions->end(); ++iter) { |
85 share_intent_service_ids_.clear(); | 60 const extensions::Extension* extension = *iter; |
86 const ExtensionSet* extension_set = extension_service->extensions(); | 61 if (ActionInfo::GetPageLauncherInfo(extension)) { |
87 WebIntentsRegistry* intents_registry = | 62 int command_id = GetCommandIdForExtension(*extension); |
88 WebIntentsRegistryFactory::GetForProfile(browser_->profile()); | 63 menu_model->AddExtension(*extension, command_id); |
89 for (ExtensionSet::const_iterator it = extension_set->begin(); | |
90 it != extension_set->end(); ++it) { | |
91 const extensions::Extension* extension = *it; | |
92 WebIntentsRegistry::IntentServiceList services; | |
93 intents_registry->GetIntentServicesForExtensionFilter( | |
94 ASCIIToUTF16(kShareIntentAction), | |
95 ASCIIToUTF16(kShareIntentMimeType), | |
96 extension->id(), | |
97 &services); | |
98 if (!services.empty()) { | |
99 int command_id = next_share_intent_command_id++; | |
100 if (command_id > SHARE_COMMAND_LAST) | |
101 break; | |
102 // TODO(skare): If an intent supports multiple services, be able to | |
103 // disambiguate. Choosing the first matches the picker behavior; see | |
104 // TODO in WebIntentPickerController::DispatchToInstalledExtension. | |
105 share_intent_service_ids_[command_id] = services[0].service_url; | |
106 menu_model->AddItem(command_id, services[0].title); | |
107 } | |
108 } | |
109 | |
110 // Add link to the Web Store to find additional share intents. | |
111 menu_model->AddItemWithStringId(CWS_FIND_SHARE_INTENTS_COMMAND, | |
112 IDS_FIND_SHARE_INTENTS); | |
113 | |
114 content::RecordAction(UserMetricsAction("ActionBox.ClickButton")); | |
115 if (send_uma_share_command_count) { | |
116 UMA_HISTOGRAM_ENUMERATION("ActionBox.ShareCommandCount", | |
117 next_share_intent_command_id - SHARE_COMMAND_FIRST, | |
118 kMaxShareItemsToShow + 1); | |
119 send_uma_share_command_count = false; | |
120 } | 64 } |
121 } | 65 } |
122 | 66 content::RecordAction(UserMetricsAction("ActionBox.ClickButton")); |
123 // Add Extensions. | |
124 next_extension_command_id_ = EXTENSION_COMMAND_FIRST; | |
125 extension_command_ids_.clear(); | |
126 const extensions::ExtensionList& extensions = | |
127 extension_service->toolbar_model()->action_box_menu_items(); | |
128 for (extensions::ExtensionList::const_iterator it = extensions.begin(); | |
129 it != extensions.end(); ++it) { | |
130 menu_model->AddExtension(**it, GetCommandIdForExtension(**it)); | |
131 } | |
132 | 67 |
133 // And show the menu. | 68 // And show the menu. |
134 delegate_->ShowMenu(menu_model.Pass()); | 69 delegate_->ShowMenu(menu_model.Pass()); |
135 } | 70 } |
136 | 71 |
137 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { | 72 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { |
138 return false; | 73 return false; |
139 } | 74 } |
140 | 75 |
141 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { | 76 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { |
142 return true; | 77 return true; |
143 } | 78 } |
144 | 79 |
145 bool ActionBoxButtonController::GetAcceleratorForCommandId( | 80 bool ActionBoxButtonController::GetAcceleratorForCommandId( |
146 int command_id, | 81 int command_id, |
147 ui::Accelerator* accelerator) { | 82 ui::Accelerator* accelerator) { |
148 return false; | 83 return false; |
149 } | 84 } |
150 | 85 |
151 void ActionBoxButtonController::ExecuteCommand(int command_id) { | 86 void ActionBoxButtonController::ExecuteCommand(int command_id) { |
152 // Handle explicit intent triggers for share intent commands. | 87 // If the command id belongs to an extension, dispatch an onClicked event |
153 if (share_intent_service_ids_.count(command_id) > 0) { | 88 // to its pageLauncher. |
154 TriggerExplicitShareIntent(share_intent_service_ids_[command_id]); | 89 ExtensionIdCommandMap::const_iterator it = |
90 extension_command_ids_.find(command_id); | |
91 if (it != extension_command_ids_.end()) { | |
92 WebContents* web_contents = chrome::GetActiveWebContents(browser_); | |
93 // TODO(rfevang): Send page title and selected text. | |
94 extensions::PageLauncherAPI::DispatchOnClickedEvent( | |
95 browser_->profile(), | |
96 it->second, | |
97 web_contents->GetURL().spec(), | |
98 web_contents->GetContentsMimeType(), | |
99 scoped_ptr<std::string>(), | |
100 scoped_ptr<std::string>()); | |
155 return; | 101 return; |
156 } | 102 } |
157 | 103 |
158 // Handle link to the CWS web store. | |
159 if (command_id == CWS_FIND_SHARE_INTENTS_COMMAND) { | |
160 NavigateToWebStoreShareIntentsList(); | |
161 return; | |
162 } | |
163 | |
164 // Handle commands associated with extensions. | |
165 // Note that the extension might have been uninstalled or disabled while the | |
166 // menu was open (sync perhaps?) but that will just fall through safely. | |
167 const extensions::Extension* extension = | |
168 GetExtensionForCommandId(command_id); | |
169 if (extension) { | |
170 // TODO(kalman): do something with the result. | |
171 extensions::ExtensionSystem::Get(browser_->profile())-> | |
172 extension_service()->toolbar_model()->ExecuteBrowserAction( | |
173 extension, browser_, NULL); | |
174 return; | |
175 } | |
176 | |
177 // Otherwise, let the browser handle the command. | 104 // Otherwise, let the browser handle the command. |
178 chrome::ExecuteCommand(browser_, command_id); | 105 chrome::ExecuteCommand(browser_, command_id); |
179 } | 106 } |
180 | 107 |
181 int ActionBoxButtonController::GetCommandIdForExtension( | 108 int ActionBoxButtonController::GetCommandIdForExtension( |
182 const extensions::Extension& extension) { | 109 const extensions::Extension& extension) { |
183 ExtensionIdCommandMap::iterator it = | 110 for (ExtensionIdCommandMap::const_iterator iter = |
184 extension_command_ids_.find(extension.id()); | 111 extension_command_ids_.begin(); |
skare_
2013/01/30 00:49:51
tiny nit: line this multiline for loop and the one
Rune Fevang
2013/01/30 01:32:12
Good eye :) Changed this one to match the other tw
| |
185 if (it != extension_command_ids_.end()) | 112 iter != extension_command_ids_.end(); ++iter) { |
186 return it->second; | 113 if (iter->second == extension.id()) { |
187 int command_id = next_extension_command_id_++; | 114 return iter->first; |
115 } | |
116 } | |
188 | 117 |
189 // Note that we deliberately don't clean up extension IDs here when | 118 int command_id = GetNextCommandId(); |
190 // extensions are unloaded, so that if they're reloaded they get assigned the | 119 extension_command_ids_[command_id] = extension.id(); |
191 // old command ID. This situation could arise if an extension is updated | |
192 // while the menu is open. On the other hand, we leak some memory... but | |
193 // that's unlikely to matter. | |
194 extension_command_ids_[extension.id()] = command_id; | |
195 | 120 |
196 return command_id; | 121 return command_id; |
197 } | 122 } |
198 | 123 |
199 const extensions::Extension* | 124 int ActionBoxButtonController::GetNextCommandId() { |
200 ActionBoxButtonController::GetExtensionForCommandId(int command_id) { | 125 int command_id = next_command_id_; |
201 for (ExtensionIdCommandMap::iterator it = extension_command_ids_.begin(); | 126 // Find an available command id to return next time the function is called. |
202 it != extension_command_ids_.end(); ++it) { | 127 do { |
203 if (it->second == command_id) { | 128 next_command_id_++; |
204 // Note: might be NULL anyway if the extension has been uninstalled. | 129 // Larger command ids are reserved for non-dynamic entries, so we start |
205 return extensions::ExtensionSystem::Get(browser_->profile())-> | 130 // reusing old ids at this point. |
206 extension_service()->extensions()->GetByID(it->first); | 131 if (next_command_id_ >= IDC_MinimumLabelValue) |
207 } | 132 next_command_id_ = 0; |
208 } | 133 } while (extension_command_ids_.find(next_command_id_) != |
134 extension_command_ids_.end()); | |
209 | 135 |
210 return NULL; | 136 return command_id; |
211 } | 137 } |
212 | 138 |
213 void ActionBoxButtonController::Observe( | 139 void ActionBoxButtonController::Observe( |
214 int type, | 140 int type, |
215 const content::NotificationSource& source, | 141 const content::NotificationSource& source, |
216 const content::NotificationDetails& details) { | 142 const content::NotificationDetails& details) { |
217 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | 143 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
218 const extensions::Extension* extension = | 144 const extensions::Extension* extension = |
219 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 145 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
220 | 146 |
147 // Remove any entry point command ids associated with the extension. | |
148 for (ExtensionIdCommandMap::iterator iter = extension_command_ids_.begin(); | |
149 iter != extension_command_ids_.end();) { | |
150 if (iter->second== extension->id()) { | |
151 extension_command_ids_.erase(iter++); | |
152 } else { | |
153 ++iter; | |
154 } | |
155 } | |
221 // TODO(kalman): if there's a menu open, remove it from that too. | 156 // TODO(kalman): if there's a menu open, remove it from that too. |
222 // We may also want to listen to EXTENSION_LOADED to do the opposite. | 157 // We may also want to listen to EXTENSION_LOADED to do the opposite. |
223 extension_command_ids_.erase(extension->id()); | |
224 } | 158 } |
225 | |
226 void ActionBoxButtonController::TriggerExplicitShareIntent( | |
227 const GURL& share_service_url) { | |
228 const GURL& current_url = chrome::GetActiveWebContents(browser_)->GetURL(); | |
229 webkit_glue::WebIntentData intent_data( | |
230 ASCIIToUTF16(kShareIntentAction), | |
231 ASCIIToUTF16(kShareIntentMimeType), | |
232 UTF8ToUTF16(current_url.spec())); | |
233 intent_data.service = share_service_url; | |
234 static_cast<content::WebContentsDelegate*>(browser_)->WebIntentDispatch( | |
235 NULL, content::WebIntentsDispatcher::Create(intent_data)); | |
236 } | |
237 | |
238 void ActionBoxButtonController::NavigateToWebStoreShareIntentsList() { | |
239 const GURL& query_url = extension_urls::GetWebstoreIntentQueryURL( | |
240 kShareIntentAction, | |
241 kShareIntentMimeType); | |
242 chrome::NavigateParams params(browser_->profile(), query_url, | |
243 content::PAGE_TRANSITION_LINK); | |
244 params.disposition = NEW_FOREGROUND_TAB; | |
245 chrome::Navigate(¶ms); | |
246 | |
247 content::RecordAction(UserMetricsAction("ActionBox.FindShareHandlers")); | |
248 send_uma_share_command_count = true; | |
249 } | |
OLD | NEW |