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/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/extension_system.h" | 11 #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" | 12 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_commands.h" | 14 #include "chrome/browser/ui/browser_commands.h" |
17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
18 #include "chrome/browser/ui/toolbar/action_box_menu_model.h" | 16 #include "chrome/browser/ui/toolbar/action_box_menu_model.h" |
19 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
20 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
21 #include "chrome/common/extensions/extension_set.h" | 19 #include "chrome/common/extensions/extension_set.h" |
22 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
23 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
24 #include "content/public/browser/user_metrics.h" | 22 #include "content/public/browser/user_metrics.h" |
25 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
26 #include "content/public/browser/web_intents_dispatcher.h" | |
27 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
28 #include "webkit/glue/web_intent_data.h" | |
29 #include "webkit/glue/webkit_glue.h" | 25 #include "webkit/glue/webkit_glue.h" |
30 | 26 |
31 namespace { | 27 namespace { |
32 | 28 |
33 #if defined(ENABLE_WEB_INTENTS) | |
34 // This indicates we need to send UMA data about the number of | |
35 // "Share with X" commands shown in the menu after user tried to | |
36 // find share extensions from web store or the first use of action | |
37 // box after browser starts. | |
38 static bool send_uma_share_command_count = true; | |
39 #endif | |
40 | |
41 // Share intents get command IDs that are beyond the maximal valid command ID | 29 // Share intents get command IDs that are beyond the maximal valid command ID |
42 // (0xDFFF) so that they are not confused with actual commands that appear in | 30 // (0xDFFF) so that they are not confused with actual commands that appear in |
43 // the menu. Extensions get a reserved block of commands after share handlers. | 31 // the menu. Extensions get a reserved block of commands after share handlers. |
44 // For more details see: chrome/app/chrome_command_ids.h | 32 // For more details see: chrome/app/chrome_command_ids.h |
45 const int kMaxShareItemsToShow = 20; // TODO(skare): Show extras in submenu. | 33 const int kMaxShareItemsToShow = 20; // TODO(skare): Show extras in submenu. |
46 enum ActionBoxLocalCommandIds { | 34 enum ActionBoxLocalCommandIds { |
47 CWS_FIND_SHARE_INTENTS_COMMAND = 0xE000, | 35 CWS_FIND_SHARE_INTENTS_COMMAND = 0xE000, |
48 SHARE_COMMAND_FIRST, | 36 SHARE_COMMAND_FIRST, |
49 SHARE_COMMAND_LAST = | 37 SHARE_COMMAND_LAST = |
50 SHARE_COMMAND_FIRST + kMaxShareItemsToShow - 1, | 38 SHARE_COMMAND_FIRST + kMaxShareItemsToShow - 1, |
51 EXTENSION_COMMAND_FIRST | 39 EXTENSION_COMMAND_FIRST |
52 }; | 40 }; |
53 | 41 |
54 const char kShareIntentAction[] = "http://webintents.org/share"; | |
55 const char kShareIntentMimeType[] = "text/uri-list"; | |
56 | |
57 } // namespace | 42 } // namespace |
58 | 43 |
59 using content::UserMetricsAction; | 44 using content::UserMetricsAction; |
60 | 45 |
61 ActionBoxButtonController::ActionBoxButtonController(Browser* browser, | 46 ActionBoxButtonController::ActionBoxButtonController(Browser* browser, |
62 Delegate* delegate) | 47 Delegate* delegate) |
63 : browser_(browser), | 48 : browser_(browser), |
64 delegate_(delegate), | 49 delegate_(delegate), |
65 next_extension_command_id_(EXTENSION_COMMAND_FIRST) { | 50 next_extension_command_id_(EXTENSION_COMMAND_FIRST) { |
66 DCHECK(browser_); | 51 DCHECK(browser_); |
67 DCHECK(delegate_); | 52 DCHECK(delegate_); |
68 registrar_.Add(this, | 53 registrar_.Add(this, |
69 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 54 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
70 content::Source<Profile>(browser->profile())); | 55 content::Source<Profile>(browser->profile())); |
71 } | 56 } |
72 | 57 |
73 ActionBoxButtonController::~ActionBoxButtonController() {} | 58 ActionBoxButtonController::~ActionBoxButtonController() {} |
74 | 59 |
75 void ActionBoxButtonController::OnButtonClicked() { | 60 void ActionBoxButtonController::OnButtonClicked() { |
76 // Build a menu model and display the menu. | 61 // Build a menu model and display the menu. |
77 scoped_ptr<ActionBoxMenuModel> menu_model( | 62 scoped_ptr<ActionBoxMenuModel> menu_model( |
78 new ActionBoxMenuModel(browser_, this)); | 63 new ActionBoxMenuModel(browser_, this)); |
79 | 64 |
80 ExtensionService* extension_service = | 65 ExtensionService* extension_service = |
81 extensions::ExtensionSystem::Get(browser_->profile())-> | 66 extensions::ExtensionSystem::Get(browser_->profile())-> |
82 extension_service(); | 67 extension_service(); |
83 | 68 |
84 #if defined(ENABLE_WEB_INTENTS) | |
85 // Add share intent triggers and a link to the web store. | |
86 // Web Intents are not currently supported in Incognito mode. | |
87 if (!browser_->profile()->IsOffTheRecord()) { | |
88 int next_share_intent_command_id = SHARE_COMMAND_FIRST; | |
89 share_intent_service_ids_.clear(); | |
90 const ExtensionSet* extension_set = extension_service->extensions(); | |
91 WebIntentsRegistry* intents_registry = | |
92 WebIntentsRegistryFactory::GetForProfile(browser_->profile()); | |
93 for (ExtensionSet::const_iterator it = extension_set->begin(); | |
94 it != extension_set->end(); ++it) { | |
95 const extensions::Extension* extension = *it; | |
96 WebIntentsRegistry::IntentServiceList services; | |
97 intents_registry->GetIntentServicesForExtensionFilter( | |
98 ASCIIToUTF16(kShareIntentAction), | |
99 ASCIIToUTF16(kShareIntentMimeType), | |
100 extension->id(), | |
101 &services); | |
102 if (!services.empty()) { | |
103 int command_id = next_share_intent_command_id++; | |
104 if (command_id > SHARE_COMMAND_LAST) | |
105 break; | |
106 // TODO(skare): If an intent supports multiple services, be able to | |
107 // disambiguate. Choosing the first matches the picker behavior; see | |
108 // TODO in WebIntentPickerController::DispatchToInstalledExtension. | |
109 share_intent_service_ids_[command_id] = services[0].service_url; | |
110 menu_model->AddItem(command_id, services[0].title); | |
111 } | |
112 } | |
113 | |
114 // Add link to the Web Store to find additional share intents. | |
115 menu_model->AddItemWithStringId(CWS_FIND_SHARE_INTENTS_COMMAND, | |
116 IDS_FIND_SHARE_INTENTS); | |
117 | |
118 content::RecordAction(UserMetricsAction("ActionBox.ClickButton")); | |
119 if (send_uma_share_command_count) { | |
120 UMA_HISTOGRAM_ENUMERATION("ActionBox.ShareCommandCount", | |
121 next_share_intent_command_id - SHARE_COMMAND_FIRST, | |
122 kMaxShareItemsToShow + 1); | |
123 send_uma_share_command_count = false; | |
124 } | |
125 } | |
126 #endif | |
127 | |
128 // Add Extensions. | 69 // Add Extensions. |
129 next_extension_command_id_ = EXTENSION_COMMAND_FIRST; | 70 next_extension_command_id_ = EXTENSION_COMMAND_FIRST; |
130 extension_command_ids_.clear(); | 71 extension_command_ids_.clear(); |
131 const extensions::ExtensionList& extensions = | 72 const extensions::ExtensionList& extensions = |
132 extension_service->toolbar_model()->action_box_menu_items(); | 73 extension_service->toolbar_model()->action_box_menu_items(); |
133 for (extensions::ExtensionList::const_iterator it = extensions.begin(); | 74 for (extensions::ExtensionList::const_iterator it = extensions.begin(); |
134 it != extensions.end(); ++it) { | 75 it != extensions.end(); ++it) { |
135 menu_model->AddExtension(**it, GetCommandIdForExtension(**it)); | 76 menu_model->AddExtension(**it, GetCommandIdForExtension(**it)); |
136 } | 77 } |
137 | 78 |
138 // And show the menu. | 79 // And show the menu. |
139 delegate_->ShowMenu(menu_model.Pass()); | 80 delegate_->ShowMenu(menu_model.Pass()); |
140 } | 81 } |
141 | 82 |
142 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { | 83 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { |
143 return false; | 84 return false; |
144 } | 85 } |
145 | 86 |
146 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { | 87 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { |
147 return true; | 88 return true; |
148 } | 89 } |
149 | 90 |
150 bool ActionBoxButtonController::GetAcceleratorForCommandId( | 91 bool ActionBoxButtonController::GetAcceleratorForCommandId( |
151 int command_id, | 92 int command_id, |
152 ui::Accelerator* accelerator) { | 93 ui::Accelerator* accelerator) { |
153 return false; | 94 return false; |
154 } | 95 } |
155 | 96 |
156 void ActionBoxButtonController::ExecuteCommand(int command_id) { | 97 void ActionBoxButtonController::ExecuteCommand(int command_id) { |
157 // Handle explicit intent triggers for share intent commands. | |
158 if (share_intent_service_ids_.count(command_id) > 0) { | |
159 TriggerExplicitShareIntent(share_intent_service_ids_[command_id]); | |
160 return; | |
161 } | |
162 | |
163 #if defined(ENABLE_WEB_INTENTS) | |
164 // Handle link to the CWS web store. | |
165 if (command_id == CWS_FIND_SHARE_INTENTS_COMMAND) { | |
166 NavigateToWebStoreShareIntentsList(); | |
167 return; | |
168 } | |
169 #endif | |
170 | |
171 // Handle commands associated with extensions. | 98 // Handle commands associated with extensions. |
172 // Note that the extension might have been uninstalled or disabled while the | 99 // Note that the extension might have been uninstalled or disabled while the |
173 // menu was open (sync perhaps?) but that will just fall through safely. | 100 // menu was open (sync perhaps?) but that will just fall through safely. |
174 const extensions::Extension* extension = | 101 const extensions::Extension* extension = |
175 GetExtensionForCommandId(command_id); | 102 GetExtensionForCommandId(command_id); |
176 if (extension) { | 103 if (extension) { |
177 // TODO(kalman): do something with the result. | 104 // TODO(kalman): do something with the result. |
178 extensions::ExtensionSystem::Get(browser_->profile())-> | 105 extensions::ExtensionSystem::Get(browser_->profile())-> |
179 extension_service()->toolbar_model()->ExecuteBrowserAction( | 106 extension_service()->toolbar_model()->ExecuteBrowserAction( |
180 extension, browser_, NULL); | 107 extension, browser_, NULL); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 const content::NotificationSource& source, | 149 const content::NotificationSource& source, |
223 const content::NotificationDetails& details) { | 150 const content::NotificationDetails& details) { |
224 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | 151 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
225 const extensions::Extension* extension = | 152 const extensions::Extension* extension = |
226 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 153 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
227 | 154 |
228 // TODO(kalman): if there's a menu open, remove it from that too. | 155 // TODO(kalman): if there's a menu open, remove it from that too. |
229 // We may also want to listen to EXTENSION_LOADED to do the opposite. | 156 // We may also want to listen to EXTENSION_LOADED to do the opposite. |
230 extension_command_ids_.erase(extension->id()); | 157 extension_command_ids_.erase(extension->id()); |
231 } | 158 } |
232 | |
233 void ActionBoxButtonController::TriggerExplicitShareIntent( | |
234 const GURL& share_service_url) { | |
235 #if defined(ENABLE_WEB_INTENTS) | |
236 const GURL& current_url = | |
237 browser_->tab_strip_model()->GetActiveWebContents()->GetURL(); | |
238 webkit_glue::WebIntentData intent_data( | |
239 ASCIIToUTF16(kShareIntentAction), | |
240 ASCIIToUTF16(kShareIntentMimeType), | |
241 UTF8ToUTF16(current_url.spec())); | |
242 intent_data.service = share_service_url; | |
243 static_cast<content::WebContentsDelegate*>(browser_)->WebIntentDispatch( | |
244 NULL, content::WebIntentsDispatcher::Create(intent_data)); | |
245 #endif | |
246 } | |
247 | |
248 void ActionBoxButtonController::NavigateToWebStoreShareIntentsList() { | |
249 #if defined(ENABLE_WEB_INTENTS) | |
250 const GURL& query_url = extension_urls::GetWebstoreIntentQueryURL( | |
251 kShareIntentAction, | |
252 kShareIntentMimeType); | |
253 chrome::NavigateParams params(browser_->profile(), query_url, | |
254 content::PAGE_TRANSITION_LINK); | |
255 params.disposition = NEW_FOREGROUND_TAB; | |
256 chrome::Navigate(¶ms); | |
257 | |
258 content::RecordAction(UserMetricsAction("ActionBox.FindShareHandlers")); | |
259 send_uma_share_command_count = true; | |
260 #endif | |
261 } | |
OLD | NEW |