blob: e9f2b8335ac1b229a17fff5855c55fc621d59502 [file] [log] [blame]
[email protected]c4ff4952010-01-08 19:12:471// Copyright (c) 2010 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
[email protected]90f39902009-10-03 04:25:379#include "app/clipboard/clipboard.h"
[email protected]a92b8642009-05-05 23:38:5610#include "app/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/command_line.h"
[email protected]6641bf662009-08-21 00:34:0912#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/path_service.h"
[email protected]c6032e82010-09-13 20:06:0514#include "base/task.h"
[email protected]ac262c9f2008-10-19 17:45:2115#include "base/thread.h"
[email protected]1c4947f2009-01-15 22:25:1116#include "base/waitable_event.h"
[email protected]0082d7e0d2010-02-27 14:34:1217#include "chrome/browser/appcache/chrome_appcache_service.h"
[email protected]e13ad79b2010-07-22 21:36:5018#include "chrome/browser/automation/automation_provider_list.h"
[email protected]d27893f62010-07-03 05:47:4219#include "chrome/browser/browser_child_process_host.h"
[email protected]bd48c2b02010-04-09 20:32:4220#include "chrome/browser/browser_list.h"
[email protected]b443cb042009-12-15 22:05:0921#include "chrome/browser/browser_main.h"
[email protected]0ac83682010-01-22 17:46:2722#include "chrome/browser/browser_process_sub_thread.h"
[email protected]ac262c9f2008-10-19 17:45:2123#include "chrome/browser/browser_trial.h"
initial.commit09911bf2008-07-26 23:55:2924#include "chrome/browser/chrome_thread.h"
[email protected]4ab4b0f2009-02-10 18:54:5025#include "chrome/browser/debugger/debugger_wrapper.h"
[email protected]40ecc902009-03-16 13:42:4726#include "chrome/browser/debugger/devtools_manager.h"
[email protected]6c69796d2010-07-16 21:41:1627#include "chrome/browser/download/download_file_manager.h"
[email protected]5ba0a2c2009-02-19 01:19:3428#include "chrome/browser/download/save_file_manager.h"
[email protected]820735792010-07-29 23:40:0129#include "chrome/browser/first_run/first_run.h"
[email protected]f7578f52010-08-30 22:22:4930#include "chrome/browser/google/google_url_tracker.h"
[email protected]dcefa302009-05-20 00:24:3931#include "chrome/browser/icon_manager.h"
[email protected]4ef795df2010-02-03 02:35:0832#include "chrome/browser/in_process_webkit/dom_storage_context.h"
[email protected]c4ff4952010-01-08 19:12:4733#include "chrome/browser/intranet_redirect_detector.h"
[email protected]0ac83682010-01-22 17:46:2734#include "chrome/browser/io_thread.h"
[email protected]dc6f4962009-02-13 01:25:5035#include "chrome/browser/metrics/metrics_service.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]4ef795df2010-02-03 02:35:0838#include "chrome/browser/net/sqlite_persistent_cookie_store.h"
[email protected]29672ab2009-10-30 03:44:0339#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]fd49e2d2009-02-20 17:21:3040#include "chrome/browser/plugin_service.h"
[email protected]492d2142010-09-10 13:55:1841#include "chrome/browser/plugin_updater.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"
initial.commit09911bf2008-07-26 23:55:2944#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2645#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]81218f42009-02-05 18:48:0846#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
47#include "chrome/browser/safe_browsing/safe_browsing_service.h"
[email protected]8b8e7c92010-08-19 18:05:5648#include "chrome/browser/sidebar/sidebar_manager.h"
[email protected]0b4d3382010-07-14 16:13:0449#include "chrome/browser/tab_closeable_state_watcher.h"
[email protected]4ef795df2010-02-03 02:35:0850#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2951#include "chrome/common/chrome_paths.h"
52#include "chrome/common/chrome_switches.h"
[email protected]27fbccc2010-03-25 03:00:4953#include "chrome/common/extensions/extension_resource.h"
[email protected]f1b6de22010-03-06 12:13:4754#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]ea587b02010-05-21 15:01:3555#include "chrome/common/json_pref_store.h"
initial.commit09911bf2008-07-26 23:55:2956#include "chrome/common/notification_service.h"
57#include "chrome/common/pref_names.h"
[email protected]4ef795df2010-02-03 02:35:0858#include "chrome/common/url_constants.h"
[email protected]5c08f222010-09-22 09:37:2159#include "chrome/common/switch_utils.h"
[email protected]bd48c2b02010-04-09 20:32:4260#include "chrome/installer/util/google_update_constants.h"
[email protected]d55aaa132009-09-28 21:08:0461#include "ipc/ipc_logging.h"
[email protected]4ef795df2010-02-03 02:35:0862#include "webkit/database/database_tracker.h"
[email protected]b112a4c2009-02-01 20:24:0163
64#if defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0565#include "views/focus/view_storage.h"
[email protected]86230b92009-11-23 20:38:3866#endif
67
[email protected]d55aaa132009-09-28 21:08:0468#if defined(IPC_MESSAGE_LOG_ENABLED)
69#include "chrome/common/plugin_messages.h"
70#include "chrome/common/render_messages.h"
71#endif
72
[email protected]3cdacd42010-04-30 18:55:5373#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
74// How often to check if the persistent instance of Chrome needs to restart
75// to install an update.
76static const int kUpdateCheckIntervalHours = 6;
77#endif
78
[email protected]c6032e82010-09-13 20:06:0579#if defined(USE_X11)
80// How long to wait for the File thread to complete during EndSession, on
81// Linux. We have a timeout here because we're unable to run the UI messageloop
82// and there's some deadlock risk. Our only option is to exit anyway.
83static const int kEndSessionTimeoutSeconds = 10;
84#endif
85
[email protected]bb975362009-01-21 01:00:2286BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
initial.commit09911bf2008-07-26 23:55:2987 : created_resource_dispatcher_host_(false),
88 created_metrics_service_(false),
89 created_io_thread_(false),
90 created_file_thread_(false),
91 created_db_thread_(false),
[email protected]914511712009-11-23 19:42:3392 created_process_launcher_thread_(false),
[email protected]875ee822010-05-18 20:58:0193 created_cache_thread_(false),
initial.commit09911bf2008-07-26 23:55:2994 created_profile_manager_(false),
95 created_local_state_(false),
[email protected]b112a4c2009-02-01 20:24:0196 created_icon_manager_(false),
97 created_debugger_wrapper_(false),
[email protected]40ecc902009-03-16 13:42:4798 created_devtools_manager_(false),
[email protected]8b8e7c92010-08-19 18:05:5699 created_sidebar_manager_(false),
[email protected]29672ab2009-10-30 03:44:03100 created_notification_ui_manager_(false),
initial.commit09911bf2008-07-26 23:55:29101 module_ref_count_(0),
[email protected]afd20c022010-06-10 00:48:20102 did_start_(false),
[email protected]1b2db1a2008-08-08 17:46:13103 checked_for_new_frames_(false),
[email protected]6641bf662009-08-21 00:34:09104 using_new_frames_(false),
105 have_inspector_files_(true) {
initial.commit09911bf2008-07-26 23:55:29106 g_browser_process = this;
[email protected]1b8d02f12009-05-05 04:14:11107 clipboard_.reset(new Clipboard);
initial.commit09911bf2008-07-26 23:55:29108 main_notification_service_.reset(new NotificationService);
109
110 // Must be created after the NotificationService.
111 print_job_manager_.reset(new printing::PrintJobManager);
112
[email protected]b797e152009-01-23 16:06:14113 shutdown_event_.reset(new base::WaitableEvent(true, false));
initial.commit09911bf2008-07-26 23:55:29114}
115
116BrowserProcessImpl::~BrowserProcessImpl() {
[email protected]4ef795df2010-02-03 02:35:08117 FilePath profile_path;
118 bool clear_local_state_on_exit;
119
120 // Store the profile path for clearing local state data on exit.
121 clear_local_state_on_exit = ShouldClearLocalState(&profile_path);
122
initial.commit09911bf2008-07-26 23:55:29123 // Delete the AutomationProviderList before NotificationService,
124 // since it may try to unregister notifications
125 // Both NotificationService and AutomationProvider are singleton instances in
126 // the BrowserProcess. Since AutomationProvider may have some active
127 // notification observers, it is essential that it gets destroyed before the
128 // NotificationService. NotificationService won't be destroyed until after
129 // this destructor is run.
130 automation_provider_list_.reset();
131
[email protected]d393a0fd2009-05-13 23:32:01132 // We need to shutdown the SdchDictionaryFetcher as it regularly holds
133 // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do
134 // a PostDelayedTask onto the IO thread. This shutdown call will both discard
135 // any pending URLFetchers, and avoid creating any more.
136 SdchDictionaryFetcher::Shutdown();
137
[email protected]c4ff4952010-01-08 19:12:47138 // We need to destroy the MetricsService, GoogleURLTracker, and
139 // IntranetRedirectDetector before the io_thread_ gets destroyed, since their
140 // destructors can call the URLFetcher destructor, which does a
141 // PostDelayedTask operation on the IO thread. (The IO thread will handle
142 // that URLFetcher operation before going away.)
initial.commit09911bf2008-07-26 23:55:29143 metrics_service_.reset();
144 google_url_tracker_.reset();
[email protected]c4ff4952010-01-08 19:12:47145 intranet_redirect_detector_.reset();
initial.commit09911bf2008-07-26 23:55:29146
[email protected]d8a899c2010-09-21 04:50:33147 // Need to clear the desktop notification balloons before the io_thread_ and
148 // before the profiles, since if there are any still showing we will access
149 // those things during teardown.
150 notification_ui_manager_.reset();
151
initial.commit09911bf2008-07-26 23:55:29152 // Need to clear profiles (download managers) before the io_thread_.
153 profile_manager_.reset();
154
initial.commit09911bf2008-07-26 23:55:29155 // Debugger must be cleaned up before IO thread and NotificationService.
156 debugger_wrapper_ = NULL;
157
158 if (resource_dispatcher_host_.get()) {
159 // Need to tell Safe Browsing Service that the IO thread is going away
160 // since it cached a pointer to it.
161 if (resource_dispatcher_host()->safe_browsing_service())
162 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
163
164 // Cancel pending requests and prevent new requests.
165 resource_dispatcher_host()->Shutdown();
166 }
167
[email protected]753efc42010-03-09 19:52:16168#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06169 // The IO thread must outlive the BACKGROUND_X11 thread.
170 background_x11_thread_.reset();
171#endif
172
initial.commit09911bf2008-07-26 23:55:29173 // Need to stop io_thread_ before resource_dispatcher_host_, since
174 // io_thread_ may still deref ResourceDispatcherHost and handle resource
175 // request before going away.
[email protected]0ac83682010-01-22 17:46:27176 io_thread_.reset();
initial.commit09911bf2008-07-26 23:55:29177
[email protected]875ee822010-05-18 20:58:01178 // The IO thread was the only user of this thread.
179 cache_thread_.reset();
180
[email protected]914511712009-11-23 19:42:33181 // Stop the process launcher thread after the IO thread, in case the IO thread
182 // posted a task to terminate a process on the process launcher thread.
183 process_launcher_thread_.reset();
184
initial.commit09911bf2008-07-26 23:55:29185 // Clean up state that lives on the file_thread_ before it goes away.
186 if (resource_dispatcher_host_.get()) {
187 resource_dispatcher_host()->download_file_manager()->Shutdown();
188 resource_dispatcher_host()->save_file_manager()->Shutdown();
189 }
190
191 // Need to stop the file_thread_ here to force it to process messages in its
192 // message loop from the previous call to shutdown the DownloadFileManager,
193 // SaveFileManager and SessionService.
194 file_thread_.reset();
195
196 // With the file_thread_ flushed, we can release any icon resources.
197 icon_manager_.reset();
198
199 // Need to destroy ResourceDispatcherHost before PluginService and
[email protected]49f28bc72010-02-04 00:10:01200 // SafeBrowsingService, since it caches a pointer to it. This also
201 // causes the webkit thread to terminate.
initial.commit09911bf2008-07-26 23:55:29202 resource_dispatcher_host_.reset();
203
204 // Wait for the pending print jobs to finish.
205 print_job_manager_->OnQuit();
206 print_job_manager_.reset();
207
[email protected]0b4d3382010-07-14 16:13:04208 // Destroy TabCloseableStateWatcher before NotificationService since the
209 // former registers for notifications.
210 tab_closeable_state_watcher_.reset();
211
initial.commit09911bf2008-07-26 23:55:29212 // Now OK to destroy NotificationService.
213 main_notification_service_.reset();
214
[email protected]49f28bc72010-02-04 00:10:01215 // Prior to clearing local state, we want to complete tasks pending
216 // on the db thread too.
217 db_thread_.reset();
218
219 // At this point, no render process exist and the file, io, db, and
220 // webkit threads in this process have all terminated, so it's safe
221 // to access local state data such as cookies, database, or local storage.
222 if (clear_local_state_on_exit)
223 ClearLocalState(profile_path);
224
initial.commit09911bf2008-07-26 23:55:29225 g_browser_process = NULL;
226}
227
[email protected]c6032e82010-09-13 20:06:05228#if defined(OS_WIN)
[email protected]295039bd2008-08-15 04:32:57229// Send a QuitTask to the given MessageLoop.
230static void PostQuit(MessageLoop* message_loop) {
231 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
232}
[email protected]c6032e82010-09-13 20:06:05233#elif defined(USE_X11)
234static void Signal(base::WaitableEvent* event) {
235 event->Signal();
236}
237#endif
initial.commit09911bf2008-07-26 23:55:29238
[email protected]b443cb042009-12-15 22:05:09239unsigned int BrowserProcessImpl::AddRefModule() {
240 DCHECK(CalledOnValidThread());
[email protected]afd20c022010-06-10 00:48:20241 did_start_ = true;
[email protected]b443cb042009-12-15 22:05:09242 module_ref_count_++;
243 return module_ref_count_;
244}
245
246unsigned int BrowserProcessImpl::ReleaseModule() {
247 DCHECK(CalledOnValidThread());
[email protected]760d970a2010-05-18 00:39:18248 DCHECK_NE(0u, module_ref_count_);
[email protected]b443cb042009-12-15 22:05:09249 module_ref_count_--;
250 if (0 == module_ref_count_) {
251 MessageLoop::current()->PostTask(
[email protected]34f73fb2010-03-24 20:50:34252 FROM_HERE, NewRunnableFunction(DidEndMainMessageLoop));
[email protected]b443cb042009-12-15 22:05:09253 MessageLoop::current()->Quit();
254 }
255 return module_ref_count_;
256}
257
initial.commit09911bf2008-07-26 23:55:29258void BrowserProcessImpl::EndSession() {
[email protected]c6032e82010-09-13 20:06:05259#if defined(OS_WIN) || defined(USE_X11)
[email protected]d65cab7a2008-08-12 01:25:41260 // Notify we are going away.
[email protected]c6032e82010-09-13 20:06:05261 shutdown_event_->Signal();
[email protected]b112a4c2009-02-01 20:24:01262#endif
[email protected]d65cab7a2008-08-12 01:25:41263
initial.commit09911bf2008-07-26 23:55:29264 // Mark all the profiles as clean.
265 ProfileManager* pm = profile_manager();
266 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
267 (*i)->MarkAsCleanShutdown();
268
269 // Tell the metrics service it was cleanly shutdown.
270 MetricsService* metrics = g_browser_process->metrics_service();
271 if (metrics && local_state()) {
272 metrics->RecordCleanShutdown();
273
274 metrics->RecordStartOfSessionEnd();
275
276 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36277 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29278 }
279
280 // We must write that the profile and metrics service shutdown cleanly,
281 // otherwise on startup we'll think we crashed. So we block until done and
282 // then proceed with normal shutdown.
[email protected]c6032e82010-09-13 20:06:05283#if defined(USE_X11)
284 // Can't run a local loop on linux. Instead create a waitable event.
285 base::WaitableEvent done_writing(false, false);
286 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
287 NewRunnableFunction(Signal, &done_writing));
288 done_writing.TimedWait(
289 base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds));
290#elif defined(OS_WIN)
291 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57292 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29293 MessageLoop::current()->Run();
[email protected]c6032e82010-09-13 20:06:05294#else
295 NOTIMPLEMENTED();
296#endif
initial.commit09911bf2008-07-26 23:55:29297}
298
[email protected]e13ad79b2010-07-22 21:36:50299ResourceDispatcherHost* BrowserProcessImpl::resource_dispatcher_host() {
300 DCHECK(CalledOnValidThread());
301 if (!created_resource_dispatcher_host_)
302 CreateResourceDispatcherHost();
303 return resource_dispatcher_host_.get();
304}
305
306MetricsService* BrowserProcessImpl::metrics_service() {
307 DCHECK(CalledOnValidThread());
308 if (!created_metrics_service_)
309 CreateMetricsService();
310 return metrics_service_.get();
311}
312
313IOThread* BrowserProcessImpl::io_thread() {
314 DCHECK(CalledOnValidThread());
315 if (!created_io_thread_)
316 CreateIOThread();
317 return io_thread_.get();
318}
319
320base::Thread* BrowserProcessImpl::file_thread() {
321 DCHECK(CalledOnValidThread());
322 if (!created_file_thread_)
323 CreateFileThread();
324 return file_thread_.get();
325}
326
327base::Thread* BrowserProcessImpl::db_thread() {
328 DCHECK(CalledOnValidThread());
329 if (!created_db_thread_)
330 CreateDBThread();
331 return db_thread_.get();
332}
333
334base::Thread* BrowserProcessImpl::process_launcher_thread() {
335 DCHECK(CalledOnValidThread());
336 if (!created_process_launcher_thread_)
337 CreateProcessLauncherThread();
338 return process_launcher_thread_.get();
339}
340
341base::Thread* BrowserProcessImpl::cache_thread() {
342 DCHECK(CalledOnValidThread());
343 if (!created_cache_thread_)
344 CreateCacheThread();
345 return cache_thread_.get();
346}
347
348#if defined(USE_X11)
349base::Thread* BrowserProcessImpl::background_x11_thread() {
350 DCHECK(CalledOnValidThread());
351 // The BACKGROUND_X11 thread is created when the IO thread is created.
352 if (!created_io_thread_)
353 CreateIOThread();
354 return background_x11_thread_.get();
355}
356#endif
357
358ProfileManager* BrowserProcessImpl::profile_manager() {
359 DCHECK(CalledOnValidThread());
360 if (!created_profile_manager_)
361 CreateProfileManager();
362 return profile_manager_.get();
363}
364
365PrefService* BrowserProcessImpl::local_state() {
366 DCHECK(CalledOnValidThread());
367 if (!created_local_state_)
368 CreateLocalState();
369 return local_state_.get();
370}
371
372DevToolsManager* BrowserProcessImpl::devtools_manager() {
373 DCHECK(CalledOnValidThread());
374 if (!created_devtools_manager_)
375 CreateDevToolsManager();
376 return devtools_manager_.get();
377}
378
[email protected]8b8e7c92010-08-19 18:05:56379SidebarManager* BrowserProcessImpl::sidebar_manager() {
380 DCHECK(CalledOnValidThread());
381 if (!created_sidebar_manager_)
382 CreateSidebarManager();
383 return sidebar_manager_.get();
384}
385
[email protected]e13ad79b2010-07-22 21:36:50386Clipboard* BrowserProcessImpl::clipboard() {
387 DCHECK(CalledOnValidThread());
388 return clipboard_.get();
389}
390
391NotificationUIManager* BrowserProcessImpl::notification_ui_manager() {
392 DCHECK(CalledOnValidThread());
393 if (!created_notification_ui_manager_)
394 CreateNotificationUIManager();
395 return notification_ui_manager_.get();
396}
397
[email protected]e13ad79b2010-07-22 21:36:50398IconManager* BrowserProcessImpl::icon_manager() {
399 DCHECK(CalledOnValidThread());
400 if (!created_icon_manager_)
401 CreateIconManager();
402 return icon_manager_.get();
403}
404
405ThumbnailGenerator* BrowserProcessImpl::GetThumbnailGenerator() {
406 return &thumbnail_generator_;
407}
408
409AutomationProviderList* BrowserProcessImpl::InitAutomationProviderList() {
410 DCHECK(CalledOnValidThread());
411 if (automation_provider_list_.get() == NULL) {
412 automation_provider_list_.reset(AutomationProviderList::GetInstance());
413 }
414 return automation_provider_list_.get();
415}
416
417void BrowserProcessImpl::InitDebuggerWrapper(int port, bool useHttp) {
418 DCHECK(CalledOnValidThread());
419 if (!created_debugger_wrapper_)
420 CreateDebuggerWrapper(port, useHttp);
421}
422
423bool BrowserProcessImpl::IsShuttingDown() {
424 DCHECK(CalledOnValidThread());
425 return did_start_ && 0 == module_ref_count_;
426}
427
initial.commit09911bf2008-07-26 23:55:29428printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
429 // TODO(abarth): DCHECK(CalledOnValidThread());
[email protected]d8922f22010-05-07 00:57:27430 // http://code.google.com/p/chromium/issues/detail?id=6828
initial.commit09911bf2008-07-26 23:55:29431 // print_job_manager_ is initialized in the constructor and destroyed in the
432 // destructor, so it should always be valid.
433 DCHECK(print_job_manager_.get());
434 return print_job_manager_.get();
435}
436
[email protected]e13ad79b2010-07-22 21:36:50437GoogleURLTracker* BrowserProcessImpl::google_url_tracker() {
438 DCHECK(CalledOnValidThread());
439 if (!google_url_tracker_.get())
440 CreateGoogleURLTracker();
441 return google_url_tracker_.get();
442}
443
444IntranetRedirectDetector* BrowserProcessImpl::intranet_redirect_detector() {
445 DCHECK(CalledOnValidThread());
446 if (!intranet_redirect_detector_.get())
447 CreateIntranetRedirectDetector();
448 return intranet_redirect_detector_.get();
449}
450
451const std::string& BrowserProcessImpl::GetApplicationLocale() {
452 DCHECK(!locale_.empty());
453 return locale_;
454}
455
456void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) {
457 locale_ = locale;
458 extension_l10n_util::SetProcessLocale(locale);
459}
460
461base::WaitableEvent* BrowserProcessImpl::shutdown_event() {
462 return shutdown_event_.get();
463}
464
465TabCloseableStateWatcher* BrowserProcessImpl::tab_closeable_state_watcher() {
466 DCHECK(CalledOnValidThread());
467 if (!tab_closeable_state_watcher_.get())
468 CreateTabCloseableStateWatcher();
469 return tab_closeable_state_watcher_.get();
470}
471
472void BrowserProcessImpl::CheckForInspectorFiles() {
473 file_thread()->message_loop()->PostTask
474 (FROM_HERE,
475 NewRunnableMethod(this, &BrowserProcessImpl::DoInspectorFilesCheck));
476}
477
478#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
479void BrowserProcessImpl::StartAutoupdateTimer() {
480 autoupdate_timer_.Start(
481 base::TimeDelta::FromHours(kUpdateCheckIntervalHours),
482 this,
483 &BrowserProcessImpl::OnAutoupdateTimer);
484}
485#endif
486
487bool BrowserProcessImpl::have_inspector_files() const {
488 return have_inspector_files_;
489}
490
[email protected]4ef795df2010-02-03 02:35:08491void BrowserProcessImpl::ClearLocalState(const FilePath& profile_path) {
492 SQLitePersistentCookieStore::ClearLocalState(profile_path.Append(
493 chrome::kCookieFilename));
494 DOMStorageContext::ClearLocalState(profile_path, chrome::kExtensionScheme);
[email protected]72cfd90f2010-02-06 03:08:04495 webkit_database::DatabaseTracker::ClearLocalState(profile_path);
[email protected]10c934052010-02-04 19:58:31496 ChromeAppCacheService::ClearLocalState(profile_path);
[email protected]4ef795df2010-02-03 02:35:08497}
498
499bool BrowserProcessImpl::ShouldClearLocalState(FilePath* profile_path) {
500 FilePath user_data_dir;
501 Profile* profile;
502
[email protected]09584972010-07-03 00:00:59503 // Check for the existence of a profile manager. When quitting early,
[email protected]ed0cf562010-02-05 22:50:41504 // e.g. because another chrome instance is running, or when invoked with
505 // options such as --uninstall or --try-chrome-again=0, the profile manager
506 // does not exist yet.
507 if (!profile_manager_.get())
508 return false;
509
[email protected]4ef795df2010-02-03 02:35:08510 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
511 profile = profile_manager_->GetDefaultProfile(user_data_dir);
[email protected]7bfc674322010-03-05 00:20:57512 if (!profile)
513 return false;
[email protected]4ef795df2010-02-03 02:35:08514 *profile_path = profile->GetPath();
515 return profile->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit);
516}
517
initial.commit09911bf2008-07-26 23:55:29518void BrowserProcessImpl::CreateResourceDispatcherHost() {
519 DCHECK(!created_resource_dispatcher_host_ &&
520 resource_dispatcher_host_.get() == NULL);
521 created_resource_dispatcher_host_ = true;
522
[email protected]79084c2d2009-11-03 23:12:42523 resource_dispatcher_host_.reset(new ResourceDispatcherHost());
initial.commit09911bf2008-07-26 23:55:29524 resource_dispatcher_host_->Initialize();
525}
526
527void BrowserProcessImpl::CreateMetricsService() {
528 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
529 created_metrics_service_ = true;
530
531 metrics_service_.reset(new MetricsService);
532}
533
534void BrowserProcessImpl::CreateIOThread() {
535 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
536 created_io_thread_ = true;
537
538 // Prior to starting the io thread, we create the plugin service as
539 // it is predominantly used from the io thread, but must be created
540 // on the main thread. The service ctor is inexpensive and does not
541 // invoke the io_thread() accessor.
542 PluginService::GetInstance();
543
[email protected]753efc42010-03-09 19:52:16544#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06545 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
546 // we start it now.
547 scoped_ptr<base::Thread> background_x11_thread(
548 new BrowserProcessSubThread(ChromeThread::BACKGROUND_X11));
549 if (!background_x11_thread->Start())
550 return;
551 background_x11_thread_.swap(background_x11_thread);
552#endif
553
[email protected]0ac83682010-01-22 17:46:27554 scoped_ptr<IOThread> thread(new IOThread);
[email protected]ab820df2008-08-26 05:55:10555 base::Thread::Options options;
556 options.message_loop_type = MessageLoop::TYPE_IO;
557 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29558 return;
559 io_thread_.swap(thread);
560}
561
562void BrowserProcessImpl::CreateFileThread() {
563 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
564 created_file_thread_ = true;
565
[email protected]ab820df2008-08-26 05:55:10566 scoped_ptr<base::Thread> thread(
567 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06568 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39569#if defined(OS_WIN)
570 // On Windows, the FILE thread needs to be have a UI message loop which pumps
571 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06572 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39573#else
574 options.message_loop_type = MessageLoop::TYPE_IO;
575#endif
[email protected]a1db3842008-09-17 22:04:06576 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29577 return;
578 file_thread_.swap(thread);
[email protected]27fbccc2010-03-25 03:00:49579
580 // ExtensionResource is in chrome/common, so it cannot depend on
581 // chrome/browser, which means it cannot lookup what the File thread is.
582 // We therefore store the thread ID from here so we can validate the proper
583 // thread usage in the ExtensionResource class.
584 ExtensionResource::set_file_thread_id(file_thread_->thread_id());
initial.commit09911bf2008-07-26 23:55:29585}
586
587void BrowserProcessImpl::CreateDBThread() {
588 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
589 created_db_thread_ = true;
590
[email protected]ab820df2008-08-26 05:55:10591 scoped_ptr<base::Thread> thread(
592 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29593 if (!thread->Start())
594 return;
595 db_thread_.swap(thread);
596}
597
[email protected]914511712009-11-23 19:42:33598void BrowserProcessImpl::CreateProcessLauncherThread() {
599 DCHECK(!created_process_launcher_thread_ && !process_launcher_thread_.get());
600 created_process_launcher_thread_ = true;
601
602 scoped_ptr<base::Thread> thread(
603 new BrowserProcessSubThread(ChromeThread::PROCESS_LAUNCHER));
604 if (!thread->Start())
605 return;
606 process_launcher_thread_.swap(thread);
607}
608
[email protected]875ee822010-05-18 20:58:01609void BrowserProcessImpl::CreateCacheThread() {
610 DCHECK(!created_cache_thread_ && !cache_thread_.get());
611 created_cache_thread_ = true;
612
613 scoped_ptr<base::Thread> thread(
614 new BrowserProcessSubThread(ChromeThread::CACHE));
615 base::Thread::Options options;
616 options.message_loop_type = MessageLoop::TYPE_IO;
617 if (!thread->StartWithOptions(options))
618 return;
619 cache_thread_.swap(thread);
620}
621
initial.commit09911bf2008-07-26 23:55:29622void BrowserProcessImpl::CreateProfileManager() {
623 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
624 created_profile_manager_ = true;
625
626 profile_manager_.reset(new ProfileManager());
627}
628
629void BrowserProcessImpl::CreateLocalState() {
630 DCHECK(!created_local_state_ && local_state_.get() == NULL);
631 created_local_state_ = true;
632
[email protected]b9636002009-03-04 00:05:25633 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29634 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]a9c23a52010-08-04 09:13:44635 local_state_.reset(PrefService::CreatePrefService(local_state_path, NULL));
[email protected]492d2142010-09-10 13:55:18636
637 // Make sure the the plugin updater gets notifications of changes
638 // in the plugin blacklist.
639 local_state_->RegisterListPref(prefs::kPluginsPluginsBlacklist);
640 plugin_state_change_registrar_.Init(local_state_.get());
641 plugin_state_change_registrar_.Add(prefs::kPluginsPluginsBlacklist,
642 PluginUpdater::GetPluginUpdater());
[email protected]fd6159a2010-09-03 09:38:39643}
initial.commit09911bf2008-07-26 23:55:29644
initial.commit09911bf2008-07-26 23:55:29645void BrowserProcessImpl::CreateIconManager() {
646 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
647 created_icon_manager_ = true;
648 icon_manager_.reset(new IconManager);
649}
650
[email protected]a9bf67982010-07-06 10:13:00651void BrowserProcessImpl::CreateDebuggerWrapper(int port, bool useHttp) {
initial.commit09911bf2008-07-26 23:55:29652 DCHECK(debugger_wrapper_.get() == NULL);
653 created_debugger_wrapper_ = true;
654
[email protected]a9bf67982010-07-06 10:13:00655 debugger_wrapper_ = new DebuggerWrapper(port, useHttp);
initial.commit09911bf2008-07-26 23:55:29656}
657
[email protected]40ecc902009-03-16 13:42:47658void BrowserProcessImpl::CreateDevToolsManager() {
[email protected]73ee01522009-06-05 10:13:44659 DCHECK(devtools_manager_.get() == NULL);
[email protected]40ecc902009-03-16 13:42:47660 created_devtools_manager_ = true;
[email protected]73ee01522009-06-05 10:13:44661 devtools_manager_ = new DevToolsManager();
[email protected]40ecc902009-03-16 13:42:47662}
663
[email protected]8b8e7c92010-08-19 18:05:56664void BrowserProcessImpl::CreateSidebarManager() {
665 DCHECK(sidebar_manager_.get() == NULL);
666 created_sidebar_manager_ = true;
667 sidebar_manager_ = new SidebarManager();
668}
669
initial.commit09911bf2008-07-26 23:55:29670void BrowserProcessImpl::CreateGoogleURLTracker() {
671 DCHECK(google_url_tracker_.get() == NULL);
672 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
673 google_url_tracker_.swap(google_url_tracker);
674}
[email protected]6641bf662009-08-21 00:34:09675
[email protected]c4ff4952010-01-08 19:12:47676void BrowserProcessImpl::CreateIntranetRedirectDetector() {
677 DCHECK(intranet_redirect_detector_.get() == NULL);
678 scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector(
679 new IntranetRedirectDetector);
680 intranet_redirect_detector_.swap(intranet_redirect_detector);
681}
682
[email protected]29672ab2009-10-30 03:44:03683void BrowserProcessImpl::CreateNotificationUIManager() {
684 DCHECK(notification_ui_manager_.get() == NULL);
685 notification_ui_manager_.reset(NotificationUIManager::Create());
686 created_notification_ui_manager_ = true;
687}
688
[email protected]0b4d3382010-07-14 16:13:04689void BrowserProcessImpl::CreateTabCloseableStateWatcher() {
690 DCHECK(tab_closeable_state_watcher_.get() == NULL);
691 tab_closeable_state_watcher_.reset(TabCloseableStateWatcher::Create());
692}
693
[email protected]6641bf662009-08-21 00:34:09694// The BrowserProcess object must outlive the file thread so we use traits
695// which don't do any management.
[email protected]c56428f22010-06-16 02:17:23696DISABLE_RUNNABLE_METHOD_REFCOUNT(BrowserProcessImpl);
[email protected]6641bf662009-08-21 00:34:09697
[email protected]d55aaa132009-09-28 21:08:04698#if defined(IPC_MESSAGE_LOG_ENABLED)
699
700void BrowserProcessImpl::SetIPCLoggingEnabled(bool enable) {
701 // First enable myself.
702 if (enable)
703 IPC::Logging::current()->Enable();
704 else
705 IPC::Logging::current()->Disable();
706
707 // Now tell subprocesses. Messages to ChildProcess-derived
708 // processes must be done on the IO thread.
709 io_thread()->message_loop()->PostTask
710 (FROM_HERE,
711 NewRunnableMethod(
712 this,
713 &BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses,
714 enable));
715
716 // Finally, tell the renderers which don't derive from ChildProcess.
717 // Messages to the renderers must be done on the UI (main) thread.
[email protected]019191a2009-10-02 20:37:27718 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
719 !i.IsAtEnd(); i.Advance())
720 i.GetCurrentValue()->Send(new ViewMsg_SetIPCLoggingEnabled(enable));
[email protected]d55aaa132009-09-28 21:08:04721}
722
723// Helper for SetIPCLoggingEnabled.
724void BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses(bool enabled) {
[email protected]d85cf072009-10-27 03:59:31725 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
[email protected]d55aaa132009-09-28 21:08:04726
[email protected]d27893f62010-07-03 05:47:42727 BrowserChildProcessHost::Iterator i; // default constr references a singleton
[email protected]34cf97d2009-09-29 22:46:11728 while (!i.Done()) {
729 i->Send(new PluginProcessMsg_SetIPCLoggingEnabled(enabled));
730 ++i;
[email protected]d55aaa132009-09-28 21:08:04731 }
732}
733
734#endif // IPC_MESSAGE_LOG_ENABLED
735
[email protected]6641bf662009-08-21 00:34:09736void BrowserProcessImpl::DoInspectorFilesCheck() {
737 // Runs on FILE thread.
738 DCHECK(file_thread_->message_loop() == MessageLoop::current());
739 bool result = false;
740
741 FilePath inspector_dir;
742 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) {
743 result = file_util::PathExists(inspector_dir);
744 }
745
[email protected]308080d2009-11-11 19:19:02746 have_inspector_files_ = result;
[email protected]6641bf662009-08-21 00:34:09747}
[email protected]bd48c2b02010-04-09 20:32:42748
[email protected]3cdacd42010-04-30 18:55:53749// Mac is currently not supported.
750#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
[email protected]bd48c2b02010-04-09 20:32:42751
752bool BrowserProcessImpl::CanAutorestartForUpdate() const {
753 // Check if browser is in the background and if it needs to be restarted to
754 // apply a pending update.
[email protected]c10fa3952010-08-24 16:52:53755 return BrowserList::size() == 0 && BrowserList::WillKeepAlive() &&
[email protected]c984d9f2010-07-20 20:52:20756 Upgrade::IsUpdatePendingRestart();
[email protected]bd48c2b02010-04-09 20:32:42757}
758
[email protected]5c08f222010-09-22 09:37:21759// Switches to add when auto-restarting Chrome.
[email protected]6c10c972010-08-11 04:15:47760const char* const kSwitchesToAddOnAutorestart[] = {
[email protected]f9a06842010-08-21 00:32:40761 switches::kNoStartupWindow
[email protected]bd48c2b02010-04-09 20:32:42762};
763
764void BrowserProcessImpl::RestartPersistentInstance() {
765 CommandLine* old_cl = CommandLine::ForCurrentProcess();
[email protected]3cdacd42010-04-30 18:55:53766 scoped_ptr<CommandLine> new_cl(new CommandLine(old_cl->GetProgram()));
[email protected]bd48c2b02010-04-09 20:32:42767
768 std::map<std::string, CommandLine::StringType> switches =
769 old_cl->GetSwitches();
770
[email protected]5c08f222010-09-22 09:37:21771 switches::RemoveSwitchesForAutostart(&switches);
[email protected]bd48c2b02010-04-09 20:32:42772
773 // Append the rest of the switches (along with their values, if any)
774 // to the new command line
775 for (std::map<std::string, CommandLine::StringType>::const_iterator i =
776 switches.begin(); i != switches.end(); ++i) {
777 CommandLine::StringType switch_value = i->second;
778 if (switch_value.length() > 0) {
[email protected]05076ba22010-07-30 05:59:57779 new_cl->AppendSwitchNative(i->first, i->second);
[email protected]bd48c2b02010-04-09 20:32:42780 } else {
[email protected]3cdacd42010-04-30 18:55:53781 new_cl->AppendSwitch(i->first);
[email protected]bd48c2b02010-04-09 20:32:42782 }
783 }
784
[email protected]6c10c972010-08-11 04:15:47785 // Ensure that our desired switches are set on the new process.
[email protected]5c08f222010-09-22 09:37:21786 for (size_t i = 0; i < arraysize(kSwitchesToAddOnAutorestart); ++i) {
[email protected]6c10c972010-08-11 04:15:47787 if (!new_cl->HasSwitch(kSwitchesToAddOnAutorestart[i]))
788 new_cl->AppendSwitch(kSwitchesToAddOnAutorestart[i]);
789 }
[email protected]bd48c2b02010-04-09 20:32:42790
[email protected]3cdacd42010-04-30 18:55:53791 DLOG(WARNING) << "Shutting down current instance of the browser.";
792 BrowserList::CloseAllBrowsersAndExit();
793
794 // Transfer ownership to Upgrade.
795 Upgrade::SetNewCommandLine(new_cl.release());
[email protected]bd48c2b02010-04-09 20:32:42796}
797
798void BrowserProcessImpl::OnAutoupdateTimer() {
799 if (CanAutorestartForUpdate()) {
[email protected]3cdacd42010-04-30 18:55:53800 DLOG(WARNING) << "Detected update. Restarting browser.";
[email protected]bd48c2b02010-04-09 20:32:42801 RestartPersistentInstance();
802 }
803}
804
[email protected]3cdacd42010-04-30 18:55:53805#endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)