blob: 61dacf389289e8022d2781f786a337e334f9489b [file] [log] [blame]
[email protected]f2a893c2011-01-05 09:38:071// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/browser_process_impl.h"
6
[email protected]3cdacd42010-04-30 18:55:537#include <map>
8
initial.commit09911bf2008-07-26 23:55:299#include "base/command_line.h"
[email protected]6641bf662009-08-21 00:34:0910#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/path_service.h"
[email protected]985655a2011-02-23 09:54:2512#include "base/synchronization/waitable_event.h"
[email protected]c6032e82010-09-13 20:06:0513#include "base/task.h"
[email protected]34b99632011-01-01 01:01:0614#include "base/threading/thread.h"
15#include "base/threading/thread_restrictions.h"
[email protected]e13ad79b2010-07-22 21:36:5016#include "chrome/browser/automation/automation_provider_list.h"
[email protected]bd48c2b02010-04-09 20:32:4217#include "chrome/browser/browser_list.h"
[email protected]b443cb042009-12-15 22:05:0918#include "chrome/browser/browser_main.h"
[email protected]0ac83682010-01-22 17:46:2719#include "chrome/browser/browser_process_sub_thread.h"
[email protected]ac262c9f2008-10-19 17:45:2120#include "chrome/browser/browser_trial.h"
[email protected]9678cb42011-01-31 18:49:4221#include "chrome/browser/debugger/browser_list_tabcontents_provider.h"
[email protected]5613126e2011-01-31 15:27:5522#include "chrome/browser/debugger/devtools_http_protocol_handler.h"
[email protected]40ecc902009-03-16 13:42:4723#include "chrome/browser/debugger/devtools_manager.h"
[email protected]5613126e2011-01-31 15:27:5524#include "chrome/browser/debugger/devtools_protocol_handler.h"
[email protected]6c69796d2010-07-16 21:41:1625#include "chrome/browser/download/download_file_manager.h"
[email protected]5ba0a2c2009-02-19 01:19:3426#include "chrome/browser/download/save_file_manager.h"
[email protected]3ce02412011-03-01 12:01:1527#include "chrome/browser/extensions/extension_event_router_forwarder.h"
[email protected]820735792010-07-29 23:40:0128#include "chrome/browser/first_run/first_run.h"
[email protected]f7578f52010-08-30 22:22:4929#include "chrome/browser/google/google_url_tracker.h"
[email protected]dcefa302009-05-20 00:24:3930#include "chrome/browser/icon_manager.h"
[email protected]c4ff4952010-01-08 19:12:4731#include "chrome/browser/intranet_redirect_detector.h"
[email protected]0ac83682010-01-22 17:46:2732#include "chrome/browser/io_thread.h"
[email protected]dc6f4962009-02-13 01:25:5033#include "chrome/browser/metrics/metrics_service.h"
[email protected]0b565182011-03-02 18:11:1534#include "chrome/browser/metrics/thread_watcher.h"
[email protected]b2fcd0e2010-12-01 15:19:4035#include "chrome/browser/net/chrome_net_log.h"
[email protected]3530cd92010-06-27 06:22:0136#include "chrome/browser/net/predictor_api.h"
[email protected]d393a0fd2009-05-13 23:32:0137#include "chrome/browser/net/sdch_dictionary_fetcher.h"
[email protected]29672ab2009-10-30 03:44:0338#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]6618d1d2010-12-15 21:18:4739#include "chrome/browser/plugin_data_remover.h"
[email protected]492d2142010-09-10 13:55:1840#include "chrome/browser/plugin_updater.h"
[email protected]985655a2011-02-23 09:54:2541#include "chrome/browser/policy/browser_policy_connector.h"
[email protected]37858e52010-08-26 00:22:0242#include "chrome/browser/prefs/pref_service.h"
[email protected]e06131d2010-02-10 18:40:3343#include "chrome/browser/printing/print_job_manager.h"
[email protected]dbeebd52010-11-16 20:34:1644#include "chrome/browser/printing/print_preview_tab_controller.h"
[email protected]8ecad5e2010-12-02 21:18:3345#include "chrome/browser/profiles/profile_manager.h"
[email protected]a7a5e992010-12-09 23:39:5146#include "chrome/browser/safe_browsing/client_side_detection_service.h"
[email protected]81218f42009-02-05 18:48:0847#include "chrome/browser/safe_browsing/safe_browsing_service.h"
[email protected]e450fa62011-02-01 12:52:5648#include "chrome/browser/shell_integration.h"
[email protected]8b8e7c92010-08-19 18:05:5649#include "chrome/browser/sidebar/sidebar_manager.h"
[email protected]0b4d3382010-07-14 16:13:0450#include "chrome/browser/tab_closeable_state_watcher.h"
[email protected]4ef795df2010-02-03 02:35:0851#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2952#include "chrome/common/chrome_paths.h"
53#include "chrome/common/chrome_switches.h"
[email protected]f1b6de22010-03-06 12:13:4754#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]985655a2011-02-23 09:54:2555#include "chrome/common/extensions/extension_resource.h"
[email protected]ea587b02010-05-21 15:01:3556#include "chrome/common/json_pref_store.h"
initial.commit09911bf2008-07-26 23:55:2957#include "chrome/common/pref_names.h"
[email protected]5c08f222010-09-22 09:37:2158#include "chrome/common/switch_utils.h"
[email protected]985655a2011-02-23 09:54:2559#include "chrome/common/url_constants.h"
[email protected]bd48c2b02010-04-09 20:32:4260#include "chrome/installer/util/google_update_constants.h"
[email protected]a01efd22011-03-01 00:38:3261#include "content/browser/browser_child_process_host.h"
62#include "content/browser/browser_thread.h"
63#include "content/browser/plugin_service.h"
64#include "content/browser/renderer_host/render_process_host.h"
65#include "content/browser/renderer_host/resource_dispatcher_host.h"
[email protected]f20d7332011-03-08 21:11:5366#include "content/common/notification_service.h"
[email protected]d55aaa132009-09-28 21:08:0467#include "ipc/ipc_logging.h"
[email protected]2dfeaf92011-01-10 21:08:2168#include "ui/base/clipboard/clipboard.h"
[email protected]c051a1b2011-01-21 23:30:1769#include "ui/base/l10n/l10n_util.h"
[email protected]4ef795df2010-02-03 02:35:0870#include "webkit/database/database_tracker.h"
[email protected]b112a4c2009-02-01 20:24:0171
72#if defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0573#include "views/focus/view_storage.h"
[email protected]86230b92009-11-23 20:38:3874#endif
75
[email protected]d55aaa132009-09-28 21:08:0476#if defined(IPC_MESSAGE_LOG_ENABLED)
[email protected]ff47b2962011-03-07 23:51:4977#include "content/common/child_process_messages.h"
[email protected]d55aaa132009-09-28 21:08:0478#endif
79
[email protected]3cdacd42010-04-30 18:55:5380#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
81// How often to check if the persistent instance of Chrome needs to restart
82// to install an update.
83static const int kUpdateCheckIntervalHours = 6;
84#endif
85
[email protected]c6032e82010-09-13 20:06:0586#if defined(USE_X11)
87// How long to wait for the File thread to complete during EndSession, on
88// Linux. We have a timeout here because we're unable to run the UI messageloop
89// and there's some deadlock risk. Our only option is to exit anyway.
90static const int kEndSessionTimeoutSeconds = 10;
91#endif
92
[email protected]bb975362009-01-21 01:00:2293BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
initial.commit09911bf2008-07-26 23:55:2994 : created_resource_dispatcher_host_(false),
95 created_metrics_service_(false),
96 created_io_thread_(false),
97 created_file_thread_(false),
98 created_db_thread_(false),
[email protected]914511712009-11-23 19:42:3399 created_process_launcher_thread_(false),
[email protected]875ee822010-05-18 20:58:01100 created_cache_thread_(false),
[email protected]0b565182011-03-02 18:11:15101 created_watchdog_thread_(false),
initial.commit09911bf2008-07-26 23:55:29102 created_profile_manager_(false),
103 created_local_state_(false),
[email protected]b112a4c2009-02-01 20:24:01104 created_icon_manager_(false),
[email protected]40ecc902009-03-16 13:42:47105 created_devtools_manager_(false),
[email protected]8b8e7c92010-08-19 18:05:56106 created_sidebar_manager_(false),
[email protected]985655a2011-02-23 09:54:25107 created_browser_policy_connector_(false),
[email protected]29672ab2009-10-30 03:44:03108 created_notification_ui_manager_(false),
[email protected]a7a5e992010-12-09 23:39:51109 created_safe_browsing_detection_service_(false),
initial.commit09911bf2008-07-26 23:55:29110 module_ref_count_(0),
[email protected]afd20c022010-06-10 00:48:20111 did_start_(false),
[email protected]1b2db1a2008-08-08 17:46:13112 checked_for_new_frames_(false),
[email protected]6641bf662009-08-21 00:34:09113 using_new_frames_(false),
114 have_inspector_files_(true) {
initial.commit09911bf2008-07-26 23:55:29115 g_browser_process = this;
[email protected]2dfeaf92011-01-10 21:08:21116 clipboard_.reset(new ui::Clipboard);
initial.commit09911bf2008-07-26 23:55:29117 main_notification_service_.reset(new NotificationService);
118
[email protected]6618d1d2010-12-15 21:18:47119 notification_registrar_.Add(this,
120 NotificationType::APP_TERMINATING,
121 NotificationService::AllSources());
122
initial.commit09911bf2008-07-26 23:55:29123 // Must be created after the NotificationService.
124 print_job_manager_.reset(new printing::PrintJobManager);
125
[email protected]b797e152009-01-23 16:06:14126 shutdown_event_.reset(new base::WaitableEvent(true, false));
[email protected]b2fcd0e2010-12-01 15:19:40127
128 net_log_.reset(new ChromeNetLog);
[email protected]3ce02412011-03-01 12:01:15129
130 extension_event_router_forwarder_ = new ExtensionEventRouterForwarder;
initial.commit09911bf2008-07-26 23:55:29131}
132
133BrowserProcessImpl::~BrowserProcessImpl() {
[email protected]4ef795df2010-02-03 02:35:08134 FilePath profile_path;
135 bool clear_local_state_on_exit;
136
137 // Store the profile path for clearing local state data on exit.
138 clear_local_state_on_exit = ShouldClearLocalState(&profile_path);
139
initial.commit09911bf2008-07-26 23:55:29140 // Delete the AutomationProviderList before NotificationService,
141 // since it may try to unregister notifications
142 // Both NotificationService and AutomationProvider are singleton instances in
143 // the BrowserProcess. Since AutomationProvider may have some active
144 // notification observers, it is essential that it gets destroyed before the
145 // NotificationService. NotificationService won't be destroyed until after
146 // this destructor is run.
147 automation_provider_list_.reset();
148
[email protected]d393a0fd2009-05-13 23:32:01149 // We need to shutdown the SdchDictionaryFetcher as it regularly holds
150 // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do
151 // a PostDelayedTask onto the IO thread. This shutdown call will both discard
152 // any pending URLFetchers, and avoid creating any more.
153 SdchDictionaryFetcher::Shutdown();
154
[email protected]51ac040762011-01-20 02:01:04155 // We need to destroy the MetricsService, GoogleURLTracker,
156 // IntranetRedirectDetector, and SafeBrowsing ClientSideDetectionService
157 // before the io_thread_ gets destroyed, since their destructors can call the
158 // URLFetcher destructor, which does a PostDelayedTask operation on the IO
159 // thread. (The IO thread will handle that URLFetcher operation before going
160 // away.)
initial.commit09911bf2008-07-26 23:55:29161 metrics_service_.reset();
162 google_url_tracker_.reset();
[email protected]c4ff4952010-01-08 19:12:47163 intranet_redirect_detector_.reset();
[email protected]51ac040762011-01-20 02:01:04164 safe_browsing_detection_service_.reset();
initial.commit09911bf2008-07-26 23:55:29165
[email protected]d8a899c2010-09-21 04:50:33166 // Need to clear the desktop notification balloons before the io_thread_ and
167 // before the profiles, since if there are any still showing we will access
168 // those things during teardown.
169 notification_ui_manager_.reset();
170
initial.commit09911bf2008-07-26 23:55:29171 // Need to clear profiles (download managers) before the io_thread_.
172 profile_manager_.reset();
173
174 // Debugger must be cleaned up before IO thread and NotificationService.
[email protected]5613126e2011-01-31 15:27:55175 if (devtools_http_handler_.get()) {
176 devtools_http_handler_->Stop();
177 devtools_http_handler_ = NULL;
178 }
179 if (devtools_legacy_handler_.get()) {
180 devtools_legacy_handler_->Stop();
181 devtools_legacy_handler_ = NULL;
182 }
initial.commit09911bf2008-07-26 23:55:29183
184 if (resource_dispatcher_host_.get()) {
185 // Need to tell Safe Browsing Service that the IO thread is going away
186 // since it cached a pointer to it.
187 if (resource_dispatcher_host()->safe_browsing_service())
188 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
189
190 // Cancel pending requests and prevent new requests.
191 resource_dispatcher_host()->Shutdown();
192 }
193
[email protected]985655a2011-02-23 09:54:25194 // The policy providers managed by |browser_policy_connector_| need to shut
195 // down while the IO and FILE threads are still alive.
196 browser_policy_connector_.reset();
[email protected]f2a893c2011-01-05 09:38:07197
[email protected]753efc42010-03-09 19:52:16198#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06199 // The IO thread must outlive the BACKGROUND_X11 thread.
200 background_x11_thread_.reset();
201#endif
202
[email protected]6618d1d2010-12-15 21:18:47203 // Wait for removing plugin data to finish before shutting down the IO thread.
204 WaitForPluginDataRemoverToFinish();
205
initial.commit09911bf2008-07-26 23:55:29206 // Need to stop io_thread_ before resource_dispatcher_host_, since
207 // io_thread_ may still deref ResourceDispatcherHost and handle resource
208 // request before going away.
[email protected]0ac83682010-01-22 17:46:27209 io_thread_.reset();
initial.commit09911bf2008-07-26 23:55:29210
[email protected]875ee822010-05-18 20:58:01211 // The IO thread was the only user of this thread.
212 cache_thread_.reset();
213
[email protected]914511712009-11-23 19:42:33214 // Stop the process launcher thread after the IO thread, in case the IO thread
215 // posted a task to terminate a process on the process launcher thread.
216 process_launcher_thread_.reset();
217
initial.commit09911bf2008-07-26 23:55:29218 // Clean up state that lives on the file_thread_ before it goes away.
219 if (resource_dispatcher_host_.get()) {
220 resource_dispatcher_host()->download_file_manager()->Shutdown();
221 resource_dispatcher_host()->save_file_manager()->Shutdown();
222 }
223
224 // Need to stop the file_thread_ here to force it to process messages in its
225 // message loop from the previous call to shutdown the DownloadFileManager,
226 // SaveFileManager and SessionService.
227 file_thread_.reset();
228
229 // With the file_thread_ flushed, we can release any icon resources.
230 icon_manager_.reset();
231
232 // Need to destroy ResourceDispatcherHost before PluginService and
[email protected]49f28bc72010-02-04 00:10:01233 // SafeBrowsingService, since it caches a pointer to it. This also
234 // causes the webkit thread to terminate.
initial.commit09911bf2008-07-26 23:55:29235 resource_dispatcher_host_.reset();
236
237 // Wait for the pending print jobs to finish.
238 print_job_manager_->OnQuit();
239 print_job_manager_.reset();
240
[email protected]0b4d3382010-07-14 16:13:04241 // Destroy TabCloseableStateWatcher before NotificationService since the
242 // former registers for notifications.
243 tab_closeable_state_watcher_.reset();
244
initial.commit09911bf2008-07-26 23:55:29245 // Now OK to destroy NotificationService.
246 main_notification_service_.reset();
247
[email protected]49f28bc72010-02-04 00:10:01248 // Prior to clearing local state, we want to complete tasks pending
249 // on the db thread too.
250 db_thread_.reset();
251
[email protected]0b565182011-03-02 18:11:15252 // Stop the watchdog thread after stopping other threads.
253 watchdog_thread_.reset();
254
[email protected]49f28bc72010-02-04 00:10:01255 // At this point, no render process exist and the file, io, db, and
256 // webkit threads in this process have all terminated, so it's safe
257 // to access local state data such as cookies, database, or local storage.
258 if (clear_local_state_on_exit)
259 ClearLocalState(profile_path);
260
initial.commit09911bf2008-07-26 23:55:29261 g_browser_process = NULL;
262}
263
[email protected]c6032e82010-09-13 20:06:05264#if defined(OS_WIN)
[email protected]295039bd2008-08-15 04:32:57265// Send a QuitTask to the given MessageLoop.
266static void PostQuit(MessageLoop* message_loop) {
267 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
268}
[email protected]c6032e82010-09-13 20:06:05269#elif defined(USE_X11)
270static void Signal(base::WaitableEvent* event) {
271 event->Signal();
272}
273#endif
initial.commit09911bf2008-07-26 23:55:29274
[email protected]b443cb042009-12-15 22:05:09275unsigned int BrowserProcessImpl::AddRefModule() {
276 DCHECK(CalledOnValidThread());
[email protected]afd20c022010-06-10 00:48:20277 did_start_ = true;
[email protected]b443cb042009-12-15 22:05:09278 module_ref_count_++;
279 return module_ref_count_;
280}
281
282unsigned int BrowserProcessImpl::ReleaseModule() {
283 DCHECK(CalledOnValidThread());
[email protected]760d970a2010-05-18 00:39:18284 DCHECK_NE(0u, module_ref_count_);
[email protected]b443cb042009-12-15 22:05:09285 module_ref_count_--;
286 if (0 == module_ref_count_) {
[email protected]7d038c32010-12-14 00:40:00287 // Allow UI and IO threads to do blocking IO on shutdown, since we do a lot
288 // of it on shutdown for valid reasons.
289 base::ThreadRestrictions::SetIOAllowed(true);
290 io_thread()->message_loop()->PostTask(
291 FROM_HERE,
292 NewRunnableFunction(&base::ThreadRestrictions::SetIOAllowed, true));
[email protected]b443cb042009-12-15 22:05:09293 MessageLoop::current()->PostTask(
[email protected]34f73fb2010-03-24 20:50:34294 FROM_HERE, NewRunnableFunction(DidEndMainMessageLoop));
[email protected]b443cb042009-12-15 22:05:09295 MessageLoop::current()->Quit();
296 }
297 return module_ref_count_;
298}
299
initial.commit09911bf2008-07-26 23:55:29300void BrowserProcessImpl::EndSession() {
[email protected]c6032e82010-09-13 20:06:05301#if defined(OS_WIN) || defined(USE_X11)
[email protected]d65cab7a2008-08-12 01:25:41302 // Notify we are going away.
[email protected]c6032e82010-09-13 20:06:05303 shutdown_event_->Signal();
[email protected]b112a4c2009-02-01 20:24:01304#endif
[email protected]d65cab7a2008-08-12 01:25:41305
initial.commit09911bf2008-07-26 23:55:29306 // Mark all the profiles as clean.
307 ProfileManager* pm = profile_manager();
308 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
309 (*i)->MarkAsCleanShutdown();
310
311 // Tell the metrics service it was cleanly shutdown.
312 MetricsService* metrics = g_browser_process->metrics_service();
313 if (metrics && local_state()) {
314 metrics->RecordCleanShutdown();
315
316 metrics->RecordStartOfSessionEnd();
317
318 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36319 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29320 }
321
322 // We must write that the profile and metrics service shutdown cleanly,
323 // otherwise on startup we'll think we crashed. So we block until done and
324 // then proceed with normal shutdown.
[email protected]c6032e82010-09-13 20:06:05325#if defined(USE_X11)
326 // Can't run a local loop on linux. Instead create a waitable event.
327 base::WaitableEvent done_writing(false, false);
[email protected]d04e7662010-10-10 22:24:48328 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]c6032e82010-09-13 20:06:05329 NewRunnableFunction(Signal, &done_writing));
330 done_writing.TimedWait(
331 base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds));
332#elif defined(OS_WIN)
[email protected]d04e7662010-10-10 22:24:48333 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57334 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29335 MessageLoop::current()->Run();
[email protected]c6032e82010-09-13 20:06:05336#else
337 NOTIMPLEMENTED();
338#endif
initial.commit09911bf2008-07-26 23:55:29339}
340
[email protected]e13ad79b2010-07-22 21:36:50341ResourceDispatcherHost* BrowserProcessImpl::resource_dispatcher_host() {
342 DCHECK(CalledOnValidThread());
343 if (!created_resource_dispatcher_host_)
344 CreateResourceDispatcherHost();
345 return resource_dispatcher_host_.get();
346}
347
348MetricsService* BrowserProcessImpl::metrics_service() {
349 DCHECK(CalledOnValidThread());
350 if (!created_metrics_service_)
351 CreateMetricsService();
352 return metrics_service_.get();
353}
354
355IOThread* BrowserProcessImpl::io_thread() {
356 DCHECK(CalledOnValidThread());
357 if (!created_io_thread_)
358 CreateIOThread();
359 return io_thread_.get();
360}
361
362base::Thread* BrowserProcessImpl::file_thread() {
363 DCHECK(CalledOnValidThread());
364 if (!created_file_thread_)
365 CreateFileThread();
366 return file_thread_.get();
367}
368
369base::Thread* BrowserProcessImpl::db_thread() {
370 DCHECK(CalledOnValidThread());
371 if (!created_db_thread_)
372 CreateDBThread();
373 return db_thread_.get();
374}
375
376base::Thread* BrowserProcessImpl::process_launcher_thread() {
377 DCHECK(CalledOnValidThread());
378 if (!created_process_launcher_thread_)
379 CreateProcessLauncherThread();
380 return process_launcher_thread_.get();
381}
382
383base::Thread* BrowserProcessImpl::cache_thread() {
384 DCHECK(CalledOnValidThread());
385 if (!created_cache_thread_)
386 CreateCacheThread();
387 return cache_thread_.get();
388}
389
390#if defined(USE_X11)
391base::Thread* BrowserProcessImpl::background_x11_thread() {
392 DCHECK(CalledOnValidThread());
393 // The BACKGROUND_X11 thread is created when the IO thread is created.
394 if (!created_io_thread_)
395 CreateIOThread();
396 return background_x11_thread_.get();
397}
398#endif
399
[email protected]0b565182011-03-02 18:11:15400WatchDogThread* BrowserProcessImpl::watchdog_thread() {
401 DCHECK(CalledOnValidThread());
402 if (!created_watchdog_thread_)
403 CreateWatchdogThread();
404 DCHECK(watchdog_thread_.get() != NULL);
405 return watchdog_thread_.get();
406}
407
[email protected]e13ad79b2010-07-22 21:36:50408ProfileManager* BrowserProcessImpl::profile_manager() {
409 DCHECK(CalledOnValidThread());
410 if (!created_profile_manager_)
411 CreateProfileManager();
412 return profile_manager_.get();
413}
414
415PrefService* BrowserProcessImpl::local_state() {
416 DCHECK(CalledOnValidThread());
417 if (!created_local_state_)
418 CreateLocalState();
419 return local_state_.get();
420}
421
422DevToolsManager* BrowserProcessImpl::devtools_manager() {
423 DCHECK(CalledOnValidThread());
424 if (!created_devtools_manager_)
425 CreateDevToolsManager();
426 return devtools_manager_.get();
427}
428
[email protected]8b8e7c92010-08-19 18:05:56429SidebarManager* BrowserProcessImpl::sidebar_manager() {
430 DCHECK(CalledOnValidThread());
431 if (!created_sidebar_manager_)
432 CreateSidebarManager();
433 return sidebar_manager_.get();
434}
435
[email protected]2dfeaf92011-01-10 21:08:21436ui::Clipboard* BrowserProcessImpl::clipboard() {
[email protected]e13ad79b2010-07-22 21:36:50437 DCHECK(CalledOnValidThread());
438 return clipboard_.get();
439}
440
[email protected]3ce02412011-03-01 12:01:15441ExtensionEventRouterForwarder*
442BrowserProcessImpl::extension_event_router_forwarder() {
443 return extension_event_router_forwarder_.get();
444}
445
[email protected]e13ad79b2010-07-22 21:36:50446NotificationUIManager* BrowserProcessImpl::notification_ui_manager() {
447 DCHECK(CalledOnValidThread());
448 if (!created_notification_ui_manager_)
449 CreateNotificationUIManager();
450 return notification_ui_manager_.get();
451}
452
[email protected]985655a2011-02-23 09:54:25453policy::BrowserPolicyConnector* BrowserProcessImpl::browser_policy_connector() {
[email protected]f2a893c2011-01-05 09:38:07454 DCHECK(CalledOnValidThread());
[email protected]985655a2011-02-23 09:54:25455 if (!created_browser_policy_connector_) {
456 DCHECK(browser_policy_connector_.get() == NULL);
457 created_browser_policy_connector_ = true;
458 browser_policy_connector_.reset(new policy::BrowserPolicyConnector());
[email protected]f2a893c2011-01-05 09:38:07459 }
[email protected]985655a2011-02-23 09:54:25460 return browser_policy_connector_.get();
[email protected]f2a893c2011-01-05 09:38:07461}
462
[email protected]e13ad79b2010-07-22 21:36:50463IconManager* BrowserProcessImpl::icon_manager() {
464 DCHECK(CalledOnValidThread());
465 if (!created_icon_manager_)
466 CreateIconManager();
467 return icon_manager_.get();
468}
469
470ThumbnailGenerator* BrowserProcessImpl::GetThumbnailGenerator() {
471 return &thumbnail_generator_;
472}
473
474AutomationProviderList* BrowserProcessImpl::InitAutomationProviderList() {
475 DCHECK(CalledOnValidThread());
476 if (automation_provider_list_.get() == NULL) {
477 automation_provider_list_.reset(AutomationProviderList::GetInstance());
478 }
479 return automation_provider_list_.get();
480}
481
[email protected]5613126e2011-01-31 15:27:55482void BrowserProcessImpl::InitDevToolsHttpProtocolHandler(
[email protected]01000cf12011-02-04 11:39:22483 const std::string& ip,
[email protected]5613126e2011-01-31 15:27:55484 int port,
485 const std::string& frontend_url) {
[email protected]e13ad79b2010-07-22 21:36:50486 DCHECK(CalledOnValidThread());
[email protected]5613126e2011-01-31 15:27:55487 devtools_http_handler_ =
[email protected]01000cf12011-02-04 11:39:22488 DevToolsHttpProtocolHandler::Start(ip,
489 port,
[email protected]9678cb42011-01-31 18:49:42490 frontend_url,
491 new BrowserListTabContentsProvider());
[email protected]5613126e2011-01-31 15:27:55492}
493
494void BrowserProcessImpl::InitDevToolsLegacyProtocolHandler(int port) {
495 DCHECK(CalledOnValidThread());
496 devtools_legacy_handler_ = DevToolsProtocolHandler::Start(port);
[email protected]e13ad79b2010-07-22 21:36:50497}
498
499bool BrowserProcessImpl::IsShuttingDown() {
500 DCHECK(CalledOnValidThread());
501 return did_start_ && 0 == module_ref_count_;
502}
503
initial.commit09911bf2008-07-26 23:55:29504printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
505 // TODO(abarth): DCHECK(CalledOnValidThread());
[email protected]d8922f22010-05-07 00:57:27506 // http://code.google.com/p/chromium/issues/detail?id=6828
initial.commit09911bf2008-07-26 23:55:29507 // print_job_manager_ is initialized in the constructor and destroyed in the
508 // destructor, so it should always be valid.
509 DCHECK(print_job_manager_.get());
510 return print_job_manager_.get();
511}
512
[email protected]dbeebd52010-11-16 20:34:16513printing::PrintPreviewTabController*
514 BrowserProcessImpl::print_preview_tab_controller() {
515 DCHECK(CalledOnValidThread());
516 if (!print_preview_tab_controller_.get())
517 CreatePrintPreviewTabController();
518 return print_preview_tab_controller_.get();
519}
520
[email protected]e13ad79b2010-07-22 21:36:50521GoogleURLTracker* BrowserProcessImpl::google_url_tracker() {
522 DCHECK(CalledOnValidThread());
523 if (!google_url_tracker_.get())
524 CreateGoogleURLTracker();
525 return google_url_tracker_.get();
526}
527
528IntranetRedirectDetector* BrowserProcessImpl::intranet_redirect_detector() {
529 DCHECK(CalledOnValidThread());
530 if (!intranet_redirect_detector_.get())
531 CreateIntranetRedirectDetector();
532 return intranet_redirect_detector_.get();
533}
534
535const std::string& BrowserProcessImpl::GetApplicationLocale() {
536 DCHECK(!locale_.empty());
537 return locale_;
538}
539
540void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) {
541 locale_ = locale;
542 extension_l10n_util::SetProcessLocale(locale);
543}
544
[email protected]073ed7b2010-09-27 09:20:02545DownloadStatusUpdater* BrowserProcessImpl::download_status_updater() {
546 return &download_status_updater_;
547}
548
[email protected]e13ad79b2010-07-22 21:36:50549base::WaitableEvent* BrowserProcessImpl::shutdown_event() {
550 return shutdown_event_.get();
551}
552
553TabCloseableStateWatcher* BrowserProcessImpl::tab_closeable_state_watcher() {
554 DCHECK(CalledOnValidThread());
555 if (!tab_closeable_state_watcher_.get())
556 CreateTabCloseableStateWatcher();
557 return tab_closeable_state_watcher_.get();
558}
559
[email protected]a7a5e992010-12-09 23:39:51560safe_browsing::ClientSideDetectionService*
561 BrowserProcessImpl::safe_browsing_detection_service() {
562 DCHECK(CalledOnValidThread());
563 if (!created_safe_browsing_detection_service_) {
564 CreateSafeBrowsingDetectionService();
565 }
566 return safe_browsing_detection_service_.get();
567}
568
[email protected]8b08a47f2011-02-25 12:36:37569bool BrowserProcessImpl::plugin_finder_disabled() const {
570 return *plugin_finder_disabled_pref_;
571}
572
[email protected]e13ad79b2010-07-22 21:36:50573void BrowserProcessImpl::CheckForInspectorFiles() {
574 file_thread()->message_loop()->PostTask
575 (FROM_HERE,
576 NewRunnableMethod(this, &BrowserProcessImpl::DoInspectorFilesCheck));
577}
578
[email protected]6618d1d2010-12-15 21:18:47579void BrowserProcessImpl::Observe(NotificationType type,
580 const NotificationSource& source,
581 const NotificationDetails& details) {
582 if (type == NotificationType::APP_TERMINATING) {
583 Profile* profile = ProfileManager::GetDefaultProfile();
584 if (profile) {
585 PrefService* prefs = profile->GetPrefs();
[email protected]2ffc3c6c2011-01-14 12:50:24586 if (prefs->GetBoolean(prefs::kClearSiteDataOnExit) &&
[email protected]6618d1d2010-12-15 21:18:47587 local_state()->GetBoolean(prefs::kClearPluginLSODataEnabled)) {
588 plugin_data_remover_ = new PluginDataRemover();
[email protected]c7d58d62011-01-21 10:27:18589 if (!plugin_data_remover_mime_type().empty())
590 plugin_data_remover_->set_mime_type(plugin_data_remover_mime_type());
591 plugin_data_remover_->StartRemoving(base::Time());
[email protected]6618d1d2010-12-15 21:18:47592 }
593 }
[email protected]e450fa62011-02-01 12:52:56594 } else if (type == NotificationType::PREF_CHANGED) {
595 std::string* pref = Details<std::string>(details).ptr();
596 if (*pref == prefs::kDefaultBrowserSettingEnabled) {
597 if (local_state_->GetBoolean(prefs::kDefaultBrowserSettingEnabled))
598 ShellIntegration::SetAsDefaultBrowser();
599 }
[email protected]6618d1d2010-12-15 21:18:47600 } else {
601 NOTREACHED();
602 }
603}
604
605void BrowserProcessImpl::WaitForPluginDataRemoverToFinish() {
[email protected]c7d58d62011-01-21 10:27:18606 if (plugin_data_remover_.get())
607 plugin_data_remover_->Wait();
[email protected]6618d1d2010-12-15 21:18:47608}
609
[email protected]e13ad79b2010-07-22 21:36:50610#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
611void BrowserProcessImpl::StartAutoupdateTimer() {
612 autoupdate_timer_.Start(
613 base::TimeDelta::FromHours(kUpdateCheckIntervalHours),
614 this,
615 &BrowserProcessImpl::OnAutoupdateTimer);
616}
617#endif
618
619bool BrowserProcessImpl::have_inspector_files() const {
620 return have_inspector_files_;
621}
622
[email protected]d6f37fc2011-02-13 23:58:41623ChromeNetLog* BrowserProcessImpl::net_log() {
624 return net_log_.get();
625}
626
[email protected]4ef795df2010-02-03 02:35:08627void BrowserProcessImpl::ClearLocalState(const FilePath& profile_path) {
[email protected]72cfd90f2010-02-06 03:08:04628 webkit_database::DatabaseTracker::ClearLocalState(profile_path);
[email protected]4ef795df2010-02-03 02:35:08629}
630
631bool BrowserProcessImpl::ShouldClearLocalState(FilePath* profile_path) {
632 FilePath user_data_dir;
633 Profile* profile;
634
[email protected]09584972010-07-03 00:00:59635 // Check for the existence of a profile manager. When quitting early,
[email protected]ed0cf562010-02-05 22:50:41636 // e.g. because another chrome instance is running, or when invoked with
637 // options such as --uninstall or --try-chrome-again=0, the profile manager
638 // does not exist yet.
639 if (!profile_manager_.get())
640 return false;
641
[email protected]4ef795df2010-02-03 02:35:08642 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
643 profile = profile_manager_->GetDefaultProfile(user_data_dir);
[email protected]7bfc674322010-03-05 00:20:57644 if (!profile)
645 return false;
[email protected]4ef795df2010-02-03 02:35:08646 *profile_path = profile->GetPath();
647 return profile->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit);
648}
649
initial.commit09911bf2008-07-26 23:55:29650void BrowserProcessImpl::CreateResourceDispatcherHost() {
651 DCHECK(!created_resource_dispatcher_host_ &&
652 resource_dispatcher_host_.get() == NULL);
653 created_resource_dispatcher_host_ = true;
654
[email protected]79084c2d2009-11-03 23:12:42655 resource_dispatcher_host_.reset(new ResourceDispatcherHost());
initial.commit09911bf2008-07-26 23:55:29656 resource_dispatcher_host_->Initialize();
657}
658
659void BrowserProcessImpl::CreateMetricsService() {
660 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
661 created_metrics_service_ = true;
662
663 metrics_service_.reset(new MetricsService);
664}
665
666void BrowserProcessImpl::CreateIOThread() {
667 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
668 created_io_thread_ = true;
669
670 // Prior to starting the io thread, we create the plugin service as
671 // it is predominantly used from the io thread, but must be created
672 // on the main thread. The service ctor is inexpensive and does not
673 // invoke the io_thread() accessor.
674 PluginService::GetInstance();
675
[email protected]753efc42010-03-09 19:52:16676#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06677 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
678 // we start it now.
679 scoped_ptr<base::Thread> background_x11_thread(
[email protected]d04e7662010-10-10 22:24:48680 new BrowserProcessSubThread(BrowserThread::BACKGROUND_X11));
[email protected]4c3cd7412009-04-22 17:56:06681 if (!background_x11_thread->Start())
682 return;
683 background_x11_thread_.swap(background_x11_thread);
684#endif
685
[email protected]3ce02412011-03-01 12:01:15686 scoped_ptr<IOThread> thread(new IOThread(
687 local_state(), net_log_.get(), extension_event_router_forwarder_.get()));
[email protected]ab820df2008-08-26 05:55:10688 base::Thread::Options options;
689 options.message_loop_type = MessageLoop::TYPE_IO;
690 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29691 return;
692 io_thread_.swap(thread);
693}
694
695void BrowserProcessImpl::CreateFileThread() {
696 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
697 created_file_thread_ = true;
698
[email protected]ab820df2008-08-26 05:55:10699 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48700 new BrowserProcessSubThread(BrowserThread::FILE));
[email protected]a1db3842008-09-17 22:04:06701 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39702#if defined(OS_WIN)
703 // On Windows, the FILE thread needs to be have a UI message loop which pumps
704 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06705 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39706#else
707 options.message_loop_type = MessageLoop::TYPE_IO;
708#endif
[email protected]a1db3842008-09-17 22:04:06709 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29710 return;
711 file_thread_.swap(thread);
712}
713
714void BrowserProcessImpl::CreateDBThread() {
715 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
716 created_db_thread_ = true;
717
[email protected]ab820df2008-08-26 05:55:10718 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48719 new BrowserProcessSubThread(BrowserThread::DB));
initial.commit09911bf2008-07-26 23:55:29720 if (!thread->Start())
721 return;
722 db_thread_.swap(thread);
723}
724
[email protected]914511712009-11-23 19:42:33725void BrowserProcessImpl::CreateProcessLauncherThread() {
726 DCHECK(!created_process_launcher_thread_ && !process_launcher_thread_.get());
727 created_process_launcher_thread_ = true;
728
729 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48730 new BrowserProcessSubThread(BrowserThread::PROCESS_LAUNCHER));
[email protected]914511712009-11-23 19:42:33731 if (!thread->Start())
732 return;
733 process_launcher_thread_.swap(thread);
734}
735
[email protected]875ee822010-05-18 20:58:01736void BrowserProcessImpl::CreateCacheThread() {
737 DCHECK(!created_cache_thread_ && !cache_thread_.get());
738 created_cache_thread_ = true;
739
740 scoped_ptr<base::Thread> thread(
[email protected]75096332010-11-09 03:28:13741 new BrowserThread(BrowserThread::CACHE));
[email protected]875ee822010-05-18 20:58:01742 base::Thread::Options options;
743 options.message_loop_type = MessageLoop::TYPE_IO;
744 if (!thread->StartWithOptions(options))
745 return;
746 cache_thread_.swap(thread);
747}
748
[email protected]0b565182011-03-02 18:11:15749void BrowserProcessImpl::CreateWatchdogThread() {
750 DCHECK(!created_watchdog_thread_ && watchdog_thread_.get() == NULL);
751 created_watchdog_thread_ = true;
752
753 scoped_ptr<WatchDogThread> thread(new WatchDogThread());
754 if (!thread->Start())
755 return;
756 watchdog_thread_.swap(thread);
757}
758
initial.commit09911bf2008-07-26 23:55:29759void BrowserProcessImpl::CreateProfileManager() {
760 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
761 created_profile_manager_ = true;
762
763 profile_manager_.reset(new ProfileManager());
764}
765
766void BrowserProcessImpl::CreateLocalState() {
767 DCHECK(!created_local_state_ && local_state_.get() == NULL);
768 created_local_state_ = true;
769
[email protected]b9636002009-03-04 00:05:25770 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29771 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]f2d1f612010-12-09 15:10:17772 local_state_.reset(
773 PrefService::CreatePrefService(local_state_path, NULL, NULL));
[email protected]492d2142010-09-10 13:55:18774
[email protected]2a7e7c152010-10-01 20:12:09775 pref_change_registrar_.Init(local_state_.get());
776
[email protected]492d2142010-09-10 13:55:18777 // Make sure the the plugin updater gets notifications of changes
[email protected]24ff43d72011-03-01 13:28:58778 // in the plugin policy lists.
779 local_state_->RegisterListPref(prefs::kPluginsDisabledPlugins);
780 pref_change_registrar_.Add(prefs::kPluginsDisabledPlugins,
781 PluginUpdater::GetInstance());
782 local_state_->RegisterListPref(prefs::kPluginsDisabledPluginsExceptions);
783 pref_change_registrar_.Add(prefs::kPluginsDisabledPluginsExceptions,
784 PluginUpdater::GetInstance());
785 local_state_->RegisterListPref(prefs::kPluginsEnabledPlugins);
786 pref_change_registrar_.Add(prefs::kPluginsEnabledPlugins,
[email protected]8e8bb6d2010-12-13 08:18:55787 PluginUpdater::GetInstance());
[email protected]2a7e7c152010-10-01 20:12:09788
789 // Initialize and set up notifications for the printing enabled
790 // preference.
791 local_state_->RegisterBooleanPref(prefs::kPrintingEnabled, true);
792 bool printing_enabled =
793 local_state_->GetBoolean(prefs::kPrintingEnabled);
794 print_job_manager_->set_printing_enabled(printing_enabled);
795 pref_change_registrar_.Add(prefs::kPrintingEnabled,
796 print_job_manager_.get());
[email protected]e450fa62011-02-01 12:52:56797
798 // Initialize the notification for the default browser setting policy.
799 local_state_->RegisterBooleanPref(prefs::kDefaultBrowserSettingEnabled,
800 false);
801 if (local_state_->IsManagedPreference(prefs::kDefaultBrowserSettingEnabled)) {
802 if (local_state_->GetBoolean(prefs::kDefaultBrowserSettingEnabled))
803 ShellIntegration::SetAsDefaultBrowser();
804 }
805 pref_change_registrar_.Add(prefs::kDefaultBrowserSettingEnabled, this);
[email protected]8b08a47f2011-02-25 12:36:37806
807 // Initialize the preference for the plugin finder policy.
808 // This preference is only needed on the IO thread so make it available there.
809 local_state_->RegisterBooleanPref(prefs::kDisablePluginFinder, false);
810 plugin_finder_disabled_pref_.Init(prefs::kDisablePluginFinder,
811 local_state_.get(), NULL);
812 plugin_finder_disabled_pref_.MoveToThread(BrowserThread::IO);
[email protected]fd6159a2010-09-03 09:38:39813}
initial.commit09911bf2008-07-26 23:55:29814
initial.commit09911bf2008-07-26 23:55:29815void BrowserProcessImpl::CreateIconManager() {
816 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
817 created_icon_manager_ = true;
818 icon_manager_.reset(new IconManager);
819}
820
[email protected]40ecc902009-03-16 13:42:47821void BrowserProcessImpl::CreateDevToolsManager() {
[email protected]73ee01522009-06-05 10:13:44822 DCHECK(devtools_manager_.get() == NULL);
[email protected]40ecc902009-03-16 13:42:47823 created_devtools_manager_ = true;
[email protected]73ee01522009-06-05 10:13:44824 devtools_manager_ = new DevToolsManager();
[email protected]40ecc902009-03-16 13:42:47825}
826
[email protected]8b8e7c92010-08-19 18:05:56827void BrowserProcessImpl::CreateSidebarManager() {
828 DCHECK(sidebar_manager_.get() == NULL);
829 created_sidebar_manager_ = true;
830 sidebar_manager_ = new SidebarManager();
831}
832
initial.commit09911bf2008-07-26 23:55:29833void BrowserProcessImpl::CreateGoogleURLTracker() {
834 DCHECK(google_url_tracker_.get() == NULL);
835 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
836 google_url_tracker_.swap(google_url_tracker);
837}
[email protected]6641bf662009-08-21 00:34:09838
[email protected]c4ff4952010-01-08 19:12:47839void BrowserProcessImpl::CreateIntranetRedirectDetector() {
840 DCHECK(intranet_redirect_detector_.get() == NULL);
841 scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector(
842 new IntranetRedirectDetector);
843 intranet_redirect_detector_.swap(intranet_redirect_detector);
844}
845
[email protected]29672ab2009-10-30 03:44:03846void BrowserProcessImpl::CreateNotificationUIManager() {
847 DCHECK(notification_ui_manager_.get() == NULL);
[email protected]b0b2a3dd2011-01-06 00:30:05848 notification_ui_manager_.reset(NotificationUIManager::Create(local_state()));
849
[email protected]29672ab2009-10-30 03:44:03850 created_notification_ui_manager_ = true;
851}
852
[email protected]0b4d3382010-07-14 16:13:04853void BrowserProcessImpl::CreateTabCloseableStateWatcher() {
854 DCHECK(tab_closeable_state_watcher_.get() == NULL);
855 tab_closeable_state_watcher_.reset(TabCloseableStateWatcher::Create());
856}
857
[email protected]dbeebd52010-11-16 20:34:16858void BrowserProcessImpl::CreatePrintPreviewTabController() {
859 DCHECK(print_preview_tab_controller_.get() == NULL);
860 print_preview_tab_controller_ = new printing::PrintPreviewTabController();
861}
862
[email protected]a7a5e992010-12-09 23:39:51863void BrowserProcessImpl::CreateSafeBrowsingDetectionService() {
864 DCHECK(safe_browsing_detection_service_.get() == NULL);
865 // Set this flag to true so that we don't retry indefinitely to
866 // create the service class if there was an error.
867 created_safe_browsing_detection_service_ = true;
868
869 FilePath model_file_path;
870 Profile* profile = profile_manager() ?
871 profile_manager()->GetDefaultProfile() : NULL;
872 if (IsSafeBrowsingDetectionServiceEnabled() &&
873 PathService::Get(chrome::DIR_USER_DATA, &model_file_path) &&
874 profile && profile->GetRequestContext()) {
875 safe_browsing_detection_service_.reset(
876 safe_browsing::ClientSideDetectionService::Create(
877 model_file_path.Append(chrome::kSafeBrowsingPhishingModelFilename),
878 profile->GetRequestContext()));
879 }
880}
881
882bool BrowserProcessImpl::IsSafeBrowsingDetectionServiceEnabled() {
883 // The safe browsing client-side detection is enabled only if the switch is
[email protected]42930de42011-01-13 23:26:28884 // enabled and when safe browsing related stats is allowed to be collected.
885 return CommandLine::ForCurrentProcess()->HasSwitch(
886 switches::kEnableClientSidePhishingDetection) &&
887 resource_dispatcher_host()->safe_browsing_service() &&
888 resource_dispatcher_host()->safe_browsing_service()->CanReportStats();
[email protected]a7a5e992010-12-09 23:39:51889}
890
[email protected]6641bf662009-08-21 00:34:09891// The BrowserProcess object must outlive the file thread so we use traits
892// which don't do any management.
[email protected]c56428f22010-06-16 02:17:23893DISABLE_RUNNABLE_METHOD_REFCOUNT(BrowserProcessImpl);
[email protected]6641bf662009-08-21 00:34:09894
[email protected]d55aaa132009-09-28 21:08:04895#if defined(IPC_MESSAGE_LOG_ENABLED)
896
897void BrowserProcessImpl::SetIPCLoggingEnabled(bool enable) {
898 // First enable myself.
899 if (enable)
[email protected]8e8bb6d2010-12-13 08:18:55900 IPC::Logging::GetInstance()->Enable();
[email protected]d55aaa132009-09-28 21:08:04901 else
[email protected]8e8bb6d2010-12-13 08:18:55902 IPC::Logging::GetInstance()->Disable();
[email protected]d55aaa132009-09-28 21:08:04903
904 // Now tell subprocesses. Messages to ChildProcess-derived
905 // processes must be done on the IO thread.
906 io_thread()->message_loop()->PostTask
907 (FROM_HERE,
908 NewRunnableMethod(
909 this,
910 &BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses,
911 enable));
912
913 // Finally, tell the renderers which don't derive from ChildProcess.
914 // Messages to the renderers must be done on the UI (main) thread.
[email protected]019191a2009-10-02 20:37:27915 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
916 !i.IsAtEnd(); i.Advance())
[email protected]ff47b2962011-03-07 23:51:49917 i.GetCurrentValue()->Send(new ChildProcessMsg_SetIPCLoggingEnabled(enable));
[email protected]d55aaa132009-09-28 21:08:04918}
919
920// Helper for SetIPCLoggingEnabled.
921void BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses(bool enabled) {
[email protected]d04e7662010-10-10 22:24:48922 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]d55aaa132009-09-28 21:08:04923
[email protected]d27893f62010-07-03 05:47:42924 BrowserChildProcessHost::Iterator i; // default constr references a singleton
[email protected]34cf97d2009-09-29 22:46:11925 while (!i.Done()) {
[email protected]ff47b2962011-03-07 23:51:49926 i->Send(new ChildProcessMsg_SetIPCLoggingEnabled(enabled));
[email protected]34cf97d2009-09-29 22:46:11927 ++i;
[email protected]d55aaa132009-09-28 21:08:04928 }
929}
930
931#endif // IPC_MESSAGE_LOG_ENABLED
932
[email protected]6641bf662009-08-21 00:34:09933void BrowserProcessImpl::DoInspectorFilesCheck() {
934 // Runs on FILE thread.
935 DCHECK(file_thread_->message_loop() == MessageLoop::current());
936 bool result = false;
937
938 FilePath inspector_dir;
939 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) {
940 result = file_util::PathExists(inspector_dir);
941 }
942
[email protected]308080d2009-11-11 19:19:02943 have_inspector_files_ = result;
[email protected]6641bf662009-08-21 00:34:09944}
[email protected]bd48c2b02010-04-09 20:32:42945
[email protected]3cdacd42010-04-30 18:55:53946// Mac is currently not supported.
947#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
[email protected]bd48c2b02010-04-09 20:32:42948
949bool BrowserProcessImpl::CanAutorestartForUpdate() const {
950 // Check if browser is in the background and if it needs to be restarted to
951 // apply a pending update.
[email protected]c10fa3952010-08-24 16:52:53952 return BrowserList::size() == 0 && BrowserList::WillKeepAlive() &&
[email protected]c984d9f2010-07-20 20:52:20953 Upgrade::IsUpdatePendingRestart();
[email protected]bd48c2b02010-04-09 20:32:42954}
955
[email protected]5c08f222010-09-22 09:37:21956// Switches to add when auto-restarting Chrome.
[email protected]6c10c972010-08-11 04:15:47957const char* const kSwitchesToAddOnAutorestart[] = {
[email protected]f9a06842010-08-21 00:32:40958 switches::kNoStartupWindow
[email protected]bd48c2b02010-04-09 20:32:42959};
960
961void BrowserProcessImpl::RestartPersistentInstance() {
962 CommandLine* old_cl = CommandLine::ForCurrentProcess();
[email protected]3cdacd42010-04-30 18:55:53963 scoped_ptr<CommandLine> new_cl(new CommandLine(old_cl->GetProgram()));
[email protected]bd48c2b02010-04-09 20:32:42964
965 std::map<std::string, CommandLine::StringType> switches =
966 old_cl->GetSwitches();
967
[email protected]5c08f222010-09-22 09:37:21968 switches::RemoveSwitchesForAutostart(&switches);
[email protected]bd48c2b02010-04-09 20:32:42969
970 // Append the rest of the switches (along with their values, if any)
971 // to the new command line
972 for (std::map<std::string, CommandLine::StringType>::const_iterator i =
973 switches.begin(); i != switches.end(); ++i) {
974 CommandLine::StringType switch_value = i->second;
975 if (switch_value.length() > 0) {
[email protected]05076ba22010-07-30 05:59:57976 new_cl->AppendSwitchNative(i->first, i->second);
[email protected]bd48c2b02010-04-09 20:32:42977 } else {
[email protected]3cdacd42010-04-30 18:55:53978 new_cl->AppendSwitch(i->first);
[email protected]bd48c2b02010-04-09 20:32:42979 }
980 }
981
[email protected]6c10c972010-08-11 04:15:47982 // Ensure that our desired switches are set on the new process.
[email protected]5c08f222010-09-22 09:37:21983 for (size_t i = 0; i < arraysize(kSwitchesToAddOnAutorestart); ++i) {
[email protected]6c10c972010-08-11 04:15:47984 if (!new_cl->HasSwitch(kSwitchesToAddOnAutorestart[i]))
985 new_cl->AppendSwitch(kSwitchesToAddOnAutorestart[i]);
986 }
[email protected]bd48c2b02010-04-09 20:32:42987
[email protected]3cdacd42010-04-30 18:55:53988 DLOG(WARNING) << "Shutting down current instance of the browser.";
989 BrowserList::CloseAllBrowsersAndExit();
990
991 // Transfer ownership to Upgrade.
992 Upgrade::SetNewCommandLine(new_cl.release());
[email protected]bd48c2b02010-04-09 20:32:42993}
994
995void BrowserProcessImpl::OnAutoupdateTimer() {
996 if (CanAutorestartForUpdate()) {
[email protected]3cdacd42010-04-30 18:55:53997 DLOG(WARNING) << "Detected update. Restarting browser.";
[email protected]bd48c2b02010-04-09 20:32:42998 RestartPersistentInstance();
999 }
1000}
1001
[email protected]3cdacd42010-04-30 18:55:531002#endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)