blob: b855ffbe1732cd824c0eb5da04e3891c399ef851 [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
155 // 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);
[email protected]d04e7662010-10-10 22:24:48286 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]c6032e82010-09-13 20:06:05287 NewRunnableFunction(Signal, &done_writing));
288 done_writing.TimedWait(
289 base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds));
290#elif defined(OS_WIN)
[email protected]d04e7662010-10-10 22:24:48291 BrowserThread::PostTask(BrowserThread::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
[email protected]073ed7b2010-09-27 09:20:02461DownloadStatusUpdater* BrowserProcessImpl::download_status_updater() {
462 return &download_status_updater_;
463}
464
[email protected]e13ad79b2010-07-22 21:36:50465base::WaitableEvent* BrowserProcessImpl::shutdown_event() {
466 return shutdown_event_.get();
467}
468
469TabCloseableStateWatcher* BrowserProcessImpl::tab_closeable_state_watcher() {
470 DCHECK(CalledOnValidThread());
471 if (!tab_closeable_state_watcher_.get())
472 CreateTabCloseableStateWatcher();
473 return tab_closeable_state_watcher_.get();
474}
475
476void BrowserProcessImpl::CheckForInspectorFiles() {
477 file_thread()->message_loop()->PostTask
478 (FROM_HERE,
479 NewRunnableMethod(this, &BrowserProcessImpl::DoInspectorFilesCheck));
480}
481
482#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
483void BrowserProcessImpl::StartAutoupdateTimer() {
484 autoupdate_timer_.Start(
485 base::TimeDelta::FromHours(kUpdateCheckIntervalHours),
486 this,
487 &BrowserProcessImpl::OnAutoupdateTimer);
488}
489#endif
490
491bool BrowserProcessImpl::have_inspector_files() const {
492 return have_inspector_files_;
493}
494
[email protected]4ef795df2010-02-03 02:35:08495void BrowserProcessImpl::ClearLocalState(const FilePath& profile_path) {
496 SQLitePersistentCookieStore::ClearLocalState(profile_path.Append(
497 chrome::kCookieFilename));
498 DOMStorageContext::ClearLocalState(profile_path, chrome::kExtensionScheme);
[email protected]72cfd90f2010-02-06 03:08:04499 webkit_database::DatabaseTracker::ClearLocalState(profile_path);
[email protected]10c934052010-02-04 19:58:31500 ChromeAppCacheService::ClearLocalState(profile_path);
[email protected]4ef795df2010-02-03 02:35:08501}
502
503bool BrowserProcessImpl::ShouldClearLocalState(FilePath* profile_path) {
504 FilePath user_data_dir;
505 Profile* profile;
506
[email protected]09584972010-07-03 00:00:59507 // Check for the existence of a profile manager. When quitting early,
[email protected]ed0cf562010-02-05 22:50:41508 // e.g. because another chrome instance is running, or when invoked with
509 // options such as --uninstall or --try-chrome-again=0, the profile manager
510 // does not exist yet.
511 if (!profile_manager_.get())
512 return false;
513
[email protected]4ef795df2010-02-03 02:35:08514 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
515 profile = profile_manager_->GetDefaultProfile(user_data_dir);
[email protected]7bfc674322010-03-05 00:20:57516 if (!profile)
517 return false;
[email protected]4ef795df2010-02-03 02:35:08518 *profile_path = profile->GetPath();
519 return profile->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit);
520}
521
initial.commit09911bf2008-07-26 23:55:29522void BrowserProcessImpl::CreateResourceDispatcherHost() {
523 DCHECK(!created_resource_dispatcher_host_ &&
524 resource_dispatcher_host_.get() == NULL);
525 created_resource_dispatcher_host_ = true;
526
[email protected]79084c2d2009-11-03 23:12:42527 resource_dispatcher_host_.reset(new ResourceDispatcherHost());
initial.commit09911bf2008-07-26 23:55:29528 resource_dispatcher_host_->Initialize();
529}
530
531void BrowserProcessImpl::CreateMetricsService() {
532 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
533 created_metrics_service_ = true;
534
535 metrics_service_.reset(new MetricsService);
536}
537
538void BrowserProcessImpl::CreateIOThread() {
539 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
540 created_io_thread_ = true;
541
542 // Prior to starting the io thread, we create the plugin service as
543 // it is predominantly used from the io thread, but must be created
544 // on the main thread. The service ctor is inexpensive and does not
545 // invoke the io_thread() accessor.
546 PluginService::GetInstance();
547
[email protected]753efc42010-03-09 19:52:16548#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06549 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
550 // we start it now.
551 scoped_ptr<base::Thread> background_x11_thread(
[email protected]d04e7662010-10-10 22:24:48552 new BrowserProcessSubThread(BrowserThread::BACKGROUND_X11));
[email protected]4c3cd7412009-04-22 17:56:06553 if (!background_x11_thread->Start())
554 return;
555 background_x11_thread_.swap(background_x11_thread);
556#endif
557
[email protected]0ac83682010-01-22 17:46:27558 scoped_ptr<IOThread> thread(new IOThread);
[email protected]ab820df2008-08-26 05:55:10559 base::Thread::Options options;
560 options.message_loop_type = MessageLoop::TYPE_IO;
561 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29562 return;
563 io_thread_.swap(thread);
564}
565
566void BrowserProcessImpl::CreateFileThread() {
567 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
568 created_file_thread_ = true;
569
[email protected]ab820df2008-08-26 05:55:10570 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48571 new BrowserProcessSubThread(BrowserThread::FILE));
[email protected]a1db3842008-09-17 22:04:06572 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39573#if defined(OS_WIN)
574 // On Windows, the FILE thread needs to be have a UI message loop which pumps
575 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06576 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39577#else
578 options.message_loop_type = MessageLoop::TYPE_IO;
579#endif
[email protected]a1db3842008-09-17 22:04:06580 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29581 return;
582 file_thread_.swap(thread);
[email protected]27fbccc2010-03-25 03:00:49583
584 // ExtensionResource is in chrome/common, so it cannot depend on
585 // chrome/browser, which means it cannot lookup what the File thread is.
586 // We therefore store the thread ID from here so we can validate the proper
587 // thread usage in the ExtensionResource class.
588 ExtensionResource::set_file_thread_id(file_thread_->thread_id());
initial.commit09911bf2008-07-26 23:55:29589}
590
591void BrowserProcessImpl::CreateDBThread() {
592 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
593 created_db_thread_ = true;
594
[email protected]ab820df2008-08-26 05:55:10595 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48596 new BrowserProcessSubThread(BrowserThread::DB));
initial.commit09911bf2008-07-26 23:55:29597 if (!thread->Start())
598 return;
599 db_thread_.swap(thread);
600}
601
[email protected]914511712009-11-23 19:42:33602void BrowserProcessImpl::CreateProcessLauncherThread() {
603 DCHECK(!created_process_launcher_thread_ && !process_launcher_thread_.get());
604 created_process_launcher_thread_ = true;
605
606 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48607 new BrowserProcessSubThread(BrowserThread::PROCESS_LAUNCHER));
[email protected]914511712009-11-23 19:42:33608 if (!thread->Start())
609 return;
610 process_launcher_thread_.swap(thread);
611}
612
[email protected]875ee822010-05-18 20:58:01613void BrowserProcessImpl::CreateCacheThread() {
614 DCHECK(!created_cache_thread_ && !cache_thread_.get());
615 created_cache_thread_ = true;
616
617 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48618 new BrowserProcessSubThread(BrowserThread::CACHE));
[email protected]875ee822010-05-18 20:58:01619 base::Thread::Options options;
620 options.message_loop_type = MessageLoop::TYPE_IO;
621 if (!thread->StartWithOptions(options))
622 return;
623 cache_thread_.swap(thread);
624}
625
initial.commit09911bf2008-07-26 23:55:29626void BrowserProcessImpl::CreateProfileManager() {
627 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
628 created_profile_manager_ = true;
629
630 profile_manager_.reset(new ProfileManager());
631}
632
633void BrowserProcessImpl::CreateLocalState() {
634 DCHECK(!created_local_state_ && local_state_.get() == NULL);
635 created_local_state_ = true;
636
[email protected]b9636002009-03-04 00:05:25637 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29638 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]a9c23a52010-08-04 09:13:44639 local_state_.reset(PrefService::CreatePrefService(local_state_path, NULL));
[email protected]492d2142010-09-10 13:55:18640
[email protected]2a7e7c152010-10-01 20:12:09641 pref_change_registrar_.Init(local_state_.get());
642
[email protected]492d2142010-09-10 13:55:18643 // Make sure the the plugin updater gets notifications of changes
644 // in the plugin blacklist.
645 local_state_->RegisterListPref(prefs::kPluginsPluginsBlacklist);
[email protected]2a7e7c152010-10-01 20:12:09646 pref_change_registrar_.Add(prefs::kPluginsPluginsBlacklist,
647 PluginUpdater::GetPluginUpdater());
648
649 // Initialize and set up notifications for the printing enabled
650 // preference.
651 local_state_->RegisterBooleanPref(prefs::kPrintingEnabled, true);
652 bool printing_enabled =
653 local_state_->GetBoolean(prefs::kPrintingEnabled);
654 print_job_manager_->set_printing_enabled(printing_enabled);
655 pref_change_registrar_.Add(prefs::kPrintingEnabled,
656 print_job_manager_.get());
[email protected]fd6159a2010-09-03 09:38:39657}
initial.commit09911bf2008-07-26 23:55:29658
initial.commit09911bf2008-07-26 23:55:29659void BrowserProcessImpl::CreateIconManager() {
660 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
661 created_icon_manager_ = true;
662 icon_manager_.reset(new IconManager);
663}
664
[email protected]a9bf67982010-07-06 10:13:00665void BrowserProcessImpl::CreateDebuggerWrapper(int port, bool useHttp) {
initial.commit09911bf2008-07-26 23:55:29666 DCHECK(debugger_wrapper_.get() == NULL);
667 created_debugger_wrapper_ = true;
668
[email protected]a9bf67982010-07-06 10:13:00669 debugger_wrapper_ = new DebuggerWrapper(port, useHttp);
initial.commit09911bf2008-07-26 23:55:29670}
671
[email protected]40ecc902009-03-16 13:42:47672void BrowserProcessImpl::CreateDevToolsManager() {
[email protected]73ee01522009-06-05 10:13:44673 DCHECK(devtools_manager_.get() == NULL);
[email protected]40ecc902009-03-16 13:42:47674 created_devtools_manager_ = true;
[email protected]73ee01522009-06-05 10:13:44675 devtools_manager_ = new DevToolsManager();
[email protected]40ecc902009-03-16 13:42:47676}
677
[email protected]8b8e7c92010-08-19 18:05:56678void BrowserProcessImpl::CreateSidebarManager() {
679 DCHECK(sidebar_manager_.get() == NULL);
680 created_sidebar_manager_ = true;
681 sidebar_manager_ = new SidebarManager();
682}
683
initial.commit09911bf2008-07-26 23:55:29684void BrowserProcessImpl::CreateGoogleURLTracker() {
685 DCHECK(google_url_tracker_.get() == NULL);
686 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
687 google_url_tracker_.swap(google_url_tracker);
688}
[email protected]6641bf662009-08-21 00:34:09689
[email protected]c4ff4952010-01-08 19:12:47690void BrowserProcessImpl::CreateIntranetRedirectDetector() {
691 DCHECK(intranet_redirect_detector_.get() == NULL);
692 scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector(
693 new IntranetRedirectDetector);
694 intranet_redirect_detector_.swap(intranet_redirect_detector);
695}
696
[email protected]29672ab2009-10-30 03:44:03697void BrowserProcessImpl::CreateNotificationUIManager() {
698 DCHECK(notification_ui_manager_.get() == NULL);
699 notification_ui_manager_.reset(NotificationUIManager::Create());
700 created_notification_ui_manager_ = true;
701}
702
[email protected]0b4d3382010-07-14 16:13:04703void BrowserProcessImpl::CreateTabCloseableStateWatcher() {
704 DCHECK(tab_closeable_state_watcher_.get() == NULL);
705 tab_closeable_state_watcher_.reset(TabCloseableStateWatcher::Create());
706}
707
[email protected]6641bf662009-08-21 00:34:09708// The BrowserProcess object must outlive the file thread so we use traits
709// which don't do any management.
[email protected]c56428f22010-06-16 02:17:23710DISABLE_RUNNABLE_METHOD_REFCOUNT(BrowserProcessImpl);
[email protected]6641bf662009-08-21 00:34:09711
[email protected]d55aaa132009-09-28 21:08:04712#if defined(IPC_MESSAGE_LOG_ENABLED)
713
714void BrowserProcessImpl::SetIPCLoggingEnabled(bool enable) {
715 // First enable myself.
716 if (enable)
717 IPC::Logging::current()->Enable();
718 else
719 IPC::Logging::current()->Disable();
720
721 // Now tell subprocesses. Messages to ChildProcess-derived
722 // processes must be done on the IO thread.
723 io_thread()->message_loop()->PostTask
724 (FROM_HERE,
725 NewRunnableMethod(
726 this,
727 &BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses,
728 enable));
729
730 // Finally, tell the renderers which don't derive from ChildProcess.
731 // Messages to the renderers must be done on the UI (main) thread.
[email protected]019191a2009-10-02 20:37:27732 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
733 !i.IsAtEnd(); i.Advance())
734 i.GetCurrentValue()->Send(new ViewMsg_SetIPCLoggingEnabled(enable));
[email protected]d55aaa132009-09-28 21:08:04735}
736
737// Helper for SetIPCLoggingEnabled.
738void BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses(bool enabled) {
[email protected]d04e7662010-10-10 22:24:48739 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]d55aaa132009-09-28 21:08:04740
[email protected]d27893f62010-07-03 05:47:42741 BrowserChildProcessHost::Iterator i; // default constr references a singleton
[email protected]34cf97d2009-09-29 22:46:11742 while (!i.Done()) {
743 i->Send(new PluginProcessMsg_SetIPCLoggingEnabled(enabled));
744 ++i;
[email protected]d55aaa132009-09-28 21:08:04745 }
746}
747
748#endif // IPC_MESSAGE_LOG_ENABLED
749
[email protected]6641bf662009-08-21 00:34:09750void BrowserProcessImpl::DoInspectorFilesCheck() {
751 // Runs on FILE thread.
752 DCHECK(file_thread_->message_loop() == MessageLoop::current());
753 bool result = false;
754
755 FilePath inspector_dir;
756 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) {
757 result = file_util::PathExists(inspector_dir);
758 }
759
[email protected]308080d2009-11-11 19:19:02760 have_inspector_files_ = result;
[email protected]6641bf662009-08-21 00:34:09761}
[email protected]bd48c2b02010-04-09 20:32:42762
[email protected]3cdacd42010-04-30 18:55:53763// Mac is currently not supported.
764#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
[email protected]bd48c2b02010-04-09 20:32:42765
766bool BrowserProcessImpl::CanAutorestartForUpdate() const {
767 // Check if browser is in the background and if it needs to be restarted to
768 // apply a pending update.
[email protected]c10fa3952010-08-24 16:52:53769 return BrowserList::size() == 0 && BrowserList::WillKeepAlive() &&
[email protected]c984d9f2010-07-20 20:52:20770 Upgrade::IsUpdatePendingRestart();
[email protected]bd48c2b02010-04-09 20:32:42771}
772
[email protected]5c08f222010-09-22 09:37:21773// Switches to add when auto-restarting Chrome.
[email protected]6c10c972010-08-11 04:15:47774const char* const kSwitchesToAddOnAutorestart[] = {
[email protected]f9a06842010-08-21 00:32:40775 switches::kNoStartupWindow
[email protected]bd48c2b02010-04-09 20:32:42776};
777
778void BrowserProcessImpl::RestartPersistentInstance() {
779 CommandLine* old_cl = CommandLine::ForCurrentProcess();
[email protected]3cdacd42010-04-30 18:55:53780 scoped_ptr<CommandLine> new_cl(new CommandLine(old_cl->GetProgram()));
[email protected]bd48c2b02010-04-09 20:32:42781
782 std::map<std::string, CommandLine::StringType> switches =
783 old_cl->GetSwitches();
784
[email protected]5c08f222010-09-22 09:37:21785 switches::RemoveSwitchesForAutostart(&switches);
[email protected]bd48c2b02010-04-09 20:32:42786
787 // Append the rest of the switches (along with their values, if any)
788 // to the new command line
789 for (std::map<std::string, CommandLine::StringType>::const_iterator i =
790 switches.begin(); i != switches.end(); ++i) {
791 CommandLine::StringType switch_value = i->second;
792 if (switch_value.length() > 0) {
[email protected]05076ba22010-07-30 05:59:57793 new_cl->AppendSwitchNative(i->first, i->second);
[email protected]bd48c2b02010-04-09 20:32:42794 } else {
[email protected]3cdacd42010-04-30 18:55:53795 new_cl->AppendSwitch(i->first);
[email protected]bd48c2b02010-04-09 20:32:42796 }
797 }
798
[email protected]6c10c972010-08-11 04:15:47799 // Ensure that our desired switches are set on the new process.
[email protected]5c08f222010-09-22 09:37:21800 for (size_t i = 0; i < arraysize(kSwitchesToAddOnAutorestart); ++i) {
[email protected]6c10c972010-08-11 04:15:47801 if (!new_cl->HasSwitch(kSwitchesToAddOnAutorestart[i]))
802 new_cl->AppendSwitch(kSwitchesToAddOnAutorestart[i]);
803 }
[email protected]bd48c2b02010-04-09 20:32:42804
[email protected]3cdacd42010-04-30 18:55:53805 DLOG(WARNING) << "Shutting down current instance of the browser.";
806 BrowserList::CloseAllBrowsersAndExit();
807
808 // Transfer ownership to Upgrade.
809 Upgrade::SetNewCommandLine(new_cl.release());
[email protected]bd48c2b02010-04-09 20:32:42810}
811
812void BrowserProcessImpl::OnAutoupdateTimer() {
813 if (CanAutorestartForUpdate()) {
[email protected]3cdacd42010-04-30 18:55:53814 DLOG(WARNING) << "Detected update. Restarting browser.";
[email protected]bd48c2b02010-04-09 20:32:42815 RestartPersistentInstance();
816 }
817}
818
[email protected]3cdacd42010-04-30 18:55:53819#endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)