OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_JUMPLIST_WIN_H_ | |
6 #define CHROME_BROWSER_JUMPLIST_WIN_H_ | |
7 | |
8 #include <stddef.h> | |
9 | |
10 #include <list> | |
11 #include <string> | |
12 #include <utility> | |
13 #include <vector> | |
14 | |
15 #include "base/files/file_path.h" | |
16 #include "base/macros.h" | |
17 #include "base/memory/weak_ptr.h" | |
18 #include "base/synchronization/lock.h" | |
19 #include "base/task/cancelable_task_tracker.h" | |
20 #include "base/timer/timer.h" | |
21 #include "chrome/browser/jumplist_updater_win.h" | |
22 #include "chrome/browser/prefs/incognito_mode_prefs.h" | |
23 #include "components/history/core/browser/history_service.h" | |
24 #include "components/history/core/browser/history_types.h" | |
25 #include "components/history/core/browser/top_sites_observer.h" | |
26 #include "components/sessions/core/tab_restore_service.h" | |
27 #include "components/sessions/core/tab_restore_service_observer.h" | |
28 #include "content/public/browser/browser_thread.h" | |
29 #include "content/public/browser/notification_observer.h" | |
30 #include "content/public/browser/notification_registrar.h" | |
31 | |
32 namespace chrome { | |
33 struct FaviconImageResult; | |
34 } | |
35 | |
36 class PrefChangeRegistrar; | |
37 class Profile; | |
38 | |
39 // A class which implements an application JumpList. | |
40 // This class encapsulates operations required for updating an application | |
41 // JumpList: | |
42 // * Retrieving "Most Visited" pages from HistoryService; | |
43 // * Retrieving strings from the application resource; | |
44 // * Adding COM objects to JumpList, etc. | |
45 // | |
46 // This class observes the tabs and policies of the given Profile and updates | |
47 // the JumpList whenever a change is detected. | |
48 // | |
49 // Updating a JumpList requires some file operations and it is not good to | |
50 // update it in a UI thread. To solve this problem, this class posts to a | |
51 // runnable method when it actually updates a JumpList. | |
52 // | |
53 // Note. base::CancelableTaskTracker is not thread safe, so we | |
54 // always delete JumpList on UI thread (the same thread it got constructed on). | |
55 class JumpList : public sessions::TabRestoreServiceObserver, | |
56 public content::NotificationObserver, | |
57 public history::TopSitesObserver, | |
58 public base::NonThreadSafe, | |
59 public base::RefCounted<JumpList> { | |
60 public: | |
61 struct JumpListData { | |
62 JumpListData(); | |
63 ~JumpListData(); | |
64 | |
65 // Lock for most_visited_pages_, recently_closed_pages_, icon_urls_ | |
66 // as they may be used by up to 2 threads. | |
67 base::Lock list_lock_; | |
68 | |
69 // A list of URLs we need to retrieve their favicons, | |
70 // protected by the list_lock_. | |
71 typedef std::pair<std::string, scoped_refptr<ShellLinkItem> > URLPair; | |
72 std::list<URLPair> icon_urls_; | |
73 | |
74 // Items in the "Most Visited" category of the application JumpList, | |
75 // protected by the list_lock_. | |
76 ShellLinkItemList most_visited_pages_; | |
77 | |
78 // Items in the "Recently Closed" category of the application JumpList, | |
79 // protected by the list_lock_. | |
80 ShellLinkItemList recently_closed_pages_; | |
81 }; | |
82 | |
83 explicit JumpList(Profile* profile); | |
84 | |
85 // NotificationObserver implementation. | |
86 void Observe(int type, | |
87 const content::NotificationSource& source, | |
88 const content::NotificationDetails& details) override; | |
89 | |
90 // Observer callback for TabRestoreService::Observer to notify when a tab is | |
91 // added or removed. | |
92 void TabRestoreServiceChanged(sessions::TabRestoreService* service) override; | |
93 | |
94 // Observer callback to notice when our associated TabRestoreService | |
95 // is destroyed. | |
96 void TabRestoreServiceDestroyed( | |
97 sessions::TabRestoreService* service) override; | |
98 | |
99 // Cancel a pending jumplist update. | |
100 void CancelPendingUpdate(); | |
101 | |
102 // Terminate the jumplist: cancel any pending updates and stop observing | |
103 // the Profile and its services. This must be called before the |profile_| | |
104 // is destroyed. | |
105 void Terminate(); | |
106 | |
107 // Returns true if the custom JumpList is enabled. | |
108 // The custom jumplist works only on Windows 7 and above. | |
109 static bool Enabled(); | |
110 | |
111 private: | |
112 friend class base::RefCounted<JumpList>; | |
113 ~JumpList() override; | |
114 | |
115 // Creates a ShellLinkItem object from a tab (or a window) and add it to the | |
116 // given list. | |
117 // These functions are copied from the RecentlyClosedTabsHandler class for | |
118 // compatibility with the new-tab page. | |
119 bool AddTab(const sessions::TabRestoreService::Tab* tab, | |
120 ShellLinkItemList* list, | |
121 size_t max_items); | |
122 void AddWindow(const sessions::TabRestoreService::Window* window, | |
123 ShellLinkItemList* list, | |
124 size_t max_items); | |
125 | |
126 // Starts loading a favicon for each URL in |icon_urls_|. | |
127 // This function sends a query to HistoryService. | |
128 // When finishing loading all favicons, this function posts a task that | |
129 // decompresses collected favicons and updates a JumpList. | |
130 void StartLoadingFavicon(); | |
131 | |
132 // A callback function for HistoryService that notify when a requested favicon | |
133 // is available. | |
134 // To avoid file operations, this function just attaches the given data to | |
135 // a ShellLinkItem object. | |
136 void OnFaviconDataAvailable( | |
137 const favicon_base::FaviconImageResult& image_result); | |
138 | |
139 // Callback for TopSites that notifies when the "Most | |
140 // Visited" list is available. This function updates the ShellLinkItemList | |
141 // objects and send another query that retrieves a favicon for each URL in | |
142 // the list. | |
143 void OnMostVisitedURLsAvailable( | |
144 const history::MostVisitedURLList& data); | |
145 | |
146 // Callback for changes to the incognito mode availability pref. | |
147 void OnIncognitoAvailabilityChanged(); | |
148 | |
149 // Helper for RunUpdate() that determines its parameters. | |
150 void PostRunUpdate(); | |
151 | |
152 // Called on a timer to invoke RunUpdateOnFileThread() after requests storms | |
153 // have subsided. | |
154 void DeferredRunUpdate(); | |
155 | |
156 // history::TopSitesObserver implementation. | |
157 void TopSitesLoaded(history::TopSites* top_sites) override; | |
158 void TopSitesChanged(history::TopSites* top_sites, | |
159 ChangeReason change_reason) override; | |
160 | |
161 // Tracks FaviconService tasks. | |
162 base::CancelableTaskTracker cancelable_task_tracker_; | |
163 | |
164 // The Profile object is used to listen for events | |
165 Profile* profile_; | |
166 | |
167 // Lives on the UI thread. | |
168 std::unique_ptr<content::NotificationRegistrar> registrar_; | |
169 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; | |
170 | |
171 // App id to associate with the jump list. | |
172 std::wstring app_id_; | |
173 | |
174 // The directory which contains JumpList icons. | |
175 base::FilePath icon_dir_; | |
176 | |
177 // Timer for requesting delayed updates of the jumplist. | |
178 base::OneShotTimer timer_; | |
179 | |
180 // Holds data that can be accessed from multiple threads. | |
181 scoped_refptr<base::RefCountedData<JumpListData>> jumplist_data_; | |
182 | |
183 // Id of last favicon task. It's used to cancel current task if a new one | |
184 // comes in before it finishes. | |
185 base::CancelableTaskTracker::TaskId task_id_; | |
186 | |
187 // For callbacks may be run after destruction. | |
188 base::WeakPtrFactory<JumpList> weak_ptr_factory_; | |
189 | |
190 DISALLOW_COPY_AND_ASSIGN(JumpList); | |
191 }; | |
192 | |
193 #endif // CHROME_BROWSER_JUMPLIST_WIN_H_ | |
OLD | NEW |