- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / history_menu_bridge.h
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.
4
5 #ifndef CHROME_BROWSER_UI_COCOA_HISTORY_MENU_BRIDGE_H_
6 #define CHROME_BROWSER_UI_COCOA_HISTORY_MENU_BRIDGE_H_
7
8 #import <Cocoa/Cocoa.h>
9 #include <map>
10 #include <vector>
11
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"
23
24 class NotificationRegistrar;
25 class PageUsageData;
26 class Profile;
27 class TabRestoreService;
28 @class HistoryMenuCocoaController;
29
30 namespace {
31 class HistoryMenuBridgeTest;
32 }
33
34 namespace chrome {
35 struct FaviconImageResult;
36 }
37
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
41 // there is new data.
42 //
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-
52 // action mechanism.
53 //
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,
62                           public MainMenuItem {
63  public:
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.
66   struct HistoryItem {
67    public:
68     HistoryItem();
69     // Copy constructor allowed.
70     HistoryItem(const HistoryItem& copy);
71     ~HistoryItem();
72
73     // The title for the menu item.
74     string16 title;
75     // The URL that will be navigated to if the user selects this item.
76     GURL url;
77     // Favicon for the URL.
78     base::scoped_nsobject<NSImage> icon;
79
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.
83     bool icon_requested;
84     // The Handle given to us by the FaviconService for the icon fetch request.
85     CancelableTaskTracker::TaskId icon_task_id;
86
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
91     // zombie.
92     base::scoped_nsobject<NSMenuItem> menu_item;
93
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
98     // this ID will be 0.
99     SessionID::id_type session_id;
100
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;
106
107    private:
108     // Copying is explicitly allowed, but assignment is not.
109     void operator=(const HistoryItem&);
110   };
111
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.
116   enum 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.
124   };
125
126   explicit HistoryMenuBridge(Profile* profile);
127   virtual ~HistoryMenuBridge();
128
129   // content::NotificationObserver:
130   virtual void Observe(int type,
131                        const content::NotificationSource& source,
132                        const content::NotificationDetails& details) OVERRIDE;
133
134   // TabRestoreServiceObserver:
135   virtual void TabRestoreServiceChanged(TabRestoreService* service) OVERRIDE;
136   virtual void TabRestoreServiceDestroyed(TabRestoreService* service) OVERRIDE;
137
138   // MainMenuItem:
139   virtual void ResetMenu() OVERRIDE;
140   virtual void BuildMenu() OVERRIDE;
141
142   // Looks up an NSMenuItem in the |menu_item_map_| and returns the
143   // corresponding HistoryItem.
144   HistoryItem* HistoryItemForMenuItem(NSMenuItem* item);
145
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();
149   Profile* profile();
150
151  protected:
152   // Return the History menu.
153   virtual NSMenu* HistoryMenu();
154
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);
161
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,
167                             NSMenu* menu,
168                             NSInteger tag,
169                             NSInteger index);
170
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().
174   void Init();
175
176   // Does the query for the history information to create the menu.
177   void CreateMenu();
178
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);
183
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);
187
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);
191
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);
198
199   // Cancels a favicon load request for a given HistoryItem, if one is in
200   // progress.
201   void CancelFaviconRequest(HistoryItem* item);
202
203  private:
204   friend class ::HistoryMenuBridgeTest;
205   friend class HistoryMenuCocoaControllerTest;
206
207   base::scoped_nsobject<HistoryMenuCocoaController> controller_;  // strong
208
209   Profile* profile_;  // weak
210   HistoryService* history_service_;  // weak
211   TabRestoreService* tab_restore_service_;  // weak
212
213   content::NotificationRegistrar registrar_;
214   CancelableRequestConsumer cancelable_request_consumer_;
215   CancelableTaskTracker cancelable_task_tracker_;
216
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_;
220
221   // Maps HistoryItems to favicon request Handles.
222   CancelableRequestConsumerTSimple<HistoryItem*> favicon_consumer_;
223
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_;
229   bool need_recreate_;
230
231   // The default favicon if a HistoryItem does not have one.
232   base::scoped_nsobject<NSImage> default_favicon_;
233
234   DISALLOW_COPY_AND_ASSIGN(HistoryMenuBridge);
235 };
236
237 #endif  // CHROME_BROWSER_UI_COCOA_HISTORY_MENU_BRIDGE_H_