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 "chrome/browser/history/history_service.h"
18 #include "chrome/browser/jumplist_updater_win.h"
19 #include "chrome/browser/prefs/incognito_mode_prefs.h"
20 #include "chrome/browser/profiles/avatar_menu.h"
21 #include "chrome/browser/profiles/avatar_menu_observer.h"
22 #include "chrome/browser/sessions/tab_restore_service.h"
23 #include "chrome/browser/sessions/tab_restore_service_observer.h"
24 #include "components/history/core/browser/history_types.h"
25 #include "components/history/core/browser/top_sites_observer.h"
26 #include "content/public/browser/browser_thread.h"
29 struct FaviconImageResult
;
33 class NotificationRegistrar
;
37 class PrefChangeRegistrar
;
40 // A class which implements an application JumpList.
41 // This class encapsulates operations required for updating an application
43 // * Retrieving "Most Visited" pages from HistoryService;
44 // * Retrieving strings from the application resource;
45 // * Creating COM objects used by JumpList from PageUsageData objects;
46 // * Adding COM objects to JumpList, etc.
48 // This class observes the tabs and policies of the given Profile and updates
49 // the JumpList whenever a change is detected.
51 // Updating a JumpList requires some file operations and it is not good to
52 // update it in a UI thread. To solve this problem, this class posts to a
53 // runnable method when it actually updates a JumpList.
55 // Note. base::CancelableTaskTracker is not thread safe, so we
56 // always delete JumpList on UI thread (the same thread it got constructed on).
57 class JumpList
: public TabRestoreServiceObserver
,
58 public content::NotificationObserver
,
59 public AvatarMenuObserver
,
60 public history::TopSitesObserver
,
61 public base::RefCountedThreadSafe
<
63 content::BrowserThread::DeleteOnUIThread
> {
65 explicit JumpList(Profile
* profile
);
67 // NotificationObserver implementation.
68 void Observe(int type
,
69 const content::NotificationSource
& source
,
70 const content::NotificationDetails
& details
) override
;
72 // Observer callback for TabRestoreService::Observer to notify when a tab is
74 void TabRestoreServiceChanged(TabRestoreService
* service
) override
;
76 // Observer callback to notice when our associated TabRestoreService
78 void TabRestoreServiceDestroyed(TabRestoreService
* service
) override
;
80 // Overridden from AvatarMenuObserver:
81 void OnAvatarMenuChanged(AvatarMenu
* avatar_menu
) override
;
83 // Cancel a pending jumplist update.
84 void CancelPendingUpdate();
86 // Terminate the jumplist: cancel any pending updates and stop observing
87 // the Profile and its services. This must be called before the |profile_|
91 // Returns true if the custom JumpList is enabled.
92 // The custom jumplist works only on Windows 7 and above.
93 static bool Enabled();
96 friend struct content::BrowserThread::DeleteOnThread
<
97 content::BrowserThread::UI
>;
98 friend class base::DeleteHelper
<JumpList
>;
101 // Creates a ShellLinkItem object from a tab (or a window) and add it to the
103 // These functions are copied from the RecentlyClosedTabsHandler class for
104 // compatibility with the new-tab page.
105 bool AddTab(const TabRestoreService::Tab
* tab
,
106 ShellLinkItemList
* list
,
108 void AddWindow(const TabRestoreService::Window
* window
,
109 ShellLinkItemList
* list
,
112 // Starts loading a favicon for each URL in |icon_urls_|.
113 // This function sends a query to HistoryService.
114 // When finishing loading all favicons, this function posts a task that
115 // decompresses collected favicons and updates a JumpList.
116 void StartLoadingFavicon();
118 // A callback function for HistoryService that notify when a requested favicon
120 // To avoid file operations, this function just attaches the given data to
121 // a ShellLinkItem object.
122 void OnFaviconDataAvailable(
123 const favicon_base::FaviconImageResult
& image_result
);
125 // Callback for TopSites that notifies when the "Most
126 // Visited" list is available. This function updates the ShellLinkItemList
127 // objects and send another query that retrieves a favicon for each URL in
129 void OnMostVisitedURLsAvailable(
130 const history::MostVisitedURLList
& data
);
132 // Callback for changes to the incognito mode availability pref.
133 void OnIncognitoAvailabilityChanged();
135 // Helper for RunUpdate() that determines its parameters.
136 void PostRunUpdate();
138 // Runnable method that updates the jumplist, once all the data
140 void RunUpdateOnFileThread(
141 IncognitoModePrefs::Availability incognito_availability
);
143 // Helper method for RunUpdate to create icon files for the asynchrounously
145 void CreateIconFiles(const ShellLinkItemList
& item_list
);
147 // Called when the list of Profiles has changed. This function updates the
148 // |profile_switcher_| ShellLinkItemList.
149 void UpdateProfileSwitcher();
151 // history::TopSitesObserver implementation.
152 void TopSitesLoaded(history::TopSites
* top_sites
) override
;
153 void TopSitesChanged(history::TopSites
* top_sites
) override
;
155 // Tracks FaviconService tasks.
156 base::CancelableTaskTracker cancelable_task_tracker_
;
158 // The Profile object is used to listen for events
161 // Lives on the UI thread.
162 scoped_ptr
<content::NotificationRegistrar
> registrar_
;
163 scoped_ptr
<PrefChangeRegistrar
> pref_change_registrar_
;
165 // App id to associate with the jump list.
166 std::wstring app_id_
;
168 // The directory which contains JumpList icons.
169 base::FilePath icon_dir_
;
171 // Items in the "Most Visited" category of the application JumpList,
172 // protected by the list_lock_.
173 ShellLinkItemList most_visited_pages_
;
175 // Items in the "Recently Closed" category of the application JumpList,
176 // protected by the list_lock_.
177 ShellLinkItemList recently_closed_pages_
;
179 // Items in the "People" category of the application JumpList, protected
180 // by the list_lock_.
181 ShellLinkItemList profile_switcher_
;
183 // A list of URLs we need to retrieve their favicons,
184 // protected by the list_lock_.
185 typedef std::pair
<std::string
, scoped_refptr
<ShellLinkItem
> > URLPair
;
186 std::list
<URLPair
> icon_urls_
;
188 // Id of last favicon task. It's used to cancel current task if a new one
189 // comes in before it finishes.
190 base::CancelableTaskTracker::TaskId task_id_
;
192 // Lock for most_visited_pages_, recently_closed_pages_, icon_urls_
193 // as they may be used by up to 3 threads.
194 base::Lock list_lock_
;
196 // For callbacks may be run after destruction.
197 base::WeakPtrFactory
<JumpList
> weak_ptr_factory_
;
199 // Contains data about existing Profiles.
200 scoped_ptr
<AvatarMenu
> avatar_menu_
;
202 // Whether the experiment that is replacing "Most Visited" category with a
203 // "People" category is enabled.
204 bool use_profiles_category_
;
206 DISALLOW_COPY_AND_ASSIGN(JumpList
);
209 #endif // CHROME_BROWSER_JUMPLIST_WIN_H_