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

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: Address comments 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 | « no previous file | chrome/browser/win/jumplist.cc » ('j') | chrome/browser/win/jumplist.cc » ('J')
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"
grt (UTC plus 2) 2017/06/12 20:43:38 this is still needed for the scoped_refptr members
chengx 2017/06/12 22:05:30 Done. Added back.
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/refcounted_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" 31 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/notification_observer.h" 32 #include "content/public/browser/notification_observer.h"
35 #include "content/public/browser/notification_registrar.h" 33 #include "content/public/browser/notification_registrar.h"
36 34
37 namespace base { 35 namespace base {
(...skipping 15 matching lines...) Expand all
53 // * Retrieving "Most Visited" pages from HistoryService; 51 // * Retrieving "Most Visited" pages from HistoryService;
54 // * Retrieving strings from the application resource; 52 // * Retrieving strings from the application resource;
55 // * Adding COM objects to JumpList, etc. 53 // * Adding COM objects to JumpList, etc.
56 // 54 //
57 // This class observes the tabs and policies of the given Profile and updates 55 // This class observes the tabs and policies of the given Profile and updates
58 // the JumpList whenever a change is detected. 56 // the JumpList whenever a change is detected.
59 // 57 //
60 // Updating a JumpList requires some file operations and it is not good to 58 // 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 59 // update it in a UI thread. To solve this problem, this class posts to a
62 // runnable method when it actually updates a JumpList. 60 // 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, 61 class JumpList : public sessions::TabRestoreServiceObserver,
67 public history::TopSitesObserver, 62 public history::TopSitesObserver,
68 public RefcountedKeyedService { 63 public RefcountedKeyedService {
69 public: 64 public:
70 struct JumpListData {
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: 65 // RefcountedKeyedService:
122 void ShutdownOnUIThread() override; 66 void ShutdownOnUIThread() override;
123 67
124 // Returns true if the custom JumpList is enabled. 68 // Returns true if the custom JumpList is enabled.
125 static bool Enabled(); 69 static bool Enabled();
126 70
127 private: 71 private:
128 friend JumpListFactory; 72 friend JumpListFactory;
129 explicit JumpList(Profile* profile); // Use JumpListFactory instead 73 explicit JumpList(Profile* profile); // Use JumpListFactory instead
130 ~JumpList() override; 74 ~JumpList() override;
131 75
132 enum class JumpListCategory { kMostVisited, kRecentlyClosed }; 76 using URLIconCache = base::flat_map<std::string, base::FilePath>;
133 77
134 // Adds a new ShellLinkItem for |tab| to |data| provided that doing so will 78 // Holds results of the RunUpdateJumpList run.
135 // not exceed |max_items|. 79 struct UpdateResults {
136 bool AddTab(const sessions::TabRestoreService::Tab& tab, 80 UpdateResults();
137 size_t max_items, 81 ~UpdateResults();
138 JumpListData* data);
139 82
140 // Adds a new ShellLinkItem for each tab in |window| to |data| provided that 83 // Icon file paths of the most visited links, indexed by tab url.
141 // doing so will not exceed |max_items|. 84 // Holding a copy of most_visited_icons_ initially, it's updated by the
85 // JumpList update run. If the update run succeeds, it overwrites
86 // most_visited_icons_.
87 URLIconCache most_visited_icons_in_update_;
88
89 // icon file paths of the recently closed links, indexed by tab url.
90 // Holding a copy of recently_closed_icons_ initially, it's updated by the
91 // JumpList update run. If the update run succeeds, it overwrites
92 // recently_closed_icons_.
93 URLIconCache recently_closed_icons_in_update;
94
95 // A flag indicating if a JumpList update run is successful.
96 bool update_success_ = false;
97
98 // A flag indicating if there is a timeout in notifying the JumpList update
99 // to shell. Note that this variable is independent of update_success_.
100 bool update_timeout_ = false;
101 };
102
103 // Adds a new ShellLinkItem for |tab| to the JumpList data provided that doing
104 // so will not exceed |max_items|.
105 bool AddTab(const sessions::TabRestoreService::Tab& tab, size_t max_items);
106
107 // Adds a new ShellLinkItem for each tab in |window| to the JumpList data
108 // provided that doing so will not exceed |max_items|.
142 void AddWindow(const sessions::TabRestoreService::Window& window, 109 void AddWindow(const sessions::TabRestoreService::Window& window,
143 size_t max_items, 110 size_t max_items);
144 JumpListData* data);
145 111
146 // Starts loading a favicon for each URL in |icon_urls_|. 112 // Starts loading a favicon for each URL in |icon_urls_|.
147 // This function sends a query to HistoryService. 113 // This function sends a query to HistoryService.
148 // When finishing loading all favicons, this function posts a task that 114 // When finishing loading all favicons, this function posts a task that
149 // decompresses collected favicons and updates a JumpList. 115 // decompresses collected favicons and updates a JumpList.
150 void StartLoadingFavicon(); 116 void StartLoadingFavicon();
151 117
152 // A callback function for HistoryService that notify when a requested favicon 118 // Callback for HistoryService that notifies when a requested favicon is
153 // is available. 119 // available. To avoid file operations, this function just attaches the given
154 // To avoid file operations, this function just attaches the given data to 120 // data to a ShellLinkItem object.
155 // a ShellLinkItem object.
156 void OnFaviconDataAvailable( 121 void OnFaviconDataAvailable(
157 const favicon_base::FaviconImageResult& image_result); 122 const favicon_base::FaviconImageResult& image_result);
158 123
159 // Callback for TopSites that notifies when the "Most Visited" list is 124 // Callback for TopSites that notifies when the "Most Visited" list is
160 // available. This function updates the ShellLinkItemList objects and 125 // available. This function updates the ShellLinkItemList objects and
161 // begins the process of fetching favicons for the URLs. 126 // begins the process of fetching favicons for the URLs.
162 void OnMostVisitedURLsAvailable( 127 void OnMostVisitedURLsAvailable(
163 const history::MostVisitedURLList& data); 128 const history::MostVisitedURLList& data);
164 129
165 // Callback for changes to the incognito mode availability pref. 130 // Callback for changes to the incognito mode availability pref.
166 void OnIncognitoAvailabilityChanged(); 131 void OnIncognitoAvailabilityChanged();
167 132
133 // sessions::TabRestoreServiceObserver:
134 void TabRestoreServiceChanged(sessions::TabRestoreService* service) override;
135 void TabRestoreServiceDestroyed(
136 sessions::TabRestoreService* service) override;
137
138 // history::TopSitesObserver:
139 void TopSitesLoaded(history::TopSites* top_sites) override;
140 void TopSitesChanged(history::TopSites* top_sites,
141 ChangeReason change_reason) override;
142
143 // Initialize the one-shot timer to update the JumpList in a while. If there
144 // is already a request queued then cancel it and post the new request. This
145 // ensures that JumpList update won't happen until there has been a brief
146 // quiet period, thus avoiding update storms.
147 void InitializeTimerForUpdate();
148
149 // Called on a timer after requests storms have subsided. Calls APIs
150 // ProcessTopSitesNotification and ProcessTabRestoreNotification on
151 // demand to do the actual work.
152 void OnDelayTimer();
153
154 // Processes notifications from TopSites service.
155 void ProcessTopSitesNotification();
156
157 // Processes notifications from TabRestore service.
158 void ProcessTabRestoreServiceNotification();
159
168 // Posts tasks to update the JumpList and delete any obsolete JumpList related 160 // Posts tasks to update the JumpList and delete any obsolete JumpList related
169 // folders. 161 // folders.
170 void PostRunUpdate(); 162 void PostRunUpdate();
171 163
172 // history::TopSitesObserver implementation. 164 // Deletes icon files in |icon_dir| which are not in |icon_cache| anymore.
173 void TopSitesLoaded(history::TopSites* top_sites) override; 165 static void DeleteIconFiles(const base::FilePath& icon_dir,
174 void TopSitesChanged(history::TopSites* top_sites, 166 URLIconCache* icon_cache);
175 ChangeReason change_reason) override;
176 167
177 // Called on a timer to update the most visited URLs after requests storms 168 // In |icon_dir|, creates at most |max_items| icon files which are not in
178 // have subsided. 169 // |icon_cache| for the asynchrounously loaded icons stored in |item_list|.
179 void DeferredTopSitesChanged(); 170 // |icon_cache| is also updated for newly created icons.
171 // Returns the number of new icon files created.
172 static int CreateIconFiles(const base::FilePath& icon_dir,
173 const ShellLinkItemList& item_list,
174 size_t max_items,
175 URLIconCache* icon_cache);
180 176
181 // Called on a timer to update the "Recently Closed" category of JumpList 177 // Updates icon files for |page_list| in |icon_dir|, which consists of
182 // after requests storms have subsided. 178 // 1) creating at most |slot_limit| new icons which are not in |icon_cache|;
183 void DeferredTabRestoreServiceChanged(); 179 // 2) deleting old icons which are not in |icon_cache|.
180 // Returns the number of new icon files created.
181 static int UpdateIconFiles(const base::FilePath& icon_dir,
182 const ShellLinkItemList& page_list,
183 size_t slot_limit,
184 URLIconCache* icon_cache);
184 185
185 // Deletes icon files of |category| in |icon_dir| which are not in the cache 186 // Updates the application JumpList, which consists of 1) create new icon
186 // anymore. 187 // files; 2) delete obsolete icon files; 3) notify the OS.
187 void DeleteIconFiles(const base::FilePath& icon_dir, 188 // Note that any timeout error along the way results in the old JumpList being
188 JumpListCategory category); 189 // left as-is, while any non-timeout error results in the old JumpList being
189 190 // left as-is, but without icon files.
190 // Creates at most |max_items| icon files of |category| in |icon_dir| for the 191 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, 192 const base::string16& app_id,
211 const base::FilePath& profile_dir, 193 const base::FilePath& profile_dir,
212 base::RefCountedData<JumpListData>* ref_counted_data); 194 const ShellLinkItemList& most_visited_pages,
195 const ShellLinkItemList& recently_closed_pages,
196 bool most_visited_pages_have_updates,
197 bool recently_closed_pages_have_updates,
198 IncognitoModePrefs::Availability incognito_availability,
199 UpdateResults* update_results);
213 200
214 // Updates the application JumpList, which consists of 1) delete old icon 201 // Callback for RunUpdateJumpList that notifies when it finishes running.
215 // files; 2) create new icon files; 3) notify the OS. This method is called 202 // Updates certain JumpList member variables and/or triggers a new JumpList
216 // from RunUpdateJumpList(). 203 // update based on |update_results|.
217 // Note that any timeout error along the way results in the old jumplist being 204 void OnRunUpdateCompletion(std::unique_ptr<UpdateResults> update_results);
218 // left as-is, while any non-timeout error results in the old jumplist being 205
219 // left as-is, but without icon files. 206 // Cancel a pending JumpList update.
220 bool UpdateJumpList(const base::string16& app_id, 207 void CancelPendingUpdate();
221 const base::FilePath& profile_dir, 208
222 const ShellLinkItemList& most_visited_pages, 209 // Terminate the JumpList: cancel any pending updates and stop observing
223 const ShellLinkItemList& recently_closed_pages, 210 // the Profile and its services. This must be called before the |profile_|
224 bool most_visited_pages_have_updates, 211 // is destroyed.
225 bool recently_closed_pages_have_updates, 212 void Terminate();
226 IncognitoModePrefs::Availability incognito_availability);
227 213
228 // Tracks FaviconService tasks. 214 // Tracks FaviconService tasks.
229 base::CancelableTaskTracker cancelable_task_tracker_; 215 base::CancelableTaskTracker cancelable_task_tracker_;
230 216
231 // The Profile object is used to listen for events. 217 // The Profile object is used to listen for events.
232 Profile* profile_; 218 Profile* profile_;
233 219
234 // Lives on the UI thread. 220 // Manages the registration of pref change observers.
235 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_; 221 std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
236 222
237 // App id to associate with the jump list. 223 // App id to associate with the JumpList.
238 base::string16 app_id_; 224 base::string16 app_id_;
239 225
240 // Timer for requesting delayed updates of the "Most Visited" category of 226 // Timer for requesting delayed JumpList updates.
241 // jumplist. 227 base::OneShotTimer timer_;
242 base::OneShotTimer timer_most_visited_;
243 228
244 // Timer for requesting delayed updates of the "Recently Closed" category of 229 // A list of URLs we need to retrieve their favicons,
245 // jumplist. 230 using UrlAndLinkItem = std::pair<std::string, scoped_refptr<ShellLinkItem>>;
246 base::OneShotTimer timer_recently_closed_; 231 std::list<UrlAndLinkItem> icon_urls_;
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 boolean flag indicating if there's a JumpList update task already posted
258 // or currently running.
259 bool update_in_progress_ = false;
260
261 // A boolean 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 JumpListIcons directory and
272 // JumpListIconsOld directory. 277 // JumpListIconsOld directory.
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 | « no previous file | chrome/browser/win/jumplist.cc » ('j') | chrome/browser/win/jumplist.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698