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_
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"
33 @
class BookmarkMenuCocoaController
;
39 class BookmarkMenuBridge
: public bookmarks::BookmarkModelObserver
,
42 BookmarkMenuBridge(Profile
* profile
, NSMenu
* menu
);
43 ~BookmarkMenuBridge() override
;
45 // bookmarks::BookmarkModelObserver:
46 void BookmarkModelLoaded(bookmarks::BookmarkModel
* model
,
47 bool ids_reassigned
) override
;
48 void BookmarkModelBeingDeleted(bookmarks::BookmarkModel
* model
) override
;
49 void BookmarkNodeMoved(bookmarks::BookmarkModel
* model
,
50 const bookmarks::BookmarkNode
* old_parent
,
52 const bookmarks::BookmarkNode
* new_parent
,
53 int new_index
) override
;
54 void BookmarkNodeAdded(bookmarks::BookmarkModel
* model
,
55 const bookmarks::BookmarkNode
* parent
,
57 void BookmarkNodeRemoved(bookmarks::BookmarkModel
* model
,
58 const bookmarks::BookmarkNode
* parent
,
60 const bookmarks::BookmarkNode
* node
,
61 const std::set
<GURL
>& removed_urls
) override
;
62 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel
* model
,
63 const std::set
<GURL
>& removed_urls
) override
;
64 void BookmarkNodeChanged(bookmarks::BookmarkModel
* model
,
65 const bookmarks::BookmarkNode
* node
) override
;
66 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel
* model
,
67 const bookmarks::BookmarkNode
* node
) override
;
68 void BookmarkNodeChildrenReordered(
69 bookmarks::BookmarkModel
* model
,
70 const bookmarks::BookmarkNode
* node
) override
;
73 void ResetMenu() override
;
74 void BuildMenu() override
;
76 // Rebuilds the main bookmark menu, if it has been marked invalid.
77 void UpdateMenu(NSMenu
* bookmark_menu
);
79 // Rebuilds a bookmark menu that's a submenu of another menu.
80 void UpdateSubMenu(NSMenu
* bookmark_menu
);
82 // I wish I had a "friend @class" construct.
83 bookmarks::BookmarkModel
* GetBookmarkModel();
84 Profile
* GetProfile();
86 // Return the Bookmark menu.
87 virtual NSMenu
* BookmarkMenu();
90 // Rebuilds the bookmark content of supplied menu.
91 void UpdateMenuInternal(NSMenu
* bookmark_menu
, bool is_submenu
);
93 // Clear all bookmarks from the given bookmark menu.
94 void ClearBookmarkMenu(NSMenu
* menu
);
96 // Mark the bookmark menu as being invalid.
97 void InvalidateMenu() { menuIsValid_
= false; }
99 // Helper for adding the node as a submenu to the menu with the |node|'s title
100 // and the given |image| as its icon.
101 // If |add_extra_items| is true, also adds extra menu items at bottom of
102 // menu, such as "Open All Bookmarks".
103 void AddNodeAsSubmenu(NSMenu
* menu
,
104 const bookmarks::BookmarkNode
* node
,
106 bool add_extra_items
);
108 // Helper for recursively adding items to our bookmark menu.
109 // All children of |node| will be added to |menu|.
110 // If |add_extra_items| is true, also adds extra menu items at bottom of
111 // menu, such as "Open All Bookmarks".
112 // TODO(jrg): add a counter to enforce maximum nodes added
113 void AddNodeToMenu(const bookmarks::BookmarkNode
* node
,
115 bool add_extra_items
);
117 // Helper for adding an item to our bookmark menu. An item which has a
118 // localized title specified by |message_id| will be added to |menu|.
119 // The item is also bound to |node| by tag. |command_id| selects the action.
120 void AddItemToMenu(int command_id
,
122 const bookmarks::BookmarkNode
* node
,
126 // This configures an NSMenuItem with all the data from a BookmarkNode. This
127 // is used to update existing menu items, as well as to configure newly
128 // created ones, like in AddNodeToMenu().
129 // |set_title| is optional since it is only needed when we get a
130 // node changed notification. On initial build of the menu we set
131 // the title as part of alloc/init.
132 void ConfigureMenuItem(const bookmarks::BookmarkNode
* node
,
136 // Returns the NSMenuItem for a given BookmarkNode.
137 NSMenuItem
* MenuItemForNode(const bookmarks::BookmarkNode
* node
);
139 // Start watching the bookmarks for changes.
140 void ObserveBookmarkModel();
143 friend class BookmarkMenuBridgeTest
;
145 // True iff the menu is up-to-date with the actual BookmarkModel.
148 Profile
* profile_
; // weak
149 BookmarkMenuCocoaController
* controller_
; // strong
151 // The folder image so we can use one copy for all.
152 base::scoped_nsobject
<NSImage
> folder_image_
;
154 // In order to appropriately update items in the bookmark menu, without
155 // forcing a rebuild, map the model's nodes to menu items.
156 std::map
<const bookmarks::BookmarkNode
*, NSMenuItem
*> bookmark_nodes_
;
159 #endif // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_