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

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

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/win/jumplist.h ('k') | chrome/browser/win/jumplist_factory.h » ('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 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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 #include "chrome/browser/win/jumplist.h" 5 #include "chrome/browser/win/jumplist.h"
6 6
7 #include "base/base_paths.h" 7 #include "base/base_paths.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 20 matching lines...) Expand all
31 #include "chrome/browser/shell_integration_win.h" 31 #include "chrome/browser/shell_integration_win.h"
32 #include "chrome/browser/win/jumplist_file_util.h" 32 #include "chrome/browser/win/jumplist_file_util.h"
33 #include "chrome/browser/win/jumplist_update_util.h" 33 #include "chrome/browser/win/jumplist_update_util.h"
34 #include "chrome/common/chrome_constants.h" 34 #include "chrome/common/chrome_constants.h"
35 #include "chrome/common/chrome_icon_resources_win.h" 35 #include "chrome/common/chrome_icon_resources_win.h"
36 #include "chrome/common/chrome_switches.h" 36 #include "chrome/common/chrome_switches.h"
37 #include "chrome/common/pref_names.h" 37 #include "chrome/common/pref_names.h"
38 #include "chrome/grit/generated_resources.h" 38 #include "chrome/grit/generated_resources.h"
39 #include "chrome/install_static/install_util.h" 39 #include "chrome/install_static/install_util.h"
40 #include "components/favicon/core/favicon_service.h" 40 #include "components/favicon/core/favicon_service.h"
41 #include "components/history/core/browser/history_service.h"
41 #include "components/history/core/browser/top_sites.h" 42 #include "components/history/core/browser/top_sites.h"
42 #include "components/prefs/pref_change_registrar.h" 43 #include "components/prefs/pref_change_registrar.h"
43 #include "components/sessions/core/session_types.h" 44 #include "components/sessions/core/session_types.h"
44 #include "components/strings/grit/components_strings.h" 45 #include "components/strings/grit/components_strings.h"
45 #include "ui/base/l10n/l10n_util.h" 46 #include "ui/base/l10n/l10n_util.h"
46 #include "ui/gfx/codec/png_codec.h" 47 #include "ui/gfx/codec/png_codec.h"
47 #include "ui/gfx/favicon_size.h" 48 #include "ui/gfx/favicon_size.h"
48 #include "ui/gfx/icon_util.h" 49 #include "ui/gfx/icon_util.h"
49 #include "ui/gfx/image/image.h" 50 #include "ui/gfx/image/image.h"
50 #include "ui/gfx/image/image_family.h" 51 #include "ui/gfx/image/image_family.h"
51 #include "ui/gfx/image/image_skia.h" 52 #include "ui/gfx/image/image_skia.h"
52 #include "ui/gfx/image/image_skia_rep.h" 53 #include "ui/gfx/image/image_skia_rep.h"
53 #include "url/gurl.h" 54 #include "url/gurl.h"
54 55
55 using content::BrowserThread;
56 using JumpListData = JumpList::JumpListData;
57
58 namespace { 56 namespace {
59 57
60 // The default maximum number of items to display in JumpList is 10. 58 // The default maximum number of items to display in JumpList is 10.
61 // https://msdn.microsoft.com/library/windows/desktop/dd378398.aspx 59 // https://msdn.microsoft.com/library/windows/desktop/dd378398.aspx
62 // The "Most visited" and "Recently closed" category titles always take 2 slots. 60 // The "Most visited" and "Recently closed" category titles always take 2 slots.
63 // For the remaining 8 slots, we allocate 5 slots to "most-visited" items and 3 61 // For the remaining 8 slots, we allocate 5 slots to "most-visited" items and 3
64 // slots to "recently-closed" items, respectively. 62 // slots to "recently-closed" items, respectively.
65 constexpr size_t kMostVisitedItems = 5; 63 constexpr size_t kMostVisitedItems = 5;
66 constexpr size_t kRecentlyClosedItems = 3; 64 constexpr size_t kRecentlyClosedItems = 3;
67 65
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 base::FilePath GenerateJumplistIconDirName( 187 base::FilePath GenerateJumplistIconDirName(
190 const base::FilePath& profile_dir, 188 const base::FilePath& profile_dir,
191 const base::FilePath::StringPieceType& suffix) { 189 const base::FilePath::StringPieceType& suffix) {
192 base::FilePath::StringType dir_name(chrome::kJumpListIconDirname); 190 base::FilePath::StringType dir_name(chrome::kJumpListIconDirname);
193 suffix.AppendToString(&dir_name); 191 suffix.AppendToString(&dir_name);
194 return profile_dir.Append(dir_name); 192 return profile_dir.Append(dir_name);
195 } 193 }
196 194
197 } // namespace 195 } // namespace
198 196
199 JumpList::JumpListData::JumpListData() {} 197 JumpList::UpdateResults::UpdateResults() {}
200 198
201 JumpList::JumpListData::~JumpListData() {} 199 JumpList::UpdateResults::~UpdateResults() {}
202 200
203 JumpList::JumpList(Profile* profile) 201 JumpList::JumpList(Profile* profile)
204 : RefcountedKeyedService(content::BrowserThread::GetTaskRunnerForThread( 202 : profile_(profile),
205 content::BrowserThread::UI)),
206 profile_(profile),
207 jumplist_data_(new base::RefCountedData<JumpListData>),
208 task_id_(base::CancelableTaskTracker::kBadTaskId),
209 update_jumplist_task_runner_(base::CreateCOMSTATaskRunnerWithTraits( 203 update_jumplist_task_runner_(base::CreateCOMSTATaskRunnerWithTraits(
210 {base::MayBlock(), base::TaskPriority::USER_VISIBLE, 204 {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
211 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})), 205 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})),
212 delete_jumplisticons_task_runner_( 206 delete_jumplisticons_task_runner_(
213 base::CreateSequencedTaskRunnerWithTraits( 207 base::CreateSequencedTaskRunnerWithTraits(
214 {base::MayBlock(), base::TaskPriority::BACKGROUND, 208 {base::MayBlock(), base::TaskPriority::BACKGROUND,
215 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})), 209 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})),
216 weak_ptr_factory_(this) { 210 weak_ptr_factory_(this) {
217 DCHECK(Enabled()); 211 DCHECK(Enabled());
218 // To update JumpList when a tab is added or removed, we add this object to 212 // To update JumpList when a tab is added or removed, we add this object to
219 // the observer list of the TabRestoreService class. 213 // the observer list of the TabRestoreService class.
220 // When we add this object to the observer list, we save the pointer to this 214 // When we add this object to the observer list, we save the pointer to this
221 // TabRestoreService object. This pointer is used when we remove this object 215 // TabRestoreService object. This pointer is used when we remove this object
222 // from the observer list. 216 // from the observer list.
223 sessions::TabRestoreService* tab_restore_service = 217 sessions::TabRestoreService* tab_restore_service =
224 TabRestoreServiceFactory::GetForProfile(profile_); 218 TabRestoreServiceFactory::GetForProfile(profile_);
225 if (!tab_restore_service) 219 if (!tab_restore_service)
226 return; 220 return;
227 221
228 app_id_ = 222 app_id_ =
229 shell_integration::win::GetChromiumModelIdForProfile(profile_->GetPath()); 223 shell_integration::win::GetChromiumModelIdForProfile(profile_->GetPath());
230 224
225 // Register as TopSitesObserver so that we can update ourselves when the
226 // TopSites changes. TopSites updates itself after a delay. This is especially
227 // noticable when your profile is empty.
231 scoped_refptr<history::TopSites> top_sites = 228 scoped_refptr<history::TopSites> top_sites =
232 TopSitesFactory::GetForProfile(profile_); 229 TopSitesFactory::GetForProfile(profile_);
233 if (top_sites) { 230 if (top_sites)
234 // Register as TopSitesObserver so that we can update ourselves when the
235 // TopSites changes. TopSites updates itself after a delay. This is
236 // especially noticable when your profile is empty.
237 top_sites->AddObserver(this); 231 top_sites->AddObserver(this);
238 } 232
233 // Register as TabRestoreServiceObserver so that we can update ourselves when
234 // recently closed tabs have changes.
239 tab_restore_service->AddObserver(this); 235 tab_restore_service->AddObserver(this);
236
237 // kIncognitoModeAvailability is monitored for changes on Incognito mode.
240 pref_change_registrar_.reset(new PrefChangeRegistrar); 238 pref_change_registrar_.reset(new PrefChangeRegistrar);
241 pref_change_registrar_->Init(profile_->GetPrefs()); 239 pref_change_registrar_->Init(profile_->GetPrefs());
240 // base::Unretained is safe since |this| is guaranteed to outlive
241 // pref_change_registrar_.
242 pref_change_registrar_->Add( 242 pref_change_registrar_->Add(
243 prefs::kIncognitoModeAvailability, 243 prefs::kIncognitoModeAvailability,
244 base::Bind(&JumpList::OnIncognitoAvailabilityChanged, this)); 244 base::Bind(&JumpList::OnIncognitoAvailabilityChanged,
245 base::Unretained(this)));
245 } 246 }
246 247
247 JumpList::~JumpList() { 248 JumpList::~JumpList() {
248 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 249 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
249 Terminate(); 250 Terminate();
250 } 251 }
251 252
252 // static 253 // static
253 bool JumpList::Enabled() { 254 bool JumpList::Enabled() {
254 return JumpListUpdater::IsEnabled(); 255 return JumpListUpdater::IsEnabled();
255 } 256 }
256 257
257 void JumpList::CancelPendingUpdate() { 258 void JumpList::CancelPendingUpdate() {
258 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 259 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
260
261 // Cancel a pending most-visited URL fetch by invalidating the weak pointer.
262 weak_ptr_factory_.InvalidateWeakPtrs();
263
264 // Cancel a pending favicon loading by invalidating its task id.
259 if (task_id_ != base::CancelableTaskTracker::kBadTaskId) { 265 if (task_id_ != base::CancelableTaskTracker::kBadTaskId) {
260 cancelable_task_tracker_.TryCancel(task_id_); 266 cancelable_task_tracker_.TryCancel(task_id_);
261 task_id_ = base::CancelableTaskTracker::kBadTaskId; 267 task_id_ = base::CancelableTaskTracker::kBadTaskId;
262 } 268 }
263 } 269 }
264 270
265 void JumpList::Terminate() { 271 void JumpList::Terminate() {
266 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 272 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
267 timer_most_visited_.Stop(); 273 timer_.Stop();
268 timer_recently_closed_.Stop();
269 CancelPendingUpdate(); 274 CancelPendingUpdate();
275 update_in_progress_ = false;
270 if (profile_) { 276 if (profile_) {
271 sessions::TabRestoreService* tab_restore_service = 277 sessions::TabRestoreService* tab_restore_service =
272 TabRestoreServiceFactory::GetForProfile(profile_); 278 TabRestoreServiceFactory::GetForProfile(profile_);
273 if (tab_restore_service) 279 if (tab_restore_service)
274 tab_restore_service->RemoveObserver(this); 280 tab_restore_service->RemoveObserver(this);
275 scoped_refptr<history::TopSites> top_sites = 281 scoped_refptr<history::TopSites> top_sites =
276 TopSitesFactory::GetForProfile(profile_); 282 TopSitesFactory::GetForProfile(profile_);
277 if (top_sites) 283 if (top_sites)
278 top_sites->RemoveObserver(this); 284 top_sites->RemoveObserver(this);
279 pref_change_registrar_.reset(); 285 pref_change_registrar_.reset();
280 } 286 }
281 profile_ = NULL; 287 profile_ = nullptr;
282 } 288 }
283 289
284 void JumpList::ShutdownOnUIThread() { 290 void JumpList::Shutdown() {
285 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 291 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
286 Terminate(); 292 Terminate();
287 } 293 }
288 294
289 void JumpList::OnMostVisitedURLsAvailable( 295 void JumpList::OnMostVisitedURLsAvailable(
290 const history::MostVisitedURLList& urls) { 296 const history::MostVisitedURLList& urls) {
291 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 297 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
292 298
293 { 299 top_sites_has_pending_notification_ = false;
294 JumpListData* data = &jumplist_data_->data;
295 base::AutoLock auto_lock(data->list_lock_);
296 300
297 // There is no need to update the JumpList if the top most visited sites in 301 // There is no need to update the JumpList if the top most visited sites in
298 // display have not changed. 302 // display have not changed.
299 if (MostVisitedItemsUnchanged(data->most_visited_pages_, urls, 303 if (MostVisitedItemsUnchanged(most_visited_pages_, urls, kMostVisitedItems))
300 kMostVisitedItems)) { 304 return;
301 return;
302 }
303 305
304 data->most_visited_pages_.clear(); 306 most_visited_pages_.clear();
305 307
306 for (size_t i = 0; i < urls.size() && i < kMostVisitedItems; i++) { 308 const size_t num_items = std::min(urls.size(), kMostVisitedItems);
307 const history::MostVisitedURL& url = urls[i]; 309 for (size_t i = 0; i < num_items; ++i) {
308 scoped_refptr<ShellLinkItem> link = CreateShellLink(); 310 const history::MostVisitedURL& url = urls[i];
309 std::string url_string = url.url.spec(); 311 scoped_refptr<ShellLinkItem> link = CreateShellLink();
310 base::string16 url_string_wide = base::UTF8ToUTF16(url_string); 312 std::string url_string = url.url.spec();
311 link->GetCommandLine()->AppendArgNative(url_string_wide); 313 base::string16 url_string_wide = base::UTF8ToUTF16(url_string);
312 link->GetCommandLine()->AppendSwitchASCII( 314 link->GetCommandLine()->AppendArgNative(url_string_wide);
313 switches::kWinJumplistAction, jumplist::kMostVisitedCategory); 315 link->GetCommandLine()->AppendSwitchASCII(switches::kWinJumplistAction,
314 link->set_title(!url.title.empty() ? url.title : url_string_wide); 316 jumplist::kMostVisitedCategory);
315 link->set_url(url_string); 317 link->set_title(!url.title.empty() ? url.title : url_string_wide);
316 data->most_visited_pages_.push_back(link); 318 link->set_url(url_string);
317 data->icon_urls_.push_back(std::make_pair(url_string, link)); 319 most_visited_pages_.push_back(link);
318 } 320 icon_urls_.emplace_back(std::move(url_string), std::move(link));
319 data->most_visited_pages_have_updates_ = true;
320 } 321 }
321 322
323 most_visited_should_update_ = true;
324
322 // Send a query that retrieves the first favicon. 325 // Send a query that retrieves the first favicon.
323 StartLoadingFavicon(); 326 StartLoadingFavicon();
324 } 327 }
325 328
326 void JumpList::TabRestoreServiceChanged(sessions::TabRestoreService* service) { 329 void JumpList::TabRestoreServiceChanged(sessions::TabRestoreService* service) {
327 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 330 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
328 331
329 // if we have a pending favicon request, cancel it here (it is out of date). 332 tab_restore_has_pending_notification_ = true;
333
334 // Postpone handling this notification until a pending update completes.
335 if (update_in_progress_)
336 return;
337
338 // if we have a pending favicon request, cancel it here as it's out of date.
330 CancelPendingUpdate(); 339 CancelPendingUpdate();
331 340
332 // Initialize the one-shot timer to update the the "Recently Closed" category 341 // Initialize the one-shot timer to update the JumpList in a while.
333 // in a while. If there is already a request queued then cancel it and post 342 InitializeTimerForUpdate();
334 // the new request. This ensures that JumpList update of the "Recently Closed"
335 // category won't happen until there has been a brief quiet period, thus
336 // avoiding update storms.
337 if (timer_recently_closed_.IsRunning()) {
338 timer_recently_closed_.Reset();
339 } else {
340 timer_recently_closed_.Start(
341 FROM_HERE, kDelayForJumplistUpdate,
342 base::Bind(&JumpList::DeferredTabRestoreServiceChanged,
343 base::Unretained(this)));
344 }
345 } 343 }
346 344
347 void JumpList::TabRestoreServiceDestroyed( 345 void JumpList::TabRestoreServiceDestroyed(
348 sessions::TabRestoreService* service) {} 346 sessions::TabRestoreService* service) {}
349 347
350 bool JumpList::AddTab(const sessions::TabRestoreService::Tab& tab, 348 bool JumpList::AddTab(const sessions::TabRestoreService::Tab& tab,
351 size_t max_items, 349 size_t max_items) {
352 JumpListData* data) {
353 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 350 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
354 data->list_lock_.AssertAcquired();
355 351
356 // This code adds the URL and the title strings of the given tab to |data|. 352 // This code adds the URL and the title strings of the given tab to the
357 if (data->recently_closed_pages_.size() >= max_items) 353 // JumpList variables.
354 if (recently_closed_pages_.size() >= max_items)
358 return false; 355 return false;
359 356
360 scoped_refptr<ShellLinkItem> link = CreateShellLink(); 357 scoped_refptr<ShellLinkItem> link = CreateShellLink();
361 const sessions::SerializedNavigationEntry& current_navigation = 358 const sessions::SerializedNavigationEntry& current_navigation =
362 tab.navigations.at(tab.current_navigation_index); 359 tab.navigations.at(tab.current_navigation_index);
363 std::string url = current_navigation.virtual_url().spec(); 360 std::string url = current_navigation.virtual_url().spec();
364 link->GetCommandLine()->AppendArgNative(base::UTF8ToUTF16(url)); 361 link->GetCommandLine()->AppendArgNative(base::UTF8ToUTF16(url));
365 link->GetCommandLine()->AppendSwitchASCII(switches::kWinJumplistAction, 362 link->GetCommandLine()->AppendSwitchASCII(switches::kWinJumplistAction,
366 jumplist::kRecentlyClosedCategory); 363 jumplist::kRecentlyClosedCategory);
367 link->set_title(current_navigation.title()); 364 link->set_title(current_navigation.title());
368 link->set_url(url); 365 link->set_url(url);
369 data->recently_closed_pages_.push_back(link); 366 recently_closed_pages_.push_back(link);
370 data->icon_urls_.push_back(std::make_pair(std::move(url), std::move(link))); 367 icon_urls_.emplace_back(std::move(url), std::move(link));
371 368
372 return true; 369 return true;
373 } 370 }
374 371
375 void JumpList::AddWindow(const sessions::TabRestoreService::Window& window, 372 void JumpList::AddWindow(const sessions::TabRestoreService::Window& window,
376 size_t max_items, 373 size_t max_items) {
377 JumpListData* data) {
378 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 374 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
379 data->list_lock_.AssertAcquired();
380
381 // This code enumerates all the tabs in the given window object and add their
382 // URLs and titles to |data|.
383 DCHECK(!window.tabs.empty()); 375 DCHECK(!window.tabs.empty());
384 376
385 for (const auto& tab : window.tabs) { 377 for (const auto& tab : window.tabs) {
386 if (!AddTab(*tab, max_items, data)) 378 if (!AddTab(*tab, max_items))
387 return; 379 return;
388 } 380 }
389 } 381 }
390 382
391 void JumpList::StartLoadingFavicon() { 383 void JumpList::StartLoadingFavicon() {
392 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 384 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
393 385
394 base::ElapsedTimer timer; 386 if (icon_urls_.empty()) {
395
396 GURL url;
397 bool waiting_for_icons = true;
398 {
399 JumpListData* data = &jumplist_data_->data;
400 base::AutoLock auto_lock(data->list_lock_);
401 waiting_for_icons = !data->icon_urls_.empty();
402 if (waiting_for_icons) {
403 // Ask FaviconService if it has a favicon of a URL.
404 // When FaviconService has one, it will call OnFaviconDataAvailable().
405 url = GURL(data->icon_urls_.front().first);
406 }
407 }
408
409 if (!waiting_for_icons) {
410 // No more favicons are needed by the application JumpList. Schedule a 387 // No more favicons are needed by the application JumpList. Schedule a
411 // RunUpdateJumpList call. 388 // RunUpdateJumpList call.
412 PostRunUpdate(); 389 PostRunUpdate();
413 return; 390 return;
414 } 391 }
415 392
393 base::ElapsedTimer timer;
394
395 // Ask FaviconService if it has a favicon of a URL.
396 // When FaviconService has one, it will call OnFaviconDataAvailable().
416 favicon::FaviconService* favicon_service = 397 favicon::FaviconService* favicon_service =
417 FaviconServiceFactory::GetForProfile(profile_, 398 FaviconServiceFactory::GetForProfile(profile_,
418 ServiceAccessType::EXPLICIT_ACCESS); 399 ServiceAccessType::EXPLICIT_ACCESS);
400 // base::Unretained is safe since |this| is guaranteed to outlive
401 // cancelable_task_tracker_.
419 task_id_ = favicon_service->GetFaviconImageForPageURL( 402 task_id_ = favicon_service->GetFaviconImageForPageURL(
420 url, 403 GURL(icon_urls_.front().first),
421 base::Bind(&JumpList::OnFaviconDataAvailable, base::Unretained(this)), 404 base::Bind(&JumpList::OnFaviconDataAvailable, base::Unretained(this)),
422 &cancelable_task_tracker_); 405 &cancelable_task_tracker_);
423 406
424 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/717236 407 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/717236
425 UMA_HISTOGRAM_TIMES("WinJumplist.StartLoadingFaviconDuration", 408 UMA_HISTOGRAM_TIMES("WinJumplist.StartLoadingFaviconDuration",
426 timer.Elapsed()); 409 timer.Elapsed());
427 } 410 }
428 411
429 void JumpList::OnFaviconDataAvailable( 412 void JumpList::OnFaviconDataAvailable(
430 const favicon_base::FaviconImageResult& image_result) { 413 const favicon_base::FaviconImageResult& image_result) {
431 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 414 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
432 415
433 base::ElapsedTimer timer; 416 base::ElapsedTimer timer;
434 417
435 // If there is currently a favicon request in progress, it is now outdated, 418 // If there is currently a favicon request in progress, it is now outdated,
436 // as we have received another, so nullify the handle from the old request. 419 // as we have received another, so nullify the handle from the old request.
437 task_id_ = base::CancelableTaskTracker::kBadTaskId; 420 task_id_ = base::CancelableTaskTracker::kBadTaskId;
438 // Lock the list to set icon data and pop the url. 421
439 { 422 // Attach the received data to the ShellLinkItem object. This data will be
440 JumpListData* data = &jumplist_data_->data; 423 // decoded by the RunUpdateJumpList method.
441 base::AutoLock auto_lock(data->list_lock_); 424 if (!icon_urls_.empty()) {
442 // Attach the received data to the ShellLinkItem object. 425 if (!image_result.image.IsEmpty() && icon_urls_.front().second.get()) {
443 // This data will be decoded by the RunUpdateJumpList
444 // method.
445 if (!image_result.image.IsEmpty() && !data->icon_urls_.empty() &&
446 data->icon_urls_.front().second.get()) {
447 gfx::ImageSkia image_skia = image_result.image.AsImageSkia(); 426 gfx::ImageSkia image_skia = image_result.image.AsImageSkia();
448 image_skia.EnsureRepsForSupportedScales(); 427 image_skia.EnsureRepsForSupportedScales();
449 std::unique_ptr<gfx::ImageSkia> deep_copy(image_skia.DeepCopy()); 428 std::unique_ptr<gfx::ImageSkia> deep_copy(image_skia.DeepCopy());
450 data->icon_urls_.front().second->set_icon_image(*deep_copy); 429 icon_urls_.front().second->set_icon_image(*deep_copy);
451 } 430 }
452 431 icon_urls_.pop_front();
453 if (!data->icon_urls_.empty())
454 data->icon_urls_.pop_front();
455 } 432 }
456 433
457 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/717236 434 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/717236
458 UMA_HISTOGRAM_TIMES("WinJumplist.OnFaviconDataAvailableDuration", 435 UMA_HISTOGRAM_TIMES("WinJumplist.OnFaviconDataAvailableDuration",
459 timer.Elapsed()); 436 timer.Elapsed());
460 437
461 // Check whether we need to load more favicons. 438 // Check whether we need to load more favicons.
462 StartLoadingFavicon(); 439 StartLoadingFavicon();
463 } 440 }
464 441
465 void JumpList::OnIncognitoAvailabilityChanged() { 442 void JumpList::OnIncognitoAvailabilityChanged() {
466 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 443 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
467 444
468 bool waiting_for_icons = true; 445 if (icon_urls_.empty())
469 {
470 JumpListData* data = &jumplist_data_->data;
471 base::AutoLock auto_lock(data->list_lock_);
472 waiting_for_icons = !data->icon_urls_.empty();
473 }
474
475 // Since neither the "Most Visited" category nor the "Recently Closed"
476 // category changes, mark the flags so that icon files for those categories
477 // won't be updated later on.
478 if (!waiting_for_icons)
479 PostRunUpdate(); 446 PostRunUpdate();
480 } 447 }
481 448
482 void JumpList::PostRunUpdate() { 449 void JumpList::PostRunUpdate() {
483 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 450 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
484 451
485 TRACE_EVENT0("browser", "JumpList::PostRunUpdate"); 452 TRACE_EVENT0("browser", "JumpList::PostRunUpdate");
486 if (!profile_) 453
487 return; 454 update_in_progress_ = true;
488 455
489 base::FilePath profile_dir = profile_->GetPath(); 456 base::FilePath profile_dir = profile_->GetPath();
490 457
491 // Check if incognito windows (or normal windows) are disabled by policy. 458 // Check if incognito windows (or normal windows) are disabled by policy.
492 IncognitoModePrefs::Availability incognito_availability = 459 IncognitoModePrefs::Availability incognito_availability =
493 IncognitoModePrefs::GetAvailability(profile_->GetPrefs()); 460 IncognitoModePrefs::GetAvailability(profile_->GetPrefs());
494 461
495 // Post a task to update the JumpList, which consists of 1) delete old icons, 462 // Make local copies of JumpList member variables and use them for an update.
496 // 2) create new icons, 3) notify the OS. 463 ShellLinkItemList local_most_visited_pages = most_visited_pages_;
497 update_jumplist_task_runner_->PostTask( 464 ShellLinkItemList local_recently_closed_pages = recently_closed_pages_;
498 FROM_HERE,
499 base::Bind(&JumpList::RunUpdateJumpList, this, incognito_availability,
500 app_id_, profile_dir, base::RetainedRef(jumplist_data_)));
501 465
502 // Post a task to delete JumpListIcons folder as it's no longer needed. 466 bool most_visited_should_update = most_visited_should_update_;
503 // Now we have JumpListIconsMostVisited folder and JumpListIconsRecentClosed 467 bool recently_closed_should_update = recently_closed_should_update_;
504 // folder instead.
505 base::FilePath icon_dir =
506 GenerateJumplistIconDirName(profile_dir, FILE_PATH_LITERAL(""));
507 468
508 delete_jumplisticons_task_runner_->PostTask( 469 auto update_results = base::MakeUnique<UpdateResults>();
509 FROM_HERE, 470 update_results->most_visited_icons_in_update = most_visited_icons_;
510 base::Bind(&DeleteDirectory, std::move(icon_dir), kFileDeleteLimit)); 471 update_results->recently_closed_icons_in_update = recently_closed_icons_;
511 472
512 // Post a task to delete JumpListIconsOld folder as it's no longer needed. 473 // Parameter evaluation order is unspecified in C++. Ensure the pointer value
513 base::FilePath icon_dir_old = 474 // is obtained before base::Passed() is called.
514 GenerateJumplistIconDirName(profile_dir, FILE_PATH_LITERAL("Old")); 475 auto* update_results_raw = update_results.get();
515 476
516 delete_jumplisticons_task_runner_->PostTask( 477 // Post a task to update the JumpList, which consists of 1) create new icons,
517 FROM_HERE, 478 // 2) delete old icons, 3) notify the OS.
518 base::Bind(&DeleteDirectory, std::move(icon_dir_old), kFileDeleteLimit)); 479 if (!update_jumplist_task_runner_->PostTaskAndReply(
480 FROM_HERE,
481 base::Bind(&JumpList::RunUpdateJumpList, app_id_, profile_dir,
482 local_most_visited_pages, local_recently_closed_pages,
483 most_visited_should_update, recently_closed_should_update,
484 incognito_availability, update_results_raw),
485 base::Bind(&JumpList::OnRunUpdateCompletion,
486 weak_ptr_factory_.GetWeakPtr(),
487 base::Passed(std::move(update_results))))) {
488 OnRunUpdateCompletion(base::MakeUnique<UpdateResults>());
489 }
519 } 490 }
520 491
521 void JumpList::TopSitesLoaded(history::TopSites* top_sites) { 492 void JumpList::TopSitesLoaded(history::TopSites* top_sites) {
522 } 493 }
523 494
524 void JumpList::TopSitesChanged(history::TopSites* top_sites, 495 void JumpList::TopSitesChanged(history::TopSites* top_sites,
525 ChangeReason change_reason) { 496 ChangeReason change_reason) {
526 // If we have a pending favicon request, cancel it here (it is out of date). 497 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
498
499 top_sites_has_pending_notification_ = true;
500
501 // Postpone handling this notification until a pending update completes.
502 if (update_in_progress_)
503 return;
504
505 // If we have a pending favicon request, cancel it here as it's out of date.
527 CancelPendingUpdate(); 506 CancelPendingUpdate();
528 507
529 // Initialize the one-shot timer to update the the "Most visited" category in 508 // Initialize the one-shot timer to update the JumpList in a while.
530 // a while. If there is already a request queued then cancel it and post the 509 InitializeTimerForUpdate();
531 // new request. This ensures that JumpList update of the "Most visited" 510 }
532 // category won't happen until there has been a brief quiet period, thus 511
533 // avoiding update storms. 512 void JumpList::InitializeTimerForUpdate() {
534 if (timer_most_visited_.IsRunning()) { 513 if (timer_.IsRunning()) {
535 timer_most_visited_.Reset(); 514 timer_.Reset();
536 } else { 515 } else {
537 timer_most_visited_.Start( 516 // base::Unretained is safe since |this| is guaranteed to outlive timer_.
538 FROM_HERE, kDelayForJumplistUpdate, 517 timer_.Start(FROM_HERE, kDelayForJumplistUpdate,
539 base::Bind(&JumpList::DeferredTopSitesChanged, base::Unretained(this))); 518 base::Bind(&JumpList::OnDelayTimer, base::Unretained(this)));
540 } 519 }
541 } 520 }
542 521
543 void JumpList::DeferredTopSitesChanged() { 522 void JumpList::OnDelayTimer() {
544 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 523 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
545 524
546 if (updates_to_skip_ > 0) { 525 if (updates_to_skip_ > 0) {
547 --updates_to_skip_; 526 --updates_to_skip_;
548 return; 527 return;
549 } 528 }
550 529
530 // Retrieve the recently closed URLs synchronously.
531 if (tab_restore_has_pending_notification_) {
532 tab_restore_has_pending_notification_ = false;
533 ProcessTabRestoreServiceNotification();
534 }
535
536 // If TopSites has updates, retrieve the URLs asynchronously, and on its
537 // completion, trigger favicon loading.
538 // Otherwise, call StartLoadingFavicon directly to start favicon loading.
539 if (top_sites_has_pending_notification_)
540 ProcessTopSitesNotification();
541 else
542 StartLoadingFavicon();
543 }
544
545 void JumpList::ProcessTopSitesNotification() {
546 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
547
551 // Opening the first tab in one session triggers a TopSite history sync. 548 // Opening the first tab in one session triggers a TopSite history sync.
552 // Delay this sync till the first tab is closed to allow the "recently closed" 549 // Delay this sync till the first tab is closed to allow the "recently closed"
553 // category from last session to stay longer. 550 // category from last session to stay longer. All previous pending
554 if (!has_tab_closed_) 551 // notifications from TopSites are ignored.
552 if (!has_tab_closed_) {
553 top_sites_has_pending_notification_ = false;
555 return; 554 return;
555 }
556 556
557 scoped_refptr<history::TopSites> top_sites = 557 scoped_refptr<history::TopSites> top_sites =
558 TopSitesFactory::GetForProfile(profile_); 558 TopSitesFactory::GetForProfile(profile_);
559 if (top_sites) { 559 if (top_sites) {
560 top_sites->GetMostVisitedURLs( 560 top_sites->GetMostVisitedURLs(
561 base::Bind(&JumpList::OnMostVisitedURLsAvailable, 561 base::Bind(&JumpList::OnMostVisitedURLsAvailable,
562 weak_ptr_factory_.GetWeakPtr()), 562 weak_ptr_factory_.GetWeakPtr()),
563 false); 563 false);
564 } 564 }
565 } 565 }
566 566
567 void JumpList::DeferredTabRestoreServiceChanged() { 567 void JumpList::ProcessTabRestoreServiceNotification() {
568 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 568 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
569 569
570 if (updates_to_skip_ > 0) {
571 --updates_to_skip_;
572 return;
573 }
574
575 // Force a TopSite history sync when closing a first tab in one session.
576 if (!has_tab_closed_) {
577 has_tab_closed_ = true;
578 scoped_refptr<history::TopSites> top_sites =
579 TopSitesFactory::GetForProfile(profile_);
580 if (top_sites)
581 top_sites->SyncWithHistory();
582 }
583
584 // Create a list of ShellLinkItems from the "Recently Closed" pages. 570 // Create a list of ShellLinkItems from the "Recently Closed" pages.
585 // As noted above, we create a ShellLinkItem objects with the following 571 // As noted above, we create a ShellLinkItem objects with the following
586 // parameters. 572 // parameters.
587 // * arguments 573 // * arguments
588 // The last URL of the tab object. 574 // The last URL of the tab object.
589 // * title 575 // * title
590 // The title of the last URL. 576 // The title of the last URL.
591 // * icon 577 // * icon
592 // An empty string. This value is to be updated in OnFaviconDataAvailable(). 578 // An empty string. This value is to be updated in OnFaviconDataAvailable().
593 579
594 sessions::TabRestoreService* tab_restore_service = 580 sessions::TabRestoreService* tab_restore_service =
595 TabRestoreServiceFactory::GetForProfile(profile_); 581 TabRestoreServiceFactory::GetForProfile(profile_);
596 582
597 { 583 recently_closed_pages_.clear();
598 JumpListData* data = &jumplist_data_->data;
599 base::AutoLock auto_lock(data->list_lock_);
600 data->recently_closed_pages_.clear();
601 584
602 for (const auto& entry : tab_restore_service->entries()) { 585 for (const auto& entry : tab_restore_service->entries()) {
603 if (data->recently_closed_pages_.size() >= kRecentlyClosedItems) 586 if (recently_closed_pages_.size() >= kRecentlyClosedItems)
587 break;
588 switch (entry->type) {
589 case sessions::TabRestoreService::TAB:
590 AddTab(static_cast<const sessions::TabRestoreService::Tab&>(*entry),
591 kRecentlyClosedItems);
604 break; 592 break;
605 switch (entry->type) { 593 case sessions::TabRestoreService::WINDOW:
606 case sessions::TabRestoreService::TAB: 594 AddWindow(
607 AddTab(static_cast<const sessions::TabRestoreService::Tab&>(*entry), 595 static_cast<const sessions::TabRestoreService::Window&>(*entry),
608 kRecentlyClosedItems, data); 596 kRecentlyClosedItems);
609 break; 597 break;
610 case sessions::TabRestoreService::WINDOW:
611 AddWindow(
612 static_cast<const sessions::TabRestoreService::Window&>(*entry),
613 kRecentlyClosedItems, data);
614 break;
615 }
616 } 598 }
617
618 data->recently_closed_pages_have_updates_ = true;
619 } 599 }
620 600
621 // Send a query that retrieves the first favicon. 601 recently_closed_should_update_ = true;
622 StartLoadingFavicon(); 602
603 // Force a TopSite history sync when closing a first tab in one session.
604 if (!has_tab_closed_) {
605 has_tab_closed_ = true;
606 scoped_refptr<history::TopSites> top_sites =
607 TopSitesFactory::GetForProfile(profile_);
608 if (top_sites)
609 top_sites->SyncWithHistory();
610 }
623 } 611 }
624 612
613 // static
625 void JumpList::DeleteIconFiles(const base::FilePath& icon_dir, 614 void JumpList::DeleteIconFiles(const base::FilePath& icon_dir,
626 JumpListCategory category) { 615 URLIconCache* icon_cache) {
627 base::flat_map<std::string, base::FilePath>* source_map = nullptr; 616 DCHECK(icon_cache);
628 switch (category) {
629 case JumpListCategory::kMostVisited:
630 source_map = &most_visited_icons_;
631 break;
632 case JumpListCategory::kRecentlyClosed:
633 source_map = &recently_closed_icons_;
634 break;
635 }
636 617
637 // Put all cached icon file paths into a set. 618 // Put all cached icon file paths into a set.
638 base::flat_set<base::FilePath> cached_files; 619 base::flat_set<base::FilePath> cached_files;
639 cached_files.reserve(source_map->size()); 620 cached_files.reserve(icon_cache->size());
640 621
641 for (const auto& url_path_pair : *source_map) 622 for (const auto& url_path_pair : *icon_cache)
642 cached_files.insert(url_path_pair.second); 623 cached_files.insert(url_path_pair.second);
643 624
644 DeleteNonCachedFiles(icon_dir, cached_files); 625 DeleteNonCachedFiles(icon_dir, cached_files);
645 } 626 }
646 627
628 // static
647 int JumpList::CreateIconFiles(const base::FilePath& icon_dir, 629 int JumpList::CreateIconFiles(const base::FilePath& icon_dir,
648 const ShellLinkItemList& item_list, 630 const ShellLinkItemList& item_list,
649 size_t max_items, 631 size_t max_items,
650 JumpListCategory category) { 632 URLIconCache* icon_cache) {
633 DCHECK(icon_cache);
634
651 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/40407. 635 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/40407.
652 SCOPED_UMA_HISTOGRAM_TIMER("WinJumplist.CreateIconFilesDuration"); 636 SCOPED_UMA_HISTOGRAM_TIMER("WinJumplist.CreateIconFilesDuration");
653 637
654 int icons_created = 0; 638 int icons_created = 0;
655 639
656 // Reuse icons for urls that were already present in the jumplist for this 640 // Reuse icons for urls that already present in the current JumpList.
657 // category. 641 URLIconCache updated_map;
658
659 base::flat_map<std::string, base::FilePath>* source_map = nullptr;
660 switch (category) {
661 case JumpListCategory::kMostVisited:
662 source_map = &most_visited_icons_;
663 break;
664 case JumpListCategory::kRecentlyClosed:
665 source_map = &recently_closed_icons_;
666 break;
667 }
668
669 base::flat_map<std::string, base::FilePath> updated_map;
670
671 for (ShellLinkItemList::const_iterator iter = item_list.begin(); 642 for (ShellLinkItemList::const_iterator iter = item_list.begin();
672 iter != item_list.end() && max_items > 0; ++iter, --max_items) { 643 iter != item_list.end() && max_items > 0; ++iter, --max_items) {
673 ShellLinkItem* item = iter->get(); 644 ShellLinkItem* item = iter->get();
674 auto cache_iter = source_map->find(item->url()); 645 auto cache_iter = icon_cache->find(item->url());
675 if (cache_iter != source_map->end()) { 646 if (cache_iter != icon_cache->end()) {
676 item->set_icon(cache_iter->second.value(), 0); 647 item->set_icon(cache_iter->second.value(), 0);
677 updated_map[item->url()] = cache_iter->second; 648 updated_map[item->url()] = cache_iter->second;
678 } else { 649 } else {
679 base::FilePath icon_path; 650 base::FilePath icon_path;
680 if (CreateIconFile(item->icon_image(), icon_dir, &icon_path)) { 651 if (CreateIconFile(item->icon_image(), icon_dir, &icon_path)) {
681 ++icons_created; 652 ++icons_created;
682 item->set_icon(icon_path.value(), 0); 653 item->set_icon(icon_path.value(), 0);
683 updated_map[item->url()] = icon_path; 654 updated_map[item->url()] = icon_path;
684 } 655 }
685 } 656 }
686 } 657 }
687 source_map->swap(updated_map); 658 icon_cache->swap(updated_map);
688 659
689 return icons_created; 660 return icons_created;
690 } 661 }
691 662
663 // static
692 int JumpList::UpdateIconFiles(const base::FilePath& icon_dir, 664 int JumpList::UpdateIconFiles(const base::FilePath& icon_dir,
693 const ShellLinkItemList& page_list, 665 const ShellLinkItemList& page_list,
694 size_t slot_limit, 666 size_t slot_limit,
695 JumpListCategory category) { 667 URLIconCache* icon_cache) {
668 DCHECK(icon_cache);
669
696 int icons_created = 0; 670 int icons_created = 0;
697 671
698 // Maximum number of icon files that each JumpList icon folder may hold, which
699 // is set to 2 times the normal amount.
700 size_t icon_limit =
701 2 * ((category == JumpListCategory::kMostVisited) ? kMostVisitedItems
702 : kRecentlyClosedItems);
703
704 // Clear the JumpList icon folder at |icon_dir| and the cache when 672 // Clear the JumpList icon folder at |icon_dir| and the cache when
705 // 1) "Most visited" category updates for the 1st time after Chrome is 673 // 1) |icon_cache| is empty. This happens when "Most visited" or "Recently
706 // launched. This actually happens right after Chrome is launched. 674 // closed" category updates for the 1st time after Chrome is launched.
707 // 2) "Recently closed" category updates for the 1st time after Chrome is 675 // 2) The number of icons in |icon_dir| has exceeded the limit.
708 // launched. 676 if (icon_cache->empty() || FilesExceedLimitInDir(icon_dir, slot_limit * 2)) {
709 // 3) The number of icons in |icon_dir| has exceeded the limit.
710 if ((category == JumpListCategory::kMostVisited &&
711 most_visited_icons_.empty()) ||
712 (category == JumpListCategory::kRecentlyClosed &&
713 recently_closed_icons_.empty()) ||
714 FilesExceedLimitInDir(icon_dir, icon_limit)) {
715 DeleteDirectoryContentAndLogRuntime(icon_dir, kFileDeleteLimit); 677 DeleteDirectoryContentAndLogRuntime(icon_dir, kFileDeleteLimit);
716 most_visited_icons_.clear(); 678 icon_cache->clear();
717 recently_closed_icons_.clear();
718 // Create new icons only when the directory exists and is empty. 679 // Create new icons only when the directory exists and is empty.
719 if (base::CreateDirectory(icon_dir) && base::IsDirectoryEmpty(icon_dir)) 680 if (base::CreateDirectory(icon_dir) && base::IsDirectoryEmpty(icon_dir))
720 icons_created += 681 icons_created +=
721 CreateIconFiles(icon_dir, page_list, slot_limit, category); 682 CreateIconFiles(icon_dir, page_list, slot_limit, icon_cache);
722 } else if (base::CreateDirectory(icon_dir)) { 683 } else if (base::CreateDirectory(icon_dir)) {
723 icons_created += CreateIconFiles(icon_dir, page_list, slot_limit, category); 684 icons_created +=
724 DeleteIconFiles(icon_dir, category); 685 CreateIconFiles(icon_dir, page_list, slot_limit, icon_cache);
686 DeleteIconFiles(icon_dir, icon_cache);
725 } 687 }
726 688
727 return icons_created; 689 return icons_created;
728 } 690 }
729 691
730 bool JumpList::UpdateJumpList( 692 // static
693 void JumpList::RunUpdateJumpList(
731 const base::string16& app_id, 694 const base::string16& app_id,
732 const base::FilePath& profile_dir, 695 const base::FilePath& profile_dir,
733 const ShellLinkItemList& most_visited_pages, 696 const ShellLinkItemList& most_visited_pages,
734 const ShellLinkItemList& recently_closed_pages, 697 const ShellLinkItemList& recently_closed_pages,
735 bool most_visited_pages_have_updates, 698 bool most_visited_should_update,
736 bool recently_closed_pages_have_updates, 699 bool recently_closed_should_update,
737 IncognitoModePrefs::Availability incognito_availability) { 700 IncognitoModePrefs::Availability incognito_availability,
701 UpdateResults* update_results) {
738 if (!JumpListUpdater::IsEnabled()) 702 if (!JumpListUpdater::IsEnabled())
739 return true; 703 return;
704
705 DCHECK(update_results);
740 706
741 JumpListUpdater jumplist_updater(app_id); 707 JumpListUpdater jumplist_updater(app_id);
742 708
743 base::ElapsedTimer begin_update_timer; 709 base::ElapsedTimer begin_update_timer;
744 710
745 if (!jumplist_updater.BeginUpdate()) 711 if (!jumplist_updater.BeginUpdate())
746 return false; 712 return;
747 713
748 // Discard this JumpList update if JumpListUpdater::BeginUpdate takes longer 714 // Discard this JumpList update if JumpListUpdater::BeginUpdate takes longer
749 // than the maximum allowed time, as it's very likely the following update 715 // than the maximum allowed time, as it's very likely the following update
750 // steps will also take a long time. As we've not updated the icons on the 716 // steps will also take a long time. As we've not updated the icons on the
751 // disk, discarding this update wont't affect the current JumpList used by OS. 717 // disk, discarding this update wont't affect the current JumpList used by OS.
752 if (begin_update_timer.Elapsed() >= kTimeOutForJumplistBeginUpdate) { 718 if (begin_update_timer.Elapsed() >= kTimeOutForJumplistBeginUpdate) {
753 updates_to_skip_ = kUpdatesToSkipUnderHeavyLoad; 719 update_results->update_timeout = true;
754 return false; 720 return;
755 } 721 }
756 722
757 // Record the desired number of icons created in this JumpList update. 723 // Record the desired number of icons created in this JumpList update.
758 int icons_created = 0; 724 int icons_created = 0;
759 725
760 // Update the icons for "Most Visisted" category of the JumpList if needed. 726 // Update the icons for "Most Visisted" category of the JumpList if needed.
761 if (most_visited_pages_have_updates) { 727 if (most_visited_should_update) {
762 base::FilePath icon_dir_most_visited = GenerateJumplistIconDirName( 728 base::FilePath icon_dir_most_visited = GenerateJumplistIconDirName(
763 profile_dir, FILE_PATH_LITERAL("MostVisited")); 729 profile_dir, FILE_PATH_LITERAL("MostVisited"));
764 730
765 icons_created += 731 icons_created += UpdateIconFiles(
766 UpdateIconFiles(icon_dir_most_visited, most_visited_pages, 732 icon_dir_most_visited, most_visited_pages, kMostVisitedItems,
767 kMostVisitedItems, JumpListCategory::kMostVisited); 733 &update_results->most_visited_icons_in_update);
768 } 734 }
769 735
770 // Update the icons for "Recently Closed" category of the JumpList if needed. 736 // Update the icons for "Recently Closed" category of the JumpList if needed.
771 if (recently_closed_pages_have_updates) { 737 if (recently_closed_should_update) {
772 base::FilePath icon_dir_recent_closed = GenerateJumplistIconDirName( 738 base::FilePath icon_dir_recent_closed = GenerateJumplistIconDirName(
773 profile_dir, FILE_PATH_LITERAL("RecentClosed")); 739 profile_dir, FILE_PATH_LITERAL("RecentClosed"));
774 740
775 icons_created += UpdateIconFiles( 741 icons_created += UpdateIconFiles(
776 icon_dir_recent_closed, recently_closed_pages, kRecentlyClosedItems, 742 icon_dir_recent_closed, recently_closed_pages, kRecentlyClosedItems,
777 JumpListCategory::kRecentlyClosed); 743 &update_results->recently_closed_icons_in_update);
778 } 744 }
779 745
780 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/40407. 746 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/40407.
781 UMA_HISTOGRAM_COUNTS_100("WinJumplist.CreateIconFilesCount", icons_created); 747 UMA_HISTOGRAM_COUNTS_100("WinJumplist.CreateIconFilesCount", icons_created);
782 748
783 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/40407. 749 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/40407.
784 SCOPED_UMA_HISTOGRAM_TIMER("WinJumplist.UpdateJumpListDuration"); 750 SCOPED_UMA_HISTOGRAM_TIMER("WinJumplist.UpdateJumpListDuration");
785 751
786 base::ElapsedTimer add_custom_category_timer; 752 base::ElapsedTimer add_custom_category_timer;
787 753
788 // Update the "Most Visited" category of the JumpList if it exists. 754 // Update the "Most Visited" category of the JumpList if it exists.
789 // This update request is applied into the JumpList when we commit this 755 // This update request is applied into the JumpList when we commit this
790 // transaction. 756 // transaction.
791 if (!jumplist_updater.AddCustomCategory( 757 if (!jumplist_updater.AddCustomCategory(
792 l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED), 758 l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED),
793 most_visited_pages, kMostVisitedItems)) { 759 most_visited_pages, kMostVisitedItems)) {
794 return false; 760 return;
795 } 761 }
796 762
797 // Update the "Recently Closed" category of the JumpList. 763 // Update the "Recently Closed" category of the JumpList.
798 if (!jumplist_updater.AddCustomCategory( 764 if (!jumplist_updater.AddCustomCategory(
799 l10n_util::GetStringUTF16(IDS_RECENTLY_CLOSED), recently_closed_pages, 765 l10n_util::GetStringUTF16(IDS_RECENTLY_CLOSED), recently_closed_pages,
800 kRecentlyClosedItems)) { 766 kRecentlyClosedItems)) {
801 return false; 767 return;
802 } 768 }
803 769
804 // If JumpListUpdater::AddCustomCategory or JumpListUpdater::CommitUpdate 770 // If JumpListUpdater::AddCustomCategory or JumpListUpdater::CommitUpdate
805 // takes longer than the maximum allowed time, skip the next 771 // takes longer than the maximum allowed time, skip the next
806 // |kUpdatesToSkipUnderHeavyLoad| updates. This update should be finished 772 // |kUpdatesToSkipUnderHeavyLoad| updates. This update should be finished
807 // because we've already updated the icons on the disk. If discarding this 773 // because we've already updated the icons on the disk. If discarding this
808 // update from here, some items in the current JumpList may not have icons 774 // update from here, some items in the current JumpList may not have icons
809 // as they've been delete from the disk. In this case, the background color of 775 // as they've been delete from the disk. In this case, the background color of
810 // the JumpList panel is used instead, which doesn't look nice. 776 // the JumpList panel is used instead, which doesn't look nice.
811 777
812 if (add_custom_category_timer.Elapsed() >= kTimeOutForAddCustomCategory) 778 if (add_custom_category_timer.Elapsed() >= kTimeOutForAddCustomCategory)
813 updates_to_skip_ = kUpdatesToSkipUnderHeavyLoad; 779 update_results->update_timeout = true;
814 780
815 // Update the "Tasks" category of the JumpList. 781 // Update the "Tasks" category of the JumpList.
816 if (!UpdateTaskCategory(&jumplist_updater, incognito_availability)) 782 if (!UpdateTaskCategory(&jumplist_updater, incognito_availability))
817 return false; 783 return;
818 784
819 base::ElapsedTimer commit_update_timer; 785 base::ElapsedTimer commit_update_timer;
820 786
821 // Commit this transaction and send the updated JumpList to Windows. 787 // Commit this transaction and send the updated JumpList to Windows.
822 bool commit_result = jumplist_updater.CommitUpdate(); 788 if (jumplist_updater.CommitUpdate())
789 update_results->update_success = true;
823 790
824 if (commit_update_timer.Elapsed() >= kTimeOutForJumplistCommitUpdate) 791 if (commit_update_timer.Elapsed() >= kTimeOutForJumplistCommitUpdate)
792 update_results->update_timeout = true;
793 }
794
795 void JumpList::OnRunUpdateCompletion(
796 std::unique_ptr<UpdateResults> update_results) {
797 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
798
799 // Update JumpList member variables based on the results from the update run
800 // just finished.
801 if (update_results->update_timeout)
825 updates_to_skip_ = kUpdatesToSkipUnderHeavyLoad; 802 updates_to_skip_ = kUpdatesToSkipUnderHeavyLoad;
826 803
827 return commit_result; 804 if (update_results->update_success) {
828 } 805 most_visited_icons_.swap(update_results->most_visited_icons_in_update);
829 806 recently_closed_icons_.swap(
830 void JumpList::RunUpdateJumpList( 807 update_results->recently_closed_icons_in_update);
831 IncognitoModePrefs::Availability incognito_availability, 808 most_visited_should_update_ = false;
832 const base::string16& app_id, 809 recently_closed_should_update_ = false;
833 const base::FilePath& profile_dir,
834 base::RefCountedData<JumpListData>* ref_counted_data) {
835 JumpListData* data = &ref_counted_data->data;
836 ShellLinkItemList local_most_visited_pages;
837 ShellLinkItemList local_recently_closed_pages;
838 bool most_visited_pages_have_updates;
839 bool recently_closed_pages_have_updates;
840
841 {
842 base::AutoLock auto_lock(data->list_lock_);
843 // Make sure we are not out of date: if icon_urls_ is not empty, then
844 // another notification has been received since we processed this one
845 if (!data->icon_urls_.empty())
846 return;
847
848 // Make local copies of lists and flags so we can release the lock.
849 local_most_visited_pages = data->most_visited_pages_;
850 local_recently_closed_pages = data->recently_closed_pages_;
851
852 most_visited_pages_have_updates = data->most_visited_pages_have_updates_;
853 recently_closed_pages_have_updates =
854 data->recently_closed_pages_have_updates_;
855
856 // Clear the flags to reflect that we'll take actions on these updates.
857 data->most_visited_pages_have_updates_ = false;
858 data->recently_closed_pages_have_updates_ = false;
859 } 810 }
860 811
861 if (!most_visited_pages_have_updates && !recently_closed_pages_have_updates) 812 update_in_progress_ = false;
862 return;
863 813
864 // Update the application JumpList. If it fails, reset the flags to true if 814 // If there is any new notification during the update run just finished, start
865 // they were so that the corresponding JumpList categories will be tried to 815 // another JumpList update.
866 // update again in the next run. 816 // Otherwise, post tasks to delete the JumpListIcons and JumpListIconsOld
867 if (!UpdateJumpList( 817 // folders as they are no longer needed. Now we have the
868 app_id, profile_dir, local_most_visited_pages, 818 // JumpListIcons{MostVisited, RecentClosed} folders instead.
869 local_recently_closed_pages, most_visited_pages_have_updates, 819 if (top_sites_has_pending_notification_ ||
870 recently_closed_pages_have_updates, incognito_availability)) { 820 tab_restore_has_pending_notification_) {
871 base::AutoLock auto_lock(data->list_lock_); 821 InitializeTimerForUpdate();
872 if (most_visited_pages_have_updates) 822 } else {
873 data->most_visited_pages_have_updates_ = true; 823 base::FilePath profile_dir = profile_->GetPath();
874 if (recently_closed_pages_have_updates) 824 base::FilePath icon_dir =
875 data->recently_closed_pages_have_updates_ = true; 825 GenerateJumplistIconDirName(profile_dir, FILE_PATH_LITERAL(""));
826 delete_jumplisticons_task_runner_->PostTask(
827 FROM_HERE,
828 base::Bind(&DeleteDirectory, std::move(icon_dir), kFileDeleteLimit));
829
830 base::FilePath icon_dir_old =
831 GenerateJumplistIconDirName(profile_dir, FILE_PATH_LITERAL("Old"));
832 delete_jumplisticons_task_runner_->PostTask(
833 FROM_HERE, base::Bind(&DeleteDirectory, std::move(icon_dir_old),
834 kFileDeleteLimit));
876 } 835 }
877 } 836 }
OLDNEW
« no previous file with comments | « chrome/browser/win/jumplist.h ('k') | chrome/browser/win/jumplist_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698