Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Side by Side Diff: chrome/browser/win/jumplist.h

Issue 2937993002: Revert of Fix stability and data racing issues, coalesce more updates for JumpList (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/ui/views/frame/browser_view.cc ('k') | chrome/browser/win/jumplist.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_WIN_JUMPLIST_H_ 5 #ifndef CHROME_BROWSER_WIN_JUMPLIST_H_
6 #define CHROME_BROWSER_WIN_JUMPLIST_H_ 6 #define CHROME_BROWSER_WIN_JUMPLIST_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <list> 10 #include <list>
11 #include <memory> 11 #include <memory>
12 #include <string> 12 #include <string>
13 #include <utility> 13 #include <utility>
14 14
15 #include "base/containers/flat_map.h" 15 #include "base/containers/flat_map.h"
16 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/ref_counted.h" 18 #include "base/memory/ref_counted.h"
19 #include "base/memory/weak_ptr.h" 19 #include "base/memory/weak_ptr.h"
20 #include "base/sequence_checker.h" 20 #include "base/sequence_checker.h"
21 #include "base/strings/string16.h" 21 #include "base/strings/string16.h"
22 #include "base/synchronization/lock.h"
22 #include "base/task/cancelable_task_tracker.h" 23 #include "base/task/cancelable_task_tracker.h"
23 #include "base/timer/timer.h" 24 #include "base/timer/timer.h"
24 #include "chrome/browser/prefs/incognito_mode_prefs.h" 25 #include "chrome/browser/prefs/incognito_mode_prefs.h"
25 #include "chrome/browser/win/jumplist_updater.h" 26 #include "chrome/browser/win/jumplist_updater.h"
27 #include "components/history/core/browser/history_service.h"
26 #include "components/history/core/browser/history_types.h" 28 #include "components/history/core/browser/history_types.h"
27 #include "components/history/core/browser/top_sites_observer.h" 29 #include "components/history/core/browser/top_sites_observer.h"
28 #include "components/keyed_service/core/keyed_service.h" 30 #include "components/keyed_service/core/refcounted_keyed_service.h"
29 #include "components/sessions/core/tab_restore_service.h" 31 #include "components/sessions/core/tab_restore_service.h"
30 #include "components/sessions/core/tab_restore_service_observer.h" 32 #include "components/sessions/core/tab_restore_service_observer.h"
33 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/notification_observer.h" 34 #include "content/public/browser/notification_observer.h"
32 #include "content/public/browser/notification_registrar.h" 35 #include "content/public/browser/notification_registrar.h"
33 36
34 namespace base { 37 namespace base {
35 class SingleThreadTaskRunner; 38 class SingleThreadTaskRunner;
36 class SequencedTaskRunner; 39 class SequencedTaskRunner;
37 } 40 }
38 41
39 namespace chrome { 42 namespace chrome {
40 struct FaviconImageResult; 43 struct FaviconImageResult;
41 } 44 }
42 45
43 class JumpListFactory; 46 class JumpListFactory;
44 class PrefChangeRegistrar; 47 class PrefChangeRegistrar;
45 class Profile; 48 class Profile;
46 49
47 // A class which implements an application JumpList. 50 // A class which implements an application JumpList.
48 // This class encapsulates operations required for updating an application 51 // This class encapsulates operations required for updating an application
49 // JumpList: 52 // JumpList:
50 // * Retrieving "Most Visited" pages from HistoryService; 53 // * Retrieving "Most Visited" pages from HistoryService;
51 // * Retrieving strings from the application resource; 54 // * Retrieving strings from the application resource;
52 // * Adding COM objects to JumpList, etc. 55 // * Adding COM objects to JumpList, etc.
53 // 56 //
54 // This class observes the tabs and policies of the given Profile and updates 57 // This class observes the tabs and policies of the given Profile and updates
55 // the JumpList whenever a change is detected. 58 // the JumpList whenever a change is detected.
56 // 59 //
57 // Updating a JumpList requires some file operations and it is not good to 60 // Updating a JumpList requires some file operations and it is not good to
58 // update it in a UI thread. To solve this problem, this class posts to a 61 // update it in a UI thread. To solve this problem, this class posts to a
59 // runnable method when it actually updates a JumpList. 62 // runnable method when it actually updates a JumpList.
63 //
64 // Note. base::CancelableTaskTracker is not thread safe, so we
65 // always delete JumpList on UI thread (the same thread it got constructed on).
60 class JumpList : public sessions::TabRestoreServiceObserver, 66 class JumpList : public sessions::TabRestoreServiceObserver,
61 public history::TopSitesObserver, 67 public history::TopSitesObserver,
62 public KeyedService { 68 public RefcountedKeyedService {
63 public: 69 public:
64 // KeyedService: 70 struct JumpListData {
65 void Shutdown() override; 71 JumpListData();
72 ~JumpListData();
73
74 // Lock for most_visited_pages_, recently_closed_pages_, icon_urls_
75 // as they may be used by up to 2 threads.
76 base::Lock list_lock_;
77
78 // A list of URLs we need to retrieve their favicons,
79 // protected by the list_lock_.
80 typedef std::pair<std::string, scoped_refptr<ShellLinkItem> > URLPair;
81 std::list<URLPair> icon_urls_;
82
83 // Items in the "Most Visited" category of the application JumpList,
84 // protected by the list_lock_.
85 ShellLinkItemList most_visited_pages_;
86
87 // Items in the "Recently Closed" category of the application JumpList,
88 // protected by the list_lock_.
89 ShellLinkItemList recently_closed_pages_;
90
91 // A boolean flag indicating if "Most Visited" category of the JumpList
92 // has new updates therefore its icons need to be updated.
93 // By default, this flag is set to false. If there's any change in
94 // TabRestoreService, this flag will be set to true.
95 bool most_visited_pages_have_updates_ = false;
96
97 // A boolean flag indicating if "Recently Closed" category of the JumpList
98 // has new updates therefore its icons need to be updated.
99 // By default, this flag is set to false. If there's any change in TopSites
100 // service, this flag will be set to true.
101 bool recently_closed_pages_have_updates_ = false;
102 };
103
104 // Observer callback for TabRestoreService::Observer to notify when a tab is
105 // added or removed.
106 void TabRestoreServiceChanged(sessions::TabRestoreService* service) override;
107
108 // Observer callback to notice when our associated TabRestoreService
109 // is destroyed.
110 void TabRestoreServiceDestroyed(
111 sessions::TabRestoreService* service) override;
112
113 // Cancel a pending jumplist update.
114 void CancelPendingUpdate();
115
116 // Terminate the jumplist: cancel any pending updates and stop observing
117 // the Profile and its services. This must be called before the |profile_|
118 // is destroyed.
119 void Terminate();
120
121 // RefcountedKeyedService:
122 void ShutdownOnUIThread() override;
66 123
67 // Returns true if the custom JumpList is enabled. 124 // Returns true if the custom JumpList is enabled.
68 static bool Enabled(); 125 static bool Enabled();
69 126
70 private: 127 private:
71 using UrlAndLinkItem = std::pair<std::string, scoped_refptr<ShellLinkItem>>;
72 using URLIconCache = base::flat_map<std::string, base::FilePath>;
73
74 // Holds results of the RunUpdateJumpList run.
75 struct UpdateResults {
76 UpdateResults();
77 ~UpdateResults();
78
79 // Icon file paths of the most visited links, indexed by tab url.
80 // Holding a copy of most_visited_icons_ initially, it's updated by the
81 // JumpList update run. If the update run succeeds, it overwrites
82 // most_visited_icons_.
83 URLIconCache most_visited_icons_in_update;
84
85 // icon file paths of the recently closed links, indexed by tab url.
86 // Holding a copy of recently_closed_icons_ initially, it's updated by the
87 // JumpList update run. If the update run succeeds, it overwrites
88 // recently_closed_icons_.
89 URLIconCache recently_closed_icons_in_update;
90
91 // A flag indicating if a JumpList update run is successful.
92 bool update_success = false;
93
94 // A flag indicating if there is a timeout in notifying the JumpList update
95 // to shell. Note that this variable is independent of update_success.
96 bool update_timeout = false;
97 };
98
99 friend JumpListFactory; 128 friend JumpListFactory;
100 explicit JumpList(Profile* profile); // Use JumpListFactory instead 129 explicit JumpList(Profile* profile); // Use JumpListFactory instead
101
102 ~JumpList() override; 130 ~JumpList() override;
103 131
104 // Adds a new ShellLinkItem for |tab| to the JumpList data provided that doing 132 enum class JumpListCategory { kMostVisited, kRecentlyClosed };
105 // so will not exceed |max_items|.
106 bool AddTab(const sessions::TabRestoreService::Tab& tab, size_t max_items);
107 133
108 // Adds a new ShellLinkItem for each tab in |window| to the JumpList data 134 // Adds a new ShellLinkItem for |tab| to |data| provided that doing so will
109 // provided that doing so will not exceed |max_items|. 135 // not exceed |max_items|.
136 bool AddTab(const sessions::TabRestoreService::Tab& tab,
137 size_t max_items,
138 JumpListData* data);
139
140 // Adds a new ShellLinkItem for each tab in |window| to |data| provided that
141 // doing so will not exceed |max_items|.
110 void AddWindow(const sessions::TabRestoreService::Window& window, 142 void AddWindow(const sessions::TabRestoreService::Window& window,
111 size_t max_items); 143 size_t max_items,
144 JumpListData* data);
112 145
113 // Starts loading a favicon for each URL in |icon_urls_|. 146 // Starts loading a favicon for each URL in |icon_urls_|.
114 // This function sends a query to HistoryService. 147 // This function sends a query to HistoryService.
115 // When finishing loading all favicons, this function posts a task that 148 // When finishing loading all favicons, this function posts a task that
116 // decompresses collected favicons and updates a JumpList. 149 // decompresses collected favicons and updates a JumpList.
117 void StartLoadingFavicon(); 150 void StartLoadingFavicon();
118 151
119 // Callback for HistoryService that notifies when a requested favicon is 152 // A callback function for HistoryService that notify when a requested favicon
120 // available. To avoid file operations, this function just attaches the given 153 // is available.
121 // |image_result| to a ShellLinkItem object. 154 // To avoid file operations, this function just attaches the given data to
155 // a ShellLinkItem object.
122 void OnFaviconDataAvailable( 156 void OnFaviconDataAvailable(
123 const favicon_base::FaviconImageResult& image_result); 157 const favicon_base::FaviconImageResult& image_result);
124 158
125 // Callback for TopSites that notifies when |data|, the "Most Visited" list, 159 // Callback for TopSites that notifies when the "Most Visited" list is
126 // is available. This function updates the ShellLinkItemList objects and 160 // available. This function updates the ShellLinkItemList objects and
127 // begins the process of fetching favicons for the URLs. 161 // begins the process of fetching favicons for the URLs.
128 void OnMostVisitedURLsAvailable( 162 void OnMostVisitedURLsAvailable(
129 const history::MostVisitedURLList& data); 163 const history::MostVisitedURLList& data);
130 164
131 // Callback for changes to the incognito mode availability pref. 165 // Callback for changes to the incognito mode availability pref.
132 void OnIncognitoAvailabilityChanged(); 166 void OnIncognitoAvailabilityChanged();
133 167
134 // sessions::TabRestoreServiceObserver:
135 void TabRestoreServiceChanged(sessions::TabRestoreService* service) override;
136 void TabRestoreServiceDestroyed(
137 sessions::TabRestoreService* service) override;
138
139 // history::TopSitesObserver:
140 void TopSitesLoaded(history::TopSites* top_sites) override;
141 void TopSitesChanged(history::TopSites* top_sites,
142 ChangeReason change_reason) override;
143
144 // Initializes the one-shot timer to update the JumpList in a while. If there
145 // is already a request queued then cancel it and post the new request. This
146 // ensures that JumpList update won't happen until there has been a brief
147 // quiet period, thus avoiding update storms.
148 void InitializeTimerForUpdate();
149
150 // Called on a timer after requests storms have subsided. Calls APIs
151 // ProcessTopSitesNotification and ProcessTabRestoreNotification on
152 // demand to do the actual work.
153 void OnDelayTimer();
154
155 // Processes notifications from TopSites service.
156 void ProcessTopSitesNotification();
157
158 // Processes notifications from TabRestore service.
159 void ProcessTabRestoreServiceNotification();
160
161 // Posts tasks to update the JumpList and delete any obsolete JumpList related 168 // Posts tasks to update the JumpList and delete any obsolete JumpList related
162 // folders. 169 // folders.
163 void PostRunUpdate(); 170 void PostRunUpdate();
164 171
165 // Deletes icon files in |icon_dir| which are not in |icon_cache| anymore. 172 // history::TopSitesObserver implementation.
166 static void DeleteIconFiles(const base::FilePath& icon_dir, 173 void TopSitesLoaded(history::TopSites* top_sites) override;
167 URLIconCache* icon_cache); 174 void TopSitesChanged(history::TopSites* top_sites,
175 ChangeReason change_reason) override;
168 176
169 // In |icon_dir|, creates at most |max_items| icon files which are not in 177 // Called on a timer to update the most visited URLs after requests storms
170 // |icon_cache| for the asynchrounously loaded icons stored in |item_list|. 178 // have subsided.
171 // |icon_cache| is also updated for newly created icons. 179 void DeferredTopSitesChanged();
180
181 // Called on a timer to update the "Recently Closed" category of JumpList
182 // after requests storms have subsided.
183 void DeferredTabRestoreServiceChanged();
184
185 // Deletes icon files of |category| in |icon_dir| which are not in the cache
186 // anymore.
187 void DeleteIconFiles(const base::FilePath& icon_dir,
188 JumpListCategory category);
189
190 // Creates at most |max_items| icon files of |category| in |icon_dir| for the
191 // asynchrounously loaded icons stored in |item_list|.
172 // Returns the number of new icon files created. 192 // Returns the number of new icon files created.
173 static int CreateIconFiles(const base::FilePath& icon_dir, 193 int CreateIconFiles(const base::FilePath& icon_dir,
174 const ShellLinkItemList& item_list, 194 const ShellLinkItemList& item_list,
175 size_t max_items, 195 size_t max_items,
176 URLIconCache* icon_cache); 196 JumpListCategory category);
177 197
178 // Updates icon files for |page_list| in |icon_dir|, which consists of 198 // Updates icon files in |icon_dir|, which includes deleting old icons and
179 // 1) creating at most |slot_limit| new icons which are not in |icon_cache|; 199 // creating at most |slot_limit| new icons for |page_list|.
180 // 2) deleting old icons which are not in |icon_cache|.
181 // Returns the number of new icon files created. 200 // Returns the number of new icon files created.
182 static int UpdateIconFiles(const base::FilePath& icon_dir, 201 int UpdateIconFiles(const base::FilePath& icon_dir,
183 const ShellLinkItemList& page_list, 202 const ShellLinkItemList& page_list,
184 size_t slot_limit, 203 size_t slot_limit,
185 URLIconCache* icon_cache); 204 JumpListCategory category);
186 205
187 // Updates the application JumpList, which consists of 1) create new icon 206 // Updates the jumplist, once all the data has been fetched. This method calls
188 // files; 2) delete obsolete icon files; 3) notify the OS. 207 // UpdateJumpList() to do most of the work.
189 // Note that any timeout error along the way results in the old JumpList being 208 void RunUpdateJumpList(
190 // left as-is, while any non-timeout error results in the old JumpList being 209 IncognitoModePrefs::Availability incognito_availability,
191 // left as-is, but without icon files.
192 static void RunUpdateJumpList(
193 const base::string16& app_id, 210 const base::string16& app_id,
194 const base::FilePath& profile_dir, 211 const base::FilePath& profile_dir,
195 const ShellLinkItemList& most_visited_pages, 212 base::RefCountedData<JumpListData>* ref_counted_data);
196 const ShellLinkItemList& recently_closed_pages,
197 bool most_visited_pages_have_updates,
198 bool recently_closed_pages_have_updates,
199 IncognitoModePrefs::Availability incognito_availability,
200 UpdateResults* update_results);
201 213
202 // Callback for RunUpdateJumpList that notifies when it finishes running. 214 // Updates the application JumpList, which consists of 1) delete old icon
203 // Updates certain JumpList member variables and/or triggers a new JumpList 215 // files; 2) create new icon files; 3) notify the OS. This method is called
204 // update based on |update_results|. 216 // from RunUpdateJumpList().
205 void OnRunUpdateCompletion(std::unique_ptr<UpdateResults> update_results); 217 // Note that any timeout error along the way results in the old jumplist being
206 218 // left as-is, while any non-timeout error results in the old jumplist being
207 // Cancels a pending JumpList update. 219 // left as-is, but without icon files.
208 void CancelPendingUpdate(); 220 bool UpdateJumpList(const base::string16& app_id,
209 221 const base::FilePath& profile_dir,
210 // Terminates the JumpList, which includes cancelling any pending updates and 222 const ShellLinkItemList& most_visited_pages,
211 // stopping observing the Profile and its services. This must be called before 223 const ShellLinkItemList& recently_closed_pages,
212 // the |profile_| is destroyed. 224 bool most_visited_pages_have_updates,
213 void Terminate(); 225 bool recently_closed_pages_have_updates,
226 IncognitoModePrefs::Availability incognito_availability);
214 227
215 // Tracks FaviconService tasks. 228 // Tracks FaviconService tasks.
216 base::CancelableTaskTracker cancelable_task_tracker_; 229 base::CancelableTaskTracker cancelable_task_tracker_;
217 230
218 // The Profile object is used to listen for events. 231 // The Profile object is used to listen for events.
219 Profile* profile_; 232 Profile* profile_;
220 233
221 // Manages the registration of pref change observers. 234 // Lives on the UI thread.
222 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; 235 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
223 236
224 // App id to associate with the JumpList. 237 // App id to associate with the jump list.
225 base::string16 app_id_; 238 base::string16 app_id_;
226 239
227 // Timer for requesting delayed JumpList updates. 240 // Timer for requesting delayed updates of the "Most Visited" category of
228 base::OneShotTimer timer_; 241 // jumplist.
242 base::OneShotTimer timer_most_visited_;
229 243
230 // A list of URLs we need to retrieve their favicons, 244 // Timer for requesting delayed updates of the "Recently Closed" category of
231 std::list<UrlAndLinkItem> icon_urls_; 245 // jumplist.
232 246 base::OneShotTimer timer_recently_closed_;
233 // Items in the "Most Visited" category of the JumpList.
234 ShellLinkItemList most_visited_pages_;
235
236 // Items in the "Recently Closed" category of the JumpList.
237 ShellLinkItemList recently_closed_pages_;
238
239 // The icon file paths of the most visited links, indexed by tab url.
240 URLIconCache most_visited_icons_;
241
242 // The icon file paths of the recently closed links, indexed by tab url.
243 URLIconCache recently_closed_icons_;
244
245 // A flag indicating if TopSites service has notifications.
246 bool top_sites_has_pending_notification_ = false;
247
248 // A flag indicating if TabRestore service has notifications.
249 bool tab_restore_has_pending_notification_ = false;
250
251 // A flag indicating if "Most Visited" category should be updated.
252 bool most_visited_should_update_ = false;
253
254 // A flag indicating if "Recently Closed" category should be updated.
255 bool recently_closed_should_update_ = false;
256
257 // A flag indicating if there's a JumpList update task already posted or
258 // currently running.
259 bool update_in_progress_ = false;
260
261 // A flag indicating if a session has at least one tab closed.
262 bool has_tab_closed_ = false;
263 247
264 // Number of updates to skip to alleviate the machine when a previous update 248 // Number of updates to skip to alleviate the machine when a previous update
265 // was too slow. Updates will be resumed when this reaches 0 again. 249 // was too slow. Updates will be resumed when this reaches 0 again.
266 int updates_to_skip_ = 0; 250 int updates_to_skip_ = 0;
267 251
252 // A boolean flag indicating if a session has at least one tab closed.
253 bool has_tab_closed_ = false;
254
255 // Holds data that can be accessed from multiple threads.
256 scoped_refptr<base::RefCountedData<JumpListData>> jumplist_data_;
257
258 // The icon file paths of the most visited links and the recently closed links
259 // in the current jumplist, indexed by tab url, respectively.
260 // They may only be accessed on update_jumplist_task_runner_.
261 base::flat_map<std::string, base::FilePath> most_visited_icons_;
262 base::flat_map<std::string, base::FilePath> recently_closed_icons_;
263
268 // Id of last favicon task. It's used to cancel current task if a new one 264 // Id of last favicon task. It's used to cancel current task if a new one
269 // comes in before it finishes. 265 // comes in before it finishes.
270 base::CancelableTaskTracker::TaskId task_id_ = 266 base::CancelableTaskTracker::TaskId task_id_;
271 base::CancelableTaskTracker::kBadTaskId;
272 267
273 // A task runner running tasks to update the JumpList. 268 // A task runner running tasks to update the JumpList.
274 scoped_refptr<base::SingleThreadTaskRunner> update_jumplist_task_runner_; 269 scoped_refptr<base::SingleThreadTaskRunner> update_jumplist_task_runner_;
275 270
276 // A task runner running tasks to delete the JumpListIcons and 271 // A task runner running tasks to delete JumpListIcons directory and
277 // JumpListIconsOld folders. 272 // JumpListIconsOld directory.
278 scoped_refptr<base::SequencedTaskRunner> delete_jumplisticons_task_runner_; 273 scoped_refptr<base::SequencedTaskRunner> delete_jumplisticons_task_runner_;
279 274
280 SEQUENCE_CHECKER(sequence_checker_); 275 SEQUENCE_CHECKER(sequence_checker_);
281 276
282 // For callbacks may run after destruction. 277 // For callbacks may be run after destruction.
283 base::WeakPtrFactory<JumpList> weak_ptr_factory_; 278 base::WeakPtrFactory<JumpList> weak_ptr_factory_;
284 279
285 DISALLOW_COPY_AND_ASSIGN(JumpList); 280 DISALLOW_COPY_AND_ASSIGN(JumpList);
286 }; 281 };
287 282
288 #endif // CHROME_BROWSER_WIN_JUMPLIST_H_ 283 #endif // CHROME_BROWSER_WIN_JUMPLIST_H_
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/frame/browser_view.cc ('k') | chrome/browser/win/jumplist.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698