Standardize usage of virtual/override/final in chrome/browser/ui/
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_menu_bridge.h
blobac2032cbbb7205b5068ca42fb29d917f8dddc46f
1 // Copyright (c) 2011 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.
5 // C++ controller for the bookmark menu; one per AppController (which
6 // means there is only one). When bookmarks are changed, this class
7 // takes care of updating Cocoa bookmark menus. This is not named
8 // BookmarkMenuController to help avoid confusion between languages.
9 // This class needs to be C++, not ObjC, since it derives from
10 // BookmarkModelObserver.
12 // Most Chromium Cocoa menu items are static from a nib (e.g. New
13 // Tab), but may be enabled/disabled under certain circumstances
14 // (e.g. Cut and Paste). In addition, most Cocoa menu items have
15 // firstResponder: as a target. Unusually, bookmark menu items are
16 // created dynamically. They also have a target of
17 // BookmarkMenuCocoaController instead of firstResponder.
18 // See BookmarkMenuBridge::AddNodeToMenu()).
20 #ifndef CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_
21 #define CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_
23 #include <map>
25 #include "base/mac/scoped_nsobject.h"
26 #import "chrome/browser/ui/cocoa/main_menu_item.h"
27 #include "components/bookmarks/browser/bookmark_model_observer.h"
29 class BookmarkNode;
30 class Profile;
31 @class NSImage;
32 @class NSMenu;
33 @class NSMenuItem;
34 @class BookmarkMenuCocoaController;
36 class BookmarkMenuBridge : public BookmarkModelObserver,
37 public MainMenuItem {
38 public:
39 BookmarkMenuBridge(Profile* profile, NSMenu* menu);
40 ~BookmarkMenuBridge() override;
42 // BookmarkModelObserver:
43 void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override;
44 void BookmarkModelBeingDeleted(BookmarkModel* model) override;
45 void BookmarkNodeMoved(BookmarkModel* model,
46 const BookmarkNode* old_parent,
47 int old_index,
48 const BookmarkNode* new_parent,
49 int new_index) override;
50 void BookmarkNodeAdded(BookmarkModel* model,
51 const BookmarkNode* parent,
52 int index) override;
53 void BookmarkNodeRemoved(BookmarkModel* model,
54 const BookmarkNode* parent,
55 int old_index,
56 const BookmarkNode* node,
57 const std::set<GURL>& removed_urls) override;
58 void BookmarkAllUserNodesRemoved(BookmarkModel* model,
59 const std::set<GURL>& removed_urls) override;
60 void BookmarkNodeChanged(BookmarkModel* model,
61 const BookmarkNode* node) override;
62 void BookmarkNodeFaviconChanged(BookmarkModel* model,
63 const BookmarkNode* node) override;
64 void BookmarkNodeChildrenReordered(BookmarkModel* model,
65 const BookmarkNode* node) override;
67 // MainMenuItem:
68 void ResetMenu() override;
69 void BuildMenu() override;
71 // Rebuilds the main bookmark menu, if it has been marked invalid.
72 void UpdateMenu(NSMenu* bookmark_menu);
74 // Rebuilds a bookmark menu that's a submenu of another menu.
75 void UpdateSubMenu(NSMenu* bookmark_menu);
77 // I wish I had a "friend @class" construct.
78 BookmarkModel* GetBookmarkModel();
79 Profile* GetProfile();
81 protected:
82 // Rebuilds the bookmark content of supplied menu.
83 void UpdateMenuInternal(NSMenu* bookmark_menu, bool is_submenu);
85 // Clear all bookmarks from the given bookmark menu.
86 void ClearBookmarkMenu(NSMenu* menu);
88 // Mark the bookmark menu as being invalid.
89 void InvalidateMenu() { menuIsValid_ = false; }
91 // Helper for adding the node as a submenu to the menu with the |node|'s title
92 // and the given |image| as its icon.
93 // If |add_extra_items| is true, also adds extra menu items at bottom of
94 // menu, such as "Open All Bookmarks".
95 void AddNodeAsSubmenu(NSMenu* menu,
96 const BookmarkNode* node,
97 NSImage* image,
98 bool add_extra_items);
100 // Helper for recursively adding items to our bookmark menu.
101 // All children of |node| will be added to |menu|.
102 // If |add_extra_items| is true, also adds extra menu items at bottom of
103 // menu, such as "Open All Bookmarks".
104 // TODO(jrg): add a counter to enforce maximum nodes added
105 void AddNodeToMenu(const BookmarkNode* node, NSMenu* menu,
106 bool add_extra_items);
108 // Helper for adding an item to our bookmark menu. An item which has a
109 // localized title specified by |message_id| will be added to |menu|.
110 // The item is also bound to |node| by tag. |command_id| selects the action.
111 void AddItemToMenu(int command_id,
112 int message_id,
113 const BookmarkNode* node,
114 NSMenu* menu,
115 bool enabled);
117 // This configures an NSMenuItem with all the data from a BookmarkNode. This
118 // is used to update existing menu items, as well as to configure newly
119 // created ones, like in AddNodeToMenu().
120 // |set_title| is optional since it is only needed when we get a
121 // node changed notification. On initial build of the menu we set
122 // the title as part of alloc/init.
123 void ConfigureMenuItem(const BookmarkNode* node, NSMenuItem* item,
124 bool set_title);
126 // Returns the NSMenuItem for a given BookmarkNode.
127 NSMenuItem* MenuItemForNode(const BookmarkNode* node);
129 // Return the Bookmark menu.
130 virtual NSMenu* BookmarkMenu();
132 // Start watching the bookmarks for changes.
133 void ObserveBookmarkModel();
135 private:
136 friend class BookmarkMenuBridgeTest;
138 // True iff the menu is up-to-date with the actual BookmarkModel.
139 bool menuIsValid_;
141 Profile* profile_; // weak
142 BookmarkMenuCocoaController* controller_; // strong
144 // The folder image so we can use one copy for all.
145 base::scoped_nsobject<NSImage> folder_image_;
147 // In order to appropriately update items in the bookmark menu, without
148 // forcing a rebuild, map the model's nodes to menu items.
149 std::map<const BookmarkNode*, NSMenuItem*> bookmark_nodes_;
152 #endif // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_