Home | History | Annotate | Download | only in toolbar
      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   // Range of command ID's to use for the items representing bookmarks in the
     83   // bookmark menu, must not overlap with that for recent tabs submenu.
     84   static const int kMinBookmarkCommandId = 1;
     85   static const int kMaxBookmarkCommandId = 1000;
     86 
     87   // Range of command ID's to use for the items in the recent tabs submenu, must
     88   // not overlap with that for bookmarks.
     89   static const int kMinRecentTabsCommandId = 1001;
     90   static const int kMaxRecentTabsCommandId = 1200;
     91 
     92   // TODO: remove |is_new_menu|.
     93   WrenchMenuModel(ui::AcceleratorProvider* provider,
     94                   Browser* browser,
     95                   bool is_new_menu);
     96   virtual ~WrenchMenuModel();
     97 
     98   // Overridden for ButtonMenuItemModel::Delegate:
     99   virtual bool DoesCommandIdDismissMenu(int command_id) const OVERRIDE;
    100 
    101   // Overridden for both ButtonMenuItemModel::Delegate and SimpleMenuModel:
    102   virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE;
    103   virtual base::string16 GetLabelForCommandId(int command_id) const OVERRIDE;
    104   virtual bool GetIconForCommandId(int command_id,
    105                                    gfx::Image* icon) const OVERRIDE;
    106   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
    107   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
    108   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
    109   virtual bool IsCommandIdVisible(int command_id) const OVERRIDE;
    110   virtual bool GetAcceleratorForCommandId(
    111       int command_id,
    112       ui::Accelerator* accelerator) OVERRIDE;
    113 
    114   // Overridden from TabStripModelObserver:
    115   virtual void ActiveTabChanged(content::WebContents* old_contents,
    116                                 content::WebContents* new_contents,
    117                                 int index,
    118                                 int reason) OVERRIDE;
    119   virtual void TabReplacedAt(TabStripModel* tab_strip_model,
    120                              content::WebContents* old_contents,
    121                              content::WebContents* new_contents,
    122                              int index) OVERRIDE;
    123   virtual void TabStripModelDeleted() OVERRIDE;
    124 
    125   // Overridden from content::NotificationObserver:
    126   virtual void Observe(int type,
    127                        const content::NotificationSource& source,
    128                        const content::NotificationDetails& details) OVERRIDE;
    129 
    130   // Getters.
    131   Browser* browser() const { return browser_; }
    132 
    133   BookmarkSubMenuModel* bookmark_sub_menu_model() const {
    134     return bookmark_sub_menu_model_.get();
    135   }
    136 
    137   // Calculates |zoom_label_| in response to a zoom change.
    138   void UpdateZoomControls();
    139 
    140  private:
    141   // Testing constructor used for mocking.
    142   friend class ::MockWrenchMenuModel;
    143   WrenchMenuModel();
    144 
    145   void Build(bool is_new_menu);
    146 
    147   void AddGlobalErrorMenuItems();
    148 
    149   // Appends everything needed for the clipboard menu: a menu break, the
    150   // clipboard menu content and the finalizing menu break. If the last break
    151   // is not needed it can be suppressed by setting |new_menu|
    152   // to false.
    153   void CreateCutCopyPasteMenu(bool new_menu);
    154 
    155   // Appends everything needed for the zoom menu: a menu break, then the zoom
    156   // menu content and then another menu break. If the new menu type is used,
    157   // |new_menu| should be set to true.
    158   void CreateZoomMenu(bool new_menu);
    159 
    160   void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change);
    161 
    162   bool ShouldShowNewIncognitoWindowMenuItem();
    163   bool ShouldShowNewWindowMenuItem();
    164 
    165   // Models for the special menu items with buttons.
    166   scoped_ptr<ui::ButtonMenuItemModel> edit_menu_item_model_;
    167   scoped_ptr<ui::ButtonMenuItemModel> zoom_menu_item_model_;
    168 
    169   // Label of the zoom label in the zoom menu item.
    170   base::string16 zoom_label_;
    171 
    172   // Tools menu.
    173   scoped_ptr<ToolsMenuModel> tools_menu_model_;
    174 
    175   // Bookmark submenu.
    176   scoped_ptr<BookmarkSubMenuModel> bookmark_sub_menu_model_;
    177 
    178   // Recent Tabs submenu.
    179   scoped_ptr<RecentTabsSubMenuModel> recent_tabs_sub_menu_model_;
    180 
    181   ui::AcceleratorProvider* provider_;  // weak
    182 
    183   Browser* browser_;  // weak
    184   TabStripModel* tab_strip_model_; // weak
    185 
    186   scoped_ptr<content::HostZoomMap::Subscription> zoom_subscription_;
    187   content::NotificationRegistrar registrar_;
    188 
    189   DISALLOW_COPY_AND_ASSIGN(WrenchMenuModel);
    190 };
    191 
    192 #endif  // CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_
    193