| 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" |
| 20 #include "chrome/common/extensions/extension_set.h" |
| 15 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 16 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
| 23 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/browser/web_intents_dispatcher.h" |
| 25 #include "grit/generated_resources.h" |
| 26 #include "webkit/glue/web_intent_data.h" |
| 27 #include "webkit/glue/webkit_glue.h" |
| 17 | 28 |
| 18 namespace { | 29 namespace { |
| 19 | 30 |
| 20 // Extensions get command IDs that are beyond the maximal valid command ID | 31 // 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 | 32 // (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 | 33 // the menu. Extensions get a reserved block of commands after share handlers. |
| 23 const int kFirstExtensionCommandId = 0xE000; | 34 // For more details see: chrome/app/chrome_command_ids.h |
| 35 const int kMaxShareItemsToShow = 20; // TODO(skare): Show extras in submenu. |
| 36 enum ActionBoxLocalCommandIds { |
| 37 CWS_FIND_SHARE_INTENTS_COMMAND = 0xE000, |
| 38 SHARE_COMMAND_FIRST, |
| 39 SHARE_COMMAND_LAST = |
| 40 SHARE_COMMAND_FIRST + kMaxShareItemsToShow - 1, |
| 41 EXTENSION_COMMAND_FIRST |
| 42 }; |
| 43 |
| 44 const char kShareIntentAction[] = "http://webintents.org/share"; |
| 45 const char kShareIntentMimeType[] = "text/uri-list"; |
| 24 | 46 |
| 25 } // namespace | 47 } // namespace |
| 26 | 48 |
| 27 ActionBoxButtonController::ActionBoxButtonController(Browser* browser, | 49 ActionBoxButtonController::ActionBoxButtonController(Browser* browser, |
| 28 Delegate* delegate) | 50 Delegate* delegate) |
| 29 : browser_(browser), | 51 : browser_(browser), |
| 30 delegate_(delegate), | 52 delegate_(delegate), |
| 31 next_extension_command_id_(kFirstExtensionCommandId) { | 53 next_extension_command_id_(EXTENSION_COMMAND_FIRST) { |
| 32 DCHECK(browser_); | 54 DCHECK(browser_); |
| 33 DCHECK(delegate_); | 55 DCHECK(delegate_); |
| 34 registrar_.Add(this, | 56 registrar_.Add(this, |
| 35 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 57 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 36 content::Source<Profile>(browser->profile())); | 58 content::Source<Profile>(browser->profile())); |
| 37 } | 59 } |
| 38 | 60 |
| 39 ActionBoxButtonController::~ActionBoxButtonController() {} | 61 ActionBoxButtonController::~ActionBoxButtonController() {} |
| 40 | 62 |
| 41 void ActionBoxButtonController::OnButtonClicked() { | 63 void ActionBoxButtonController::OnButtonClicked() { |
| 42 // Creating the action box menu will populate it with the bookmark star etc, | 64 // Build a menu model and display the menu. |
| 43 // but no extensions. | |
| 44 scoped_ptr<ActionBoxMenuModel> menu_model( | 65 scoped_ptr<ActionBoxMenuModel> menu_model( |
| 45 new ActionBoxMenuModel(browser_, this)); | 66 new ActionBoxMenuModel(browser_, this)); |
| 46 | 67 |
| 47 // Add the extensions. | 68 // Add share intent triggers and a link to the web store. |
| 69 // Web Intents are not currently supported in Incognito mode. |
| 48 ExtensionService* extension_service = | 70 ExtensionService* extension_service = |
| 49 extensions::ExtensionSystem::Get(browser_->profile())-> | 71 extensions::ExtensionSystem::Get(browser_->profile())-> |
| 50 extension_service(); | 72 extension_service(); |
| 73 if (!browser_->profile()->IsOffTheRecord()) { |
| 74 int next_share_intent_command_id = SHARE_COMMAND_FIRST; |
| 75 share_intent_service_ids_.clear(); |
| 76 const ExtensionSet* extension_set = extension_service->extensions(); |
| 77 WebIntentsRegistry* intents_registry = |
| 78 WebIntentsRegistryFactory::GetForProfile(browser_->profile()); |
| 79 for (ExtensionSet::const_iterator it = extension_set->begin(); |
| 80 it != extension_set->end(); ++it) { |
| 81 const extensions::Extension* extension = *it; |
| 82 WebIntentsRegistry::IntentServiceList services; |
| 83 intents_registry->GetIntentServicesForExtensionFilter( |
| 84 ASCIIToUTF16(kShareIntentAction), |
| 85 ASCIIToUTF16(kShareIntentMimeType), |
| 86 extension->id(), |
| 87 &services); |
| 88 if (!services.empty()) { |
| 89 int command_id = next_share_intent_command_id++; |
| 90 if (command_id > SHARE_COMMAND_LAST) |
| 91 break; |
| 92 // TODO(skare): If an intent supports multiple services, be able to |
| 93 // disambiguate. Choosing the first matches the picker behavior; see |
| 94 // TODO in WebIntentPickerController::DispatchToInstalledExtension. |
| 95 share_intent_service_ids_[command_id] = services[0].service_url; |
| 96 menu_model->AddItem(command_id, services[0].title); |
| 97 } |
| 98 } |
| 99 |
| 100 // Add link to the Web Store to find additional share intents. |
| 101 menu_model->AddItemWithStringId(CWS_FIND_SHARE_INTENTS_COMMAND, |
| 102 IDS_FIND_SHARE_INTENTS); |
| 103 } |
| 104 |
| 105 // Add Extensions. |
| 106 next_extension_command_id_ = EXTENSION_COMMAND_FIRST; |
| 107 extension_command_ids_.clear(); |
| 51 const extensions::ExtensionList& extensions = | 108 const extensions::ExtensionList& extensions = |
| 52 extension_service->toolbar_model()->action_box_menu_items(); | 109 extension_service->toolbar_model()->action_box_menu_items(); |
| 53 for (extensions::ExtensionList::const_iterator it = extensions.begin(); | 110 for (extensions::ExtensionList::const_iterator it = extensions.begin(); |
| 54 it != extensions.end(); ++it) { | 111 it != extensions.end(); ++it) { |
| 55 menu_model->AddExtension(**it, GetCommandIdForExtension(**it)); | 112 menu_model->AddExtension(**it, GetCommandIdForExtension(**it)); |
| 56 } | 113 } |
| 57 | 114 |
| 115 // And show the menu. |
| 58 delegate_->ShowMenu(menu_model.Pass()); | 116 delegate_->ShowMenu(menu_model.Pass()); |
| 59 } | 117 } |
| 60 | 118 |
| 61 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { | 119 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { |
| 62 return false; | 120 return false; |
| 63 } | 121 } |
| 64 | 122 |
| 65 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { | 123 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { |
| 66 return true; | 124 return true; |
| 67 } | 125 } |
| 68 | 126 |
| 69 bool ActionBoxButtonController::GetAcceleratorForCommandId( | 127 bool ActionBoxButtonController::GetAcceleratorForCommandId( |
| 70 int command_id, | 128 int command_id, |
| 71 ui::Accelerator* accelerator) { | 129 ui::Accelerator* accelerator) { |
| 72 return false; | 130 return false; |
| 73 } | 131 } |
| 74 | 132 |
| 75 void ActionBoxButtonController::ExecuteCommand(int command_id) { | 133 void ActionBoxButtonController::ExecuteCommand(int command_id) { |
| 76 // It might be a command associated with an extension. | 134 // Handle explicit intent triggers for share intent commands. |
| 135 if (share_intent_service_ids_.count(command_id) > 0) { |
| 136 TriggerExplicitShareIntent(share_intent_service_ids_[command_id]); |
| 137 return; |
| 138 } |
| 139 |
| 140 // Handle link to the CWS web store. |
| 141 if (command_id == CWS_FIND_SHARE_INTENTS_COMMAND) { |
| 142 NavigateToWebStoreShareIntentsList(); |
| 143 return; |
| 144 } |
| 145 |
| 146 // Handle commands associated with extensions. |
| 77 // Note that the extension might have been uninstalled or disabled while the | 147 // 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. | 148 // menu was open (sync perhaps?) but that will just fall through safely. |
| 79 const extensions::Extension* extension = | 149 const extensions::Extension* extension = |
| 80 GetExtensionForCommandId(command_id); | 150 GetExtensionForCommandId(command_id); |
| 81 if (extension) { | 151 if (extension) { |
| 82 // TODO(kalman): do something with the result. | 152 // TODO(kalman): do something with the result. |
| 83 extensions::ExtensionSystem::Get(browser_->profile())-> | 153 extensions::ExtensionSystem::Get(browser_->profile())-> |
| 84 extension_service()->toolbar_model()->ExecuteBrowserAction( | 154 extension_service()->toolbar_model()->ExecuteBrowserAction( |
| 85 extension, browser_, NULL); | 155 extension, browser_, NULL); |
| 86 return; | 156 return; |
| 87 } | 157 } |
| 88 | 158 |
| 159 // Otherwise, let the browser handle the command. |
| 89 chrome::ExecuteCommand(browser_, command_id); | 160 chrome::ExecuteCommand(browser_, command_id); |
| 90 } | 161 } |
| 91 | 162 |
| 92 int ActionBoxButtonController::GetCommandIdForExtension( | 163 int ActionBoxButtonController::GetCommandIdForExtension( |
| 93 const extensions::Extension& extension) { | 164 const extensions::Extension& extension) { |
| 94 ExtensionIdCommandMap::iterator it = | 165 ExtensionIdCommandMap::iterator it = |
| 95 extension_command_ids_.find(extension.id()); | 166 extension_command_ids_.find(extension.id()); |
| 96 if (it != extension_command_ids_.end()) | 167 if (it != extension_command_ids_.end()) |
| 97 return it->second; | 168 return it->second; |
| 98 int command_id = next_extension_command_id_++; | 169 int command_id = next_extension_command_id_++; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 126 const content::NotificationSource& source, | 197 const content::NotificationSource& source, |
| 127 const content::NotificationDetails& details) { | 198 const content::NotificationDetails& details) { |
| 128 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | 199 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 129 const extensions::Extension* extension = | 200 const extensions::Extension* extension = |
| 130 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 201 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 131 | 202 |
| 132 // TODO(kalman): if there's a menu open, remove it from that too. | 203 // 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. | 204 // We may also want to listen to EXTENSION_LOADED to do the opposite. |
| 134 extension_command_ids_.erase(extension->id()); | 205 extension_command_ids_.erase(extension->id()); |
| 135 } | 206 } |
| 207 |
| 208 void ActionBoxButtonController::TriggerExplicitShareIntent( |
| 209 const GURL& share_service_url) { |
| 210 const GURL& current_url = chrome::GetActiveWebContents(browser_)->GetURL(); |
| 211 webkit_glue::WebIntentData intent_data( |
| 212 ASCIIToUTF16(kShareIntentAction), |
| 213 ASCIIToUTF16(kShareIntentMimeType), |
| 214 UTF8ToUTF16(current_url.spec())); |
| 215 intent_data.service = share_service_url; |
| 216 static_cast<content::WebContentsDelegate*>(browser_)->WebIntentDispatch( |
| 217 NULL, content::WebIntentsDispatcher::Create(intent_data)); |
| 218 } |
| 219 |
| 220 void ActionBoxButtonController::NavigateToWebStoreShareIntentsList() { |
| 221 const GURL& query_url = extension_urls::GetWebstoreIntentQueryURL( |
| 222 kShareIntentAction, |
| 223 kShareIntentMimeType); |
| 224 chrome::NavigateParams params(browser_->profile(), query_url, |
| 225 content::PAGE_TRANSITION_LINK); |
| 226 params.disposition = NEW_FOREGROUND_TAB; |
| 227 chrome::Navigate(¶ms); |
| 228 } |
| OLD | NEW |