blob: 8dcdbb6349fc4680e1778bc5f3a90ff50a92b520 [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]8b8e7c92010-08-19 18:05:5646#include "chrome/browser/sidebar/sidebar_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]8b8e7c92010-08-19 18:05:5689 created_sidebar_manager_(false),
[email protected]29672ab2009-10-30 03:44:0390 created_notification_ui_manager_(false),
initial.commit09911bf2008-07-26 23:55:2991 module_ref_count_(0),
[email protected]afd20c022010-06-10 00:48:2092 did_start_(false),
[email protected]1b2db1a2008-08-08 17:46:1393 checked_for_new_frames_(false),
[email protected]6641bf662009-08-21 00:34:0994 using_new_frames_(false),
95 have_inspector_files_(true) {
initial.commit09911bf2008-07-26 23:55:2996 g_browser_process = this;
[email protected]1b8d02f12009-05-05 04:14:1197 clipboard_.reset(new Clipboard);
initial.commit09911bf2008-07-26 23:55:2998 main_notification_service_.reset(new NotificationService);
99
100 // Must be created after the NotificationService.
101 print_job_manager_.reset(new printing::PrintJobManager);
102
[email protected]b797e152009-01-23 16:06:14103 shutdown_event_.reset(new base::WaitableEvent(true, false));
initial.commit09911bf2008-07-26 23:55:29104}
105
106BrowserProcessImpl::~BrowserProcessImpl() {
[email protected]4ef795df2010-02-03 02:35:08107 FilePath profile_path;
108 bool clear_local_state_on_exit;
109
110 // Store the profile path for clearing local state data on exit.
111 clear_local_state_on_exit = ShouldClearLocalState(&profile_path);
112
initial.commit09911bf2008-07-26 23:55:29113 // Delete the AutomationProviderList before NotificationService,
114 // since it may try to unregister notifications
115 // Both NotificationService and AutomationProvider are singleton instances in
116 // the BrowserProcess. Since AutomationProvider may have some active
117 // notification observers, it is essential that it gets destroyed before the
118 // NotificationService. NotificationService won't be destroyed until after
119 // this destructor is run.
120 automation_provider_list_.reset();
121
[email protected]d393a0fd2009-05-13 23:32:01122 // We need to shutdown the SdchDictionaryFetcher as it regularly holds
123 // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do
124 // a PostDelayedTask onto the IO thread. This shutdown call will both discard
125 // any pending URLFetchers, and avoid creating any more.
126 SdchDictionaryFetcher::Shutdown();
127
[email protected]c4ff4952010-01-08 19:12:47128 // We need to destroy the MetricsService, GoogleURLTracker, and
129 // IntranetRedirectDetector before the io_thread_ gets destroyed, since their
130 // destructors can call the URLFetcher destructor, which does a
131 // PostDelayedTask operation on the IO thread. (The IO thread will handle
132 // that URLFetcher operation before going away.)
initial.commit09911bf2008-07-26 23:55:29133 metrics_service_.reset();
134 google_url_tracker_.reset();
[email protected]c4ff4952010-01-08 19:12:47135 intranet_redirect_detector_.reset();
initial.commit09911bf2008-07-26 23:55:29136
137 // Need to clear profiles (download managers) before the io_thread_.
138 profile_manager_.reset();
139
[email protected]97452c92010-08-05 02:04:13140 // Need to clear the desktop notification balloons before the io_thread_,
141 // since if there are any left showing we will post tasks.
142 notification_ui_manager_.reset();
143
initial.commit09911bf2008-07-26 23:55:29144 // Debugger must be cleaned up before IO thread and NotificationService.
145 debugger_wrapper_ = NULL;
146
147 if (resource_dispatcher_host_.get()) {
148 // Need to tell Safe Browsing Service that the IO thread is going away
149 // since it cached a pointer to it.
150 if (resource_dispatcher_host()->safe_browsing_service())
151 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
152
153 // Cancel pending requests and prevent new requests.
154 resource_dispatcher_host()->Shutdown();
155 }
156
[email protected]753efc42010-03-09 19:52:16157#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06158 // The IO thread must outlive the BACKGROUND_X11 thread.
159 background_x11_thread_.reset();
160#endif
161
initial.commit09911bf2008-07-26 23:55:29162 // Need to stop io_thread_ before resource_dispatcher_host_, since
163 // io_thread_ may still deref ResourceDispatcherHost and handle resource
164 // request before going away.
[email protected]0ac83682010-01-22 17:46:27165 io_thread_.reset();
initial.commit09911bf2008-07-26 23:55:29166
[email protected]875ee822010-05-18 20:58:01167 // The IO thread was the only user of this thread.
168 cache_thread_.reset();
169
[email protected]914511712009-11-23 19:42:33170 // Stop the process launcher thread after the IO thread, in case the IO thread
171 // posted a task to terminate a process on the process launcher thread.
172 process_launcher_thread_.reset();
173
initial.commit09911bf2008-07-26 23:55:29174 // Clean up state that lives on the file_thread_ before it goes away.
175 if (resource_dispatcher_host_.get()) {
176 resource_dispatcher_host()->download_file_manager()->Shutdown();
177 resource_dispatcher_host()->save_file_manager()->Shutdown();
178 }
179
180 // Need to stop the file_thread_ here to force it to process messages in its
181 // message loop from the previous call to shutdown the DownloadFileManager,
182 // SaveFileManager and SessionService.
183 file_thread_.reset();
184
185 // With the file_thread_ flushed, we can release any icon resources.
186 icon_manager_.reset();
187
188 // Need to destroy ResourceDispatcherHost before PluginService and
[email protected]49f28bc72010-02-04 00:10:01189 // SafeBrowsingService, since it caches a pointer to it. This also
190 // causes the webkit thread to terminate.
initial.commit09911bf2008-07-26 23:55:29191 resource_dispatcher_host_.reset();
192
193 // Wait for the pending print jobs to finish.
194 print_job_manager_->OnQuit();
195 print_job_manager_.reset();
196
[email protected]0b4d3382010-07-14 16:13:04197 // Destroy TabCloseableStateWatcher before NotificationService since the
198 // former registers for notifications.
199 tab_closeable_state_watcher_.reset();
200
initial.commit09911bf2008-07-26 23:55:29201 // Now OK to destroy NotificationService.
202 main_notification_service_.reset();
203
[email protected]49f28bc72010-02-04 00:10:01204 // Prior to clearing local state, we want to complete tasks pending
205 // on the db thread too.
206 db_thread_.reset();
207
208 // At this point, no render process exist and the file, io, db, and
209 // webkit threads in this process have all terminated, so it's safe
210 // to access local state data such as cookies, database, or local storage.
211 if (clear_local_state_on_exit)
212 ClearLocalState(profile_path);
213
initial.commit09911bf2008-07-26 23:55:29214 g_browser_process = NULL;
215}
216
[email protected]295039bd2008-08-15 04:32:57217// Send a QuitTask to the given MessageLoop.
218static void PostQuit(MessageLoop* message_loop) {
219 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
220}
initial.commit09911bf2008-07-26 23:55:29221
[email protected]b443cb042009-12-15 22:05:09222unsigned int BrowserProcessImpl::AddRefModule() {
223 DCHECK(CalledOnValidThread());
[email protected]afd20c022010-06-10 00:48:20224 did_start_ = true;
[email protected]b443cb042009-12-15 22:05:09225 module_ref_count_++;
226 return module_ref_count_;
227}
228
229unsigned int BrowserProcessImpl::ReleaseModule() {
230 DCHECK(CalledOnValidThread());
[email protected]760d970a2010-05-18 00:39:18231 DCHECK_NE(0u, module_ref_count_);
[email protected]b443cb042009-12-15 22:05:09232 module_ref_count_--;
233 if (0 == module_ref_count_) {
234 MessageLoop::current()->PostTask(
[email protected]34f73fb2010-03-24 20:50:34235 FROM_HERE, NewRunnableFunction(DidEndMainMessageLoop));
[email protected]b443cb042009-12-15 22:05:09236 MessageLoop::current()->Quit();
237 }
238 return module_ref_count_;
239}
240
initial.commit09911bf2008-07-26 23:55:29241void BrowserProcessImpl::EndSession() {
[email protected]b112a4c2009-02-01 20:24:01242#if defined(OS_WIN)
[email protected]d65cab7a2008-08-12 01:25:41243 // Notify we are going away.
[email protected]b797e152009-01-23 16:06:14244 ::SetEvent(shutdown_event_->handle());
[email protected]b112a4c2009-02-01 20:24:01245#endif
[email protected]d65cab7a2008-08-12 01:25:41246
initial.commit09911bf2008-07-26 23:55:29247 // Mark all the profiles as clean.
248 ProfileManager* pm = profile_manager();
249 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
250 (*i)->MarkAsCleanShutdown();
251
252 // Tell the metrics service it was cleanly shutdown.
253 MetricsService* metrics = g_browser_process->metrics_service();
254 if (metrics && local_state()) {
255 metrics->RecordCleanShutdown();
256
257 metrics->RecordStartOfSessionEnd();
258
259 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36260 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29261 }
262
263 // We must write that the profile and metrics service shutdown cleanly,
264 // otherwise on startup we'll think we crashed. So we block until done and
265 // then proceed with normal shutdown.
266 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57267 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29268 MessageLoop::current()->Run();
269}
270
[email protected]e13ad79b2010-07-22 21:36:50271ResourceDispatcherHost* BrowserProcessImpl::resource_dispatcher_host() {
272 DCHECK(CalledOnValidThread());
273 if (!created_resource_dispatcher_host_)
274 CreateResourceDispatcherHost();
275 return resource_dispatcher_host_.get();
276}
277
278MetricsService* BrowserProcessImpl::metrics_service() {
279 DCHECK(CalledOnValidThread());
280 if (!created_metrics_service_)
281 CreateMetricsService();
282 return metrics_service_.get();
283}
284
285IOThread* BrowserProcessImpl::io_thread() {
286 DCHECK(CalledOnValidThread());
287 if (!created_io_thread_)
288 CreateIOThread();
289 return io_thread_.get();
290}
291
292base::Thread* BrowserProcessImpl::file_thread() {
293 DCHECK(CalledOnValidThread());
294 if (!created_file_thread_)
295 CreateFileThread();
296 return file_thread_.get();
297}
298
299base::Thread* BrowserProcessImpl::db_thread() {
300 DCHECK(CalledOnValidThread());
301 if (!created_db_thread_)
302 CreateDBThread();
303 return db_thread_.get();
304}
305
306base::Thread* BrowserProcessImpl::process_launcher_thread() {
307 DCHECK(CalledOnValidThread());
308 if (!created_process_launcher_thread_)
309 CreateProcessLauncherThread();
310 return process_launcher_thread_.get();
311}
312
313base::Thread* BrowserProcessImpl::cache_thread() {
314 DCHECK(CalledOnValidThread());
315 if (!created_cache_thread_)
316 CreateCacheThread();
317 return cache_thread_.get();
318}
319
320#if defined(USE_X11)
321base::Thread* BrowserProcessImpl::background_x11_thread() {
322 DCHECK(CalledOnValidThread());
323 // The BACKGROUND_X11 thread is created when the IO thread is created.
324 if (!created_io_thread_)
325 CreateIOThread();
326 return background_x11_thread_.get();
327}
328#endif
329
330ProfileManager* BrowserProcessImpl::profile_manager() {
331 DCHECK(CalledOnValidThread());
332 if (!created_profile_manager_)
333 CreateProfileManager();
334 return profile_manager_.get();
335}
336
337PrefService* BrowserProcessImpl::local_state() {
338 DCHECK(CalledOnValidThread());
339 if (!created_local_state_)
340 CreateLocalState();
341 return local_state_.get();
342}
343
344DevToolsManager* BrowserProcessImpl::devtools_manager() {
345 DCHECK(CalledOnValidThread());
346 if (!created_devtools_manager_)
347 CreateDevToolsManager();
348 return devtools_manager_.get();
349}
350
[email protected]8b8e7c92010-08-19 18:05:56351SidebarManager* BrowserProcessImpl::sidebar_manager() {
352 DCHECK(CalledOnValidThread());
353 if (!created_sidebar_manager_)
354 CreateSidebarManager();
355 return sidebar_manager_.get();
356}
357
[email protected]e13ad79b2010-07-22 21:36:50358Clipboard* BrowserProcessImpl::clipboard() {
359 DCHECK(CalledOnValidThread());
360 return clipboard_.get();
361}
362
363NotificationUIManager* BrowserProcessImpl::notification_ui_manager() {
364 DCHECK(CalledOnValidThread());
365 if (!created_notification_ui_manager_)
366 CreateNotificationUIManager();
367 return notification_ui_manager_.get();
368}
369
[email protected]e13ad79b2010-07-22 21:36:50370IconManager* BrowserProcessImpl::icon_manager() {
371 DCHECK(CalledOnValidThread());
372 if (!created_icon_manager_)
373 CreateIconManager();
374 return icon_manager_.get();
375}
376
377ThumbnailGenerator* BrowserProcessImpl::GetThumbnailGenerator() {
378 return &thumbnail_generator_;
379}
380
381AutomationProviderList* BrowserProcessImpl::InitAutomationProviderList() {
382 DCHECK(CalledOnValidThread());
383 if (automation_provider_list_.get() == NULL) {
384 automation_provider_list_.reset(AutomationProviderList::GetInstance());
385 }
386 return automation_provider_list_.get();
387}
388
389void BrowserProcessImpl::InitDebuggerWrapper(int port, bool useHttp) {
390 DCHECK(CalledOnValidThread());
391 if (!created_debugger_wrapper_)
392 CreateDebuggerWrapper(port, useHttp);
393}
394
395bool BrowserProcessImpl::IsShuttingDown() {
396 DCHECK(CalledOnValidThread());
397 return did_start_ && 0 == module_ref_count_;
398}
399
initial.commit09911bf2008-07-26 23:55:29400printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
401 // TODO(abarth): DCHECK(CalledOnValidThread());
[email protected]d8922f22010-05-07 00:57:27402 // http://code.google.com/p/chromium/issues/detail?id=6828
initial.commit09911bf2008-07-26 23:55:29403 // print_job_manager_ is initialized in the constructor and destroyed in the
404 // destructor, so it should always be valid.
405 DCHECK(print_job_manager_.get());
406 return print_job_manager_.get();
407}
408
[email protected]e13ad79b2010-07-22 21:36:50409GoogleURLTracker* BrowserProcessImpl::google_url_tracker() {
410 DCHECK(CalledOnValidThread());
411 if (!google_url_tracker_.get())
412 CreateGoogleURLTracker();
413 return google_url_tracker_.get();
414}
415
416IntranetRedirectDetector* BrowserProcessImpl::intranet_redirect_detector() {
417 DCHECK(CalledOnValidThread());
418 if (!intranet_redirect_detector_.get())
419 CreateIntranetRedirectDetector();
420 return intranet_redirect_detector_.get();
421}
422
423const std::string& BrowserProcessImpl::GetApplicationLocale() {
424 DCHECK(!locale_.empty());
425 return locale_;
426}
427
428void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) {
429 locale_ = locale;
430 extension_l10n_util::SetProcessLocale(locale);
431}
432
433base::WaitableEvent* BrowserProcessImpl::shutdown_event() {
434 return shutdown_event_.get();
435}
436
437TabCloseableStateWatcher* BrowserProcessImpl::tab_closeable_state_watcher() {
438 DCHECK(CalledOnValidThread());
439 if (!tab_closeable_state_watcher_.get())
440 CreateTabCloseableStateWatcher();
441 return tab_closeable_state_watcher_.get();
442}
443
444void BrowserProcessImpl::CheckForInspectorFiles() {
445 file_thread()->message_loop()->PostTask
446 (FROM_HERE,
447 NewRunnableMethod(this, &BrowserProcessImpl::DoInspectorFilesCheck));
448}
449
450#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
451void BrowserProcessImpl::StartAutoupdateTimer() {
452 autoupdate_timer_.Start(
453 base::TimeDelta::FromHours(kUpdateCheckIntervalHours),
454 this,
455 &BrowserProcessImpl::OnAutoupdateTimer);
456}
457#endif
458
459bool BrowserProcessImpl::have_inspector_files() const {
460 return have_inspector_files_;
461}
462
[email protected]4ef795df2010-02-03 02:35:08463void BrowserProcessImpl::ClearLocalState(const FilePath& profile_path) {
464 SQLitePersistentCookieStore::ClearLocalState(profile_path.Append(
465 chrome::kCookieFilename));
466 DOMStorageContext::ClearLocalState(profile_path, chrome::kExtensionScheme);
[email protected]72cfd90f2010-02-06 03:08:04467 webkit_database::DatabaseTracker::ClearLocalState(profile_path);
[email protected]10c934052010-02-04 19:58:31468 ChromeAppCacheService::ClearLocalState(profile_path);
[email protected]4ef795df2010-02-03 02:35:08469}
470
471bool BrowserProcessImpl::ShouldClearLocalState(FilePath* profile_path) {
472 FilePath user_data_dir;
473 Profile* profile;
474
[email protected]09584972010-07-03 00:00:59475 // Check for the existence of a profile manager. When quitting early,
[email protected]ed0cf562010-02-05 22:50:41476 // e.g. because another chrome instance is running, or when invoked with
477 // options such as --uninstall or --try-chrome-again=0, the profile manager
478 // does not exist yet.
479 if (!profile_manager_.get())
480 return false;
481
[email protected]4ef795df2010-02-03 02:35:08482 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
483 profile = profile_manager_->GetDefaultProfile(user_data_dir);
[email protected]7bfc674322010-03-05 00:20:57484 if (!profile)
485 return false;
[email protected]4ef795df2010-02-03 02:35:08486 *profile_path = profile->GetPath();
487 return profile->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit);
488}
489
initial.commit09911bf2008-07-26 23:55:29490void BrowserProcessImpl::CreateResourceDispatcherHost() {
491 DCHECK(!created_resource_dispatcher_host_ &&
492 resource_dispatcher_host_.get() == NULL);
493 created_resource_dispatcher_host_ = true;
494
[email protected]79084c2d2009-11-03 23:12:42495 resource_dispatcher_host_.reset(new ResourceDispatcherHost());
initial.commit09911bf2008-07-26 23:55:29496 resource_dispatcher_host_->Initialize();
497}
498
499void BrowserProcessImpl::CreateMetricsService() {
500 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
501 created_metrics_service_ = true;
502
503 metrics_service_.reset(new MetricsService);
504}
505
506void BrowserProcessImpl::CreateIOThread() {
507 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
508 created_io_thread_ = true;
509
510 // Prior to starting the io thread, we create the plugin service as
511 // it is predominantly used from the io thread, but must be created
512 // on the main thread. The service ctor is inexpensive and does not
513 // invoke the io_thread() accessor.
514 PluginService::GetInstance();
515
[email protected]753efc42010-03-09 19:52:16516#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06517 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
518 // we start it now.
519 scoped_ptr<base::Thread> background_x11_thread(
520 new BrowserProcessSubThread(ChromeThread::BACKGROUND_X11));
521 if (!background_x11_thread->Start())
522 return;
523 background_x11_thread_.swap(background_x11_thread);
524#endif
525
[email protected]0ac83682010-01-22 17:46:27526 scoped_ptr<IOThread> thread(new IOThread);
[email protected]ab820df2008-08-26 05:55:10527 base::Thread::Options options;
528 options.message_loop_type = MessageLoop::TYPE_IO;
529 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29530 return;
531 io_thread_.swap(thread);
532}
533
534void BrowserProcessImpl::CreateFileThread() {
535 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
536 created_file_thread_ = true;
537
[email protected]ab820df2008-08-26 05:55:10538 scoped_ptr<base::Thread> thread(
539 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06540 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39541#if defined(OS_WIN)
542 // On Windows, the FILE thread needs to be have a UI message loop which pumps
543 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06544 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39545#else
546 options.message_loop_type = MessageLoop::TYPE_IO;
547#endif
[email protected]a1db3842008-09-17 22:04:06548 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29549 return;
550 file_thread_.swap(thread);
[email protected]27fbccc2010-03-25 03:00:49551
552 // ExtensionResource is in chrome/common, so it cannot depend on
553 // chrome/browser, which means it cannot lookup what the File thread is.
554 // We therefore store the thread ID from here so we can validate the proper
555 // thread usage in the ExtensionResource class.
556 ExtensionResource::set_file_thread_id(file_thread_->thread_id());
initial.commit09911bf2008-07-26 23:55:29557}
558
559void BrowserProcessImpl::CreateDBThread() {
560 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
561 created_db_thread_ = true;
562
[email protected]ab820df2008-08-26 05:55:10563 scoped_ptr<base::Thread> thread(
564 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29565 if (!thread->Start())
566 return;
567 db_thread_.swap(thread);
568}
569
[email protected]914511712009-11-23 19:42:33570void BrowserProcessImpl::CreateProcessLauncherThread() {
571 DCHECK(!created_process_launcher_thread_ && !process_launcher_thread_.get());
572 created_process_launcher_thread_ = true;
573
574 scoped_ptr<base::Thread> thread(
575 new BrowserProcessSubThread(ChromeThread::PROCESS_LAUNCHER));
576 if (!thread->Start())
577 return;
578 process_launcher_thread_.swap(thread);
579}
580
[email protected]875ee822010-05-18 20:58:01581void BrowserProcessImpl::CreateCacheThread() {
582 DCHECK(!created_cache_thread_ && !cache_thread_.get());
583 created_cache_thread_ = true;
584
585 scoped_ptr<base::Thread> thread(
586 new BrowserProcessSubThread(ChromeThread::CACHE));
587 base::Thread::Options options;
588 options.message_loop_type = MessageLoop::TYPE_IO;
589 if (!thread->StartWithOptions(options))
590 return;
591 cache_thread_.swap(thread);
592}
593
initial.commit09911bf2008-07-26 23:55:29594void BrowserProcessImpl::CreateProfileManager() {
595 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
596 created_profile_manager_ = true;
597
598 profile_manager_.reset(new ProfileManager());
599}
600
601void BrowserProcessImpl::CreateLocalState() {
602 DCHECK(!created_local_state_ && local_state_.get() == NULL);
603 created_local_state_ = true;
604
[email protected]b9636002009-03-04 00:05:25605 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29606 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]a9c23a52010-08-04 09:13:44607 local_state_.reset(PrefService::CreatePrefService(local_state_path, NULL));
[email protected]d8b08c92010-06-07 13:13:28608 }
initial.commit09911bf2008-07-26 23:55:29609
initial.commit09911bf2008-07-26 23:55:29610void BrowserProcessImpl::CreateIconManager() {
611 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
612 created_icon_manager_ = true;
613 icon_manager_.reset(new IconManager);
614}
615
[email protected]a9bf67982010-07-06 10:13:00616void BrowserProcessImpl::CreateDebuggerWrapper(int port, bool useHttp) {
initial.commit09911bf2008-07-26 23:55:29617 DCHECK(debugger_wrapper_.get() == NULL);
618 created_debugger_wrapper_ = true;
619
[email protected]a9bf67982010-07-06 10:13:00620 debugger_wrapper_ = new DebuggerWrapper(port, useHttp);
initial.commit09911bf2008-07-26 23:55:29621}
622
[email protected]40ecc902009-03-16 13:42:47623void BrowserProcessImpl::CreateDevToolsManager() {
[email protected]73ee01522009-06-05 10:13:44624 DCHECK(devtools_manager_.get() == NULL);
[email protected]40ecc902009-03-16 13:42:47625 created_devtools_manager_ = true;
[email protected]73ee01522009-06-05 10:13:44626 devtools_manager_ = new DevToolsManager();
[email protected]40ecc902009-03-16 13:42:47627}
628
[email protected]8b8e7c92010-08-19 18:05:56629void BrowserProcessImpl::CreateSidebarManager() {
630 DCHECK(sidebar_manager_.get() == NULL);
631 created_sidebar_manager_ = true;
632 sidebar_manager_ = new SidebarManager();
633}
634
initial.commit09911bf2008-07-26 23:55:29635void BrowserProcessImpl::CreateGoogleURLTracker() {
636 DCHECK(google_url_tracker_.get() == NULL);
637 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
638 google_url_tracker_.swap(google_url_tracker);
639}
[email protected]6641bf662009-08-21 00:34:09640
[email protected]c4ff4952010-01-08 19:12:47641void BrowserProcessImpl::CreateIntranetRedirectDetector() {
642 DCHECK(intranet_redirect_detector_.get() == NULL);
643 scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector(
644 new IntranetRedirectDetector);
645 intranet_redirect_detector_.swap(intranet_redirect_detector);
646}
647
[email protected]29672ab2009-10-30 03:44:03648void BrowserProcessImpl::CreateNotificationUIManager() {
649 DCHECK(notification_ui_manager_.get() == NULL);
650 notification_ui_manager_.reset(NotificationUIManager::Create());
651 created_notification_ui_manager_ = true;
652}
653
[email protected]0b4d3382010-07-14 16:13:04654void BrowserProcessImpl::CreateTabCloseableStateWatcher() {
655 DCHECK(tab_closeable_state_watcher_.get() == NULL);
656 tab_closeable_state_watcher_.reset(TabCloseableStateWatcher::Create());
657}
658
[email protected]6641bf662009-08-21 00:34:09659// The BrowserProcess object must outlive the file thread so we use traits
660// which don't do any management.
[email protected]c56428f22010-06-16 02:17:23661DISABLE_RUNNABLE_METHOD_REFCOUNT(BrowserProcessImpl);
[email protected]6641bf662009-08-21 00:34:09662
[email protected]d55aaa132009-09-28 21:08:04663#if defined(IPC_MESSAGE_LOG_ENABLED)
664
665void BrowserProcessImpl::SetIPCLoggingEnabled(bool enable) {
666 // First enable myself.
667 if (enable)
668 IPC::Logging::current()->Enable();
669 else
670 IPC::Logging::current()->Disable();
671
672 // Now tell subprocesses. Messages to ChildProcess-derived
673 // processes must be done on the IO thread.
674 io_thread()->message_loop()->PostTask
675 (FROM_HERE,
676 NewRunnableMethod(
677 this,
678 &BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses,
679 enable));
680
681 // Finally, tell the renderers which don't derive from ChildProcess.
682 // Messages to the renderers must be done on the UI (main) thread.
[email protected]019191a2009-10-02 20:37:27683 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
684 !i.IsAtEnd(); i.Advance())
685 i.GetCurrentValue()->Send(new ViewMsg_SetIPCLoggingEnabled(enable));
[email protected]d55aaa132009-09-28 21:08:04686}
687
688// Helper for SetIPCLoggingEnabled.
689void BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses(bool enabled) {
[email protected]d85cf072009-10-27 03:59:31690 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
[email protected]d55aaa132009-09-28 21:08:04691
[email protected]d27893f62010-07-03 05:47:42692 BrowserChildProcessHost::Iterator i; // default constr references a singleton
[email protected]34cf97d2009-09-29 22:46:11693 while (!i.Done()) {
694 i->Send(new PluginProcessMsg_SetIPCLoggingEnabled(enabled));
695 ++i;
[email protected]d55aaa132009-09-28 21:08:04696 }
697}
698
699#endif // IPC_MESSAGE_LOG_ENABLED
700
[email protected]6641bf662009-08-21 00:34:09701void BrowserProcessImpl::DoInspectorFilesCheck() {
702 // Runs on FILE thread.
703 DCHECK(file_thread_->message_loop() == MessageLoop::current());
704 bool result = false;
705
706 FilePath inspector_dir;
707 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) {
708 result = file_util::PathExists(inspector_dir);
709 }
710
[email protected]308080d2009-11-11 19:19:02711 have_inspector_files_ = result;
[email protected]6641bf662009-08-21 00:34:09712}
[email protected]bd48c2b02010-04-09 20:32:42713
[email protected]3cdacd42010-04-30 18:55:53714// Mac is currently not supported.
715#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
[email protected]bd48c2b02010-04-09 20:32:42716
717bool BrowserProcessImpl::CanAutorestartForUpdate() const {
718 // Check if browser is in the background and if it needs to be restarted to
719 // apply a pending update.
[email protected]c10fa3952010-08-24 16:52:53720 return BrowserList::size() == 0 && BrowserList::WillKeepAlive() &&
[email protected]c984d9f2010-07-20 20:52:20721 Upgrade::IsUpdatePendingRestart();
[email protected]bd48c2b02010-04-09 20:32:42722}
723
724// Switches enumerated here will be removed when a background instance of
725// Chrome restarts itself. If your key is designed to only be used once,
726// or if it does not make sense when restarting a background instance to
727// pick up an automatic update, be sure to add it to this list.
728const char* const kSwitchesToRemoveOnAutorestart[] = {
[email protected]6c10c972010-08-11 04:15:47729 switches::kApp,
730 switches::kFirstRun,
731 switches::kImport,
732 switches::kImportFromFile,
733 switches::kMakeDefaultBrowser
734};
735
736const char* const kSwitchesToAddOnAutorestart[] = {
[email protected]f9a06842010-08-21 00:32:40737 switches::kNoStartupWindow
[email protected]bd48c2b02010-04-09 20:32:42738};
739
740void BrowserProcessImpl::RestartPersistentInstance() {
741 CommandLine* old_cl = CommandLine::ForCurrentProcess();
[email protected]3cdacd42010-04-30 18:55:53742 scoped_ptr<CommandLine> new_cl(new CommandLine(old_cl->GetProgram()));
[email protected]bd48c2b02010-04-09 20:32:42743
744 std::map<std::string, CommandLine::StringType> switches =
745 old_cl->GetSwitches();
746
747 // Remove the keys that we shouldn't pass through during restart.
[email protected]3cdacd42010-04-30 18:55:53748 for (size_t i = 0; i < arraysize(kSwitchesToRemoveOnAutorestart); i++) {
[email protected]bd48c2b02010-04-09 20:32:42749 switches.erase(kSwitchesToRemoveOnAutorestart[i]);
750 }
751
752 // Append the rest of the switches (along with their values, if any)
753 // to the new command line
754 for (std::map<std::string, CommandLine::StringType>::const_iterator i =
755 switches.begin(); i != switches.end(); ++i) {
756 CommandLine::StringType switch_value = i->second;
757 if (switch_value.length() > 0) {
[email protected]05076ba22010-07-30 05:59:57758 new_cl->AppendSwitchNative(i->first, i->second);
[email protected]bd48c2b02010-04-09 20:32:42759 } else {
[email protected]3cdacd42010-04-30 18:55:53760 new_cl->AppendSwitch(i->first);
[email protected]bd48c2b02010-04-09 20:32:42761 }
762 }
763
[email protected]6c10c972010-08-11 04:15:47764 // Ensure that our desired switches are set on the new process.
765 for (size_t i = 0; i < arraysize(kSwitchesToAddOnAutorestart); i++) {
766 if (!new_cl->HasSwitch(kSwitchesToAddOnAutorestart[i]))
767 new_cl->AppendSwitch(kSwitchesToAddOnAutorestart[i]);
768 }
[email protected]bd48c2b02010-04-09 20:32:42769
[email protected]3cdacd42010-04-30 18:55:53770 DLOG(WARNING) << "Shutting down current instance of the browser.";
771 BrowserList::CloseAllBrowsersAndExit();
772
773 // Transfer ownership to Upgrade.
774 Upgrade::SetNewCommandLine(new_cl.release());
[email protected]bd48c2b02010-04-09 20:32:42775}
776
777void BrowserProcessImpl::OnAutoupdateTimer() {
778 if (CanAutorestartForUpdate()) {
[email protected]3cdacd42010-04-30 18:55:53779 DLOG(WARNING) << "Detected update. Restarting browser.";
[email protected]bd48c2b02010-04-09 20:32:42780 RestartPersistentInstance();
781 }
782}
783
[email protected]3cdacd42010-04-30 18:55:53784#endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)