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_
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"
30 struct FaviconImageResult
;
33 class PrefChangeRegistrar
;
36 // A class which implements an application JumpList.
37 // This class encapsulates operations required for updating an application
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 sessions::TabRestoreServiceObserver
,
53 public content::NotificationObserver
,
54 public history::TopSitesObserver
,
55 public base::RefCountedThreadSafe
<
57 content::BrowserThread::DeleteOnUIThread
> {
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
68 void TabRestoreServiceChanged(sessions::TabRestoreService
* service
) override
;
70 // Observer callback to notice when our associated TabRestoreService
72 void TabRestoreServiceDestroyed(
73 sessions::TabRestoreService
* service
) override
;
75 // Cancel a pending jumplist update.
76 void CancelPendingUpdate();
78 // Terminate the jumplist: cancel any pending updates and stop observing
79 // the Profile and its services. This must be called before the |profile_|
83 // Returns true if the custom JumpList is enabled.
84 // The custom jumplist works only on Windows 7 and above.
85 static bool Enabled();
88 friend struct content::BrowserThread::DeleteOnThread
<
89 content::BrowserThread::UI
>;
90 friend class base::DeleteHelper
<JumpList
>;
93 // Creates a ShellLinkItem object from a tab (or a window) and add it to the
95 // These functions are copied from the RecentlyClosedTabsHandler class for
96 // compatibility with the new-tab page.
97 bool AddTab(const sessions::TabRestoreService::Tab
* tab
,
98 ShellLinkItemList
* list
,
100 void AddWindow(const sessions::TabRestoreService::Window
* window
,
101 ShellLinkItemList
* list
,
104 // Starts loading a favicon for each URL in |icon_urls_|.
105 // This function sends a query to HistoryService.
106 // When finishing loading all favicons, this function posts a task that
107 // decompresses collected favicons and updates a JumpList.
108 void StartLoadingFavicon();
110 // A callback function for HistoryService that notify when a requested favicon
112 // To avoid file operations, this function just attaches the given data to
113 // a ShellLinkItem object.
114 void OnFaviconDataAvailable(
115 const favicon_base::FaviconImageResult
& image_result
);
117 // Callback for TopSites that notifies when the "Most
118 // Visited" list is available. This function updates the ShellLinkItemList
119 // objects and send another query that retrieves a favicon for each URL in
121 void OnMostVisitedURLsAvailable(
122 const history::MostVisitedURLList
& data
);
124 // Callback for changes to the incognito mode availability pref.
125 void OnIncognitoAvailabilityChanged();
127 // Helper for RunUpdate() that determines its parameters.
128 void PostRunUpdate();
130 // Called on a timer to invoke RunUpdateOnFileThread() after requests storms
132 void DeferredRunUpdate();
134 // Runnable method that updates the jumplist, once all the data
136 void RunUpdateOnFileThread(
137 IncognitoModePrefs::Availability incognito_availability
);
139 // Helper method for RunUpdate to create icon files for the asynchrounously
141 void CreateIconFiles(const ShellLinkItemList
& item_list
);
143 // history::TopSitesObserver implementation.
144 void TopSitesLoaded(history::TopSites
* top_sites
) override
;
145 void TopSitesChanged(history::TopSites
* top_sites
,
146 ChangeReason change_reason
) override
;
148 // Tracks FaviconService tasks.
149 base::CancelableTaskTracker cancelable_task_tracker_
;
151 // The Profile object is used to listen for events
154 // Lives on the UI thread.
155 scoped_ptr
<content::NotificationRegistrar
> registrar_
;
156 scoped_ptr
<PrefChangeRegistrar
> pref_change_registrar_
;
158 // App id to associate with the jump list.
159 std::wstring app_id_
;
161 // The directory which contains JumpList icons.
162 base::FilePath icon_dir_
;
164 // Items in the "Most Visited" category of the application JumpList,
165 // protected by the list_lock_.
166 ShellLinkItemList most_visited_pages_
;
168 // Items in the "Recently Closed" category of the application JumpList,
169 // protected by the list_lock_.
170 ShellLinkItemList recently_closed_pages_
;
172 // Timer for requesting delayed updates of the jumplist.
173 base::OneShotTimer
<JumpList
> timer_
;
175 // A list of URLs we need to retrieve their favicons,
176 // protected by the list_lock_.
177 typedef std::pair
<std::string
, scoped_refptr
<ShellLinkItem
> > URLPair
;
178 std::list
<URLPair
> icon_urls_
;
180 // Id of last favicon task. It's used to cancel current task if a new one
181 // comes in before it finishes.
182 base::CancelableTaskTracker::TaskId task_id_
;
184 // Lock for most_visited_pages_, recently_closed_pages_, icon_urls_
185 // as they may be used by up to 3 threads.
186 base::Lock list_lock_
;
188 // For callbacks may be run after destruction.
189 base::WeakPtrFactory
<JumpList
> weak_ptr_factory_
;
191 DISALLOW_COPY_AND_ASSIGN(JumpList
);
194 #endif // CHROME_BROWSER_JUMPLIST_WIN_H_