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" |
| 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_share_intent_command_id_(SHARE_COMMAND_FIRST), |
| 54 next_extension_command_id_(EXTENSION_COMMAND_FIRST) { | |
| 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 // Build a menu model and display the menu. |
| 43 // but no extensions. | |
| 44 scoped_ptr<ActionBoxMenuModel> menu_model( | 66 scoped_ptr<ActionBoxMenuModel> menu_model( |
| 45 new ActionBoxMenuModel(browser_, this)); | 67 new ActionBoxMenuModel(browser_, this)); |
| 46 | 68 |
| 47 // Add the extensions. | 69 // Add share intent triggers and a link to the web store. |
| 70 // Web Intents are not currently supported in Incognito mode. | |
| 48 ExtensionService* extension_service = | 71 ExtensionService* extension_service = |
| 49 extensions::ExtensionSystem::Get(browser_->profile())-> | 72 extensions::ExtensionSystem::Get(browser_->profile())-> |
| 50 extension_service(); | 73 extension_service(); |
| 74 if (!browser_->profile()->IsOffTheRecord()) { | |
| 75 next_share_intent_command_id_ = SHARE_COMMAND_FIRST; | |
|
Peter Kasting
2012/10/15 19:33:09
Nit: Why does this need to be a member? It seems
skare_
2012/10/15 21:07:27
Done.
| |
| 76 share_intent_service_ids_.clear(); | |
| 77 const ExtensionSet* extension_set = extension_service->extensions(); | |
| 78 WebIntentsRegistry* intents_registry = | |
| 79 WebIntentsRegistryFactory::GetForProfile(browser_->profile()); | |
|
Peter Kasting
2012/10/15 19:33:09
Nit: Indent 4
skare_
2012/10/15 21:07:27
Done.
| |
| 80 for (ExtensionSet::const_iterator it = extension_set->begin(); | |
| 81 it != extension_set->end(); ++it) { | |
| 82 const extensions::Extension* extension = *it; | |
| 83 WebIntentsRegistry::IntentServiceList services; | |
| 84 intents_registry->GetIntentServicesForExtensionFilter( | |
| 85 ASCIIToUTF16(kShareIntentAction), | |
| 86 ASCIIToUTF16(kShareIntentMimeType), | |
| 87 extension->id(), | |
| 88 &services); | |
| 89 if (services.size() > 0) { | |
|
Peter Kasting
2012/10/15 19:33:09
Nit: !empty()
skare_
2012/10/15 21:07:27
Done.
| |
| 90 int command_id = next_share_intent_command_id_++; | |
| 91 if (command_id > SHARE_COMMAND_LAST) { | |
|
Peter Kasting
2012/10/15 19:33:09
Nit: {} unnecessary
skare_
2012/10/15 21:07:27
Done.
| |
| 92 break; | |
| 93 } | |
| 94 // TODO(skare): If an intent supports multiple services, be able to | |
| 95 // disambiguate. Choosing the first matches the picker behavior; see | |
| 96 // TODO in WebIntentPickerController::DispatchToInstalledExtension. | |
| 97 share_intent_service_ids_[command_id] = services[0].service_url; | |
| 98 menu_model->AddItem(command_id, services[0].title); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 // Add link to the Web Store to find additional share intents. | |
| 103 menu_model->AddItemWithStringId(CWS_FIND_SHARE_INTENTS_COMMAND, | |
| 104 IDS_FIND_SHARE_INTENTS); | |
| 105 } | |
| 106 | |
| 107 // Add Extensions. | |
| 108 next_extension_command_id_ = EXTENSION_COMMAND_FIRST; | |
| 109 extension_command_ids_.clear(); | |
| 51 const extensions::ExtensionList& extensions = | 110 const extensions::ExtensionList& extensions = |
| 52 extension_service->toolbar_model()->action_box_menu_items(); | 111 extension_service->toolbar_model()->action_box_menu_items(); |
| 53 for (extensions::ExtensionList::const_iterator it = extensions.begin(); | 112 for (extensions::ExtensionList::const_iterator it = extensions.begin(); |
| 54 it != extensions.end(); ++it) { | 113 it != extensions.end(); ++it) { |
| 55 menu_model->AddExtension(**it, GetCommandIdForExtension(**it)); | 114 menu_model->AddExtension(**it, GetCommandIdForExtension(**it)); |
| 56 } | 115 } |
| 57 | 116 |
| 117 // And show the menu. | |
| 58 delegate_->ShowMenu(menu_model.Pass()); | 118 delegate_->ShowMenu(menu_model.Pass()); |
| 59 } | 119 } |
| 60 | 120 |
| 61 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { | 121 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { |
| 62 return false; | 122 return false; |
| 63 } | 123 } |
| 64 | 124 |
| 65 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { | 125 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { |
| 66 return true; | 126 return true; |
| 67 } | 127 } |
| 68 | 128 |
| 69 bool ActionBoxButtonController::GetAcceleratorForCommandId( | 129 bool ActionBoxButtonController::GetAcceleratorForCommandId( |
| 70 int command_id, | 130 int command_id, |
| 71 ui::Accelerator* accelerator) { | 131 ui::Accelerator* accelerator) { |
| 72 return false; | 132 return false; |
| 73 } | 133 } |
| 74 | 134 |
| 75 void ActionBoxButtonController::ExecuteCommand(int command_id) { | 135 void ActionBoxButtonController::ExecuteCommand(int command_id) { |
| 76 // It might be a command associated with an extension. | 136 // Handle explicit intent triggers for share intent commands. |
| 137 if (share_intent_service_ids_.count(command_id) > 0) { | |
| 138 TriggerExplicitShareIntent(share_intent_service_ids_[command_id]); | |
|
Peter Kasting
2012/10/15 19:33:09
Nit: I probably wouldn't call these out to their o
skare_
2012/10/15 21:07:27
Just to keep this function from growing too long;
Peter Kasting
2012/10/15 21:54:39
It doesn't really matter much. There are pros and
| |
| 139 return; | |
| 140 } | |
| 141 | |
| 142 // Handle link to the CWS web store. | |
| 143 if (command_id == CWS_FIND_SHARE_INTENTS_COMMAND) { | |
| 144 NavigateToWebStoreShareIntentsList(); | |
| 145 return; | |
| 146 } | |
| 147 | |
| 148 // Handle commands associated with extensions. | |
| 77 // Note that the extension might have been uninstalled or disabled while the | 149 // 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. | 150 // menu was open (sync perhaps?) but that will just fall through safely. |
| 79 const extensions::Extension* extension = | 151 const extensions::Extension* extension = |
| 80 GetExtensionForCommandId(command_id); | 152 GetExtensionForCommandId(command_id); |
| 81 if (extension) { | 153 if (extension) { |
| 82 // TODO(kalman): do something with the result. | 154 // TODO(kalman): do something with the result. |
| 83 extensions::ExtensionSystem::Get(browser_->profile())-> | 155 extensions::ExtensionSystem::Get(browser_->profile())-> |
| 84 extension_service()->toolbar_model()->ExecuteBrowserAction( | 156 extension_service()->toolbar_model()->ExecuteBrowserAction( |
| 85 extension, browser_, NULL); | 157 extension, browser_, NULL); |
| 86 return; | 158 return; |
| 87 } | 159 } |
| 88 | 160 |
| 161 // Otherwise, let the browser handle the command. | |
| 89 chrome::ExecuteCommand(browser_, command_id); | 162 chrome::ExecuteCommand(browser_, command_id); |
| 90 } | 163 } |
| 91 | 164 |
| 92 int ActionBoxButtonController::GetCommandIdForExtension( | 165 int ActionBoxButtonController::GetCommandIdForExtension( |
| 93 const extensions::Extension& extension) { | 166 const extensions::Extension& extension) { |
| 94 ExtensionIdCommandMap::iterator it = | 167 ExtensionIdCommandMap::iterator it = |
| 95 extension_command_ids_.find(extension.id()); | 168 extension_command_ids_.find(extension.id()); |
| 96 if (it != extension_command_ids_.end()) | 169 if (it != extension_command_ids_.end()) |
| 97 return it->second; | 170 return it->second; |
| 98 int command_id = next_extension_command_id_++; | 171 int command_id = next_extension_command_id_++; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 126 const content::NotificationSource& source, | 199 const content::NotificationSource& source, |
| 127 const content::NotificationDetails& details) { | 200 const content::NotificationDetails& details) { |
| 128 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | 201 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 129 const extensions::Extension* extension = | 202 const extensions::Extension* extension = |
| 130 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 203 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 131 | 204 |
| 132 // TODO(kalman): if there's a menu open, remove it from that too. | 205 // 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. | 206 // We may also want to listen to EXTENSION_LOADED to do the opposite. |
| 134 extension_command_ids_.erase(extension->id()); | 207 extension_command_ids_.erase(extension->id()); |
| 135 } | 208 } |
| 209 | |
| 210 void ActionBoxButtonController::TriggerExplicitShareIntent( | |
| 211 const GURL& share_service_url) { | |
| 212 const GURL& current_url = chrome::GetActiveWebContents(browser_)->GetURL(); | |
| 213 webkit_glue::WebIntentData intent_data( | |
| 214 ASCIIToUTF16(kShareIntentAction), | |
| 215 ASCIIToUTF16(kShareIntentMimeType), | |
| 216 UTF8ToUTF16(current_url.spec())); | |
| 217 intent_data.service = share_service_url; | |
| 218 scoped_ptr<content::WebIntentsDispatcher> dispatcher( | |
|
Peter Kasting
2012/10/15 19:33:09
Nit: Given that we don't use exceptions, is there
skare_
2012/10/15 21:07:27
Done.
| |
| 219 content::WebIntentsDispatcher::Create(intent_data)); | |
| 220 static_cast<content::WebContentsDelegate*>(browser_)-> | |
| 221 WebIntentDispatch(NULL, dispatcher.release()); | |
| 222 } | |
| 223 | |
| 224 void ActionBoxButtonController::NavigateToWebStoreShareIntentsList() { | |
| 225 GURL query_url = extension_urls::GetWebstoreIntentQueryURL( | |
|
Peter Kasting
2012/10/15 19:33:09
Nit: Above you used a const GURL&, here a GURL. W
skare_
2012/10/15 21:07:27
made const&
| |
| 226 kShareIntentAction, | |
| 227 kShareIntentMimeType); | |
| 228 chrome::NavigateParams params(browser_->profile(), query_url, | |
| 229 content::PAGE_TRANSITION_LINK); | |
| 230 params.disposition = NEW_FOREGROUND_TAB; | |
| 231 chrome::Navigate(¶ms); | |
| 232 } | |
| OLD | NEW |