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 "chrome/browser/sessions/tab_restore_service.h"
21 #include "chrome/browser/sessions/tab_restore_service_observer.h"
22 #include "components/history/core/browser/history_service.h"
23 #include "components/history/core/browser/history_types.h"
24 #include "components/history/core/browser/top_sites_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 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(TabRestoreService
* service
) override
;
70 // Observer callback to notice when our associated TabRestoreService
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_|
82 // Returns true if the custom JumpList is enabled.
83 // The custom jumplist works only on Windows 7 and above.
84 static bool Enabled();
87 friend struct content::BrowserThread::DeleteOnThread
<
88 content::BrowserThread::UI
>;
89 friend class base::DeleteHelper
<JumpList
>;
92 // Creates a ShellLinkItem object from a tab (or a window) and add it to the
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
,
99 void AddWindow(const TabRestoreService::Window
* window
,
100 ShellLinkItemList
* list
,
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
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
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
131 void DeferredRunUpdate();
133 // Runnable method that updates the jumplist, once all the data
135 void RunUpdateOnFileThread(
136 IncognitoModePrefs::Availability incognito_availability
);
138 // Helper method for RunUpdate to create icon files for the asynchrounously
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
) override
;
146 // Tracks FaviconService tasks.
147 base::CancelableTaskTracker cancelable_task_tracker_
;
149 // The Profile object is used to listen for events
152 // Lives on the UI thread.
153 scoped_ptr
<content::NotificationRegistrar
> registrar_
;
154 scoped_ptr
<PrefChangeRegistrar
> pref_change_registrar_
;
156 // App id to associate with the jump list.
157 std::wstring app_id_
;
159 // The directory which contains JumpList icons.
160 base::FilePath icon_dir_
;
162 // Items in the "Most Visited" category of the application JumpList,
163 // protected by the list_lock_.
164 ShellLinkItemList most_visited_pages_
;
166 // Items in the "Recently Closed" category of the application JumpList,
167 // protected by the list_lock_.
168 ShellLinkItemList recently_closed_pages_
;
170 // Timer for requesting delayed updates of the jumplist.
171 base::OneShotTimer
<JumpList
> timer_
;
173 // A list of URLs we need to retrieve their favicons,
174 // protected by the list_lock_.
175 typedef std::pair
<std::string
, scoped_refptr
<ShellLinkItem
> > URLPair
;
176 std::list
<URLPair
> icon_urls_
;
178 // Id of last favicon task. It's used to cancel current task if a new one
179 // comes in before it finishes.
180 base::CancelableTaskTracker::TaskId task_id_
;
182 // Lock for most_visited_pages_, recently_closed_pages_, icon_urls_
183 // as they may be used by up to 3 threads.
184 base::Lock list_lock_
;
186 // For callbacks may be run after destruction.
187 base::WeakPtrFactory
<JumpList
> weak_ptr_factory_
;
189 DISALLOW_COPY_AND_ASSIGN(JumpList
);
192 #endif // CHROME_BROWSER_JUMPLIST_WIN_H_