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 kCWSFindShareIntentsCommandId = 0xE000, | |
|
sky
2012/10/11 23:36:25
Enums use ALL_CAPS style.
skare_
2012/10/12 01:02:06
Done.
Switched to IDC_* names for command ids even
sky
2012/10/12 14:33:25
Don't do that, it's easy to misread when using IDC
skare_
2012/10/12 20:21:07
Done.
| |
| 37 kFirstShareIntentCommandId, | |
| 38 kLastShareIntentCommandId = | |
| 39 kFirstShareIntentCommandId + kMaxShareItemsToShow - 1, | |
| 40 kFirstExtensionCommandId | |
| 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_(kFirstShareIntentCommandId), |
| 53 next_extension_command_id_(kFirstExtensionCommandId), | |
| 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() { |
| 63 weak_ptr_factory_.InvalidateWeakPtrs(); | |
|
sky
2012/10/11 23:36:25
Isn't this done for you by the destructor of WeakP
skare_
2012/10/12 01:02:06
Done.
| |
| 64 } | |
| 40 | 65 |
| 41 void ActionBoxButtonController::OnButtonClicked() { | 66 void ActionBoxButtonController::OnButtonClicked() { |
| 42 // Creating the action box menu will populate it with the bookmark star etc, | 67 // Request registered share intents; populate and show menu in the callback. |
| 43 // but no extensions. | 68 WebIntentsRegistry* intents_registry = |
| 69 WebIntentsRegistryFactory::GetForProfile(browser_->profile()); | |
| 70 intents_registry->GetIntentServices( | |
| 71 ASCIIToUTF16(kShareIntentAction), | |
| 72 ASCIIToUTF16(kShareIntentMimeType), | |
| 73 base::Bind( | |
| 74 &ActionBoxButtonController::OnWebIntentServicesAvailable, | |
| 75 weak_ptr_factory_.GetWeakPtr())); | |
| 76 } | |
| 77 | |
| 78 void ActionBoxButtonController::OnWebIntentServicesAvailable( | |
| 79 const std::vector<webkit_glue::WebIntentServiceData>& services) { | |
| 44 scoped_ptr<ActionBoxMenuModel> menu_model( | 80 scoped_ptr<ActionBoxMenuModel> menu_model( |
| 45 new ActionBoxMenuModel(browser_, this)); | 81 new ActionBoxMenuModel(browser_, this)); |
| 46 | 82 |
| 47 // Add the extensions. | 83 // Add Share intent items. |
| 84 next_share_intent_command_id_ = kFirstShareIntentCommandId; | |
| 85 share_intent_service_ids_.clear(); | |
| 86 for (size_t i = 0; i < services.size(); ++i) { | |
| 87 int command_id = next_share_intent_command_id_++; | |
| 88 if (command_id > kLastShareIntentCommandId) { | |
| 89 break; | |
| 90 } | |
| 91 share_intent_service_ids_[command_id] = services[i].service_url; | |
| 92 menu_model->AddItem(command_id, services[i].title); | |
| 93 } | |
| 94 | |
| 95 // Add link to the Web Store to find additional share intents. | |
| 96 menu_model->AddItemWithStringId(kCWSFindShareIntentsCommandId, | |
| 97 IDS_FIND_SHARE_INTENTS); | |
| 98 | |
| 99 // Add Extensions. | |
| 100 next_extension_command_id_ = kFirstExtensionCommandId; | |
| 101 extension_command_ids_.clear(); | |
| 48 ExtensionService* extension_service = | 102 ExtensionService* extension_service = |
| 49 extensions::ExtensionSystem::Get(browser_->profile())-> | 103 extensions::ExtensionSystem::Get(browser_->profile())-> |
| 50 extension_service(); | 104 extension_service(); |
| 51 const extensions::ExtensionList& extensions = | 105 const extensions::ExtensionList& extensions = |
| 52 extension_service->toolbar_model()->action_box_menu_items(); | 106 extension_service->toolbar_model()->action_box_menu_items(); |
| 53 for (extensions::ExtensionList::const_iterator it = extensions.begin(); | 107 for (extensions::ExtensionList::const_iterator it = extensions.begin(); |
| 54 it != extensions.end(); ++it) { | 108 it != extensions.end(); ++it) { |
| 55 menu_model->AddExtension(**it, GetCommandIdForExtension(**it)); | 109 menu_model->AddExtension(**it, GetCommandIdForExtension(**it)); |
| 56 } | 110 } |
| 57 | 111 |
| 112 // And show the menu. | |
|
sky
2012/10/11 23:36:25
How do you know how long it takes to get the servi
skare_
2012/10/12 01:02:06
is the pattern to watch for closed and then cancel
sky
2012/10/12 14:33:25
I don't know that there is anything you could look
skare_
2012/10/12 20:21:07
Resolved -- in the latest revision the population
| |
| 58 delegate_->ShowMenu(menu_model.Pass()); | 113 delegate_->ShowMenu(menu_model.Pass()); |
| 59 } | 114 } |
| 60 | 115 |
| 61 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { | 116 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { |
| 62 return false; | 117 return false; |
| 63 } | 118 } |
| 64 | 119 |
| 65 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { | 120 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { |
| 66 return true; | 121 return true; |
| 67 } | 122 } |
| 68 | 123 |
| 69 bool ActionBoxButtonController::GetAcceleratorForCommandId( | 124 bool ActionBoxButtonController::GetAcceleratorForCommandId( |
| 70 int command_id, | 125 int command_id, |
| 71 ui::Accelerator* accelerator) { | 126 ui::Accelerator* accelerator) { |
| 72 return false; | 127 return false; |
| 73 } | 128 } |
| 74 | 129 |
| 75 void ActionBoxButtonController::ExecuteCommand(int command_id) { | 130 void ActionBoxButtonController::ExecuteCommand(int command_id) { |
| 76 // It might be a command associated with an extension. | 131 // Handle explicit intent triggers for share intent commands. |
| 132 if (share_intent_service_ids_.count(command_id) > 0) { | |
| 133 TriggerExplicitShareIntent(share_intent_service_ids_[command_id]); | |
| 134 return; | |
| 135 } | |
| 136 | |
| 137 // Handle link to the CWS web store. | |
| 138 if (command_id == kCWSFindShareIntentsCommandId) { | |
| 139 NavigateToWebStoreShareIntentsList(); | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 // Handle commands associated with extensions. | |
| 77 // Note that the extension might have been uninstalled or disabled while the | 144 // 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. | 145 // menu was open (sync perhaps?) but that will just fall through safely. |
| 79 const extensions::Extension* extension = | 146 const extensions::Extension* extension = |
| 80 GetExtensionForCommandId(command_id); | 147 GetExtensionForCommandId(command_id); |
| 81 if (extension) { | 148 if (extension) { |
| 82 // TODO(kalman): do something with the result. | 149 // TODO(kalman): do something with the result. |
| 83 extensions::ExtensionSystem::Get(browser_->profile())-> | 150 extensions::ExtensionSystem::Get(browser_->profile())-> |
| 84 extension_service()->toolbar_model()->ExecuteBrowserAction( | 151 extension_service()->toolbar_model()->ExecuteBrowserAction( |
| 85 extension, browser_, NULL); | 152 extension, browser_, NULL); |
| 86 return; | 153 return; |
| 87 } | 154 } |
| 88 | 155 |
| 156 // Otherwise, let the browser handle the command. | |
| 89 chrome::ExecuteCommand(browser_, command_id); | 157 chrome::ExecuteCommand(browser_, command_id); |
| 90 } | 158 } |
| 91 | 159 |
| 92 int ActionBoxButtonController::GetCommandIdForExtension( | 160 int ActionBoxButtonController::GetCommandIdForExtension( |
| 93 const extensions::Extension& extension) { | 161 const extensions::Extension& extension) { |
| 94 ExtensionIdCommandMap::iterator it = | 162 ExtensionIdCommandMap::iterator it = |
| 95 extension_command_ids_.find(extension.id()); | 163 extension_command_ids_.find(extension.id()); |
| 96 if (it != extension_command_ids_.end()) | 164 if (it != extension_command_ids_.end()) |
| 97 return it->second; | 165 return it->second; |
| 98 int command_id = next_extension_command_id_++; | 166 int command_id = next_extension_command_id_++; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 126 const content::NotificationSource& source, | 194 const content::NotificationSource& source, |
| 127 const content::NotificationDetails& details) { | 195 const content::NotificationDetails& details) { |
| 128 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | 196 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 129 const extensions::Extension* extension = | 197 const extensions::Extension* extension = |
| 130 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 198 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 131 | 199 |
| 132 // TODO(kalman): if there's a menu open, remove it from that too. | 200 // 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. | 201 // We may also want to listen to EXTENSION_LOADED to do the opposite. |
| 134 extension_command_ids_.erase(extension->id()); | 202 extension_command_ids_.erase(extension->id()); |
| 135 } | 203 } |
| 204 | |
| 205 void ActionBoxButtonController::NavigateToWebStoreShareIntentsList() { | |
|
sky
2012/10/11 23:36:25
make order match header.
skare_
2012/10/12 01:02:06
Done.
| |
| 206 Profile* profile = Profile::FromBrowserContext( | |
|
sky
2012/10/11 23:36:25
browser_->profile()
skare_
2012/10/12 01:02:06
done and inlined below, thanks.
| |
| 207 chrome::GetActiveWebContents(browser_)->GetBrowserContext()); | |
| 208 if (!profile) | |
|
sky
2012/10/11 23:36:25
Remove this.
skare_
2012/10/12 01:02:06
Done.
| |
| 209 return; | |
| 210 GURL query_url = extension_urls::GetWebstoreIntentQueryURL( | |
| 211 "http://webintents.org/share", | |
| 212 "text/uri-list"); | |
| 213 | |
| 214 chrome::NavigateParams params(profile, query_url, | |
| 215 content::PAGE_TRANSITION_LINK); | |
| 216 params.disposition = NEW_FOREGROUND_TAB; | |
| 217 chrome::Navigate(¶ms); | |
| 218 } | |
| 219 | |
| 220 void ActionBoxButtonController::TriggerExplicitShareIntent( | |
| 221 const GURL& share_service_url) { | |
| 222 const GURL& current_url = chrome::GetActiveWebContents(browser_)->GetURL(); | |
| 223 webkit_glue::WebIntentData intent_data( | |
| 224 ASCIIToUTF16(kShareIntentAction), | |
| 225 ASCIIToUTF16(kShareIntentMimeType), | |
| 226 UTF8ToUTF16(current_url.spec())); | |
| 227 intent_data.service = share_service_url; | |
| 228 scoped_ptr<content::WebIntentsDispatcher> dispatcher( | |
| 229 content::WebIntentsDispatcher::Create(intent_data)); | |
| 230 static_cast<content::WebContentsDelegate*>(browser_)-> | |
| 231 WebIntentDispatch(NULL, dispatcher.release()); | |
| 232 } | |
| OLD | NEW |