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

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

Issue 2931573003: Fix stability and data racing issues, coalesce more updates for JumpList (Closed)
Patch Set: Remove null check 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"
23 #include "base/task/cancelable_task_tracker.h" 22 #include "base/task/cancelable_task_tracker.h"
24 #include "base/timer/timer.h" 23 #include "base/timer/timer.h"
25 #include "chrome/browser/prefs/incognito_mode_prefs.h" 24 #include "chrome/browser/prefs/incognito_mode_prefs.h"
26 #include "chrome/browser/win/jumplist_updater.h" 25 #include "chrome/browser/win/jumplist_updater.h"
27 #include "components/history/core/browser/history_service.h"
28 #include "components/history/core/browser/history_types.h" 26 #include "components/history/core/browser/history_types.h"
29 #include "components/history/core/browser/top_sites_observer.h" 27 #include "components/history/core/browser/top_sites_observer.h"
30 #include "components/keyed_service/core/refcounted_keyed_service.h" 28 #include "components/keyed_service/core/keyed_service.h"
31 #include "components/sessions/core/tab_restore_service.h" 29 #include "components/sessions/core/tab_restore_service.h"
32 #include "components/sessions/core/tab_restore_service_observer.h" 30 #include "components/sessions/core/tab_restore_service_observer.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/notification_observer.h" 31 #include "content/public/browser/notification_observer.h"
35 #include "content/public/browser/notification_registrar.h" 32 #include "content/public/browser/notification_registrar.h"
36 33
37 namespace base { 34 namespace base {
38 class SingleThreadTaskRunner; 35 class SingleThreadTaskRunner;
39 class SequencedTaskRunner; 36 class SequencedTaskRunner;
40 } 37 }
41 38
42 namespace chrome { 39 namespace chrome {
43 struct FaviconImageResult; 40 struct FaviconImageResult;
44 } 41 }
45 42
46 class JumpListFactory; 43 class JumpListFactory;
47 class PrefChangeRegistrar; 44 class PrefChangeRegistrar;
48 class Profile; 45 class Profile;
49 46
50 // A class which implements an application JumpList. 47 // A class which implements an application JumpList.
51 // This class encapsulates operations required for updating an application 48 // This class encapsulates operations required for updating an application
52 // JumpList: 49 // JumpList:
53 // * Retrieving "Most Visited" pages from HistoryService; 50 // * Retrieving "Most Visited" pages from HistoryService;
54 // * Retrieving strings from the application resource; 51 // * Retrieving strings from the application resource;
55 // * Adding COM objects to JumpList, etc. 52 // * Adding COM objects to JumpList, etc.
56 // 53 //
57 // This class observes the tabs and policies of the given Profile and updates 54 // This class observes the tabs and policies of the given Profile and updates
58 // the JumpList whenever a change is detected. 55 // the JumpList whenever a change is detected.
59 // 56 //
60 // Updating a JumpList requires some file operations and it is not good to 57 // Updating a JumpList requires some file operations and it is not good to
61 // update it in a UI thread. To solve this problem, this class posts to a 58 // update it in a UI thread. To solve this problem, this class posts to a
62 // runnable method when it actually updates a JumpList. 59 // 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).
66 class JumpList : public sessions::TabRestoreServiceObserver, 60 class JumpList : public sessions::TabRestoreServiceObserver,
67 public history::TopSitesObserver, 61 public history::TopSitesObserver,
68 public RefcountedKeyedService { 62 public KeyedService {
69 public: 63 public:
70 struct JumpListData { 64 // KeyedService:
71 JumpListData(); 65 void Shutdown() override;
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;
123 66
124 // Returns true if the custom JumpList is enabled. 67 // Returns true if the custom JumpList is enabled.
125 static bool Enabled(); 68 static bool Enabled();
126 69
127 private: 70 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
128 friend JumpListFactory; 99 friend JumpListFactory;
129 explicit JumpList(Profile* profile); // Use JumpListFactory instead 100 explicit JumpList(Profile* profile); // Use JumpListFactory instead
101
130 ~JumpList() override; 102 ~JumpList() override;
131 103
132 enum class JumpListCategory { kMostVisited, kRecentlyClosed }; 104 // Adds a new ShellLinkItem for |tab| to the JumpList data provided that doing
105 // so will not exceed |max_items|.
106 bool AddTab(const sessions::TabRestoreService::Tab& tab, size_t max_items);
133 107
134 // Adds a new ShellLinkItem for |tab| to |data| provided that doing so will 108 // Adds a new ShellLinkItem for each tab in |window| to the JumpList data
135 // not exceed |max_items|. 109 // provided that doing so will 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|.
142 void AddWindow(const sessions::TabRestoreService::Window& window, 110 void AddWindow(const sessions::TabRestoreService::Window& window,
143 size_t max_items, 111 size_t max_items);
144 JumpListData* data);
145 112
146 // Starts loading a favicon for each URL in |icon_urls_|. 113 // Starts loading a favicon for each URL in |icon_urls_|.
147 // This function sends a query to HistoryService. 114 // This function sends a query to HistoryService.
148 // When finishing loading all favicons, this function posts a task that 115 // When finishing loading all favicons, this function posts a task that
149 // decompresses collected favicons and updates a JumpList. 116 // decompresses collected favicons and updates a JumpList.
150 void StartLoadingFavicon(); 117 void StartLoadingFavicon();
151 118
152 // A callback function for HistoryService that notify when a requested favicon 119 // Callback for HistoryService that notifies when a requested favicon is
153 // is available. 120 // available. To avoid file operations, this function just attaches the given
154 // To avoid file operations, this function just attaches the given data to 121 // |image_result| to a ShellLinkItem object.
155 // a ShellLinkItem object.
156 void OnFaviconDataAvailable( 122 void OnFaviconDataAvailable(
157 const favicon_base::FaviconImageResult& image_result); 123 const favicon_base::FaviconImageResult& image_result);
158 124
159 // Callback for TopSites that notifies when the "Most Visited" list is 125 // Callback for TopSites that notifies when |data|, the "Most Visited" list,
160 // available. This function updates the ShellLinkItemList objects and 126 // is available. This function updates the ShellLinkItemList objects and
161 // begins the process of fetching favicons for the URLs. 127 // begins the process of fetching favicons for the URLs.
162 void OnMostVisitedURLsAvailable( 128 void OnMostVisitedURLsAvailable(
163 const history::MostVisitedURLList& data); 129 const history::MostVisitedURLList& data);
164 130
165 // Callback for changes to the incognito mode availability pref. 131 // Callback for changes to the incognito mode availability pref.
166 void OnIncognitoAvailabilityChanged(); 132 void OnIncognitoAvailabilityChanged();
167 133
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
168 // Posts tasks to update the JumpList and delete any obsolete JumpList related 161 // Posts tasks to update the JumpList and delete any obsolete JumpList related
169 // folders. 162 // folders.
170 void PostRunUpdate(); 163 void PostRunUpdate();
171 164
172 // history::TopSitesObserver implementation. 165 // Deletes icon files in |icon_dir| which are not in |icon_cache| anymore.
173 void TopSitesLoaded(history::TopSites* top_sites) override; 166 static void DeleteIconFiles(const base::FilePath& icon_dir,
174 void TopSitesChanged(history::TopSites* top_sites, 167 URLIconCache* icon_cache);
175 ChangeReason change_reason) override;
176 168
177 // Called on a timer to update the most visited URLs after requests storms 169 // In |icon_dir|, creates at most |max_items| icon files which are not in
178 // have subsided. 170 // |icon_cache| for the asynchrounously loaded icons stored in |item_list|.
179 void DeferredTopSitesChanged(); 171 // |icon_cache| is also updated for newly created icons.
172 // Returns the number of new icon files created.
173 static int CreateIconFiles(const base::FilePath& icon_dir,
174 const ShellLinkItemList& item_list,
175 size_t max_items,
176 URLIconCache* icon_cache);
180 177
181 // Called on a timer to update the "Recently Closed" category of JumpList 178 // Updates icon files for |page_list| in |icon_dir|, which consists of
182 // after requests storms have subsided. 179 // 1) creating at most |slot_limit| new icons which are not in |icon_cache|;
183 void DeferredTabRestoreServiceChanged(); 180 // 2) deleting old icons which are not in |icon_cache|.
181 // Returns the number of new icon files created.
182 static int UpdateIconFiles(const base::FilePath& icon_dir,
183 const ShellLinkItemList& page_list,
184 size_t slot_limit,
185 URLIconCache* icon_cache);
184 186
185 // Deletes icon files of |category| in |icon_dir| which are not in the cache 187 // Updates the application JumpList, which consists of 1) create new icon
186 // anymore. 188 // files; 2) delete obsolete icon files; 3) notify the OS.
187 void DeleteIconFiles(const base::FilePath& icon_dir, 189 // Note that any timeout error along the way results in the old JumpList being
188 JumpListCategory category); 190 // left as-is, while any non-timeout error results in the old JumpList being
189 191 // left as-is, but without icon files.
190 // Creates at most |max_items| icon files of |category| in |icon_dir| for the 192 static void RunUpdateJumpList(
191 // asynchrounously loaded icons stored in |item_list|.
192 // Returns the number of new icon files created.
193 int CreateIconFiles(const base::FilePath& icon_dir,
194 const ShellLinkItemList& item_list,
195 size_t max_items,
196 JumpListCategory category);
197
198 // Updates icon files in |icon_dir|, which includes deleting old icons and
199 // creating at most |slot_limit| new icons for |page_list|.
200 // Returns the number of new icon files created.
201 int UpdateIconFiles(const base::FilePath& icon_dir,
202 const ShellLinkItemList& page_list,
203 size_t slot_limit,
204 JumpListCategory category);
205
206 // Updates the jumplist, once all the data has been fetched. This method calls
207 // UpdateJumpList() to do most of the work.
208 void RunUpdateJumpList(
209 IncognitoModePrefs::Availability incognito_availability,
210 const base::string16& app_id, 193 const base::string16& app_id,
211 const base::FilePath& profile_dir, 194 const base::FilePath& profile_dir,
212 base::RefCountedData<JumpListData>* ref_counted_data); 195 const ShellLinkItemList& most_visited_pages,
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);
213 201
214 // Updates the application JumpList, which consists of 1) delete old icon 202 // Callback for RunUpdateJumpList that notifies when it finishes running.
215 // files; 2) create new icon files; 3) notify the OS. This method is called 203 // Updates certain JumpList member variables and/or triggers a new JumpList
216 // from RunUpdateJumpList(). 204 // update based on |update_results|.
217 // Note that any timeout error along the way results in the old jumplist being 205 void OnRunUpdateCompletion(std::unique_ptr<UpdateResults> update_results);
218 // left as-is, while any non-timeout error results in the old jumplist being 206
219 // left as-is, but without icon files. 207 // Cancels a pending JumpList update.
220 bool UpdateJumpList(const base::string16& app_id, 208 void CancelPendingUpdate();
221 const base::FilePath& profile_dir, 209
222 const ShellLinkItemList& most_visited_pages, 210 // Terminates the JumpList, which includes cancelling any pending updates and
223 const ShellLinkItemList& recently_closed_pages, 211 // stopping observing the Profile and its services. This must be called before
224 bool most_visited_pages_have_updates, 212 // the |profile_| is destroyed.
225 bool recently_closed_pages_have_updates, 213 void Terminate();
226 IncognitoModePrefs::Availability incognito_availability);
227 214
228 // Tracks FaviconService tasks. 215 // Tracks FaviconService tasks.
229 base::CancelableTaskTracker cancelable_task_tracker_; 216 base::CancelableTaskTracker cancelable_task_tracker_;
230 217
231 // The Profile object is used to listen for events. 218 // The Profile object is used to listen for events.
232 Profile* profile_; 219 Profile* profile_;
233 220
234 // Lives on the UI thread. 221 // Manages the registration of pref change observers.
235 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; 222 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
236 223
237 // App id to associate with the jump list. 224 // App id to associate with the JumpList.
238 base::string16 app_id_; 225 base::string16 app_id_;
239 226
240 // Timer for requesting delayed updates of the "Most Visited" category of 227 // Timer for requesting delayed JumpList updates.
241 // jumplist. 228 base::OneShotTimer timer_;
242 base::OneShotTimer timer_most_visited_;
243 229
244 // Timer for requesting delayed updates of the "Recently Closed" category of 230 // A list of URLs we need to retrieve their favicons,
245 // jumplist. 231 std::list<UrlAndLinkItem> icon_urls_;
246 base::OneShotTimer timer_recently_closed_; 232
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;
247 263
248 // Number of updates to skip to alleviate the machine when a previous update 264 // Number of updates to skip to alleviate the machine when a previous update
249 // was too slow. Updates will be resumed when this reaches 0 again. 265 // was too slow. Updates will be resumed when this reaches 0 again.
250 int updates_to_skip_ = 0; 266 int updates_to_skip_ = 0;
251 267
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
264 // Id of last favicon task. It's used to cancel current task if a new one 268 // Id of last favicon task. It's used to cancel current task if a new one
265 // comes in before it finishes. 269 // comes in before it finishes.
266 base::CancelableTaskTracker::TaskId task_id_; 270 base::CancelableTaskTracker::TaskId task_id_ =
271 base::CancelableTaskTracker::kBadTaskId;
267 272
268 // A task runner running tasks to update the JumpList. 273 // A task runner running tasks to update the JumpList.
269 scoped_refptr<base::SingleThreadTaskRunner> update_jumplist_task_runner_; 274 scoped_refptr<base::SingleThreadTaskRunner> update_jumplist_task_runner_;
270 275
271 // A task runner running tasks to delete JumpListIcons directory and 276 // A task runner running tasks to delete the JumpListIcons and
272 // JumpListIconsOld directory. 277 // JumpListIconsOld folders.
273 scoped_refptr<base::SequencedTaskRunner> delete_jumplisticons_task_runner_; 278 scoped_refptr<base::SequencedTaskRunner> delete_jumplisticons_task_runner_;
274 279
275 SEQUENCE_CHECKER(sequence_checker_); 280 SEQUENCE_CHECKER(sequence_checker_);
276 281
277 // For callbacks may be run after destruction. 282 // For callbacks may run after destruction.
278 base::WeakPtrFactory<JumpList> weak_ptr_factory_; 283 base::WeakPtrFactory<JumpList> weak_ptr_factory_;
279 284
280 DISALLOW_COPY_AND_ASSIGN(JumpList); 285 DISALLOW_COPY_AND_ASSIGN(JumpList);
281 }; 286 };
282 287
283 #endif // CHROME_BROWSER_WIN_JUMPLIST_H_ 288 #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