| Index: chrome/browser/ui/toolbar/action_box_button_controller.cc
|
| diff --git a/chrome/browser/ui/toolbar/action_box_button_controller.cc b/chrome/browser/ui/toolbar/action_box_button_controller.cc
|
| index 5044d8f856925c53a4f5efe3073742efbf53e67f..030053166b63085ad7d5b68c5af9249392bc78e1 100644
|
| --- a/chrome/browser/ui/toolbar/action_box_button_controller.cc
|
| +++ b/chrome/browser/ui/toolbar/action_box_button_controller.cc
|
| @@ -7,6 +7,8 @@
|
| #include "base/logging.h"
|
| #include "base/metrics/histogram.h"
|
| #include "base/utf_string_conversions.h"
|
| +#include "chrome/app/chrome_command_ids.h"
|
| +#include "chrome/browser/extensions/api/page_launcher/page_launcher_api.h"
|
| #include "chrome/browser/extensions/extension_service.h"
|
| #include "chrome/browser/extensions/extension_system.h"
|
| #include "chrome/browser/profiles/profile.h"
|
| @@ -15,39 +17,27 @@
|
| #include "chrome/browser/ui/tabs/tab_strip_model.h"
|
| #include "chrome/browser/ui/toolbar/action_box_menu_model.h"
|
| #include "chrome/common/chrome_notification_types.h"
|
| +#include "chrome/common/extensions/api/extension_action/action_info.h"
|
| #include "chrome/common/extensions/extension.h"
|
| #include "chrome/common/extensions/extension_set.h"
|
| #include "content/public/browser/notification_service.h"
|
| #include "content/public/browser/notification_source.h"
|
| #include "content/public/browser/user_metrics.h"
|
| #include "content/public/browser/web_contents.h"
|
| -#include "grit/generated_resources.h"
|
| -#include "webkit/glue/webkit_glue.h"
|
| -
|
| -namespace {
|
| -
|
| -// Share intents get command IDs that are beyond the maximal valid command ID
|
| -// (0xDFFF) so that they are not confused with actual commands that appear in
|
| -// the menu. Extensions get a reserved block of commands after share handlers.
|
| -// For more details see: chrome/app/chrome_command_ids.h
|
| -const int kMaxShareItemsToShow = 20; // TODO(skare): Show extras in submenu.
|
| -enum ActionBoxLocalCommandIds {
|
| - CWS_FIND_SHARE_INTENTS_COMMAND = 0xE000,
|
| - SHARE_COMMAND_FIRST,
|
| - SHARE_COMMAND_LAST =
|
| - SHARE_COMMAND_FIRST + kMaxShareItemsToShow - 1,
|
| - EXTENSION_COMMAND_FIRST
|
| -};
|
| -
|
| -} // namespace
|
|
|
| using content::UserMetricsAction;
|
| +using content::WebContents;
|
| +using extensions::ActionInfo;
|
| +
|
| +void ActionBoxButtonController::Delegate::ShowMenu(
|
| + scoped_ptr<ActionBoxMenuModel> menu_model) {
|
| +}
|
|
|
| ActionBoxButtonController::ActionBoxButtonController(Browser* browser,
|
| Delegate* delegate)
|
| : browser_(browser),
|
| delegate_(delegate),
|
| - next_extension_command_id_(EXTENSION_COMMAND_FIRST) {
|
| + next_command_id_(0) {
|
| DCHECK(browser_);
|
| DCHECK(delegate_);
|
| registrar_.Add(this,
|
| @@ -62,19 +52,18 @@ void ActionBoxButtonController::OnButtonClicked() {
|
| scoped_ptr<ActionBoxMenuModel> menu_model(
|
| new ActionBoxMenuModel(browser_, this));
|
|
|
| - ExtensionService* extension_service =
|
| + const ExtensionSet* extensions =
|
| extensions::ExtensionSystem::Get(browser_->profile())->
|
| - extension_service();
|
| -
|
| - // Add Extensions.
|
| - next_extension_command_id_ = EXTENSION_COMMAND_FIRST;
|
| - extension_command_ids_.clear();
|
| - const extensions::ExtensionList& extensions =
|
| - extension_service->toolbar_model()->action_box_menu_items();
|
| - for (extensions::ExtensionList::const_iterator it = extensions.begin();
|
| - it != extensions.end(); ++it) {
|
| - menu_model->AddExtension(**it, GetCommandIdForExtension(**it));
|
| + extension_service()->extensions();
|
| + for (ExtensionSet::const_iterator it = extensions->begin();
|
| + it != extensions->end(); ++it) {
|
| + const extensions::Extension* extension = *it;
|
| + if (ActionInfo::GetPageLauncherInfo(extension)) {
|
| + int command_id = GetCommandIdForExtension(*extension);
|
| + menu_model->AddExtension(*extension, command_id);
|
| + }
|
| }
|
| + content::RecordAction(UserMetricsAction("ActionBox.ClickButton"));
|
|
|
| // And show the menu.
|
| delegate_->ShowMenu(menu_model.Pass());
|
| @@ -95,16 +84,21 @@ bool ActionBoxButtonController::GetAcceleratorForCommandId(
|
| }
|
|
|
| void ActionBoxButtonController::ExecuteCommand(int command_id) {
|
| - // Handle commands associated with extensions.
|
| - // Note that the extension might have been uninstalled or disabled while the
|
| - // menu was open (sync perhaps?) but that will just fall through safely.
|
| - const extensions::Extension* extension =
|
| - GetExtensionForCommandId(command_id);
|
| - if (extension) {
|
| - // TODO(kalman): do something with the result.
|
| - extensions::ExtensionSystem::Get(browser_->profile())->
|
| - extension_service()->toolbar_model()->ExecuteBrowserAction(
|
| - extension, browser_, NULL);
|
| + // If the command id belongs to an extension, dispatch an onClicked event
|
| + // to its pageLauncher.
|
| + ExtensionIdCommandMap::const_iterator it =
|
| + extension_command_ids_.find(command_id);
|
| + if (it != extension_command_ids_.end()) {
|
| + WebContents* web_contents =
|
| + browser_->tab_strip_model()->GetActiveWebContents();
|
| + // TODO(rfevang): Send page title and selected text.
|
| + extensions::PageLauncherAPI::DispatchOnClickedEvent(
|
| + browser_->profile(),
|
| + it->second,
|
| + web_contents->GetURL(),
|
| + web_contents->GetContentsMimeType(),
|
| + NULL,
|
| + NULL);
|
| return;
|
| }
|
|
|
| @@ -114,34 +108,32 @@ void ActionBoxButtonController::ExecuteCommand(int command_id) {
|
|
|
| int ActionBoxButtonController::GetCommandIdForExtension(
|
| const extensions::Extension& extension) {
|
| - ExtensionIdCommandMap::iterator it =
|
| - extension_command_ids_.find(extension.id());
|
| - if (it != extension_command_ids_.end())
|
| - return it->second;
|
| - int command_id = next_extension_command_id_++;
|
| -
|
| - // Note that we deliberately don't clean up extension IDs here when
|
| - // extensions are unloaded, so that if they're reloaded they get assigned the
|
| - // old command ID. This situation could arise if an extension is updated
|
| - // while the menu is open. On the other hand, we leak some memory... but
|
| - // that's unlikely to matter.
|
| - extension_command_ids_[extension.id()] = command_id;
|
| + for (ExtensionIdCommandMap::const_iterator it =
|
| + extension_command_ids_.begin();
|
| + it != extension_command_ids_.end(); ++it) {
|
| + if (it->second == extension.id())
|
| + return it->first;
|
| + }
|
| +
|
| + int command_id = GetNextCommandId();
|
| + extension_command_ids_[command_id] = extension.id();
|
|
|
| return command_id;
|
| }
|
|
|
| -const extensions::Extension*
|
| - ActionBoxButtonController::GetExtensionForCommandId(int command_id) {
|
| - for (ExtensionIdCommandMap::iterator it = extension_command_ids_.begin();
|
| - it != extension_command_ids_.end(); ++it) {
|
| - if (it->second == command_id) {
|
| - // Note: might be NULL anyway if the extension has been uninstalled.
|
| - return extensions::ExtensionSystem::Get(browser_->profile())->
|
| - extension_service()->extensions()->GetByID(it->first);
|
| - }
|
| - }
|
| +int ActionBoxButtonController::GetNextCommandId() {
|
| + int command_id = next_command_id_;
|
| + // Find an available command id to return next time the function is called.
|
| + do {
|
| + next_command_id_++;
|
| + // Larger command ids are reserved for non-dynamic entries, so we start
|
| + // reusing old ids at this point.
|
| + if (next_command_id_ >= IDC_MinimumLabelValue)
|
| + next_command_id_ = 0;
|
| + } while (extension_command_ids_.find(next_command_id_) !=
|
| + extension_command_ids_.end());
|
|
|
| - return NULL;
|
| + return command_id;
|
| }
|
|
|
| void ActionBoxButtonController::Observe(
|
| @@ -152,7 +144,14 @@ void ActionBoxButtonController::Observe(
|
| const extensions::Extension* extension =
|
| content::Details<extensions::UnloadedExtensionInfo>(details)->extension;
|
|
|
| + // Remove any entry point command ids associated with the extension.
|
| + for (ExtensionIdCommandMap::iterator it = extension_command_ids_.begin();
|
| + it != extension_command_ids_.end();) {
|
| + if (it->second== extension->id())
|
| + extension_command_ids_.erase(it++);
|
| + else
|
| + ++it;
|
| + }
|
| // TODO(kalman): if there's a menu open, remove it from that too.
|
| // We may also want to listen to EXTENSION_LOADED to do the opposite.
|
| - extension_command_ids_.erase(extension->id());
|
| }
|
|
|