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

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

Powered by Google App Engine
This is Rietveld 408576698