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

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

Issue 2779403002: Migrate jumplist update task from FILE thread to base/task_scheduler (Closed)
Patch Set: Use CreateCOMSTATaskRunnerWithTraits. Created 3 years, 8 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 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/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_macros.h"
13 #include "base/path_service.h" 13 #include "base/path_service.h"
14 #include "base/sequenced_task_runner.h" 14 #include "base/single_thread_task_runner.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "base/task_scheduler/post_task.h" 17 #include "base/task_scheduler/post_task.h"
18 #include "base/threading/thread.h" 18 #include "base/threading/thread.h"
19 #include "base/threading/thread_restrictions.h" 19 #include "base/threading/thread_restrictions.h"
20 #include "base/trace_event/trace_event.h" 20 #include "base/trace_event/trace_event.h"
21 #include "chrome/browser/chrome_notification_types.h" 21 #include "chrome/browser/chrome_notification_types.h"
22 #include "chrome/browser/favicon/favicon_service_factory.h" 22 #include "chrome/browser/favicon/favicon_service_factory.h"
23 #include "chrome/browser/history/top_sites_factory.h" 23 #include "chrome/browser/history/top_sites_factory.h"
24 #include "chrome/browser/metrics/jumplist_metrics_win.h" 24 #include "chrome/browser/metrics/jumplist_metrics_win.h"
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 return false; 221 return false;
222 222
223 // Commit this transaction and send the updated JumpList to Windows. 223 // Commit this transaction and send the updated JumpList to Windows.
224 if (!jumplist_updater.CommitUpdate()) 224 if (!jumplist_updater.CommitUpdate())
225 return false; 225 return false;
226 226
227 return true; 227 return true;
228 } 228 }
229 229
230 // Updates the jumplist, once all the data has been fetched. 230 // Updates the jumplist, once all the data has been fetched.
231 void RunUpdateOnFileThread( 231 void RunUpdateJumpList(IncognitoModePrefs::Availability incognito_availability,
232 IncognitoModePrefs::Availability incognito_availability, 232 const std::wstring& app_id,
233 const std::wstring& app_id, 233 const base::FilePath& icon_dir,
234 const base::FilePath& icon_dir, 234 base::RefCountedData<JumpListData>* ref_counted_data) {
235 base::RefCountedData<JumpListData>* ref_counted_data,
236 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner) {
237 JumpListData* data = &ref_counted_data->data; 235 JumpListData* data = &ref_counted_data->data;
238 ShellLinkItemList local_most_visited_pages; 236 ShellLinkItemList local_most_visited_pages;
239 ShellLinkItemList local_recently_closed_pages; 237 ShellLinkItemList local_recently_closed_pages;
240 238
241 { 239 {
242 base::AutoLock auto_lock(data->list_lock_); 240 base::AutoLock auto_lock(data->list_lock_);
243 // Make sure we are not out of date: if icon_urls_ is not empty, then 241 // Make sure we are not out of date: if icon_urls_ is not empty, then
244 // another notification has been received since we processed this one 242 // another notification has been received since we processed this one
245 if (!data->icon_urls_.empty()) 243 if (!data->icon_urls_.empty())
246 return; 244 return;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 // Create icon files for shortcuts in the "Recently Closed" 279 // Create icon files for shortcuts in the "Recently Closed"
282 // category. 280 // category.
283 CreateIconFiles(icon_dir, local_recently_closed_pages); 281 CreateIconFiles(icon_dir, local_recently_closed_pages);
284 } 282 }
285 283
286 // Create a new JumpList and replace the current JumpList with it. The 284 // Create a new JumpList and replace the current JumpList with it. The
287 // jumplist links are updated anyway, while the jumplist icons may not as 285 // jumplist links are updated anyway, while the jumplist icons may not as
288 // mentioned above. 286 // mentioned above.
289 UpdateJumpList(app_id.c_str(), local_most_visited_pages, 287 UpdateJumpList(app_id.c_str(), local_most_visited_pages,
290 local_recently_closed_pages, incognito_availability); 288 local_recently_closed_pages, incognito_availability);
291
292 // Post a background task to delete JumpListIconsOld folder if it exists and
293 // log the delete results to UMA.
294 base::FilePath icon_dir_old = icon_dir.DirName().Append(
295 icon_dir.BaseName().value() + FILE_PATH_LITERAL("Old"));
296
297 if (base::DirectoryExists(icon_dir_old)) {
298 sequenced_task_runner->PostTask(
299 FROM_HERE, base::Bind(&DeleteDirectoryAndLogResults, icon_dir_old,
300 kFileDeleteLimit));
301 }
302 } 289 }
303 290
304 } // namespace 291 } // namespace
305 292
306 JumpList::JumpListData::JumpListData() {} 293 JumpList::JumpListData::JumpListData() {}
307 294
308 JumpList::JumpListData::~JumpListData() {} 295 JumpList::JumpListData::~JumpListData() {}
309 296
310 JumpList::JumpList(Profile* profile) 297 JumpList::JumpList(Profile* profile)
311 : RefcountedKeyedService(content::BrowserThread::GetTaskRunnerForThread( 298 : RefcountedKeyedService(content::BrowserThread::GetTaskRunnerForThread(
312 content::BrowserThread::UI)), 299 content::BrowserThread::UI)),
313 profile_(profile), 300 profile_(profile),
314 jumplist_data_(new base::RefCountedData<JumpListData>), 301 jumplist_data_(new base::RefCountedData<JumpListData>),
315 task_id_(base::CancelableTaskTracker::kBadTaskId), 302 task_id_(base::CancelableTaskTracker::kBadTaskId),
316 sequenced_task_runner_(base::CreateSequencedTaskRunnerWithTraits( 303 single_thread_task_runner_(base::CreateCOMSTATaskRunnerWithTraits(
317 base::TaskTraits() 304 base::TaskTraits()
318 .WithPriority(base::TaskPriority::BACKGROUND) 305 .WithPriority(base::TaskPriority::BACKGROUND)
319 .WithShutdownBehavior( 306 .WithShutdownBehavior(
320 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) 307 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)
321 .MayBlock())), 308 .MayBlock())),
322 weak_ptr_factory_(this) { 309 weak_ptr_factory_(this) {
323 DCHECK(Enabled()); 310 DCHECK(Enabled());
324 // To update JumpList when a tab is added or removed, we add this object to 311 // To update JumpList when a tab is added or removed, we add this object to
325 // the observer list of the TabRestoreService class. 312 // the observer list of the TabRestoreService class.
326 // When we add this object to the observer list, we save the pointer to this 313 // When we add this object to the observer list, we save the pointer to this
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 waiting_for_icons = !data->icon_urls_.empty(); 511 waiting_for_icons = !data->icon_urls_.empty();
525 if (waiting_for_icons) { 512 if (waiting_for_icons) {
526 // Ask FaviconService if it has a favicon of a URL. 513 // Ask FaviconService if it has a favicon of a URL.
527 // When FaviconService has one, it will call OnFaviconDataAvailable(). 514 // When FaviconService has one, it will call OnFaviconDataAvailable().
528 url = GURL(data->icon_urls_.front().first); 515 url = GURL(data->icon_urls_.front().first);
529 } 516 }
530 } 517 }
531 518
532 if (!waiting_for_icons) { 519 if (!waiting_for_icons) {
533 // No more favicons are needed by the application JumpList. Schedule a 520 // No more favicons are needed by the application JumpList. Schedule a
534 // RunUpdateOnFileThread call. 521 // RunUpdateJumpList call.
535 PostRunUpdate(); 522 PostRunUpdate();
536 return; 523 return;
537 } 524 }
538 525
539 favicon::FaviconService* favicon_service = 526 favicon::FaviconService* favicon_service =
540 FaviconServiceFactory::GetForProfile(profile_, 527 FaviconServiceFactory::GetForProfile(profile_,
541 ServiceAccessType::EXPLICIT_ACCESS); 528 ServiceAccessType::EXPLICIT_ACCESS);
542 task_id_ = favicon_service->GetFaviconImageForPageURL( 529 task_id_ = favicon_service->GetFaviconImageForPageURL(
543 url, 530 url,
544 base::Bind(&JumpList::OnFaviconDataAvailable, base::Unretained(this)), 531 base::Bind(&JumpList::OnFaviconDataAvailable, base::Unretained(this)),
545 &cancelable_task_tracker_); 532 &cancelable_task_tracker_);
546 } 533 }
547 534
548 void JumpList::OnFaviconDataAvailable( 535 void JumpList::OnFaviconDataAvailable(
549 const favicon_base::FaviconImageResult& image_result) { 536 const favicon_base::FaviconImageResult& image_result) {
550 DCHECK(CalledOnValidThread()); 537 DCHECK(CalledOnValidThread());
551 538
552 // If there is currently a favicon request in progress, it is now outdated, 539 // If there is currently a favicon request in progress, it is now outdated,
553 // as we have received another, so nullify the handle from the old request. 540 // as we have received another, so nullify the handle from the old request.
554 task_id_ = base::CancelableTaskTracker::kBadTaskId; 541 task_id_ = base::CancelableTaskTracker::kBadTaskId;
555 // Lock the list to set icon data and pop the url. 542 // Lock the list to set icon data and pop the url.
556 { 543 {
557 JumpListData* data = &jumplist_data_->data; 544 JumpListData* data = &jumplist_data_->data;
558 base::AutoLock auto_lock(data->list_lock_); 545 base::AutoLock auto_lock(data->list_lock_);
559 // Attach the received data to the ShellLinkItem object. 546 // Attach the received data to the ShellLinkItem object.
560 // This data will be decoded by the RunUpdateOnFileThread method. 547 // This data will be decoded by the RunUpdateJumpList method.
561 if (!image_result.image.IsEmpty() && !data->icon_urls_.empty() && 548 if (!image_result.image.IsEmpty() && !data->icon_urls_.empty() &&
562 data->icon_urls_.front().second.get()) { 549 data->icon_urls_.front().second.get()) {
563 gfx::ImageSkia image_skia = image_result.image.AsImageSkia(); 550 gfx::ImageSkia image_skia = image_result.image.AsImageSkia();
564 image_skia.EnsureRepsForSupportedScales(); 551 image_skia.EnsureRepsForSupportedScales();
565 std::unique_ptr<gfx::ImageSkia> deep_copy(image_skia.DeepCopy()); 552 std::unique_ptr<gfx::ImageSkia> deep_copy(image_skia.DeepCopy());
566 data->icon_urls_.front().second->set_icon_image(*deep_copy); 553 data->icon_urls_.front().second->set_icon_image(*deep_copy);
567 } 554 }
568 555
569 if (!data->icon_urls_.empty()) 556 if (!data->icon_urls_.empty())
570 data->icon_urls_.pop_front(); 557 data->icon_urls_.pop_front();
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 595
609 void JumpList::DeferredRunUpdate() { 596 void JumpList::DeferredRunUpdate() {
610 DCHECK(CalledOnValidThread()); 597 DCHECK(CalledOnValidThread());
611 598
612 TRACE_EVENT0("browser", "JumpList::DeferredRunUpdate"); 599 TRACE_EVENT0("browser", "JumpList::DeferredRunUpdate");
613 // Check if incognito windows (or normal windows) are disabled by policy. 600 // Check if incognito windows (or normal windows) are disabled by policy.
614 IncognitoModePrefs::Availability incognito_availability = 601 IncognitoModePrefs::Availability incognito_availability =
615 profile_ ? IncognitoModePrefs::GetAvailability(profile_->GetPrefs()) 602 profile_ ? IncognitoModePrefs::GetAvailability(profile_->GetPrefs())
616 : IncognitoModePrefs::ENABLED; 603 : IncognitoModePrefs::ENABLED;
617 604
618 BrowserThread::PostTask( 605 // Post a task to update the jumplist used by the shell.
619 BrowserThread::FILE, FROM_HERE, 606 single_thread_task_runner_->PostTask(
620 base::Bind(&RunUpdateOnFileThread, incognito_availability, app_id_, 607 FROM_HERE, base::Bind(&RunUpdateJumpList, incognito_availability, app_id_,
621 icon_dir_, base::RetainedRef(jumplist_data_), 608 icon_dir_, base::RetainedRef(jumplist_data_)));
622 sequenced_task_runner_)); 609
610 // Post a task to delete JumpListIconsOld folder if it exists and log the
611 // delete results to UMA.
612 base::FilePath icon_dir_old = icon_dir_.DirName().Append(
613 icon_dir_.BaseName().value() + FILE_PATH_LITERAL("Old"));
614
615 single_thread_task_runner_->PostTask(
616 FROM_HERE, base::Bind(&DeleteDirectoryAndLogResults, icon_dir_old,
gab 2017/04/04 21:11:46 std::move(icon_dir_old) and #include <utilit> f
gab 2017/04/04 21:12:06 On 2017/04/04 21:11:46, gab wrote: > std::move(ico
chengx 2017/04/05 00:23:23 Done.
617 kFileDeleteLimit));
623 } 618 }
624 619
625 void JumpList::TopSitesLoaded(history::TopSites* top_sites) { 620 void JumpList::TopSitesLoaded(history::TopSites* top_sites) {
626 } 621 }
627 622
628 void JumpList::TopSitesChanged(history::TopSites* top_sites, 623 void JumpList::TopSitesChanged(history::TopSites* top_sites,
629 ChangeReason change_reason) { 624 ChangeReason change_reason) {
630 top_sites->GetMostVisitedURLs( 625 top_sites->GetMostVisitedURLs(
631 base::Bind(&JumpList::OnMostVisitedURLsAvailable, 626 base::Bind(&JumpList::OnMostVisitedURLsAvailable,
632 weak_ptr_factory_.GetWeakPtr()), 627 weak_ptr_factory_.GetWeakPtr()),
633 false); 628 false);
634 } 629 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698