Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(411)

Side by Side Diff: chrome/browser/ui/toolbar/action_box_button_controller.cc

Issue 12095023: Allow platform apps to add themselves to the Action Box. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/tabs/tab_strip_model.h" 17 #include "chrome/browser/ui/tabs/tab_strip_model.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();
not at google - send to devlin 2013/02/01 16:39:47 nit: this class uses "it" everywhere for iterator
Rune Fevang 2013/02/01 22:12:54 Done.
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 =
93 browser_->tab_strip_model()->GetActiveWebContents();
94 // TODO(rfevang): Send page title and selected text.
95 extensions::PageLauncherAPI::DispatchOnClickedEvent(
96 browser_->profile(),
97 it->second,
98 web_contents->GetURL().spec(),
not at google - send to devlin 2013/02/01 16:39:47 send the url as a GURL?
Rune Fevang 2013/02/01 22:12:54 Done.
99 web_contents->GetContentsMimeType(),
100 scoped_ptr<std::string>(),
101 scoped_ptr<std::string>());
155 return; 102 return;
156 } 103 }
157 104
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. 105 // Otherwise, let the browser handle the command.
178 chrome::ExecuteCommand(browser_, command_id); 106 chrome::ExecuteCommand(browser_, command_id);
179 } 107 }
180 108
181 int ActionBoxButtonController::GetCommandIdForExtension( 109 int ActionBoxButtonController::GetCommandIdForExtension(
182 const extensions::Extension& extension) { 110 const extensions::Extension& extension) {
183 ExtensionIdCommandMap::iterator it = 111 for (ExtensionIdCommandMap::const_iterator iter =
not at google - send to devlin 2013/02/01 16:39:47 iter -> it rename is arbitrary, please revert (als
Rune Fevang 2013/02/01 22:12:54 Done.
184 extension_command_ids_.find(extension.id()); 112 extension_command_ids_.begin();
185 if (it != extension_command_ids_.end()) 113 iter != extension_command_ids_.end(); ++iter) {
186 return it->second; 114 if (iter->second == extension.id()) {
187 int command_id = next_extension_command_id_++; 115 return iter->first;
116 }
not at google - send to devlin 2013/02/01 16:39:47 single-line ifs omit {}
Rune Fevang 2013/02/01 22:12:54 Done.
117 }
188 118
189 // Note that we deliberately don't clean up extension IDs here when 119 int command_id = GetNextCommandId();
190 // extensions are unloaded, so that if they're reloaded they get assigned the 120 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 121
196 return command_id; 122 return command_id;
197 } 123 }
198 124
199 const extensions::Extension* 125 int ActionBoxButtonController::GetNextCommandId() {
200 ActionBoxButtonController::GetExtensionForCommandId(int command_id) { 126 int command_id = next_command_id_;
201 for (ExtensionIdCommandMap::iterator it = extension_command_ids_.begin(); 127 // Find an available command id to return next time the function is called.
202 it != extension_command_ids_.end(); ++it) { 128 do {
203 if (it->second == command_id) { 129 next_command_id_++;
204 // Note: might be NULL anyway if the extension has been uninstalled. 130 // Larger command ids are reserved for non-dynamic entries, so we start
205 return extensions::ExtensionSystem::Get(browser_->profile())-> 131 // reusing old ids at this point.
206 extension_service()->extensions()->GetByID(it->first); 132 if (next_command_id_ >= IDC_MinimumLabelValue)
207 } 133 next_command_id_ = 0;
208 } 134 } while (extension_command_ids_.find(next_command_id_) !=
135 extension_command_ids_.end());
209 136
210 return NULL; 137 return command_id;
211 } 138 }
212 139
213 void ActionBoxButtonController::Observe( 140 void ActionBoxButtonController::Observe(
214 int type, 141 int type,
215 const content::NotificationSource& source, 142 const content::NotificationSource& source,
216 const content::NotificationDetails& details) { 143 const content::NotificationDetails& details) {
217 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); 144 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED);
218 const extensions::Extension* extension = 145 const extensions::Extension* extension =
219 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; 146 content::Details<extensions::UnloadedExtensionInfo>(details)->extension;
220 147
148 // Remove any entry point command ids associated with the extension.
149 for (ExtensionIdCommandMap::iterator iter = extension_command_ids_.begin();
not at google - send to devlin 2013/02/01 16:39:47 ditto
Rune Fevang 2013/02/01 22:12:54 Done.
150 iter != extension_command_ids_.end();) {
151 if (iter->second== extension->id()) {
152 extension_command_ids_.erase(iter++);
153 } else {
154 ++iter;
155 }
not at google - send to devlin 2013/02/01 16:39:47 single-line ifs omit {}
Rune Fevang 2013/02/01 22:12:54 Done.
156 }
221 // TODO(kalman): if there's a menu open, remove it from that too. 157 // 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. 158 // We may also want to listen to EXTENSION_LOADED to do the opposite.
223 extension_command_ids_.erase(extension->id());
224 } 159 }
225
226 void ActionBoxButtonController::TriggerExplicitShareIntent(
227 const GURL& share_service_url) {
228 const GURL& current_url =
229 browser_->tab_strip_model()->GetActiveWebContents()->GetURL();
230 webkit_glue::WebIntentData intent_data(
231 ASCIIToUTF16(kShareIntentAction),
232 ASCIIToUTF16(kShareIntentMimeType),
233 UTF8ToUTF16(current_url.spec()));
234 intent_data.service = share_service_url;
235 static_cast<content::WebContentsDelegate*>(browser_)->WebIntentDispatch(
236 NULL, content::WebIntentsDispatcher::Create(intent_data));
237 }
238
239 void ActionBoxButtonController::NavigateToWebStoreShareIntentsList() {
240 const GURL& query_url = extension_urls::GetWebstoreIntentQueryURL(
241 kShareIntentAction,
242 kShareIntentMimeType);
243 chrome::NavigateParams params(browser_->profile(), query_url,
244 content::PAGE_TRANSITION_LINK);
245 params.disposition = NEW_FOREGROUND_TAB;
246 chrome::Navigate(&params);
247
248 content::RecordAction(UserMetricsAction("ActionBox.FindShareHandlers"));
249 send_uma_share_command_count = true;
250 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698