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