Chromium Code Reviews| 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/utf_string_conversions.h" | |
| 8 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/intents/web_intents_registry_factory.h" | |
| 12 #include "chrome/browser/intents/web_intents_registry.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_commands.h" | 15 #include "chrome/browser/ui/browser_commands.h" |
| 16 #include "chrome/browser/ui/browser_tabstrip.h" | |
| 12 #include "chrome/browser/ui/toolbar/action_box_menu_model.h" | 17 #include "chrome/browser/ui/toolbar/action_box_menu_model.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
| 15 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 16 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
| 22 #include "content/public/browser/web_contents.h" | |
| 23 #include "content/public/browser/web_intents_dispatcher.h" | |
| 24 #include "grit/generated_resources.h" | |
| 25 #include "webkit/glue/web_intent_data.h" | |
| 26 #include "webkit/glue/webkit_glue.h" | |
| 17 | 27 |
| 18 namespace { | 28 namespace { |
| 19 | 29 |
| 20 // Extensions get command IDs that are beyond the maximal valid command ID | 30 // Share intents get command IDs that are beyond the maximal valid command ID |
| 21 // (0xDFFF) so that they are not confused with actual commands that appear in | 31 // (0xDFFF) so that they are not confused with actual commands that appear in |
| 22 // the menu. For more details see: chrome/app/chrome_command_ids.h | 32 // the menu. Extensions get a reserved block of commands after share handlers. |
| 23 const int kFirstExtensionCommandId = 0xE000; | 33 // For more details see: chrome/app/chrome_command_ids.h |
| 34 const int kMaxShareItemsToShow = 20; // TODO(skare): Show extras in submenu. | |
| 35 enum ActionBoxLocalCommandIds { | |
| 36 IDC_CWS_FIND_SHARE_INTENTS = 0xE000, | |
| 37 IDC_ACTIONBOX_SHARE_FIRST, | |
| 38 IDC_ACTIONBOX_SHARE_LAST = | |
| 39 IDC_ACTIONBOX_SHARE_FIRST + kMaxShareItemsToShow - 1, | |
| 40 IDC_ACTIONBOX_EXTENSION_FIRST | |
| 41 }; | |
| 42 | |
| 43 const char kShareIntentAction[] = "http://webintents.org/share"; | |
| 44 const char kShareIntentMimeType[] = "text/uri-list"; | |
| 24 | 45 |
| 25 } // namespace | 46 } // namespace |
| 26 | 47 |
| 27 ActionBoxButtonController::ActionBoxButtonController(Browser* browser, | 48 ActionBoxButtonController::ActionBoxButtonController(Browser* browser, |
| 28 Delegate* delegate) | 49 Delegate* delegate) |
| 29 : browser_(browser), | 50 : browser_(browser), |
| 30 delegate_(delegate), | 51 delegate_(delegate), |
| 31 next_extension_command_id_(kFirstExtensionCommandId) { | 52 next_share_intent_command_id_(IDC_ACTIONBOX_SHARE_FIRST), |
| 53 next_extension_command_id_(IDC_ACTIONBOX_EXTENSION_FIRST), | |
| 54 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
| 32 DCHECK(browser_); | 55 DCHECK(browser_); |
| 33 DCHECK(delegate_); | 56 DCHECK(delegate_); |
| 34 registrar_.Add(this, | 57 registrar_.Add(this, |
| 35 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 58 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 36 content::Source<Profile>(browser->profile())); | 59 content::Source<Profile>(browser->profile())); |
| 37 } | 60 } |
| 38 | 61 |
| 39 ActionBoxButtonController::~ActionBoxButtonController() {} | 62 ActionBoxButtonController::~ActionBoxButtonController() {} |
| 40 | 63 |
| 41 void ActionBoxButtonController::OnButtonClicked() { | 64 void ActionBoxButtonController::OnButtonClicked() { |
| 42 // Creating the action box menu will populate it with the bookmark star etc, | 65 // Request registered share intents; populate and show menu in the callback. |
| 43 // but no extensions. | 66 WebIntentsRegistry* intents_registry = |
| 44 scoped_ptr<ActionBoxMenuModel> menu_model( | 67 WebIntentsRegistryFactory::GetForProfile(browser_->profile()); |
| 45 new ActionBoxMenuModel(browser_, this)); | 68 intents_registry->GetIntentServices( |
| 46 | 69 ASCIIToUTF16(kShareIntentAction), |
| 47 // Add the extensions. | 70 ASCIIToUTF16(kShareIntentMimeType), |
| 48 ExtensionService* extension_service = | 71 base::Bind( |
| 49 extensions::ExtensionSystem::Get(browser_->profile())-> | 72 &ActionBoxButtonController::OnWebIntentServicesAvailable, |
| 50 extension_service(); | 73 weak_ptr_factory_.GetWeakPtr())); |
| 51 const extensions::ExtensionList& extensions = | |
| 52 extension_service->toolbar_model()->action_box_menu_items(); | |
| 53 for (extensions::ExtensionList::const_iterator it = extensions.begin(); | |
| 54 it != extensions.end(); ++it) { | |
| 55 menu_model->AddExtension(**it, GetCommandIdForExtension(**it)); | |
| 56 } | |
| 57 | |
| 58 delegate_->ShowMenu(menu_model.Pass()); | |
| 59 } | 74 } |
| 60 | 75 |
| 61 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { | 76 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { |
| 62 return false; | 77 return false; |
| 63 } | 78 } |
| 64 | 79 |
| 65 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { | 80 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { |
| 66 return true; | 81 return true; |
| 67 } | 82 } |
| 68 | 83 |
| 69 bool ActionBoxButtonController::GetAcceleratorForCommandId( | 84 bool ActionBoxButtonController::GetAcceleratorForCommandId( |
| 70 int command_id, | 85 int command_id, |
| 71 ui::Accelerator* accelerator) { | 86 ui::Accelerator* accelerator) { |
| 72 return false; | 87 return false; |
| 73 } | 88 } |
| 74 | 89 |
| 75 void ActionBoxButtonController::ExecuteCommand(int command_id) { | 90 void ActionBoxButtonController::ExecuteCommand(int command_id) { |
| 76 // It might be a command associated with an extension. | 91 // Handle explicit intent triggers for share intent commands. |
| 92 if (share_intent_service_ids_.count(command_id) > 0) { | |
| 93 TriggerExplicitShareIntent(share_intent_service_ids_[command_id]); | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 // Handle link to the CWS web store. | |
| 98 if (command_id == IDC_CWS_FIND_SHARE_INTENTS) { | |
| 99 NavigateToWebStoreShareIntentsList(); | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 // Handle commands associated with extensions. | |
| 77 // Note that the extension might have been uninstalled or disabled while the | 104 // Note that the extension might have been uninstalled or disabled while the |
| 78 // menu was open (sync perhaps?) but that will just fall through safely. | 105 // menu was open (sync perhaps?) but that will just fall through safely. |
| 79 const extensions::Extension* extension = | 106 const extensions::Extension* extension = |
| 80 GetExtensionForCommandId(command_id); | 107 GetExtensionForCommandId(command_id); |
| 81 if (extension) { | 108 if (extension) { |
| 82 // TODO(kalman): do something with the result. | 109 // TODO(kalman): do something with the result. |
| 83 extensions::ExtensionSystem::Get(browser_->profile())-> | 110 extensions::ExtensionSystem::Get(browser_->profile())-> |
| 84 extension_service()->toolbar_model()->ExecuteBrowserAction( | 111 extension_service()->toolbar_model()->ExecuteBrowserAction( |
| 85 extension, browser_, NULL); | 112 extension, browser_, NULL); |
| 86 return; | 113 return; |
| 87 } | 114 } |
| 88 | 115 |
| 116 // Otherwise, let the browser handle the command. | |
| 89 chrome::ExecuteCommand(browser_, command_id); | 117 chrome::ExecuteCommand(browser_, command_id); |
| 90 } | 118 } |
| 91 | 119 |
| 92 int ActionBoxButtonController::GetCommandIdForExtension( | 120 int ActionBoxButtonController::GetCommandIdForExtension( |
| 93 const extensions::Extension& extension) { | 121 const extensions::Extension& extension) { |
| 94 ExtensionIdCommandMap::iterator it = | 122 ExtensionIdCommandMap::iterator it = |
| 95 extension_command_ids_.find(extension.id()); | 123 extension_command_ids_.find(extension.id()); |
| 96 if (it != extension_command_ids_.end()) | 124 if (it != extension_command_ids_.end()) |
| 97 return it->second; | 125 return it->second; |
| 98 int command_id = next_extension_command_id_++; | 126 int command_id = next_extension_command_id_++; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 126 const content::NotificationSource& source, | 154 const content::NotificationSource& source, |
| 127 const content::NotificationDetails& details) { | 155 const content::NotificationDetails& details) { |
| 128 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | 156 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 129 const extensions::Extension* extension = | 157 const extensions::Extension* extension = |
| 130 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 158 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 131 | 159 |
| 132 // TODO(kalman): if there's a menu open, remove it from that too. | 160 // TODO(kalman): if there's a menu open, remove it from that too. |
| 133 // We may also want to listen to EXTENSION_LOADED to do the opposite. | 161 // We may also want to listen to EXTENSION_LOADED to do the opposite. |
| 134 extension_command_ids_.erase(extension->id()); | 162 extension_command_ids_.erase(extension->id()); |
| 135 } | 163 } |
| 164 | |
| 165 void ActionBoxButtonController::OnWebIntentServicesAvailable( | |
| 166 const std::vector<webkit_glue::WebIntentServiceData>& services) { | |
| 167 scoped_ptr<ActionBoxMenuModel> menu_model( | |
| 168 new ActionBoxMenuModel(browser_, this)); | |
| 169 | |
| 170 // Add Share intent items. | |
| 171 next_share_intent_command_id_ = IDC_ACTIONBOX_SHARE_FIRST; | |
| 172 share_intent_service_ids_.clear(); | |
| 173 for (size_t i = 0; i < services.size(); ++i) { | |
| 174 int command_id = next_share_intent_command_id_++; | |
| 175 if (command_id > IDC_ACTIONBOX_SHARE_LAST) { | |
| 176 break; | |
| 177 } | |
| 178 share_intent_service_ids_[command_id] = services[i].service_url; | |
| 179 menu_model->AddItem(command_id, services[i].title); | |
| 180 } | |
| 181 | |
| 182 // Add link to the Web Store to find additional share intents. | |
| 183 menu_model->AddItemWithStringId(IDC_CWS_FIND_SHARE_INTENTS, | |
| 184 IDS_FIND_SHARE_INTENTS); | |
| 185 | |
| 186 // Add Extensions. | |
| 187 next_extension_command_id_ = IDC_ACTIONBOX_EXTENSION_FIRST; | |
| 188 extension_command_ids_.clear(); | |
| 189 ExtensionService* extension_service = | |
| 190 extensions::ExtensionSystem::Get(browser_->profile())-> | |
| 191 extension_service(); | |
| 192 const extensions::ExtensionList& extensions = | |
| 193 extension_service->toolbar_model()->action_box_menu_items(); | |
| 194 for (extensions::ExtensionList::const_iterator it = extensions.begin(); | |
| 195 it != extensions.end(); ++it) { | |
| 196 menu_model->AddExtension(**it, GetCommandIdForExtension(**it)); | |
| 197 } | |
| 198 | |
| 199 // And show the menu. | |
| 200 delegate_->ShowMenu(menu_model.Pass()); | |
| 201 } | |
| 202 | |
| 203 void ActionBoxButtonController::TriggerExplicitShareIntent( | |
| 204 const GURL& share_service_url) { | |
| 205 const GURL& current_url = chrome::GetActiveWebContents(browser_)->GetURL(); | |
| 206 webkit_glue::WebIntentData intent_data( | |
| 207 ASCIIToUTF16(kShareIntentAction), | |
| 208 ASCIIToUTF16(kShareIntentMimeType), | |
| 209 UTF8ToUTF16(current_url.spec())); | |
| 210 intent_data.service = share_service_url; | |
| 211 scoped_ptr<content::WebIntentsDispatcher> dispatcher( | |
| 212 content::WebIntentsDispatcher::Create(intent_data)); | |
| 213 static_cast<content::WebContentsDelegate*>(browser_)-> | |
|
Greg Billock
2012/10/12 18:41:50
The new API for you is now checked in. You can cal
skare_
2012/10/12 23:42:51
Thanks! I made this change in a local git branch a
| |
| 214 WebIntentDispatch(NULL, dispatcher.release()); | |
| 215 } | |
| 216 | |
| 217 void ActionBoxButtonController::NavigateToWebStoreShareIntentsList() { | |
| 218 GURL query_url = extension_urls::GetWebstoreIntentQueryURL( | |
| 219 "http://webintents.org/share", | |
| 220 "text/uri-list"); | |
| 221 chrome::NavigateParams params(browser_->profile(), query_url, | |
| 222 content::PAGE_TRANSITION_LINK); | |
| 223 params.disposition = NEW_FOREGROUND_TAB; | |
| 224 chrome::Navigate(¶ms); | |
| 225 } | |
| OLD | NEW |