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 #ifndef CHROME_BROWSER_UI_COCOA_HISTORY_MENU_BRIDGE_H_
6 #define CHROME_BROWSER_UI_COCOA_HISTORY_MENU_BRIDGE_H_
8 #import <Cocoa/Cocoa.h>
12 #include "base/mac/scoped_nsobject.h"
13 #include "base/memory/ref_counted.h"
14 #include "chrome/browser/common/cancelable_request.h"
15 #import "chrome/browser/favicon/favicon_service.h"
16 #include "chrome/browser/history/history_service.h"
17 #include "chrome/browser/sessions/session_id.h"
18 #include "chrome/browser/sessions/tab_restore_service.h"
19 #include "chrome/browser/sessions/tab_restore_service_observer.h"
20 #import "chrome/browser/ui/cocoa/main_menu_item.h"
21 #include "chrome/common/cancelable_task_tracker.h"
22 #include "content/public/browser/notification_observer.h"
24 class NotificationRegistrar;
27 class TabRestoreService;
28 @class HistoryMenuCocoaController;
31 class HistoryMenuBridgeTest;
35 struct FaviconImageResult;
38 // C++ bridge for the history menu; one per AppController (means there
39 // is only one). This class observes various data sources, namely the
40 // HistoryService and the TabRestoreService, and then updates the NSMenu when
43 // The history menu is broken up into sections: most visisted and recently
44 // closed. The overall menu has a tag of IDC_HISTORY_MENU, with the user content
45 // items having the local tags defined in the enum below. Items within a section
46 // all share the same tag. The structure of the menu is laid out in MainMenu.xib
47 // and the generated content is inserted after the Title elements. The recently
48 // closed section is special in that those menu items can have submenus to list
49 // all the tabs within that closed window. By convention, these submenu items
50 // have a tag that's equal to the parent + 1. Tags within the history menu have
51 // a range of [400,500) and do not go through CommandDispatch for their target-
54 // These menu items do not use firstResponder as their target. Rather, they are
55 // hooked directly up to the HistoryMenuCocoaController that then bridges back
56 // to this class. These items are created via the AddItemToMenu() helper. Also,
57 // unlike the typical ownership model, this bridge owns its controller. The
58 // controller is very thin and only exists to interact with Cocoa, but this
59 // class does the bulk of the work.
60 class HistoryMenuBridge : public content::NotificationObserver,
61 public TabRestoreServiceObserver,
64 // This is a generalization of the data we store in the history menu because
65 // we pull things from different sources with different data types.
69 // Copy constructor allowed.
70 HistoryItem(const HistoryItem& copy);
73 // The title for the menu item.
75 // The URL that will be navigated to if the user selects this item.
77 // Favicon for the URL.
78 base::scoped_nsobject<NSImage> icon;
80 // If the icon is being requested from the FaviconService, |icon_requested|
81 // will be true and |icon_task_id| will be valid. If this is false, then
82 // |icon_task_id| will be CancelableTaskTracker::kBadTaskId.
84 // The Handle given to us by the FaviconService for the icon fetch request.
85 CancelableTaskTracker::TaskId icon_task_id;
87 // The pointer to the item after it has been created. Strong; NSMenu also
88 // retains this. During a rebuild flood (if the user closes a lot of tabs
89 // quickly), the NSMenu can release the item before the HistoryItem has
90 // been fully deleted. If this were a weak pointer, it would result in a
92 base::scoped_nsobject<NSMenuItem> menu_item;
94 // This ID is unique for a browser session and can be passed to the
95 // TabRestoreService to re-open the closed window or tab that this
96 // references. A non-0 session ID indicates that this is an entry can be
97 // restored that way. Otherwise, the URL will be used to open the item and
99 SessionID::id_type session_id;
101 // If the HistoryItem is a window, this will be the vector of tabs. Note
102 // that this is a list of weak references. The |menu_item_map_| is the owner
103 // of all items. If it is not a window, then the entry is a single page and
104 // the vector will be empty.
105 std::vector<HistoryItem*> tabs;
108 // Copying is explicitly allowed, but assignment is not.
109 void operator=(const HistoryItem&);
112 // These tags are not global view tags and are local to the history menu. The
113 // normal procedure for menu items is to go through CommandDispatch, but since
114 // history menu items are hooked directly up to their target, they do not need
115 // to have the global IDC view tags.
117 kRecentlyClosedSeparator = 400, // Item before recently closed section.
118 kRecentlyClosedTitle = 401, // Title of recently closed section.
119 kRecentlyClosed = 420, // Used for items in the recently closed section.
120 kVisitedSeparator = 440, // Separator before visited section.
121 kVisitedTitle = 441, // Title of the visited section.
122 kVisited = 460, // Used for all entries in the visited section.
123 kShowFullSeparator = 480 // Separator after the visited section.
126 explicit HistoryMenuBridge(Profile* profile);
127 virtual ~HistoryMenuBridge();
129 // content::NotificationObserver:
130 virtual void Observe(int type,
131 const content::NotificationSource& source,
132 const content::NotificationDetails& details) OVERRIDE;
134 // TabRestoreServiceObserver:
135 virtual void TabRestoreServiceChanged(TabRestoreService* service) OVERRIDE;
136 virtual void TabRestoreServiceDestroyed(TabRestoreService* service) OVERRIDE;
139 virtual void ResetMenu() OVERRIDE;
140 virtual void BuildMenu() OVERRIDE;
142 // Looks up an NSMenuItem in the |menu_item_map_| and returns the
143 // corresponding HistoryItem.
144 HistoryItem* HistoryItemForMenuItem(NSMenuItem* item);
146 // I wish I has a "friend @class" construct. These are used by the HMCC
147 // to access model information when responding to actions.
148 HistoryService* service();
152 // Return the History menu.
153 virtual NSMenu* HistoryMenu();
155 // Clear items in the given |menu|. Menu items in the same section are given
156 // the same tag. This will go through the entire history menu, removing all
157 // items with a given tag. Note that this will recurse to submenus, removing
158 // child items from the menu item map. This will only remove items that have
159 // a target hooked up to the |controller_|.
160 void ClearMenuSection(NSMenu* menu, NSInteger tag);
162 // Adds a given title and URL to the passed-in menu with a certain tag and
163 // index. This will add |item| and the newly created menu item to the
164 // |menu_item_map_|, which takes ownership. Items are deleted in
165 // ClearMenuSection(). This returns the new menu item that was just added.
166 NSMenuItem* AddItemToMenu(HistoryItem* item,
171 // Called by the ctor if |service_| is ready at the time, or by a
172 // notification receiver. Finishes initialization tasks by subscribing for
173 // change notifications and calling CreateMenu().
176 // Does the query for the history information to create the menu.
179 // Callback method for when HistoryService query results are ready with the
180 // most recently-visited sites.
181 void OnVisitedHistoryResults(CancelableRequestProvider::Handle handle,
182 history::QueryResults* results);
184 // Creates a HistoryItem* for the given tab entry. Caller takes ownership of
185 // the result and must delete it when finished.
186 HistoryItem* HistoryItemForTab(const TabRestoreService::Tab& entry);
188 // Helper function that sends an async request to the FaviconService to get
189 // an icon. The callback will update the NSMenuItem directly.
190 void GetFaviconForHistoryItem(HistoryItem* item);
192 // Callback for the FaviconService to return favicon image data when we
193 // request it. This decodes the raw data, updates the HistoryItem, and then
194 // sets the image on the menu. Called on the same same thread that
195 // GetFaviconForHistoryItem() was called on (UI thread).
196 void GotFaviconData(HistoryItem* item,
197 const chrome::FaviconImageResult& image_result);
199 // Cancels a favicon load request for a given HistoryItem, if one is in
201 void CancelFaviconRequest(HistoryItem* item);
204 friend class ::HistoryMenuBridgeTest;
205 friend class HistoryMenuCocoaControllerTest;
207 base::scoped_nsobject<HistoryMenuCocoaController> controller_; // strong
209 Profile* profile_; // weak
210 HistoryService* history_service_; // weak
211 TabRestoreService* tab_restore_service_; // weak
213 content::NotificationRegistrar registrar_;
214 CancelableRequestConsumer cancelable_request_consumer_;
215 CancelableTaskTracker cancelable_task_tracker_;
217 // Mapping of NSMenuItems to HistoryItems. This owns the HistoryItems until
218 // they are removed and deleted via ClearMenuSection().
219 std::map<NSMenuItem*, HistoryItem*> menu_item_map_;
221 // Maps HistoryItems to favicon request Handles.
222 CancelableRequestConsumerTSimple<HistoryItem*> favicon_consumer_;
224 // Requests to re-create the menu are coalesced. |create_in_progress_| is true
225 // when either waiting for the history service to return query results, or
226 // when the menu is rebuilding. |need_recreate_| is true whenever a rebuild
227 // has been scheduled but is waiting for the current one to finish.
228 bool create_in_progress_;
231 // The default favicon if a HistoryItem does not have one.
232 base::scoped_nsobject<NSImage> default_favicon_;
234 DISALLOW_COPY_AND_ASSIGN(HistoryMenuBridge);
237 #endif // CHROME_BROWSER_UI_COCOA_HISTORY_MENU_BRIDGE_H_