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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_ 6 #define CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_ 7 8 #include "base/files/file_path.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" 11 #include "content/public/browser/host_zoom_map.h" 12 #include "content/public/browser/notification_observer.h" 13 #include "content/public/browser/notification_registrar.h" 14 #include "ui/base/accelerators/accelerator.h" 15 #include "ui/base/models/button_menu_item_model.h" 16 #include "ui/base/models/simple_menu_model.h" 17 18 class BookmarkSubMenuModel; 19 class Browser; 20 class RecentTabsSubMenuModel; 21 class TabStripModel; 22 23 namespace { 24 class MockWrenchMenuModel; 25 } // namespace 26 27 // A menu model that builds the contents of an encoding menu. 28 class EncodingMenuModel : public ui::SimpleMenuModel, 29 public ui::SimpleMenuModel::Delegate { 30 public: 31 explicit EncodingMenuModel(Browser* browser); 32 virtual ~EncodingMenuModel(); 33 34 // Overridden from ui::SimpleMenuModel::Delegate: 35 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; 36 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; 37 virtual bool GetAcceleratorForCommandId( 38 int command_id, 39 ui::Accelerator* accelerator) OVERRIDE; 40 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; 41 42 private: 43 void Build(); 44 45 Browser* browser_; // weak 46 47 DISALLOW_COPY_AND_ASSIGN(EncodingMenuModel); 48 }; 49 50 // A menu model that builds the contents of the zoom menu. 51 class ZoomMenuModel : public ui::SimpleMenuModel { 52 public: 53 explicit ZoomMenuModel(ui::SimpleMenuModel::Delegate* delegate); 54 virtual ~ZoomMenuModel(); 55 56 private: 57 void Build(); 58 59 DISALLOW_COPY_AND_ASSIGN(ZoomMenuModel); 60 }; 61 62 class ToolsMenuModel : public ui::SimpleMenuModel { 63 public: 64 ToolsMenuModel(ui::SimpleMenuModel::Delegate* delegate, Browser* browser); 65 virtual ~ToolsMenuModel(); 66 67 private: 68 void Build(Browser* browser); 69 70 scoped_ptr<EncodingMenuModel> encoding_menu_model_; 71 72 DISALLOW_COPY_AND_ASSIGN(ToolsMenuModel); 73 }; 74 75 // A menu model that builds the contents of the wrench menu. 76 class WrenchMenuModel : public ui::SimpleMenuModel, 77 public ui::SimpleMenuModel::Delegate, 78 public ui::ButtonMenuItemModel::Delegate, 79 public TabStripModelObserver, 80 public content::NotificationObserver { 81 public: 82 // TODO: remove |is_new_menu|. 83 WrenchMenuModel(ui::AcceleratorProvider* provider, 84 Browser* browser, 85 bool is_new_menu); 86 virtual ~WrenchMenuModel(); 87 88 // Overridden for ButtonMenuItemModel::Delegate: 89 virtual bool DoesCommandIdDismissMenu(int command_id) const OVERRIDE; 90 91 // Overridden for both ButtonMenuItemModel::Delegate and SimpleMenuModel: 92 virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE; 93 virtual string16 GetLabelForCommandId(int command_id) const OVERRIDE; 94 virtual bool GetIconForCommandId(int command_id, 95 gfx::Image* icon) const OVERRIDE; 96 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; 97 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; 98 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; 99 virtual bool IsCommandIdVisible(int command_id) const OVERRIDE; 100 virtual bool GetAcceleratorForCommandId( 101 int command_id, 102 ui::Accelerator* accelerator) OVERRIDE; 103 104 // Overridden from TabStripModelObserver: 105 virtual void ActiveTabChanged(content::WebContents* old_contents, 106 content::WebContents* new_contents, 107 int index, 108 int reason) OVERRIDE; 109 virtual void TabReplacedAt(TabStripModel* tab_strip_model, 110 content::WebContents* old_contents, 111 content::WebContents* new_contents, 112 int index) OVERRIDE; 113 virtual void TabStripModelDeleted() OVERRIDE; 114 115 // Overridden from content::NotificationObserver: 116 virtual void Observe(int type, 117 const content::NotificationSource& source, 118 const content::NotificationDetails& details) OVERRIDE; 119 120 // Getters. 121 Browser* browser() const { return browser_; } 122 123 BookmarkSubMenuModel* bookmark_sub_menu_model() const { 124 return bookmark_sub_menu_model_.get(); 125 } 126 127 // Calculates |zoom_label_| in response to a zoom change. 128 void UpdateZoomControls(); 129 130 private: 131 // Testing constructor used for mocking. 132 friend class ::MockWrenchMenuModel; 133 WrenchMenuModel(); 134 135 void Build(bool is_new_menu); 136 137 void AddGlobalErrorMenuItems(); 138 139 // Appends everything needed for the clipboard menu: a menu break, the 140 // clipboard menu content and the finalizing menu break. If the last break 141 // is not needed it can be suppressed by setting |new_menu| 142 // to false. 143 void CreateCutCopyPasteMenu(bool new_menu); 144 145 // Appends everything needed for the zoom menu: a menu break, then the zoom 146 // menu content and then another menu break. If the new menu type is used, 147 // |new_menu| should be set to true. 148 void CreateZoomMenu(bool new_menu); 149 150 void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change); 151 152 bool ShouldShowNewIncognitoWindowMenuItem(); 153 bool ShouldShowNewWindowMenuItem(); 154 155 // Models for the special menu items with buttons. 156 scoped_ptr<ui::ButtonMenuItemModel> edit_menu_item_model_; 157 scoped_ptr<ui::ButtonMenuItemModel> zoom_menu_item_model_; 158 159 // Label of the zoom label in the zoom menu item. 160 string16 zoom_label_; 161 162 // Tools menu. 163 scoped_ptr<ToolsMenuModel> tools_menu_model_; 164 165 // Bookmark submenu. 166 scoped_ptr<BookmarkSubMenuModel> bookmark_sub_menu_model_; 167 168 // Recent Tabs submenu. 169 scoped_ptr<RecentTabsSubMenuModel> recent_tabs_sub_menu_model_; 170 171 ui::AcceleratorProvider* provider_; // weak 172 173 Browser* browser_; // weak 174 TabStripModel* tab_strip_model_; // weak 175 176 content::HostZoomMap::ZoomLevelChangedCallback zoom_callback_; 177 content::NotificationRegistrar registrar_; 178 179 DISALLOW_COPY_AND_ASSIGN(WrenchMenuModel); 180 }; 181 182 #endif // CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_ 183