| 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/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
| 11 #include "chrome/browser/chrome_to_mobile_service.h" |
| 11 #include "chrome/browser/extensions/api/page_launcher/page_launcher_api.h" | 12 #include "chrome/browser/extensions/api/page_launcher/page_launcher_api.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/extension_system.h" | 14 #include "chrome/browser/extensions/extension_system.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" |
| 15 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/browser_commands.h" | 18 #include "chrome/browser/ui/browser_commands.h" |
| 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 18 #include "chrome/browser/ui/toolbar/action_box_menu_model.h" | 20 #include "chrome/browser/ui/toolbar/action_box_menu_model.h" |
| 19 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
| 20 #include "chrome/common/extensions/api/extension_action/action_info.h" | 22 #include "chrome/common/extensions/api/extension_action/action_info.h" |
| 21 #include "chrome/common/extensions/extension.h" | 23 #include "chrome/common/extensions/extension.h" |
| 22 #include "chrome/common/extensions/extension_set.h" | 24 #include "chrome/common/extensions/extension_set.h" |
| 23 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
| 24 #include "content/public/browser/notification_source.h" | 26 #include "content/public/browser/notification_source.h" |
| 25 #include "content/public/browser/render_widget_host_view.h" | 27 #include "content/public/browser/render_widget_host_view.h" |
| 26 #include "content/public/browser/user_metrics.h" | 28 #include "content/public/browser/user_metrics.h" |
| 27 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
| 30 #include "grit/generated_resources.h" |
| 31 #include "grit/theme_resources.h" |
| 32 #include "ui/base/resource/resource_bundle.h" |
| 33 |
| 28 | 34 |
| 29 using content::UserMetricsAction; | 35 using content::UserMetricsAction; |
| 30 using content::WebContents; | 36 using content::WebContents; |
| 31 using extensions::ActionInfo; | 37 using extensions::ActionInfo; |
| 32 | 38 |
| 33 void ActionBoxButtonController::Delegate::ShowMenu( | 39 void ActionBoxButtonController::Delegate::ShowMenu( |
| 34 scoped_ptr<ActionBoxMenuModel> menu_model) { | 40 scoped_ptr<ActionBoxMenuModel> menu_model) { |
| 35 } | 41 } |
| 36 | 42 |
| 37 ActionBoxButtonController::ActionBoxButtonController(Browser* browser, | 43 ActionBoxButtonController::ActionBoxButtonController(Browser* browser, |
| 38 Delegate* delegate) | 44 Delegate* delegate) |
| 39 : browser_(browser), | 45 : browser_(browser), |
| 40 delegate_(delegate), | 46 delegate_(delegate), |
| 41 next_command_id_(0) { | 47 next_command_id_(0) { |
| 42 DCHECK(browser_); | 48 DCHECK(browser_); |
| 43 DCHECK(delegate_); | 49 DCHECK(delegate_); |
| 44 registrar_.Add(this, | 50 registrar_.Add(this, |
| 45 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 51 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 46 content::Source<Profile>(browser->profile())); | 52 content::Source<Profile>(browser->profile())); |
| 47 } | 53 } |
| 48 | 54 |
| 49 ActionBoxButtonController::~ActionBoxButtonController() {} | 55 ActionBoxButtonController::~ActionBoxButtonController() {} |
| 50 | 56 |
| 51 void ActionBoxButtonController::OnButtonClicked() { | 57 scoped_ptr<ActionBoxMenuModel> ActionBoxButtonController::CreateMenuModel() { |
| 52 // Build a menu model and display the menu. | 58 // Build a menu model and display the menu. |
| 53 scoped_ptr<ActionBoxMenuModel> menu_model( | 59 scoped_ptr<ActionBoxMenuModel> menu_model( |
| 54 new ActionBoxMenuModel(browser_, this)); | 60 new ActionBoxMenuModel(browser_->profile(), this)); |
| 61 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 55 | 62 |
| 56 const ExtensionSet* extensions = | 63 // In some unit tests, GetActiveWebContents can return NULL. |
| 64 bool starred = browser_->tab_strip_model()->GetActiveWebContents() && |
| 65 BookmarkTabHelper::FromWebContents(browser_->tab_strip_model()-> |
| 66 GetActiveWebContents())->is_starred(); |
| 67 menu_model->AddItemWithStringId( |
| 68 IDC_BOOKMARK_PAGE_FROM_STAR, |
| 69 starred ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR); |
| 70 menu_model->SetIcon( |
| 71 menu_model->GetIndexOfCommandId(IDC_BOOKMARK_PAGE_FROM_STAR), |
| 72 rb.GetNativeImageNamed(starred ? IDR_STAR_LIT : IDR_STAR)); |
| 73 |
| 74 // TODO(msw): Show the item as disabled for chrome: and file: scheme pages? |
| 75 if (ChromeToMobileService::UpdateAndGetCommandState(browser_)) { |
| 76 menu_model->AddItemWithStringId(IDC_CHROME_TO_MOBILE_PAGE, |
| 77 IDS_CHROME_TO_MOBILE_BUBBLE_TOOLTIP); |
| 78 menu_model->SetIcon( |
| 79 menu_model->GetIndexOfCommandId(IDC_CHROME_TO_MOBILE_PAGE), |
| 80 rb.GetNativeImageNamed(IDR_MOBILE)); |
| 81 } |
| 82 |
| 83 ExtensionService* extension_service = |
| 57 extensions::ExtensionSystem::Get(browser_->profile())-> | 84 extensions::ExtensionSystem::Get(browser_->profile())-> |
| 58 extension_service()->extensions(); | 85 extension_service(); |
| 59 for (ExtensionSet::const_iterator it = extensions->begin(); | 86 if (extension_service) { |
| 60 it != extensions->end(); ++it) { | 87 const ExtensionSet* extensions = extension_service->extensions(); |
| 61 const extensions::Extension* extension = *it; | 88 for (ExtensionSet::const_iterator it = extensions->begin(); |
| 62 if (ActionInfo::GetPageLauncherInfo(extension)) { | 89 it != extensions->end(); ++it) { |
| 63 int command_id = GetCommandIdForExtension(*extension); | 90 const extensions::Extension* extension = *it; |
| 64 menu_model->AddExtension(*extension, command_id); | 91 if (ActionInfo::GetPageLauncherInfo(extension)) { |
| 92 int command_id = GetCommandIdForExtension(*extension); |
| 93 menu_model->AddExtension(*extension, command_id); |
| 94 } |
| 65 } | 95 } |
| 66 } | 96 } |
| 97 return menu_model.Pass(); |
| 98 } |
| 99 |
| 100 void ActionBoxButtonController::OnButtonClicked() { |
| 67 content::RecordAction(UserMetricsAction("ActionBox.ClickButton")); | 101 content::RecordAction(UserMetricsAction("ActionBox.ClickButton")); |
| 68 | 102 delegate_->ShowMenu(CreateMenuModel()); |
| 69 // And show the menu. | |
| 70 delegate_->ShowMenu(menu_model.Pass()); | |
| 71 } | 103 } |
| 72 | 104 |
| 73 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { | 105 bool ActionBoxButtonController::IsCommandIdChecked(int command_id) const { |
| 74 return false; | 106 return false; |
| 75 } | 107 } |
| 76 | 108 |
| 77 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { | 109 bool ActionBoxButtonController::IsCommandIdEnabled(int command_id) const { |
| 78 return true; | 110 return true; |
| 79 } | 111 } |
| 80 | 112 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 for (ExtensionIdCommandMap::iterator it = extension_command_ids_.begin(); | 185 for (ExtensionIdCommandMap::iterator it = extension_command_ids_.begin(); |
| 154 it != extension_command_ids_.end();) { | 186 it != extension_command_ids_.end();) { |
| 155 if (it->second== extension->id()) | 187 if (it->second== extension->id()) |
| 156 extension_command_ids_.erase(it++); | 188 extension_command_ids_.erase(it++); |
| 157 else | 189 else |
| 158 ++it; | 190 ++it; |
| 159 } | 191 } |
| 160 // TODO(kalman): if there's a menu open, remove it from that too. | 192 // TODO(kalman): if there's a menu open, remove it from that too. |
| 161 // We may also want to listen to EXTENSION_LOADED to do the opposite. | 193 // We may also want to listen to EXTENSION_LOADED to do the opposite. |
| 162 } | 194 } |
| OLD | NEW |