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

Side by Side Diff: chrome/browser/chrome_content_browser_client.cc

Issue 949293002: Implement a poor man's PostAfterStartupTask() function. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chrome_content_browser_client.h" 5 #include "chrome/browser/chrome_content_browser_client.h"
6 6
7 #include <set> 7 #include <set>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/files/scoped_file.h" 13 #include "base/files/scoped_file.h"
14 #include "base/i18n/icu_util.h" 14 #include "base/i18n/icu_util.h"
15 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
16 #include "base/path_service.h" 16 #include "base/path_service.h"
17 #include "base/prefs/pref_service.h" 17 #include "base/prefs/pref_service.h"
18 #include "base/prefs/scoped_user_pref_update.h" 18 #include "base/prefs/scoped_user_pref_update.h"
19 #include "base/rand_util.h"
19 #include "base/strings/string_number_conversions.h" 20 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
21 #include "base/threading/sequenced_worker_pool.h" 22 #include "base/threading/sequenced_worker_pool.h"
23 #include "base/time/time.h"
22 #include "chrome/browser/browser_about_handler.h" 24 #include "chrome/browser/browser_about_handler.h"
23 #include "chrome/browser/browser_process.h" 25 #include "chrome/browser/browser_process.h"
24 #include "chrome/browser/browser_shutdown.h" 26 #include "chrome/browser/browser_shutdown.h"
25 #include "chrome/browser/browsing_data/browsing_data_helper.h" 27 #include "chrome/browser/browsing_data/browsing_data_helper.h"
26 #include "chrome/browser/browsing_data/browsing_data_remover.h" 28 #include "chrome/browser/browsing_data/browsing_data_remover.h"
27 #include "chrome/browser/character_encoding.h" 29 #include "chrome/browser/character_encoding.h"
30 #include "chrome/browser/chrome_browser_main.h"
28 #include "chrome/browser/chrome_content_browser_client_parts.h" 31 #include "chrome/browser/chrome_content_browser_client_parts.h"
29 #include "chrome/browser/chrome_net_benchmarking_message_filter.h" 32 #include "chrome/browser/chrome_net_benchmarking_message_filter.h"
30 #include "chrome/browser/chrome_quota_permission_context.h" 33 #include "chrome/browser/chrome_quota_permission_context.h"
31 #include "chrome/browser/content_settings/cookie_settings.h" 34 #include "chrome/browser/content_settings/cookie_settings.h"
32 #include "chrome/browser/content_settings/tab_specific_content_settings.h" 35 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
33 #include "chrome/browser/defaults.h" 36 #include "chrome/browser/defaults.h"
34 #include "chrome/browser/download/download_prefs.h" 37 #include "chrome/browser/download/download_prefs.h"
35 #include "chrome/browser/font_family_cache.h" 38 #include "chrome/browser/font_family_cache.h"
36 #include "chrome/browser/geolocation/chrome_access_token_store.h" 39 #include "chrome/browser/geolocation/chrome_access_token_store.h"
37 #include "chrome/browser/geolocation/geolocation_permission_context.h" 40 #include "chrome/browser/geolocation/geolocation_permission_context.h"
(...skipping 650 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 #endif 691 #endif
689 692
690 #if !defined(OS_ANDROID) 693 #if !defined(OS_ANDROID)
691 TtsExtensionEngine* tts_extension_engine = TtsExtensionEngine::GetInstance(); 694 TtsExtensionEngine* tts_extension_engine = TtsExtensionEngine::GetInstance();
692 TtsController::GetInstance()->SetTtsEngineDelegate(tts_extension_engine); 695 TtsController::GetInstance()->SetTtsEngineDelegate(tts_extension_engine);
693 #endif 696 #endif
694 697
695 #if defined(ENABLE_EXTENSIONS) 698 #if defined(ENABLE_EXTENSIONS)
696 extra_parts_.push_back(new ChromeContentBrowserClientExtensionsPart); 699 extra_parts_.push_back(new ChromeContentBrowserClientExtensionsPart);
697 #endif 700 #endif
701
702 weak_this_ = weak_factory_.GetWeakPtr();
698 } 703 }
699 704
700 ChromeContentBrowserClient::~ChromeContentBrowserClient() { 705 ChromeContentBrowserClient::~ChromeContentBrowserClient() {
701 for (int i = static_cast<int>(extra_parts_.size()) - 1; i >= 0; --i) 706 for (int i = static_cast<int>(extra_parts_.size()) - 1; i >= 0; --i)
702 delete extra_parts_[i]; 707 delete extra_parts_[i];
703 extra_parts_.clear(); 708 extra_parts_.clear();
704 } 709 }
705 710
706 // static 711 // static
707 void ChromeContentBrowserClient::RegisterProfilePrefs( 712 void ChromeContentBrowserClient::RegisterProfilePrefs(
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 784
780 #if defined(USE_X11) 785 #if defined(USE_X11)
781 main_parts->AddParts(new ChromeBrowserMainExtraPartsX11()); 786 main_parts->AddParts(new ChromeBrowserMainExtraPartsX11());
782 #endif 787 #endif
783 788
784 chrome::AddMetricsExtraParts(main_parts); 789 chrome::AddMetricsExtraParts(main_parts);
785 790
786 return main_parts; 791 return main_parts;
787 } 792 }
788 793
794 void ChromeContentBrowserClient::PostAfterStartupTask(
795 const tracked_objects::Location& from_here,
796 const scoped_refptr<base::TaskRunner>& task_runner,
797 const base::Closure& task) {
798 if (IsBrowserStartupComplete()) {
799 task_runner->PostTask(from_here, task);
800 return;
801 }
802
803 scoped_ptr<AfterStartupTask> queued_task(
804 new AfterStartupTask(from_here, task_runner, task));
805 QueueAfterStartupTask(queued_task.Pass());
806 }
807
789 std::string ChromeContentBrowserClient::GetStoragePartitionIdForSite( 808 std::string ChromeContentBrowserClient::GetStoragePartitionIdForSite(
790 content::BrowserContext* browser_context, 809 content::BrowserContext* browser_context,
791 const GURL& site) { 810 const GURL& site) {
792 std::string partition_id; 811 std::string partition_id;
793 812
794 // The partition ID for webview guest processes is the string value of its 813 // The partition ID for webview guest processes is the string value of its
795 // SiteInstance URL - "chrome-guest://app_id/persist?partition". 814 // SiteInstance URL - "chrome-guest://app_id/persist?partition".
796 if (site.SchemeIs(content::kGuestScheme)) { 815 if (site.SchemeIs(content::kGuestScheme)) {
797 partition_id = site.spec(); 816 partition_id = site.spec();
798 } else if (site.GetOrigin().spec() == kChromeUIChromeSigninURL) { 817 } else if (site.GetOrigin().spec() == kChromeUIChromeSigninURL) {
(...skipping 1750 matching lines...) Expand 10 before | Expand all | Expand 10 after
2549 Profile* profile = 2568 Profile* profile =
2550 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 2569 Profile::FromBrowserContext(web_contents->GetBrowserContext());
2551 prerender::PrerenderManager* prerender_manager = 2570 prerender::PrerenderManager* prerender_manager =
2552 prerender::PrerenderManagerFactory::GetForProfile(profile); 2571 prerender::PrerenderManagerFactory::GetForProfile(profile);
2553 if (prerender_manager && 2572 if (prerender_manager &&
2554 prerender_manager->IsWebContentsPrerendering(web_contents, nullptr)) { 2573 prerender_manager->IsWebContentsPrerendering(web_contents, nullptr)) {
2555 *visibility_state = blink::WebPageVisibilityStatePrerender; 2574 *visibility_state = blink::WebPageVisibilityStatePrerender;
2556 } 2575 }
2557 } 2576 }
2558 2577
2578 void ChromeContentBrowserClient::QueueAfterStartupTask(
2579 scoped_ptr<AfterStartupTask> queued_task) {
2580 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
2581 BrowserThread::PostTask(
2582 BrowserThread::UI,
2583 FROM_HERE,
2584 base::Bind(&ChromeContentBrowserClient::QueueAfterStartupTask,
2585 weak_this_, base::Passed(queued_task.Pass())));
2586 return;
2587 }
2588
2589 if (IsBrowserStartupComplete()) {
2590 ScheduleRunAfterStartupTask(queued_task.Pass());
2591 return;
2592 }
2593
2594 if (after_startup_tasks_.empty()) {
2595 BrowserThread::PostDelayedTask(
2596 BrowserThread::UI,
2597 FROM_HERE,
2598 base::Bind(&ChromeContentBrowserClient::OnAfterStartupTasksTimer,
2599 weak_this_),
2600 base::TimeDelta::FromSeconds(5));
2601 }
2602
2603 after_startup_tasks_.push_back(queued_task.release());
2604 }
2605
2606 void ChromeContentBrowserClient::OnAfterStartupTasksTimer() {
2607 if (!IsBrowserStartupComplete()) {
2608 BrowserThread::PostDelayedTask(
2609 BrowserThread::UI,
2610 FROM_HERE,
2611 base::Bind(&ChromeContentBrowserClient::OnAfterStartupTasksTimer,
2612 weak_this_),
2613 base::TimeDelta::FromSeconds(5));
2614 return;
2615 }
2616 for (AfterStartupTask* queued_task : after_startup_tasks_)
2617 ScheduleRunAfterStartupTask(make_scoped_ptr(queued_task));
2618 after_startup_tasks_.clear();
2619 }
2620
2621 void ChromeContentBrowserClient::ScheduleRunAfterStartupTask(
2622 scoped_ptr<AfterStartupTask> queued_task) {
2623 // Spread their execution over 30 seconds.
2624 const int kMinDelay = 0;
2625 const int kMaxDelay = 30;
jam 2015/03/26 15:38:58 What is the purpose of this random delay?
michaeln 2015/03/26 19:52:33 To avoid initiating a bunch of work at the same ti
michaeln 2015/03/26 20:17:52 I'll compute the delay more directly instead of ra
jam 2015/03/26 20:22:08 hmm, I understand the motivation but it's not clea
michaeln 2015/03/26 21:39:36 And if the list were very large, what would your g
jam 2015/03/27 15:50:29 ok that seems fair.
2626 scoped_refptr<base::TaskRunner> target_runner = queued_task->task_runner;
2627 tracked_objects::Location from_here = queued_task->from_here;
2628 target_runner->PostDelayedTask(
2629 from_here,
2630 base::Bind(&ChromeContentBrowserClient::RunAfterStartupTask,
2631 weak_this_, base::Passed(queued_task.Pass())),
2632 base::TimeDelta::FromSeconds(base::RandInt(kMinDelay, kMaxDelay)));
2633 }
2634
2635 void ChromeContentBrowserClient::RunAfterStartupTask(
2636 scoped_ptr<AfterStartupTask> queued_task) {
2637 // We're careful to delete the caller's |task| on the target runner's thread.
2638 DCHECK(queued_task->task_runner->RunsTasksOnCurrentThread());
2639 queued_task->task.Run();
2640 }
2641
2559 #if defined(ENABLE_WEBRTC) 2642 #if defined(ENABLE_WEBRTC)
2560 void ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch( 2643 void ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch(
2561 base::CommandLine* to_command_line, 2644 base::CommandLine* to_command_line,
2562 const base::CommandLine& from_command_line, 2645 const base::CommandLine& from_command_line,
2563 VersionInfo::Channel channel) { 2646 VersionInfo::Channel channel) {
2564 #if defined(OS_ANDROID) 2647 #if defined(OS_ANDROID)
2565 const VersionInfo::Channel kMaxDisableEncryptionChannel = 2648 const VersionInfo::Channel kMaxDisableEncryptionChannel =
2566 VersionInfo::CHANNEL_BETA; 2649 VersionInfo::CHANNEL_BETA;
2567 #else 2650 #else
2568 const VersionInfo::Channel kMaxDisableEncryptionChannel = 2651 const VersionInfo::Channel kMaxDisableEncryptionChannel =
2569 VersionInfo::CHANNEL_DEV; 2652 VersionInfo::CHANNEL_DEV;
2570 #endif 2653 #endif
2571 if (channel <= kMaxDisableEncryptionChannel) { 2654 if (channel <= kMaxDisableEncryptionChannel) {
2572 static const char* const kWebRtcDevSwitchNames[] = { 2655 static const char* const kWebRtcDevSwitchNames[] = {
2573 switches::kDisableWebRtcEncryption, 2656 switches::kDisableWebRtcEncryption,
2574 }; 2657 };
2575 to_command_line->CopySwitchesFrom(from_command_line, 2658 to_command_line->CopySwitchesFrom(from_command_line,
2576 kWebRtcDevSwitchNames, 2659 kWebRtcDevSwitchNames,
2577 arraysize(kWebRtcDevSwitchNames)); 2660 arraysize(kWebRtcDevSwitchNames));
2578 } 2661 }
2579 } 2662 }
2580 #endif // defined(ENABLE_WEBRTC) 2663 #endif // defined(ENABLE_WEBRTC)
2581 2664
2582 } // namespace chrome 2665 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698