OLD | NEW |
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/browser_process_impl.h" | 5 #include "chrome/browser/browser_process_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <map> | 9 #include <map> |
10 #include <utility> | 10 #include <utility> |
(...skipping 28 matching lines...) Expand all Loading... |
39 #include "chrome/browser/devtools/devtools_auto_opener.h" | 39 #include "chrome/browser/devtools/devtools_auto_opener.h" |
40 #include "chrome/browser/devtools/remote_debugging_server.h" | 40 #include "chrome/browser/devtools/remote_debugging_server.h" |
41 #include "chrome/browser/download/download_request_limiter.h" | 41 #include "chrome/browser/download/download_request_limiter.h" |
42 #include "chrome/browser/download/download_status_updater.h" | 42 #include "chrome/browser/download/download_status_updater.h" |
43 #include "chrome/browser/gpu/gl_string_manager.h" | 43 #include "chrome/browser/gpu/gl_string_manager.h" |
44 #include "chrome/browser/gpu/gpu_mode_manager.h" | 44 #include "chrome/browser/gpu/gpu_mode_manager.h" |
45 #include "chrome/browser/icon_manager.h" | 45 #include "chrome/browser/icon_manager.h" |
46 #include "chrome/browser/intranet_redirect_detector.h" | 46 #include "chrome/browser/intranet_redirect_detector.h" |
47 #include "chrome/browser/io_thread.h" | 47 #include "chrome/browser/io_thread.h" |
48 #include "chrome/browser/lifetime/application_lifetime.h" | 48 #include "chrome/browser/lifetime/application_lifetime.h" |
49 #include "chrome/browser/lifetime/keep_alive_registry.h" | |
50 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" | 49 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" |
51 #include "chrome/browser/metrics/chrome_metrics_services_manager_client.h" | 50 #include "chrome/browser/metrics/chrome_metrics_services_manager_client.h" |
52 #include "chrome/browser/metrics/thread_watcher.h" | 51 #include "chrome/browser/metrics/thread_watcher.h" |
53 #include "chrome/browser/net/chrome_net_log_helper.h" | 52 #include "chrome/browser/net/chrome_net_log_helper.h" |
54 #include "chrome/browser/net/crl_set_fetcher.h" | 53 #include "chrome/browser/net/crl_set_fetcher.h" |
55 #include "chrome/browser/notifications/notification_ui_manager.h" | 54 #include "chrome/browser/notifications/notification_ui_manager.h" |
56 #include "chrome/browser/plugins/chrome_plugin_service_filter.h" | 55 #include "chrome/browser/plugins/chrome_plugin_service_filter.h" |
57 #include "chrome/browser/plugins/plugin_finder.h" | 56 #include "chrome/browser/plugins/plugin_finder.h" |
58 #include "chrome/browser/prefs/browser_prefs.h" | 57 #include "chrome/browser/prefs/browser_prefs.h" |
59 #include "chrome/browser/prefs/chrome_pref_service_factory.h" | 58 #include "chrome/browser/prefs/chrome_pref_service_factory.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 | 114 |
116 #if defined(OS_WIN) | 115 #if defined(OS_WIN) |
117 #include "base/win/windows_version.h" | 116 #include "base/win/windows_version.h" |
118 #include "components/startup_metric_utils/common/pre_read_field_trial_utils_win.
h" | 117 #include "components/startup_metric_utils/common/pre_read_field_trial_utils_win.
h" |
119 #include "ui/views/focus/view_storage.h" | 118 #include "ui/views/focus/view_storage.h" |
120 #elif defined(OS_MACOSX) | 119 #elif defined(OS_MACOSX) |
121 #include "chrome/browser/chrome_browser_main_mac.h" | 120 #include "chrome/browser/chrome_browser_main_mac.h" |
122 #endif | 121 #endif |
123 | 122 |
124 #if !defined(OS_ANDROID) | 123 #if !defined(OS_ANDROID) |
| 124 #include "chrome/browser/lifetime/keep_alive_registry.h" |
125 #include "chrome/browser/ui/user_manager.h" | 125 #include "chrome/browser/ui/user_manager.h" |
126 #include "components/gcm_driver/gcm_client_factory.h" | 126 #include "components/gcm_driver/gcm_client_factory.h" |
127 #include "components/gcm_driver/gcm_desktop_utils.h" | 127 #include "components/gcm_driver/gcm_desktop_utils.h" |
128 #endif | 128 #endif |
129 | 129 |
130 #if BUILDFLAG(ENABLE_BACKGROUND) | 130 #if BUILDFLAG(ENABLE_BACKGROUND) |
131 #include "chrome/browser/background/background_mode_manager.h" | 131 #include "chrome/browser/background/background_mode_manager.h" |
132 #endif | 132 #endif |
133 | 133 |
134 #if defined(ENABLE_EXTENSIONS) | 134 #if defined(ENABLE_EXTENSIONS) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 BrowserProcessImpl::BrowserProcessImpl( | 183 BrowserProcessImpl::BrowserProcessImpl( |
184 base::SequencedTaskRunner* local_state_task_runner, | 184 base::SequencedTaskRunner* local_state_task_runner, |
185 const base::CommandLine& command_line) | 185 const base::CommandLine& command_line) |
186 : created_watchdog_thread_(false), | 186 : created_watchdog_thread_(false), |
187 created_browser_policy_connector_(false), | 187 created_browser_policy_connector_(false), |
188 created_profile_manager_(false), | 188 created_profile_manager_(false), |
189 created_local_state_(false), | 189 created_local_state_(false), |
190 created_icon_manager_(false), | 190 created_icon_manager_(false), |
191 created_notification_ui_manager_(false), | 191 created_notification_ui_manager_(false), |
192 created_safe_browsing_service_(false), | 192 created_safe_browsing_service_(false), |
193 module_ref_count_(0), | 193 shutting_down_(false), |
194 did_start_(false), | |
195 tearing_down_(false), | 194 tearing_down_(false), |
196 download_status_updater_(new DownloadStatusUpdater), | 195 download_status_updater_(new DownloadStatusUpdater), |
197 local_state_task_runner_(local_state_task_runner), | 196 local_state_task_runner_(local_state_task_runner), |
198 cached_default_web_client_state_(shell_integration::UNKNOWN_DEFAULT) { | 197 cached_default_web_client_state_(shell_integration::UNKNOWN_DEFAULT) { |
199 g_browser_process = this; | 198 g_browser_process = this; |
200 platform_part_.reset(new BrowserProcessPlatformPart()); | 199 platform_part_.reset(new BrowserProcessPlatformPart()); |
201 | 200 |
202 #if defined(ENABLE_PRINTING) | 201 #if defined(ENABLE_PRINTING) |
203 // Must be created after the NotificationService. | 202 // Must be created after the NotificationService. |
204 print_job_manager_.reset(new printing::PrintJobManager); | 203 print_job_manager_.reset(new printing::PrintJobManager); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 | 235 |
237 extensions_browser_client_.reset( | 236 extensions_browser_client_.reset( |
238 new extensions::ChromeExtensionsBrowserClient); | 237 new extensions::ChromeExtensionsBrowserClient); |
239 extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get()); | 238 extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get()); |
240 #endif | 239 #endif |
241 | 240 |
242 message_center::MessageCenter::Initialize(); | 241 message_center::MessageCenter::Initialize(); |
243 | 242 |
244 update_client::UpdateQueryParams::SetDelegate( | 243 update_client::UpdateQueryParams::SetDelegate( |
245 ChromeUpdateQueryParamsDelegate::GetInstance()); | 244 ChromeUpdateQueryParamsDelegate::GetInstance()); |
| 245 |
| 246 #if !defined(OS_ANDROID) |
| 247 KeepAliveRegistry::GetInstance()->AddObserver(this); |
| 248 #endif // !defined(OS_ANDROID) |
246 } | 249 } |
247 | 250 |
248 BrowserProcessImpl::~BrowserProcessImpl() { | 251 BrowserProcessImpl::~BrowserProcessImpl() { |
| 252 #if !defined(OS_ANDROID) |
| 253 KeepAliveRegistry::GetInstance()->RemoveObserver(this); |
| 254 #endif // !defined(OS_ANDROID) |
| 255 |
249 tracked_objects::ThreadData::EnsureCleanupWasCalled(4); | 256 tracked_objects::ThreadData::EnsureCleanupWasCalled(4); |
250 | 257 |
251 g_browser_process = NULL; | 258 g_browser_process = NULL; |
252 } | 259 } |
253 | 260 |
254 #if !defined(OS_ANDROID) | 261 #if !defined(OS_ANDROID) |
255 void BrowserProcessImpl::StartTearDown() { | 262 void BrowserProcessImpl::StartTearDown() { |
256 TRACE_EVENT0("shutdown", "BrowserProcessImpl::StartTearDown"); | 263 TRACE_EVENT0("shutdown", "BrowserProcessImpl::StartTearDown"); |
257 // TODO(crbug.com/560486): Fix the tests that make the check of | 264 // TODO(crbug.com/560486): Fix the tests that make the check of |
258 // |tearing_down_| necessary in IsShuttingDown(). | 265 // |tearing_down_| necessary in IsShuttingDown(). |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 // as io_thread_.global_ cleanup happens in CleanUp on the IO | 362 // as io_thread_.global_ cleanup happens in CleanUp on the IO |
356 // thread, i.e. as the thread exits its message loop. | 363 // thread, i.e. as the thread exits its message loop. |
357 // | 364 // |
358 // This is important also because in various places, the | 365 // This is important also because in various places, the |
359 // IOThread object being NULL is considered synonymous with the | 366 // IOThread object being NULL is considered synonymous with the |
360 // IO thread having stopped. | 367 // IO thread having stopped. |
361 io_thread_.reset(); | 368 io_thread_.reset(); |
362 } | 369 } |
363 #endif // !defined(OS_ANDROID) | 370 #endif // !defined(OS_ANDROID) |
364 | 371 |
365 unsigned int BrowserProcessImpl::AddRefModule() { | |
366 DCHECK(CalledOnValidThread()); | |
367 | |
368 // CHECK(!IsShuttingDown()); | |
369 if (IsShuttingDown()) { | |
370 // Copy the stacktrace which released the final reference onto our stack so | |
371 // it will be available in the crash report for inspection. | |
372 base::debug::StackTrace callstack = release_last_reference_callstack_; | |
373 base::debug::Alias(&callstack); | |
374 CHECK(false); | |
375 } | |
376 | |
377 did_start_ = true; | |
378 module_ref_count_++; | |
379 return module_ref_count_; | |
380 } | |
381 | |
382 unsigned int BrowserProcessImpl::ReleaseModule() { | |
383 DCHECK(CalledOnValidThread()); | |
384 DCHECK_NE(0u, module_ref_count_); | |
385 module_ref_count_--; | |
386 if (0 == module_ref_count_) { | |
387 release_last_reference_callstack_ = base::debug::StackTrace(); | |
388 | |
389 #if defined(ENABLE_PRINTING) | |
390 // Wait for the pending print jobs to finish. Don't do this later, since | |
391 // this might cause a nested message loop to run, and we don't want pending | |
392 // tasks to run once teardown has started. | |
393 print_job_manager_->Shutdown(); | |
394 #endif | |
395 | |
396 #if defined(LEAK_SANITIZER) | |
397 // Check for memory leaks now, before we start shutting down threads. Doing | |
398 // this early means we won't report any shutdown-only leaks (as they have | |
399 // not yet happened at this point). | |
400 // If leaks are found, this will make the process exit immediately. | |
401 __lsan_do_leak_check(); | |
402 #endif | |
403 | |
404 CHECK(base::MessageLoop::current()->is_running()); | |
405 | |
406 #if defined(OS_MACOSX) | |
407 base::MessageLoop::current()->PostTask( | |
408 FROM_HERE, | |
409 base::Bind(ChromeBrowserMainPartsMac::DidEndMainMessageLoop)); | |
410 #endif | |
411 base::MessageLoop::current()->QuitWhenIdle(); | |
412 } | |
413 return module_ref_count_; | |
414 } | |
415 | |
416 namespace { | 372 namespace { |
417 | 373 |
418 // Used at the end of session to block the UI thread for completion of sentinel | 374 // Used at the end of session to block the UI thread for completion of sentinel |
419 // tasks on the set of threads used to persist profile data and local state. | 375 // tasks on the set of threads used to persist profile data and local state. |
420 // This is done to ensure that the data has been persisted to disk before | 376 // This is done to ensure that the data has been persisted to disk before |
421 // continuing. | 377 // continuing. |
422 class RundownTaskCounter : | 378 class RundownTaskCounter : |
423 public base::RefCountedThreadSafe<RundownTaskCounter> { | 379 public base::RefCountedThreadSafe<RundownTaskCounter> { |
424 public: | 380 public: |
425 RundownTaskCounter(); | 381 RundownTaskCounter(); |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 // is started with several profiles or existing browser process is reused. | 638 // is started with several profiles or existing browser process is reused. |
683 if (!devtools_auto_opener_.get()) | 639 if (!devtools_auto_opener_.get()) |
684 devtools_auto_opener_.reset(new DevToolsAutoOpener()); | 640 devtools_auto_opener_.reset(new DevToolsAutoOpener()); |
685 #endif | 641 #endif |
686 } | 642 } |
687 | 643 |
688 bool BrowserProcessImpl::IsShuttingDown() { | 644 bool BrowserProcessImpl::IsShuttingDown() { |
689 DCHECK(CalledOnValidThread()); | 645 DCHECK(CalledOnValidThread()); |
690 // TODO(crbug.com/560486): Fix the tests that make the check of | 646 // TODO(crbug.com/560486): Fix the tests that make the check of |
691 // |tearing_down_| necessary here. | 647 // |tearing_down_| necessary here. |
692 return (did_start_ && 0 == module_ref_count_) || tearing_down_; | 648 return shutting_down_ || tearing_down_; |
693 } | 649 } |
694 | 650 |
695 printing::PrintJobManager* BrowserProcessImpl::print_job_manager() { | 651 printing::PrintJobManager* BrowserProcessImpl::print_job_manager() { |
696 DCHECK(CalledOnValidThread()); | 652 DCHECK(CalledOnValidThread()); |
697 return print_job_manager_.get(); | 653 return print_job_manager_.get(); |
698 } | 654 } |
699 | 655 |
700 printing::PrintPreviewDialogController* | 656 printing::PrintPreviewDialogController* |
701 BrowserProcessImpl::print_preview_dialog_controller() { | 657 BrowserProcessImpl::print_preview_dialog_controller() { |
702 #if defined(ENABLE_PRINT_PREVIEW) | 658 #if defined(ENABLE_PRINT_PREVIEW) |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
969 ResourceDispatcherHost::Get()->SetDelegate( | 925 ResourceDispatcherHost::Get()->SetDelegate( |
970 resource_dispatcher_host_delegate_.get()); | 926 resource_dispatcher_host_delegate_.get()); |
971 | 927 |
972 pref_change_registrar_.Add( | 928 pref_change_registrar_.Add( |
973 prefs::kAllowCrossOriginAuthPrompt, | 929 prefs::kAllowCrossOriginAuthPrompt, |
974 base::Bind(&BrowserProcessImpl::ApplyAllowCrossOriginAuthPromptPolicy, | 930 base::Bind(&BrowserProcessImpl::ApplyAllowCrossOriginAuthPromptPolicy, |
975 base::Unretained(this))); | 931 base::Unretained(this))); |
976 ApplyAllowCrossOriginAuthPromptPolicy(); | 932 ApplyAllowCrossOriginAuthPromptPolicy(); |
977 } | 933 } |
978 | 934 |
| 935 void BrowserProcessImpl::OnKeepAliveStateChanged(bool is_keeping_alive) { |
| 936 if (is_keeping_alive) |
| 937 Pin(); |
| 938 else |
| 939 Unpin(); |
| 940 } |
| 941 |
| 942 void BrowserProcessImpl::OnKeepAliveRestartStateChanged(bool can_restart){ |
| 943 } |
| 944 |
979 void BrowserProcessImpl::CreateWatchdogThread() { | 945 void BrowserProcessImpl::CreateWatchdogThread() { |
980 DCHECK(!created_watchdog_thread_ && watchdog_thread_.get() == NULL); | 946 DCHECK(!created_watchdog_thread_ && watchdog_thread_.get() == NULL); |
981 created_watchdog_thread_ = true; | 947 created_watchdog_thread_ = true; |
982 | 948 |
983 scoped_ptr<WatchDogThread> thread(new WatchDogThread()); | 949 scoped_ptr<WatchDogThread> thread(new WatchDogThread()); |
984 base::Thread::Options options; | 950 base::Thread::Options options; |
985 options.timer_slack = base::TIMER_SLACK_MAXIMUM; | 951 options.timer_slack = base::TIMER_SLACK_MAXIMUM; |
986 if (!thread->StartWithOptions(options)) | 952 if (!thread->StartWithOptions(options)) |
987 return; | 953 return; |
988 watchdog_thread_.swap(thread); | 954 watchdog_thread_.swap(thread); |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1230 } | 1196 } |
1231 | 1197 |
1232 void BrowserProcessImpl::CacheDefaultWebClientState() { | 1198 void BrowserProcessImpl::CacheDefaultWebClientState() { |
1233 #if defined(OS_CHROMEOS) | 1199 #if defined(OS_CHROMEOS) |
1234 cached_default_web_client_state_ = shell_integration::IS_DEFAULT; | 1200 cached_default_web_client_state_ = shell_integration::IS_DEFAULT; |
1235 #elif !defined(OS_ANDROID) | 1201 #elif !defined(OS_ANDROID) |
1236 cached_default_web_client_state_ = shell_integration::GetDefaultBrowser(); | 1202 cached_default_web_client_state_ = shell_integration::GetDefaultBrowser(); |
1237 #endif | 1203 #endif |
1238 } | 1204 } |
1239 | 1205 |
| 1206 void BrowserProcessImpl::Pin() { |
| 1207 DCHECK(CalledOnValidThread()); |
| 1208 |
| 1209 // CHECK(!IsShuttingDown()); |
| 1210 if (IsShuttingDown()) { |
| 1211 // Copy the stacktrace which released the final reference onto our stack so |
| 1212 // it will be available in the crash report for inspection. |
| 1213 base::debug::StackTrace callstack = release_last_reference_callstack_; |
| 1214 base::debug::Alias(&callstack); |
| 1215 CHECK(false); |
| 1216 } |
| 1217 } |
| 1218 |
| 1219 void BrowserProcessImpl::Unpin() { |
| 1220 DCHECK(CalledOnValidThread()); |
| 1221 release_last_reference_callstack_ = base::debug::StackTrace(); |
| 1222 |
| 1223 shutting_down_ = true; |
| 1224 #if defined(ENABLE_PRINTING) |
| 1225 // Wait for the pending print jobs to finish. Don't do this later, since |
| 1226 // this might cause a nested message loop to run, and we don't want pending |
| 1227 // tasks to run once teardown has started. |
| 1228 print_job_manager_->Shutdown(); |
| 1229 #endif |
| 1230 |
| 1231 #if defined(LEAK_SANITIZER) |
| 1232 // Check for memory leaks now, before we start shutting down threads. Doing |
| 1233 // this early means we won't report any shutdown-only leaks (as they have |
| 1234 // not yet happened at this point). |
| 1235 // If leaks are found, this will make the process exit immediately. |
| 1236 __lsan_do_leak_check(); |
| 1237 #endif |
| 1238 |
| 1239 CHECK(base::MessageLoop::current()->is_running()); |
| 1240 |
| 1241 #if defined(OS_MACOSX) |
| 1242 base::MessageLoop::current()->PostTask( |
| 1243 FROM_HERE, base::Bind(ChromeBrowserMainPartsMac::DidEndMainMessageLoop)); |
| 1244 #endif |
| 1245 base::MessageLoop::current()->QuitWhenIdle(); |
| 1246 |
| 1247 #if !defined(OS_ANDROID) |
| 1248 chrome::ShutdownIfNeeded(); |
| 1249 #endif // !defined(OS_ANDROID) |
| 1250 } |
| 1251 |
1240 // Mac is currently not supported. | 1252 // Mac is currently not supported. |
1241 #if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS) | 1253 #if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS) |
1242 | 1254 |
1243 bool BrowserProcessImpl::CanAutorestartForUpdate() const { | 1255 bool BrowserProcessImpl::CanAutorestartForUpdate() const { |
1244 // Check if browser is in the background and if it needs to be restarted to | 1256 // Check if browser is in the background and if it needs to be restarted to |
1245 // apply a pending update. | 1257 // apply a pending update. |
1246 return chrome::GetTotalBrowserCount() == 0 && | 1258 return chrome::GetTotalBrowserCount() == 0 && |
1247 KeepAliveRegistry::GetInstance()->IsKeepingAlive() && | 1259 KeepAliveRegistry::GetInstance()->IsKeepingAlive() && |
1248 upgrade_util::IsUpdatePendingRestart(); | 1260 upgrade_util::IsUpdatePendingRestart(); |
1249 } | 1261 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1295 } | 1307 } |
1296 | 1308 |
1297 void BrowserProcessImpl::OnAutoupdateTimer() { | 1309 void BrowserProcessImpl::OnAutoupdateTimer() { |
1298 if (CanAutorestartForUpdate()) { | 1310 if (CanAutorestartForUpdate()) { |
1299 DLOG(WARNING) << "Detected update. Restarting browser."; | 1311 DLOG(WARNING) << "Detected update. Restarting browser."; |
1300 RestartBackgroundInstance(); | 1312 RestartBackgroundInstance(); |
1301 } | 1313 } |
1302 } | 1314 } |
1303 | 1315 |
1304 #endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS) | 1316 #endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS) |
OLD | NEW |