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/sessions/tab_restore_service.h"
21 #include "chrome/browser/sessions/tab_restore_service_observer.h"
22 #include "components/history/core/browser/history_types.h"
23 #include "components/history/core/browser/top_sites_observer.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/notification_observer.h"
28 struct FaviconImageResult
;
32 class NotificationRegistrar
;
35 class PrefChangeRegistrar
;
38 // A class which implements an application JumpList.
39 // This class encapsulates operations required for updating an application
41 // * Retrieving "Most Visited" pages from HistoryService;
42 // * Retrieving strings from the application resource;
43 // * Adding COM objects to JumpList, etc.
45 // This class observes the tabs and policies of the given Profile and updates
46 // the JumpList whenever a change is detected.
48 // Updating a JumpList requires some file operations and it is not good to
49 // update it in a UI thread. To solve this problem, this class posts to a
50 // runnable method when it actually updates a JumpList.
52 // Note. base::CancelableTaskTracker is not thread safe, so we
53 // always delete JumpList on UI thread (the same thread it got constructed on).
54 class JumpList
: public TabRestoreServiceObserver
,
55 public content::NotificationObserver
,
56 public history::TopSitesObserver
,
57 public base::RefCountedThreadSafe
<
59 content::BrowserThread::DeleteOnUIThread
> {
61 explicit JumpList(Profile
* profile
);
63 // NotificationObserver implementation.
64 void Observe(int type
,
65 const content::NotificationSource
& source
,
66 const content::NotificationDetails
& details
) override
;
68 // Observer callback for TabRestoreService::Observer to notify when a tab is
70 void TabRestoreServiceChanged(TabRestoreService
* service
) override
;
72 // Observer callback to notice when our associated TabRestoreService
74 void TabRestoreServiceDestroyed(TabRestoreService
* service
) override
;
76 // Cancel a pending jumplist update.
77 void CancelPendingUpdate();
79 // Terminate the jumplist: cancel any pending updates and stop observing
80 // the Profile and its services. This must be called before the |profile_|
84 // Returns true if the custom JumpList is enabled.
85 // The custom jumplist works only on Windows 7 and above.
86 static bool Enabled();
89 friend struct content::BrowserThread::DeleteOnThread
<
90 content::BrowserThread::UI
>;
91 friend class base::DeleteHelper
<JumpList
>;
94 // Creates a ShellLinkItem object from a tab (or a window) and add it to the
96 // These functions are copied from the RecentlyClosedTabsHandler class for
97 // compatibility with the new-tab page.
98 bool AddTab(const TabRestoreService::Tab
* tab
,
99 ShellLinkItemList
* list
,
101 void AddWindow(const TabRestoreService::Window
* window
,
102 ShellLinkItemList
* list
,
105 // Starts loading a favicon for each URL in |icon_urls_|.
106 // This function sends a query to HistoryService.
107 // When finishing loading all favicons, this function posts a task that
108 // decompresses collected favicons and updates a JumpList.
109 void StartLoadingFavicon();
111 // A callback function for HistoryService that notify when a requested favicon
113 // To avoid file operations, this function just attaches the given data to
114 // a ShellLinkItem object.
115 void OnFaviconDataAvailable(
116 const favicon_base::FaviconImageResult
& image_result
);
118 // Callback for TopSites that notifies when the "Most
119 // Visited" list is available. This function updates the ShellLinkItemList
120 // objects and send another query that retrieves a favicon for each URL in
122 void OnMostVisitedURLsAvailable(
123 const history::MostVisitedURLList
& data
);
125 // Callback for changes to the incognito mode availability pref.
126 void OnIncognitoAvailabilityChanged();
128 // Helper for RunUpdate() that determines its parameters.
129 void PostRunUpdate();
131 // Runnable method that updates the jumplist, once all the data
133 void RunUpdateOnFileThread(
134 IncognitoModePrefs::Availability incognito_availability
);
136 // Helper method for RunUpdate to create icon files for the asynchrounously
138 void CreateIconFiles(const ShellLinkItemList
& item_list
);
140 // history::TopSitesObserver implementation.
141 void TopSitesLoaded(history::TopSites
* top_sites
) override
;
142 void TopSitesChanged(history::TopSites
* top_sites
) override
;
144 // Tracks FaviconService tasks.
145 base::CancelableTaskTracker cancelable_task_tracker_
;
147 // The Profile object is used to listen for events
150 // Lives on the UI thread.
151 scoped_ptr
<content::NotificationRegistrar
> registrar_
;
152 scoped_ptr
<PrefChangeRegistrar
> pref_change_registrar_
;
154 // App id to associate with the jump list.
155 std::wstring app_id_
;
157 // The directory which contains JumpList icons.
158 base::FilePath icon_dir_
;
160 // Items in the "Most Visited" category of the application JumpList,
161 // protected by the list_lock_.
162 ShellLinkItemList most_visited_pages_
;
164 // Items in the "Recently Closed" category of the application JumpList,
165 // protected by the list_lock_.
166 ShellLinkItemList recently_closed_pages_
;
168 // A list of URLs we need to retrieve their favicons,
169 // protected by the list_lock_.
170 typedef std::pair
<std::string
, scoped_refptr
<ShellLinkItem
> > URLPair
;
171 std::list
<URLPair
> icon_urls_
;
173 // Id of last favicon task. It's used to cancel current task if a new one
174 // comes in before it finishes.
175 base::CancelableTaskTracker::TaskId task_id_
;
177 // Lock for most_visited_pages_, recently_closed_pages_, icon_urls_
178 // as they may be used by up to 3 threads.
179 base::Lock list_lock_
;
181 // For callbacks may be run after destruction.
182 base::WeakPtrFactory
<JumpList
> weak_ptr_factory_
;
184 DISALLOW_COPY_AND_ASSIGN(JumpList
);
187 #endif // CHROME_BROWSER_JUMPLIST_WIN_H_