blob: a1fbf55de03019e2807e51daa5c298b6ecdc48d9 [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]ac262c9f2008-10-19 17:45:2114#include "base/thread.h"
[email protected]1c4947f2009-01-15 22:25:1115#include "base/waitable_event.h"
[email protected]0082d7e0d2010-02-27 14:34:1216#include "chrome/browser/appcache/chrome_appcache_service.h"
[email protected]e13ad79b2010-07-22 21:36:5017#include "chrome/browser/automation/automation_provider_list.h"
[email protected]d27893f62010-07-03 05:47:4218#include "chrome/browser/browser_child_process_host.h"
[email protected]bd48c2b02010-04-09 20:32:4219#include "chrome/browser/browser_list.h"
[email protected]b443cb042009-12-15 22:05:0920#include "chrome/browser/browser_main.h"
[email protected]0ac83682010-01-22 17:46:2721#include "chrome/browser/browser_process_sub_thread.h"
[email protected]ac262c9f2008-10-19 17:45:2122#include "chrome/browser/browser_trial.h"
initial.commit09911bf2008-07-26 23:55:2923#include "chrome/browser/chrome_thread.h"
[email protected]4ab4b0f2009-02-10 18:54:5024#include "chrome/browser/debugger/debugger_wrapper.h"
[email protected]40ecc902009-03-16 13:42:4725#include "chrome/browser/debugger/devtools_manager.h"
[email protected]6c69796d2010-07-16 21:41:1626#include "chrome/browser/download/download_file_manager.h"
[email protected]5ba0a2c2009-02-19 01:19:3427#include "chrome/browser/download/save_file_manager.h"
[email protected]820735792010-07-29 23:40:0128#include "chrome/browser/first_run/first_run.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/browser/google_url_tracker.h"
[email protected]dcefa302009-05-20 00:24:3930#include "chrome/browser/icon_manager.h"
[email protected]4ef795df2010-02-03 02:35:0831#include "chrome/browser/in_process_webkit/dom_storage_context.h"
[email protected]c4ff4952010-01-08 19:12:4732#include "chrome/browser/intranet_redirect_detector.h"
[email protected]0ac83682010-01-22 17:46:2733#include "chrome/browser/io_thread.h"
[email protected]dc6f4962009-02-13 01:25:5034#include "chrome/browser/metrics/metrics_service.h"
[email protected]3530cd92010-06-27 06:22:0135#include "chrome/browser/net/predictor_api.h"
[email protected]d393a0fd2009-05-13 23:32:0136#include "chrome/browser/net/sdch_dictionary_fetcher.h"
[email protected]4ef795df2010-02-03 02:35:0837#include "chrome/browser/net/sqlite_persistent_cookie_store.h"
[email protected]29672ab2009-10-30 03:44:0338#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]fd49e2d2009-02-20 17:21:3039#include "chrome/browser/plugin_service.h"
[email protected]052313b2010-02-19 09:43:0840#include "chrome/browser/pref_service.h"
[email protected]e06131d2010-02-10 18:40:3341#include "chrome/browser/printing/print_job_manager.h"
initial.commit09911bf2008-07-26 23:55:2942#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2643#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]81218f42009-02-05 18:48:0844#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
45#include "chrome/browser/safe_browsing/safe_browsing_service.h"
[email protected]ccb55cf52010-03-06 22:02:0446#include "chrome/browser/status_icons/status_tray_manager.h"
[email protected]0b4d3382010-07-14 16:13:0447#include "chrome/browser/tab_closeable_state_watcher.h"
[email protected]4ef795df2010-02-03 02:35:0848#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2949#include "chrome/common/chrome_paths.h"
50#include "chrome/common/chrome_switches.h"
[email protected]27fbccc2010-03-25 03:00:4951#include "chrome/common/extensions/extension_resource.h"
[email protected]f1b6de22010-03-06 12:13:4752#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]ea587b02010-05-21 15:01:3553#include "chrome/common/json_pref_store.h"
initial.commit09911bf2008-07-26 23:55:2954#include "chrome/common/notification_service.h"
55#include "chrome/common/pref_names.h"
[email protected]4ef795df2010-02-03 02:35:0856#include "chrome/common/url_constants.h"
[email protected]bd48c2b02010-04-09 20:32:4257#include "chrome/installer/util/google_update_constants.h"
[email protected]d55aaa132009-09-28 21:08:0458#include "ipc/ipc_logging.h"
[email protected]4ef795df2010-02-03 02:35:0859#include "webkit/database/database_tracker.h"
[email protected]b112a4c2009-02-01 20:24:0160
61#if defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0562#include "views/focus/view_storage.h"
[email protected]86230b92009-11-23 20:38:3863#endif
64
[email protected]d55aaa132009-09-28 21:08:0465#if defined(IPC_MESSAGE_LOG_ENABLED)
66#include "chrome/common/plugin_messages.h"
67#include "chrome/common/render_messages.h"
68#endif
69
[email protected]3cdacd42010-04-30 18:55:5370#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
71// How often to check if the persistent instance of Chrome needs to restart
72// to install an update.
73static const int kUpdateCheckIntervalHours = 6;
74#endif
75
[email protected]bb975362009-01-21 01:00:2276BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
initial.commit09911bf2008-07-26 23:55:2977 : created_resource_dispatcher_host_(false),
78 created_metrics_service_(false),
79 created_io_thread_(false),
80 created_file_thread_(false),
81 created_db_thread_(false),
[email protected]914511712009-11-23 19:42:3382 created_process_launcher_thread_(false),
[email protected]875ee822010-05-18 20:58:0183 created_cache_thread_(false),
initial.commit09911bf2008-07-26 23:55:2984 created_profile_manager_(false),
85 created_local_state_(false),
[email protected]b112a4c2009-02-01 20:24:0186 created_icon_manager_(false),
87 created_debugger_wrapper_(false),
[email protected]40ecc902009-03-16 13:42:4788 created_devtools_manager_(false),
[email protected]29672ab2009-10-30 03:44:0389 created_notification_ui_manager_(false),
initial.commit09911bf2008-07-26 23:55:2990 module_ref_count_(0),
[email protected]afd20c022010-06-10 00:48:2091 did_start_(false),
[email protected]1b2db1a2008-08-08 17:46:1392 checked_for_new_frames_(false),
[email protected]6641bf662009-08-21 00:34:0993 using_new_frames_(false),
94 have_inspector_files_(true) {
initial.commit09911bf2008-07-26 23:55:2995 g_browser_process = this;
[email protected]1b8d02f12009-05-05 04:14:1196 clipboard_.reset(new Clipboard);
initial.commit09911bf2008-07-26 23:55:2997 main_notification_service_.reset(new NotificationService);
98
99 // Must be created after the NotificationService.
100 print_job_manager_.reset(new printing::PrintJobManager);
101
[email protected]b797e152009-01-23 16:06:14102 shutdown_event_.reset(new base::WaitableEvent(true, false));
initial.commit09911bf2008-07-26 23:55:29103}
104
105BrowserProcessImpl::~BrowserProcessImpl() {
[email protected]4ef795df2010-02-03 02:35:08106 FilePath profile_path;
107 bool clear_local_state_on_exit;
108
109 // Store the profile path for clearing local state data on exit.
110 clear_local_state_on_exit = ShouldClearLocalState(&profile_path);
111
initial.commit09911bf2008-07-26 23:55:29112 // Delete the AutomationProviderList before NotificationService,
113 // since it may try to unregister notifications
114 // Both NotificationService and AutomationProvider are singleton instances in
115 // the BrowserProcess. Since AutomationProvider may have some active
116 // notification observers, it is essential that it gets destroyed before the
117 // NotificationService. NotificationService won't be destroyed until after
118 // this destructor is run.
119 automation_provider_list_.reset();
120
[email protected]d393a0fd2009-05-13 23:32:01121 // We need to shutdown the SdchDictionaryFetcher as it regularly holds
122 // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do
123 // a PostDelayedTask onto the IO thread. This shutdown call will both discard
124 // any pending URLFetchers, and avoid creating any more.
125 SdchDictionaryFetcher::Shutdown();
126
[email protected]c4ff4952010-01-08 19:12:47127 // We need to destroy the MetricsService, GoogleURLTracker, and
128 // IntranetRedirectDetector before the io_thread_ gets destroyed, since their
129 // destructors can call the URLFetcher destructor, which does a
130 // PostDelayedTask operation on the IO thread. (The IO thread will handle
131 // that URLFetcher operation before going away.)
initial.commit09911bf2008-07-26 23:55:29132 metrics_service_.reset();
133 google_url_tracker_.reset();
[email protected]c4ff4952010-01-08 19:12:47134 intranet_redirect_detector_.reset();
initial.commit09911bf2008-07-26 23:55:29135
136 // Need to clear profiles (download managers) before the io_thread_.
137 profile_manager_.reset();
138
[email protected]97452c92010-08-05 02:04:13139 // Need to clear the desktop notification balloons before the io_thread_,
140 // since if there are any left showing we will post tasks.
141 notification_ui_manager_.reset();
142
initial.commit09911bf2008-07-26 23:55:29143 // Debugger must be cleaned up before IO thread and NotificationService.
144 debugger_wrapper_ = NULL;
145
146 if (resource_dispatcher_host_.get()) {
147 // Need to tell Safe Browsing Service that the IO thread is going away
148 // since it cached a pointer to it.
149 if (resource_dispatcher_host()->safe_browsing_service())
150 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
151
152 // Cancel pending requests and prevent new requests.
153 resource_dispatcher_host()->Shutdown();
154 }
155
[email protected]753efc42010-03-09 19:52:16156#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06157 // The IO thread must outlive the BACKGROUND_X11 thread.
158 background_x11_thread_.reset();
159#endif
160
initial.commit09911bf2008-07-26 23:55:29161 // Need to stop io_thread_ before resource_dispatcher_host_, since
162 // io_thread_ may still deref ResourceDispatcherHost and handle resource
163 // request before going away.
[email protected]0ac83682010-01-22 17:46:27164 io_thread_.reset();
initial.commit09911bf2008-07-26 23:55:29165
[email protected]875ee822010-05-18 20:58:01166 // The IO thread was the only user of this thread.
167 cache_thread_.reset();
168
[email protected]914511712009-11-23 19:42:33169 // Stop the process launcher thread after the IO thread, in case the IO thread
170 // posted a task to terminate a process on the process launcher thread.
171 process_launcher_thread_.reset();
172
initial.commit09911bf2008-07-26 23:55:29173 // Clean up state that lives on the file_thread_ before it goes away.
174 if (resource_dispatcher_host_.get()) {
175 resource_dispatcher_host()->download_file_manager()->Shutdown();
176 resource_dispatcher_host()->save_file_manager()->Shutdown();
177 }
178
179 // Need to stop the file_thread_ here to force it to process messages in its
180 // message loop from the previous call to shutdown the DownloadFileManager,
181 // SaveFileManager and SessionService.
182 file_thread_.reset();
183
184 // With the file_thread_ flushed, we can release any icon resources.
185 icon_manager_.reset();
186
187 // Need to destroy ResourceDispatcherHost before PluginService and
[email protected]49f28bc72010-02-04 00:10:01188 // SafeBrowsingService, since it caches a pointer to it. This also
189 // causes the webkit thread to terminate.
initial.commit09911bf2008-07-26 23:55:29190 resource_dispatcher_host_.reset();
191
192 // Wait for the pending print jobs to finish.
193 print_job_manager_->OnQuit();
194 print_job_manager_.reset();
195
[email protected]0b4d3382010-07-14 16:13:04196 // Destroy TabCloseableStateWatcher before NotificationService since the
197 // former registers for notifications.
198 tab_closeable_state_watcher_.reset();
199
initial.commit09911bf2008-07-26 23:55:29200 // Now OK to destroy NotificationService.
201 main_notification_service_.reset();
202
[email protected]49f28bc72010-02-04 00:10:01203 // Prior to clearing local state, we want to complete tasks pending
204 // on the db thread too.
205 db_thread_.reset();
206
207 // At this point, no render process exist and the file, io, db, and
208 // webkit threads in this process have all terminated, so it's safe
209 // to access local state data such as cookies, database, or local storage.
210 if (clear_local_state_on_exit)
211 ClearLocalState(profile_path);
212
initial.commit09911bf2008-07-26 23:55:29213 g_browser_process = NULL;
214}
215
[email protected]295039bd2008-08-15 04:32:57216// Send a QuitTask to the given MessageLoop.
217static void PostQuit(MessageLoop* message_loop) {
218 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
219}
initial.commit09911bf2008-07-26 23:55:29220
[email protected]b443cb042009-12-15 22:05:09221unsigned int BrowserProcessImpl::AddRefModule() {
222 DCHECK(CalledOnValidThread());
[email protected]afd20c022010-06-10 00:48:20223 did_start_ = true;
[email protected]b443cb042009-12-15 22:05:09224 module_ref_count_++;
225 return module_ref_count_;
226}
227
228unsigned int BrowserProcessImpl::ReleaseModule() {
229 DCHECK(CalledOnValidThread());
[email protected]760d970a2010-05-18 00:39:18230 DCHECK_NE(0u, module_ref_count_);
[email protected]b443cb042009-12-15 22:05:09231 module_ref_count_--;
232 if (0 == module_ref_count_) {
233 MessageLoop::current()->PostTask(
[email protected]34f73fb2010-03-24 20:50:34234 FROM_HERE, NewRunnableFunction(DidEndMainMessageLoop));
[email protected]b443cb042009-12-15 22:05:09235 MessageLoop::current()->Quit();
236 }
237 return module_ref_count_;
238}
239
initial.commit09911bf2008-07-26 23:55:29240void BrowserProcessImpl::EndSession() {
[email protected]b112a4c2009-02-01 20:24:01241#if defined(OS_WIN)
[email protected]d65cab7a2008-08-12 01:25:41242 // Notify we are going away.
[email protected]b797e152009-01-23 16:06:14243 ::SetEvent(shutdown_event_->handle());
[email protected]b112a4c2009-02-01 20:24:01244#endif
[email protected]d65cab7a2008-08-12 01:25:41245
initial.commit09911bf2008-07-26 23:55:29246 // Mark all the profiles as clean.
247 ProfileManager* pm = profile_manager();
248 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
249 (*i)->MarkAsCleanShutdown();
250
251 // Tell the metrics service it was cleanly shutdown.
252 MetricsService* metrics = g_browser_process->metrics_service();
253 if (metrics && local_state()) {
254 metrics->RecordCleanShutdown();
255
256 metrics->RecordStartOfSessionEnd();
257
258 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36259 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29260 }
261
262 // We must write that the profile and metrics service shutdown cleanly,
263 // otherwise on startup we'll think we crashed. So we block until done and
264 // then proceed with normal shutdown.
265 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57266 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29267 MessageLoop::current()->Run();
268}
269
[email protected]e13ad79b2010-07-22 21:36:50270ResourceDispatcherHost* BrowserProcessImpl::resource_dispatcher_host() {
271 DCHECK(CalledOnValidThread());
272 if (!created_resource_dispatcher_host_)
273 CreateResourceDispatcherHost();
274 return resource_dispatcher_host_.get();
275}
276
277MetricsService* BrowserProcessImpl::metrics_service() {
278 DCHECK(CalledOnValidThread());
279 if (!created_metrics_service_)
280 CreateMetricsService();
281 return metrics_service_.get();
282}
283
284IOThread* BrowserProcessImpl::io_thread() {
285 DCHECK(CalledOnValidThread());
286 if (!created_io_thread_)
287 CreateIOThread();
288 return io_thread_.get();
289}
290
291base::Thread* BrowserProcessImpl::file_thread() {
292 DCHECK(CalledOnValidThread());
293 if (!created_file_thread_)
294 CreateFileThread();
295 return file_thread_.get();
296}
297
298base::Thread* BrowserProcessImpl::db_thread() {
299 DCHECK(CalledOnValidThread());
300 if (!created_db_thread_)
301 CreateDBThread();
302 return db_thread_.get();
303}
304
305base::Thread* BrowserProcessImpl::process_launcher_thread() {
306 DCHECK(CalledOnValidThread());
307 if (!created_process_launcher_thread_)
308 CreateProcessLauncherThread();
309 return process_launcher_thread_.get();
310}
311
312base::Thread* BrowserProcessImpl::cache_thread() {
313 DCHECK(CalledOnValidThread());
314 if (!created_cache_thread_)
315 CreateCacheThread();
316 return cache_thread_.get();
317}
318
319#if defined(USE_X11)
320base::Thread* BrowserProcessImpl::background_x11_thread() {
321 DCHECK(CalledOnValidThread());
322 // The BACKGROUND_X11 thread is created when the IO thread is created.
323 if (!created_io_thread_)
324 CreateIOThread();
325 return background_x11_thread_.get();
326}
327#endif
328
329ProfileManager* BrowserProcessImpl::profile_manager() {
330 DCHECK(CalledOnValidThread());
331 if (!created_profile_manager_)
332 CreateProfileManager();
333 return profile_manager_.get();
334}
335
336PrefService* BrowserProcessImpl::local_state() {
337 DCHECK(CalledOnValidThread());
338 if (!created_local_state_)
339 CreateLocalState();
340 return local_state_.get();
341}
342
343DevToolsManager* BrowserProcessImpl::devtools_manager() {
344 DCHECK(CalledOnValidThread());
345 if (!created_devtools_manager_)
346 CreateDevToolsManager();
347 return devtools_manager_.get();
348}
349
350Clipboard* BrowserProcessImpl::clipboard() {
351 DCHECK(CalledOnValidThread());
352 return clipboard_.get();
353}
354
355NotificationUIManager* BrowserProcessImpl::notification_ui_manager() {
356 DCHECK(CalledOnValidThread());
357 if (!created_notification_ui_manager_)
358 CreateNotificationUIManager();
359 return notification_ui_manager_.get();
360}
361
362StatusTrayManager* BrowserProcessImpl::status_tray_manager() {
363 DCHECK(CalledOnValidThread());
364 if (!status_tray_manager_.get())
365 CreateStatusTrayManager();
366 return status_tray_manager_.get();
367}
368
369IconManager* BrowserProcessImpl::icon_manager() {
370 DCHECK(CalledOnValidThread());
371 if (!created_icon_manager_)
372 CreateIconManager();
373 return icon_manager_.get();
374}
375
376ThumbnailGenerator* BrowserProcessImpl::GetThumbnailGenerator() {
377 return &thumbnail_generator_;
378}
379
380AutomationProviderList* BrowserProcessImpl::InitAutomationProviderList() {
381 DCHECK(CalledOnValidThread());
382 if (automation_provider_list_.get() == NULL) {
383 automation_provider_list_.reset(AutomationProviderList::GetInstance());
384 }
385 return automation_provider_list_.get();
386}
387
388void BrowserProcessImpl::InitDebuggerWrapper(int port, bool useHttp) {
389 DCHECK(CalledOnValidThread());
390 if (!created_debugger_wrapper_)
391 CreateDebuggerWrapper(port, useHttp);
392}
393
394bool BrowserProcessImpl::IsShuttingDown() {
395 DCHECK(CalledOnValidThread());
396 return did_start_ && 0 == module_ref_count_;
397}
398
initial.commit09911bf2008-07-26 23:55:29399printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
400 // TODO(abarth): DCHECK(CalledOnValidThread());
[email protected]d8922f22010-05-07 00:57:27401 // http://code.google.com/p/chromium/issues/detail?id=6828
initial.commit09911bf2008-07-26 23:55:29402 // print_job_manager_ is initialized in the constructor and destroyed in the
403 // destructor, so it should always be valid.
404 DCHECK(print_job_manager_.get());
405 return print_job_manager_.get();
406}
407
[email protected]e13ad79b2010-07-22 21:36:50408GoogleURLTracker* BrowserProcessImpl::google_url_tracker() {
409 DCHECK(CalledOnValidThread());
410 if (!google_url_tracker_.get())
411 CreateGoogleURLTracker();
412 return google_url_tracker_.get();
413}
414
415IntranetRedirectDetector* BrowserProcessImpl::intranet_redirect_detector() {
416 DCHECK(CalledOnValidThread());
417 if (!intranet_redirect_detector_.get())
418 CreateIntranetRedirectDetector();
419 return intranet_redirect_detector_.get();
420}
421
422const std::string& BrowserProcessImpl::GetApplicationLocale() {
423 DCHECK(!locale_.empty());
424 return locale_;
425}
426
427void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) {
428 locale_ = locale;
429 extension_l10n_util::SetProcessLocale(locale);
430}
431
432base::WaitableEvent* BrowserProcessImpl::shutdown_event() {
433 return shutdown_event_.get();
434}
435
436TabCloseableStateWatcher* BrowserProcessImpl::tab_closeable_state_watcher() {
437 DCHECK(CalledOnValidThread());
438 if (!tab_closeable_state_watcher_.get())
439 CreateTabCloseableStateWatcher();
440 return tab_closeable_state_watcher_.get();
441}
442
443void BrowserProcessImpl::CheckForInspectorFiles() {
444 file_thread()->message_loop()->PostTask
445 (FROM_HERE,
446 NewRunnableMethod(this, &BrowserProcessImpl::DoInspectorFilesCheck));
447}
448
449#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
450void BrowserProcessImpl::StartAutoupdateTimer() {
451 autoupdate_timer_.Start(
452 base::TimeDelta::FromHours(kUpdateCheckIntervalHours),
453 this,
454 &BrowserProcessImpl::OnAutoupdateTimer);
455}
456#endif
457
458bool BrowserProcessImpl::have_inspector_files() const {
459 return have_inspector_files_;
460}
461
[email protected]4ef795df2010-02-03 02:35:08462void BrowserProcessImpl::ClearLocalState(const FilePath& profile_path) {
463 SQLitePersistentCookieStore::ClearLocalState(profile_path.Append(
464 chrome::kCookieFilename));
465 DOMStorageContext::ClearLocalState(profile_path, chrome::kExtensionScheme);
[email protected]72cfd90f2010-02-06 03:08:04466 webkit_database::DatabaseTracker::ClearLocalState(profile_path);
[email protected]10c934052010-02-04 19:58:31467 ChromeAppCacheService::ClearLocalState(profile_path);
[email protected]4ef795df2010-02-03 02:35:08468}
469
470bool BrowserProcessImpl::ShouldClearLocalState(FilePath* profile_path) {
471 FilePath user_data_dir;
472 Profile* profile;
473
[email protected]09584972010-07-03 00:00:59474 // Check for the existence of a profile manager. When quitting early,
[email protected]ed0cf562010-02-05 22:50:41475 // e.g. because another chrome instance is running, or when invoked with
476 // options such as --uninstall or --try-chrome-again=0, the profile manager
477 // does not exist yet.
478 if (!profile_manager_.get())
479 return false;
480
[email protected]4ef795df2010-02-03 02:35:08481 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
482 profile = profile_manager_->GetDefaultProfile(user_data_dir);
[email protected]7bfc674322010-03-05 00:20:57483 if (!profile)
484 return false;
[email protected]4ef795df2010-02-03 02:35:08485 *profile_path = profile->GetPath();
486 return profile->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit);
487}
488
initial.commit09911bf2008-07-26 23:55:29489void BrowserProcessImpl::CreateResourceDispatcherHost() {
490 DCHECK(!created_resource_dispatcher_host_ &&
491 resource_dispatcher_host_.get() == NULL);
492 created_resource_dispatcher_host_ = true;
493
[email protected]79084c2d2009-11-03 23:12:42494 resource_dispatcher_host_.reset(new ResourceDispatcherHost());
initial.commit09911bf2008-07-26 23:55:29495 resource_dispatcher_host_->Initialize();
496}
497
498void BrowserProcessImpl::CreateMetricsService() {
499 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
500 created_metrics_service_ = true;
501
502 metrics_service_.reset(new MetricsService);
503}
504
505void BrowserProcessImpl::CreateIOThread() {
506 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
507 created_io_thread_ = true;
508
509 // Prior to starting the io thread, we create the plugin service as
510 // it is predominantly used from the io thread, but must be created
511 // on the main thread. The service ctor is inexpensive and does not
512 // invoke the io_thread() accessor.
513 PluginService::GetInstance();
514
[email protected]753efc42010-03-09 19:52:16515#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06516 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
517 // we start it now.
518 scoped_ptr<base::Thread> background_x11_thread(
519 new BrowserProcessSubThread(ChromeThread::BACKGROUND_X11));
520 if (!background_x11_thread->Start())
521 return;
522 background_x11_thread_.swap(background_x11_thread);
523#endif
524
[email protected]0ac83682010-01-22 17:46:27525 scoped_ptr<IOThread> thread(new IOThread);
[email protected]ab820df2008-08-26 05:55:10526 base::Thread::Options options;
527 options.message_loop_type = MessageLoop::TYPE_IO;
528 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29529 return;
530 io_thread_.swap(thread);
531}
532
533void BrowserProcessImpl::CreateFileThread() {
534 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
535 created_file_thread_ = true;
536
[email protected]ab820df2008-08-26 05:55:10537 scoped_ptr<base::Thread> thread(
538 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06539 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39540#if defined(OS_WIN)
541 // On Windows, the FILE thread needs to be have a UI message loop which pumps
542 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06543 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39544#else
545 options.message_loop_type = MessageLoop::TYPE_IO;
546#endif
[email protected]a1db3842008-09-17 22:04:06547 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29548 return;
549 file_thread_.swap(thread);
[email protected]27fbccc2010-03-25 03:00:49550
551 // ExtensionResource is in chrome/common, so it cannot depend on
552 // chrome/browser, which means it cannot lookup what the File thread is.
553 // We therefore store the thread ID from here so we can validate the proper
554 // thread usage in the ExtensionResource class.
555 ExtensionResource::set_file_thread_id(file_thread_->thread_id());
initial.commit09911bf2008-07-26 23:55:29556}
557
558void BrowserProcessImpl::CreateDBThread() {
559 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
560 created_db_thread_ = true;
561
[email protected]ab820df2008-08-26 05:55:10562 scoped_ptr<base::Thread> thread(
563 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29564 if (!thread->Start())
565 return;
566 db_thread_.swap(thread);
567}
568
[email protected]914511712009-11-23 19:42:33569void BrowserProcessImpl::CreateProcessLauncherThread() {
570 DCHECK(!created_process_launcher_thread_ && !process_launcher_thread_.get());
571 created_process_launcher_thread_ = true;
572
573 scoped_ptr<base::Thread> thread(
574 new BrowserProcessSubThread(ChromeThread::PROCESS_LAUNCHER));
575 if (!thread->Start())
576 return;
577 process_launcher_thread_.swap(thread);
578}
579
[email protected]875ee822010-05-18 20:58:01580void BrowserProcessImpl::CreateCacheThread() {
581 DCHECK(!created_cache_thread_ && !cache_thread_.get());
582 created_cache_thread_ = true;
583
584 scoped_ptr<base::Thread> thread(
585 new BrowserProcessSubThread(ChromeThread::CACHE));
586 base::Thread::Options options;
587 options.message_loop_type = MessageLoop::TYPE_IO;
588 if (!thread->StartWithOptions(options))
589 return;
590 cache_thread_.swap(thread);
591}
592
initial.commit09911bf2008-07-26 23:55:29593void BrowserProcessImpl::CreateProfileManager() {
594 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
595 created_profile_manager_ = true;
596
597 profile_manager_.reset(new ProfileManager());
598}
599
600void BrowserProcessImpl::CreateLocalState() {
601 DCHECK(!created_local_state_ && local_state_.get() == NULL);
602 created_local_state_ = true;
603
[email protected]b9636002009-03-04 00:05:25604 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29605 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]a9c23a52010-08-04 09:13:44606 local_state_.reset(PrefService::CreatePrefService(local_state_path, NULL));
[email protected]d8b08c92010-06-07 13:13:28607 }
initial.commit09911bf2008-07-26 23:55:29608
initial.commit09911bf2008-07-26 23:55:29609void BrowserProcessImpl::CreateIconManager() {
610 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
611 created_icon_manager_ = true;
612 icon_manager_.reset(new IconManager);
613}
614
[email protected]a9bf67982010-07-06 10:13:00615void BrowserProcessImpl::CreateDebuggerWrapper(int port, bool useHttp) {
initial.commit09911bf2008-07-26 23:55:29616 DCHECK(debugger_wrapper_.get() == NULL);
617 created_debugger_wrapper_ = true;
618
[email protected]a9bf67982010-07-06 10:13:00619 debugger_wrapper_ = new DebuggerWrapper(port, useHttp);
initial.commit09911bf2008-07-26 23:55:29620}
621
[email protected]40ecc902009-03-16 13:42:47622void BrowserProcessImpl::CreateDevToolsManager() {
[email protected]73ee01522009-06-05 10:13:44623 DCHECK(devtools_manager_.get() == NULL);
[email protected]40ecc902009-03-16 13:42:47624 created_devtools_manager_ = true;
[email protected]73ee01522009-06-05 10:13:44625 devtools_manager_ = new DevToolsManager();
[email protected]40ecc902009-03-16 13:42:47626}
627
initial.commit09911bf2008-07-26 23:55:29628void BrowserProcessImpl::CreateGoogleURLTracker() {
629 DCHECK(google_url_tracker_.get() == NULL);
630 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
631 google_url_tracker_.swap(google_url_tracker);
632}
[email protected]6641bf662009-08-21 00:34:09633
[email protected]c4ff4952010-01-08 19:12:47634void BrowserProcessImpl::CreateIntranetRedirectDetector() {
635 DCHECK(intranet_redirect_detector_.get() == NULL);
636 scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector(
637 new IntranetRedirectDetector);
638 intranet_redirect_detector_.swap(intranet_redirect_detector);
639}
640
[email protected]29672ab2009-10-30 03:44:03641void BrowserProcessImpl::CreateNotificationUIManager() {
642 DCHECK(notification_ui_manager_.get() == NULL);
643 notification_ui_manager_.reset(NotificationUIManager::Create());
644 created_notification_ui_manager_ = true;
645}
646
[email protected]ccb55cf52010-03-06 22:02:04647void BrowserProcessImpl::CreateStatusTrayManager() {
648 DCHECK(status_tray_manager_.get() == NULL);
649 status_tray_manager_.reset(new StatusTrayManager());
650}
651
[email protected]0b4d3382010-07-14 16:13:04652void BrowserProcessImpl::CreateTabCloseableStateWatcher() {
653 DCHECK(tab_closeable_state_watcher_.get() == NULL);
654 tab_closeable_state_watcher_.reset(TabCloseableStateWatcher::Create());
655}
656
[email protected]6641bf662009-08-21 00:34:09657// The BrowserProcess object must outlive the file thread so we use traits
658// which don't do any management.
[email protected]c56428f22010-06-16 02:17:23659DISABLE_RUNNABLE_METHOD_REFCOUNT(BrowserProcessImpl);
[email protected]6641bf662009-08-21 00:34:09660
[email protected]d55aaa132009-09-28 21:08:04661#if defined(IPC_MESSAGE_LOG_ENABLED)
662
663void BrowserProcessImpl::SetIPCLoggingEnabled(bool enable) {
664 // First enable myself.
665 if (enable)
666 IPC::Logging::current()->Enable();
667 else
668 IPC::Logging::current()->Disable();
669
670 // Now tell subprocesses. Messages to ChildProcess-derived
671 // processes must be done on the IO thread.
672 io_thread()->message_loop()->PostTask
673 (FROM_HERE,
674 NewRunnableMethod(
675 this,
676 &BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses,
677 enable));
678
679 // Finally, tell the renderers which don't derive from ChildProcess.
680 // Messages to the renderers must be done on the UI (main) thread.
[email protected]019191a2009-10-02 20:37:27681 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
682 !i.IsAtEnd(); i.Advance())
683 i.GetCurrentValue()->Send(new ViewMsg_SetIPCLoggingEnabled(enable));
[email protected]d55aaa132009-09-28 21:08:04684}
685
686// Helper for SetIPCLoggingEnabled.
687void BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses(bool enabled) {
[email protected]d85cf072009-10-27 03:59:31688 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
[email protected]d55aaa132009-09-28 21:08:04689
[email protected]d27893f62010-07-03 05:47:42690 BrowserChildProcessHost::Iterator i; // default constr references a singleton
[email protected]34cf97d2009-09-29 22:46:11691 while (!i.Done()) {
692 i->Send(new PluginProcessMsg_SetIPCLoggingEnabled(enabled));
693 ++i;
[email protected]d55aaa132009-09-28 21:08:04694 }
695}
696
697#endif // IPC_MESSAGE_LOG_ENABLED
698
[email protected]6641bf662009-08-21 00:34:09699void BrowserProcessImpl::DoInspectorFilesCheck() {
700 // Runs on FILE thread.
701 DCHECK(file_thread_->message_loop() == MessageLoop::current());
702 bool result = false;
703
704 FilePath inspector_dir;
705 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) {
706 result = file_util::PathExists(inspector_dir);
707 }
708
[email protected]308080d2009-11-11 19:19:02709 have_inspector_files_ = result;
[email protected]6641bf662009-08-21 00:34:09710}
[email protected]bd48c2b02010-04-09 20:32:42711
[email protected]3cdacd42010-04-30 18:55:53712// Mac is currently not supported.
713#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
[email protected]bd48c2b02010-04-09 20:32:42714
715bool BrowserProcessImpl::CanAutorestartForUpdate() const {
716 // Check if browser is in the background and if it needs to be restarted to
717 // apply a pending update.
[email protected]c984d9f2010-07-20 20:52:20718 return BrowserList::size() == 0 && !BrowserList::WillKeepAlive() &&
719 Upgrade::IsUpdatePendingRestart();
[email protected]bd48c2b02010-04-09 20:32:42720}
721
722// Switches enumerated here will be removed when a background instance of
723// Chrome restarts itself. If your key is designed to only be used once,
724// or if it does not make sense when restarting a background instance to
725// pick up an automatic update, be sure to add it to this list.
726const char* const kSwitchesToRemoveOnAutorestart[] = {
[email protected]6c10c972010-08-11 04:15:47727 switches::kApp,
728 switches::kFirstRun,
729 switches::kImport,
730 switches::kImportFromFile,
731 switches::kMakeDefaultBrowser
732};
733
734const char* const kSwitchesToAddOnAutorestart[] = {
735 switches::kNoStartupWindow,
736 switches::kRestoreBackgroundContents
[email protected]bd48c2b02010-04-09 20:32:42737};
738
739void BrowserProcessImpl::RestartPersistentInstance() {
740 CommandLine* old_cl = CommandLine::ForCurrentProcess();
[email protected]3cdacd42010-04-30 18:55:53741 scoped_ptr<CommandLine> new_cl(new CommandLine(old_cl->GetProgram()));
[email protected]bd48c2b02010-04-09 20:32:42742
743 std::map<std::string, CommandLine::StringType> switches =
744 old_cl->GetSwitches();
745
746 // Remove the keys that we shouldn't pass through during restart.
[email protected]3cdacd42010-04-30 18:55:53747 for (size_t i = 0; i < arraysize(kSwitchesToRemoveOnAutorestart); i++) {
[email protected]bd48c2b02010-04-09 20:32:42748 switches.erase(kSwitchesToRemoveOnAutorestart[i]);
749 }
750
751 // Append the rest of the switches (along with their values, if any)
752 // to the new command line
753 for (std::map<std::string, CommandLine::StringType>::const_iterator i =
754 switches.begin(); i != switches.end(); ++i) {
755 CommandLine::StringType switch_value = i->second;
756 if (switch_value.length() > 0) {
[email protected]05076ba22010-07-30 05:59:57757 new_cl->AppendSwitchNative(i->first, i->second);
[email protected]bd48c2b02010-04-09 20:32:42758 } else {
[email protected]3cdacd42010-04-30 18:55:53759 new_cl->AppendSwitch(i->first);
[email protected]bd48c2b02010-04-09 20:32:42760 }
761 }
762
[email protected]6c10c972010-08-11 04:15:47763 // Ensure that our desired switches are set on the new process.
764 for (size_t i = 0; i < arraysize(kSwitchesToAddOnAutorestart); i++) {
765 if (!new_cl->HasSwitch(kSwitchesToAddOnAutorestart[i]))
766 new_cl->AppendSwitch(kSwitchesToAddOnAutorestart[i]);
767 }
[email protected]bd48c2b02010-04-09 20:32:42768
[email protected]3cdacd42010-04-30 18:55:53769 DLOG(WARNING) << "Shutting down current instance of the browser.";
770 BrowserList::CloseAllBrowsersAndExit();
771
772 // Transfer ownership to Upgrade.
773 Upgrade::SetNewCommandLine(new_cl.release());
[email protected]bd48c2b02010-04-09 20:32:42774}
775
776void BrowserProcessImpl::OnAutoupdateTimer() {
777 if (CanAutorestartForUpdate()) {
[email protected]3cdacd42010-04-30 18:55:53778 DLOG(WARNING) << "Detected update. Restarting browser.";
[email protected]bd48c2b02010-04-09 20:32:42779 RestartPersistentInstance();
780 }
781}
782
[email protected]3cdacd42010-04-30 18:55:53783#endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)