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 "content/public/browser/browser_thread.h"
28 struct FaviconImageResult
;
32 class NotificationRegistrar
;
36 class PrefChangeRegistrar
;
39 // A class which implements an application JumpList.
40 // This class encapsulates operations required for updating an application
42 // * Retrieving "Most Visited" pages from HistoryService;
43 // * Retrieving strings from the application resource;
44 // * Creating COM objects used by JumpList from PageUsageData objects;
45 // * Adding COM objects to JumpList, etc.
47 // This class observes the tabs and policies of the given Profile and updates
48 // the JumpList whenever a change is detected.
50 // Updating a JumpList requires some file operations and it is not good to
51 // update it in a UI thread. To solve this problem, this class posts to a
52 // runnable method when it actually updates a JumpList.
54 // Note. base::CancelableTaskTracker is not thread safe, so we
55 // always delete JumpList on UI thread (the same thread it got constructed on).
56 class JumpList
: public TabRestoreServiceObserver
,
57 public content::NotificationObserver
,
58 public AvatarMenuObserver
,
59 public base::RefCountedThreadSafe
<
60 JumpList
, content::BrowserThread::DeleteOnUIThread
> {
62 explicit JumpList(Profile
* profile
);
64 // NotificationObserver implementation.
65 void Observe(int type
,
66 const content::NotificationSource
& source
,
67 const content::NotificationDetails
& details
) override
;
69 // Observer callback for TabRestoreService::Observer to notify when a tab is
71 void TabRestoreServiceChanged(TabRestoreService
* service
) override
;
73 // Observer callback to notice when our associated TabRestoreService
75 void TabRestoreServiceDestroyed(TabRestoreService
* service
) override
;
77 // Overridden from AvatarMenuObserver:
78 void OnAvatarMenuChanged(AvatarMenu
* avatar_menu
) override
;
80 // Cancel a pending jumplist update.
81 void CancelPendingUpdate();
83 // Terminate the jumplist: cancel any pending updates and stop observing
84 // the Profile and its services. This must be called before the |profile_|
88 // Returns true if the custom JumpList is enabled.
89 // The custom jumplist works only on Windows 7 and above.
90 static bool Enabled();
93 friend struct content::BrowserThread::DeleteOnThread
<
94 content::BrowserThread::UI
>;
95 friend class base::DeleteHelper
<JumpList
>;
98 // Creates a ShellLinkItem object from a tab (or a window) and add it to the
100 // These functions are copied from the RecentlyClosedTabsHandler class for
101 // compatibility with the new-tab page.
102 bool AddTab(const TabRestoreService::Tab
* tab
,
103 ShellLinkItemList
* list
,
105 void AddWindow(const TabRestoreService::Window
* window
,
106 ShellLinkItemList
* list
,
109 // Starts loading a favicon for each URL in |icon_urls_|.
110 // This function sends a query to HistoryService.
111 // When finishing loading all favicons, this function posts a task that
112 // decompresses collected favicons and updates a JumpList.
113 void StartLoadingFavicon();
115 // A callback function for HistoryService that notify when a requested favicon
117 // To avoid file operations, this function just attaches the given data to
118 // a ShellLinkItem object.
119 void OnFaviconDataAvailable(
120 const favicon_base::FaviconImageResult
& image_result
);
122 // Callback for TopSites that notifies when the "Most
123 // Visited" list is available. This function updates the ShellLinkItemList
124 // objects and send another query that retrieves a favicon for each URL in
126 void OnMostVisitedURLsAvailable(
127 const history::MostVisitedURLList
& data
);
129 // Callback for changes to the incognito mode availability pref.
130 void OnIncognitoAvailabilityChanged();
132 // Helper for RunUpdate() that determines its parameters.
133 void PostRunUpdate();
135 // Runnable method that updates the jumplist, once all the data
137 void RunUpdateOnFileThread(
138 IncognitoModePrefs::Availability incognito_availability
);
140 // Helper method for RunUpdate to create icon files for the asynchrounously
142 void CreateIconFiles(const ShellLinkItemList
& item_list
);
144 // Called when the list of Profiles has changed. This function updates the
145 // |profile_switcher_| ShellLinkItemList.
146 void UpdateProfileSwitcher();
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 // Items in the "People" category of the application JumpList, protected
173 // by the list_lock_.
174 ShellLinkItemList profile_switcher_
;
176 // A list of URLs we need to retrieve their favicons,
177 // protected by the list_lock_.
178 typedef std::pair
<std::string
, scoped_refptr
<ShellLinkItem
> > URLPair
;
179 std::list
<URLPair
> icon_urls_
;
181 // Id of last favicon task. It's used to cancel current task if a new one
182 // comes in before it finishes.
183 base::CancelableTaskTracker::TaskId task_id_
;
185 // Lock for most_visited_pages_, recently_closed_pages_, icon_urls_
186 // as they may be used by up to 3 threads.
187 base::Lock list_lock_
;
189 // For callbacks may be run after destruction.
190 base::WeakPtrFactory
<JumpList
> weak_ptr_factory_
;
192 // Contains data about existing Profiles.
193 scoped_ptr
<AvatarMenu
> avatar_menu_
;
195 // Whether the experiment that is replacing "Most Visited" category with a
196 // "People" category is enabled.
197 bool use_profiles_category_
;
199 DISALLOW_COPY_AND_ASSIGN(JumpList
);
202 #endif // CHROME_BROWSER_JUMPLIST_WIN_H_