[sessions]: Componentize TabRestore code
[chromium-blink-merge.git] / chrome / browser / jumplist_win.h
blobbc75e51ce46b18cc0c8cdd49308e17300285ae99
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.
5 #ifndef CHROME_BROWSER_JUMPLIST_WIN_H_
6 #define CHROME_BROWSER_JUMPLIST_WIN_H_
8 #include <list>
9 #include <string>
10 #include <utility>
11 #include <vector>
13 #include "base/files/file_path.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/synchronization/lock.h"
16 #include "base/task/cancelable_task_tracker.h"
17 #include "base/timer/timer.h"
18 #include "chrome/browser/jumplist_updater_win.h"
19 #include "chrome/browser/prefs/incognito_mode_prefs.h"
20 #include "components/history/core/browser/history_service.h"
21 #include "components/history/core/browser/history_types.h"
22 #include "components/history/core/browser/top_sites_observer.h"
23 #include "components/sessions/core/tab_restore_service.h"
24 #include "components/sessions/core/tab_restore_service_observer.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/notification_observer.h"
27 #include "content/public/browser/notification_registrar.h"
29 namespace chrome {
30 struct FaviconImageResult;
33 class PrefChangeRegistrar;
34 class Profile;
36 // A class which implements an application JumpList.
37 // This class encapsulates operations required for updating an application
38 // JumpList:
39 // * Retrieving "Most Visited" pages from HistoryService;
40 // * Retrieving strings from the application resource;
41 // * Adding COM objects to JumpList, etc.
43 // This class observes the tabs and policies of the given Profile and updates
44 // the JumpList whenever a change is detected.
46 // Updating a JumpList requires some file operations and it is not good to
47 // update it in a UI thread. To solve this problem, this class posts to a
48 // runnable method when it actually updates a JumpList.
50 // Note. base::CancelableTaskTracker is not thread safe, so we
51 // always delete JumpList on UI thread (the same thread it got constructed on).
52 class JumpList : public TabRestoreServiceObserver,
53 public content::NotificationObserver,
54 public history::TopSitesObserver,
55 public base::RefCountedThreadSafe<
56 JumpList,
57 content::BrowserThread::DeleteOnUIThread> {
58 public:
59 explicit JumpList(Profile* profile);
61 // NotificationObserver implementation.
62 void Observe(int type,
63 const content::NotificationSource& source,
64 const content::NotificationDetails& details) override;
66 // Observer callback for TabRestoreService::Observer to notify when a tab is
67 // added or removed.
68 void TabRestoreServiceChanged(TabRestoreService* service) override;
70 // Observer callback to notice when our associated TabRestoreService
71 // is destroyed.
72 void TabRestoreServiceDestroyed(TabRestoreService* service) override;
74 // Cancel a pending jumplist update.
75 void CancelPendingUpdate();
77 // Terminate the jumplist: cancel any pending updates and stop observing
78 // the Profile and its services. This must be called before the |profile_|
79 // is destroyed.
80 void Terminate();
82 // Returns true if the custom JumpList is enabled.
83 // The custom jumplist works only on Windows 7 and above.
84 static bool Enabled();
86 private:
87 friend struct content::BrowserThread::DeleteOnThread<
88 content::BrowserThread::UI>;
89 friend class base::DeleteHelper<JumpList>;
90 ~JumpList() override;
92 // Creates a ShellLinkItem object from a tab (or a window) and add it to the
93 // given list.
94 // These functions are copied from the RecentlyClosedTabsHandler class for
95 // compatibility with the new-tab page.
96 bool AddTab(const TabRestoreService::Tab* tab,
97 ShellLinkItemList* list,
98 size_t max_items);
99 void AddWindow(const TabRestoreService::Window* window,
100 ShellLinkItemList* list,
101 size_t max_items);
103 // Starts loading a favicon for each URL in |icon_urls_|.
104 // This function sends a query to HistoryService.
105 // When finishing loading all favicons, this function posts a task that
106 // decompresses collected favicons and updates a JumpList.
107 void StartLoadingFavicon();
109 // A callback function for HistoryService that notify when a requested favicon
110 // is available.
111 // To avoid file operations, this function just attaches the given data to
112 // a ShellLinkItem object.
113 void OnFaviconDataAvailable(
114 const favicon_base::FaviconImageResult& image_result);
116 // Callback for TopSites that notifies when the "Most
117 // Visited" list is available. This function updates the ShellLinkItemList
118 // objects and send another query that retrieves a favicon for each URL in
119 // the list.
120 void OnMostVisitedURLsAvailable(
121 const history::MostVisitedURLList& data);
123 // Callback for changes to the incognito mode availability pref.
124 void OnIncognitoAvailabilityChanged();
126 // Helper for RunUpdate() that determines its parameters.
127 void PostRunUpdate();
129 // Called on a timer to invoke RunUpdateOnFileThread() after requests storms
130 // have subsided.
131 void DeferredRunUpdate();
133 // Runnable method that updates the jumplist, once all the data
134 // has been fetched.
135 void RunUpdateOnFileThread(
136 IncognitoModePrefs::Availability incognito_availability);
138 // Helper method for RunUpdate to create icon files for the asynchrounously
139 // loaded icons.
140 void CreateIconFiles(const ShellLinkItemList& item_list);
142 // history::TopSitesObserver implementation.
143 void TopSitesLoaded(history::TopSites* top_sites) override;
144 void TopSitesChanged(history::TopSites* top_sites,
145 ChangeReason change_reason) override;
147 // Tracks FaviconService tasks.
148 base::CancelableTaskTracker cancelable_task_tracker_;
150 // The Profile object is used to listen for events
151 Profile* profile_;
153 // Lives on the UI thread.
154 scoped_ptr<content::NotificationRegistrar> registrar_;
155 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
157 // App id to associate with the jump list.
158 std::wstring app_id_;
160 // The directory which contains JumpList icons.
161 base::FilePath icon_dir_;
163 // Items in the "Most Visited" category of the application JumpList,
164 // protected by the list_lock_.
165 ShellLinkItemList most_visited_pages_;
167 // Items in the "Recently Closed" category of the application JumpList,
168 // protected by the list_lock_.
169 ShellLinkItemList recently_closed_pages_;
171 // Timer for requesting delayed updates of the jumplist.
172 base::OneShotTimer<JumpList> timer_;
174 // A list of URLs we need to retrieve their favicons,
175 // protected by the list_lock_.
176 typedef std::pair<std::string, scoped_refptr<ShellLinkItem> > URLPair;
177 std::list<URLPair> icon_urls_;
179 // Id of last favicon task. It's used to cancel current task if a new one
180 // comes in before it finishes.
181 base::CancelableTaskTracker::TaskId task_id_;
183 // Lock for most_visited_pages_, recently_closed_pages_, icon_urls_
184 // as they may be used by up to 3 threads.
185 base::Lock list_lock_;
187 // For callbacks may be run after destruction.
188 base::WeakPtrFactory<JumpList> weak_ptr_factory_;
190 DISALLOW_COPY_AND_ASSIGN(JumpList);
193 #endif // CHROME_BROWSER_JUMPLIST_WIN_H_