blob: 652f7b90e62de7b62eb9542c7a1ce638b1450a48 [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]ed7e6dd2010-10-12 02:02:4523#include "chrome/browser/browser_thread.h"
[email protected]ac262c9f2008-10-19 17:45:2124#include "chrome/browser/browser_trial.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]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]b2fcd0e2010-12-01 15:19:4035#include "chrome/browser/net/chrome_net_log.h"
[email protected]3530cd92010-06-27 06:22:0136#include "chrome/browser/net/predictor_api.h"
[email protected]d393a0fd2009-05-13 23:32:0137#include "chrome/browser/net/sdch_dictionary_fetcher.h"
[email protected]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"
[email protected]dbeebd52010-11-16 20:34:1644#include "chrome/browser/printing/print_preview_tab_controller.h"
[email protected]8ecad5e2010-12-02 21:18:3345#include "chrome/browser/profiles/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2646#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]81218f42009-02-05 18:48:0847#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
48#include "chrome/browser/safe_browsing/safe_browsing_service.h"
[email protected]8b8e7c92010-08-19 18:05:5649#include "chrome/browser/sidebar/sidebar_manager.h"
[email protected]0b4d3382010-07-14 16:13:0450#include "chrome/browser/tab_closeable_state_watcher.h"
[email protected]4ef795df2010-02-03 02:35:0851#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2952#include "chrome/common/chrome_paths.h"
53#include "chrome/common/chrome_switches.h"
[email protected]27fbccc2010-03-25 03:00:4954#include "chrome/common/extensions/extension_resource.h"
[email protected]f1b6de22010-03-06 12:13:4755#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]ea587b02010-05-21 15:01:3556#include "chrome/common/json_pref_store.h"
initial.commit09911bf2008-07-26 23:55:2957#include "chrome/common/notification_service.h"
58#include "chrome/common/pref_names.h"
[email protected]4ef795df2010-02-03 02:35:0859#include "chrome/common/url_constants.h"
[email protected]5c08f222010-09-22 09:37:2160#include "chrome/common/switch_utils.h"
[email protected]bd48c2b02010-04-09 20:32:4261#include "chrome/installer/util/google_update_constants.h"
[email protected]d55aaa132009-09-28 21:08:0462#include "ipc/ipc_logging.h"
[email protected]4ef795df2010-02-03 02:35:0863#include "webkit/database/database_tracker.h"
[email protected]b112a4c2009-02-01 20:24:0164
65#if defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0566#include "views/focus/view_storage.h"
[email protected]86230b92009-11-23 20:38:3867#endif
68
[email protected]d55aaa132009-09-28 21:08:0469#if defined(IPC_MESSAGE_LOG_ENABLED)
70#include "chrome/common/plugin_messages.h"
71#include "chrome/common/render_messages.h"
72#endif
73
[email protected]3cdacd42010-04-30 18:55:5374#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
75// How often to check if the persistent instance of Chrome needs to restart
76// to install an update.
77static const int kUpdateCheckIntervalHours = 6;
78#endif
79
[email protected]c6032e82010-09-13 20:06:0580#if defined(USE_X11)
81// How long to wait for the File thread to complete during EndSession, on
82// Linux. We have a timeout here because we're unable to run the UI messageloop
83// and there's some deadlock risk. Our only option is to exit anyway.
84static const int kEndSessionTimeoutSeconds = 10;
85#endif
86
[email protected]bb975362009-01-21 01:00:2287BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
initial.commit09911bf2008-07-26 23:55:2988 : created_resource_dispatcher_host_(false),
89 created_metrics_service_(false),
90 created_io_thread_(false),
91 created_file_thread_(false),
92 created_db_thread_(false),
[email protected]914511712009-11-23 19:42:3393 created_process_launcher_thread_(false),
[email protected]875ee822010-05-18 20:58:0194 created_cache_thread_(false),
initial.commit09911bf2008-07-26 23:55:2995 created_profile_manager_(false),
96 created_local_state_(false),
[email protected]b112a4c2009-02-01 20:24:0197 created_icon_manager_(false),
98 created_debugger_wrapper_(false),
[email protected]40ecc902009-03-16 13:42:4799 created_devtools_manager_(false),
[email protected]8b8e7c92010-08-19 18:05:56100 created_sidebar_manager_(false),
[email protected]29672ab2009-10-30 03:44:03101 created_notification_ui_manager_(false),
initial.commit09911bf2008-07-26 23:55:29102 module_ref_count_(0),
[email protected]afd20c022010-06-10 00:48:20103 did_start_(false),
[email protected]1b2db1a2008-08-08 17:46:13104 checked_for_new_frames_(false),
[email protected]6641bf662009-08-21 00:34:09105 using_new_frames_(false),
106 have_inspector_files_(true) {
initial.commit09911bf2008-07-26 23:55:29107 g_browser_process = this;
[email protected]1b8d02f12009-05-05 04:14:11108 clipboard_.reset(new Clipboard);
initial.commit09911bf2008-07-26 23:55:29109 main_notification_service_.reset(new NotificationService);
110
111 // Must be created after the NotificationService.
112 print_job_manager_.reset(new printing::PrintJobManager);
113
[email protected]b797e152009-01-23 16:06:14114 shutdown_event_.reset(new base::WaitableEvent(true, false));
[email protected]b2fcd0e2010-12-01 15:19:40115
116 net_log_.reset(new ChromeNetLog);
initial.commit09911bf2008-07-26 23:55:29117}
118
119BrowserProcessImpl::~BrowserProcessImpl() {
[email protected]4ef795df2010-02-03 02:35:08120 FilePath profile_path;
121 bool clear_local_state_on_exit;
122
123 // Store the profile path for clearing local state data on exit.
124 clear_local_state_on_exit = ShouldClearLocalState(&profile_path);
125
initial.commit09911bf2008-07-26 23:55:29126 // Delete the AutomationProviderList before NotificationService,
127 // since it may try to unregister notifications
128 // Both NotificationService and AutomationProvider are singleton instances in
129 // the BrowserProcess. Since AutomationProvider may have some active
130 // notification observers, it is essential that it gets destroyed before the
131 // NotificationService. NotificationService won't be destroyed until after
132 // this destructor is run.
133 automation_provider_list_.reset();
134
[email protected]d393a0fd2009-05-13 23:32:01135 // We need to shutdown the SdchDictionaryFetcher as it regularly holds
136 // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do
137 // a PostDelayedTask onto the IO thread. This shutdown call will both discard
138 // any pending URLFetchers, and avoid creating any more.
139 SdchDictionaryFetcher::Shutdown();
140
[email protected]c4ff4952010-01-08 19:12:47141 // We need to destroy the MetricsService, GoogleURLTracker, and
142 // IntranetRedirectDetector before the io_thread_ gets destroyed, since their
143 // destructors can call the URLFetcher destructor, which does a
144 // PostDelayedTask operation on the IO thread. (The IO thread will handle
145 // that URLFetcher operation before going away.)
initial.commit09911bf2008-07-26 23:55:29146 metrics_service_.reset();
147 google_url_tracker_.reset();
[email protected]c4ff4952010-01-08 19:12:47148 intranet_redirect_detector_.reset();
initial.commit09911bf2008-07-26 23:55:29149
[email protected]d8a899c2010-09-21 04:50:33150 // Need to clear the desktop notification balloons before the io_thread_ and
151 // before the profiles, since if there are any still showing we will access
152 // those things during teardown.
153 notification_ui_manager_.reset();
154
initial.commit09911bf2008-07-26 23:55:29155 // Need to clear profiles (download managers) before the io_thread_.
156 profile_manager_.reset();
157
158 // Debugger must be cleaned up before IO thread and NotificationService.
159 debugger_wrapper_ = NULL;
160
161 if (resource_dispatcher_host_.get()) {
162 // Need to tell Safe Browsing Service that the IO thread is going away
163 // since it cached a pointer to it.
164 if (resource_dispatcher_host()->safe_browsing_service())
165 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
166
167 // Cancel pending requests and prevent new requests.
168 resource_dispatcher_host()->Shutdown();
169 }
170
[email protected]753efc42010-03-09 19:52:16171#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06172 // The IO thread must outlive the BACKGROUND_X11 thread.
173 background_x11_thread_.reset();
174#endif
175
initial.commit09911bf2008-07-26 23:55:29176 // Need to stop io_thread_ before resource_dispatcher_host_, since
177 // io_thread_ may still deref ResourceDispatcherHost and handle resource
178 // request before going away.
[email protected]0ac83682010-01-22 17:46:27179 io_thread_.reset();
initial.commit09911bf2008-07-26 23:55:29180
[email protected]875ee822010-05-18 20:58:01181 // The IO thread was the only user of this thread.
182 cache_thread_.reset();
183
[email protected]914511712009-11-23 19:42:33184 // Stop the process launcher thread after the IO thread, in case the IO thread
185 // posted a task to terminate a process on the process launcher thread.
186 process_launcher_thread_.reset();
187
initial.commit09911bf2008-07-26 23:55:29188 // Clean up state that lives on the file_thread_ before it goes away.
189 if (resource_dispatcher_host_.get()) {
190 resource_dispatcher_host()->download_file_manager()->Shutdown();
191 resource_dispatcher_host()->save_file_manager()->Shutdown();
192 }
193
194 // Need to stop the file_thread_ here to force it to process messages in its
195 // message loop from the previous call to shutdown the DownloadFileManager,
196 // SaveFileManager and SessionService.
197 file_thread_.reset();
198
199 // With the file_thread_ flushed, we can release any icon resources.
200 icon_manager_.reset();
201
202 // Need to destroy ResourceDispatcherHost before PluginService and
[email protected]49f28bc72010-02-04 00:10:01203 // SafeBrowsingService, since it caches a pointer to it. This also
204 // causes the webkit thread to terminate.
initial.commit09911bf2008-07-26 23:55:29205 resource_dispatcher_host_.reset();
206
207 // Wait for the pending print jobs to finish.
208 print_job_manager_->OnQuit();
209 print_job_manager_.reset();
210
[email protected]0b4d3382010-07-14 16:13:04211 // Destroy TabCloseableStateWatcher before NotificationService since the
212 // former registers for notifications.
213 tab_closeable_state_watcher_.reset();
214
initial.commit09911bf2008-07-26 23:55:29215 // Now OK to destroy NotificationService.
216 main_notification_service_.reset();
217
[email protected]49f28bc72010-02-04 00:10:01218 // Prior to clearing local state, we want to complete tasks pending
219 // on the db thread too.
220 db_thread_.reset();
221
222 // At this point, no render process exist and the file, io, db, and
223 // webkit threads in this process have all terminated, so it's safe
224 // to access local state data such as cookies, database, or local storage.
225 if (clear_local_state_on_exit)
226 ClearLocalState(profile_path);
227
initial.commit09911bf2008-07-26 23:55:29228 g_browser_process = NULL;
229}
230
[email protected]c6032e82010-09-13 20:06:05231#if defined(OS_WIN)
[email protected]295039bd2008-08-15 04:32:57232// Send a QuitTask to the given MessageLoop.
233static void PostQuit(MessageLoop* message_loop) {
234 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
235}
[email protected]c6032e82010-09-13 20:06:05236#elif defined(USE_X11)
237static void Signal(base::WaitableEvent* event) {
238 event->Signal();
239}
240#endif
initial.commit09911bf2008-07-26 23:55:29241
[email protected]b443cb042009-12-15 22:05:09242unsigned int BrowserProcessImpl::AddRefModule() {
243 DCHECK(CalledOnValidThread());
[email protected]afd20c022010-06-10 00:48:20244 did_start_ = true;
[email protected]b443cb042009-12-15 22:05:09245 module_ref_count_++;
246 return module_ref_count_;
247}
248
249unsigned int BrowserProcessImpl::ReleaseModule() {
250 DCHECK(CalledOnValidThread());
[email protected]760d970a2010-05-18 00:39:18251 DCHECK_NE(0u, module_ref_count_);
[email protected]b443cb042009-12-15 22:05:09252 module_ref_count_--;
253 if (0 == module_ref_count_) {
254 MessageLoop::current()->PostTask(
[email protected]34f73fb2010-03-24 20:50:34255 FROM_HERE, NewRunnableFunction(DidEndMainMessageLoop));
[email protected]b443cb042009-12-15 22:05:09256 MessageLoop::current()->Quit();
257 }
258 return module_ref_count_;
259}
260
initial.commit09911bf2008-07-26 23:55:29261void BrowserProcessImpl::EndSession() {
[email protected]c6032e82010-09-13 20:06:05262#if defined(OS_WIN) || defined(USE_X11)
[email protected]d65cab7a2008-08-12 01:25:41263 // Notify we are going away.
[email protected]c6032e82010-09-13 20:06:05264 shutdown_event_->Signal();
[email protected]b112a4c2009-02-01 20:24:01265#endif
[email protected]d65cab7a2008-08-12 01:25:41266
initial.commit09911bf2008-07-26 23:55:29267 // Mark all the profiles as clean.
268 ProfileManager* pm = profile_manager();
269 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
270 (*i)->MarkAsCleanShutdown();
271
272 // Tell the metrics service it was cleanly shutdown.
273 MetricsService* metrics = g_browser_process->metrics_service();
274 if (metrics && local_state()) {
275 metrics->RecordCleanShutdown();
276
277 metrics->RecordStartOfSessionEnd();
278
279 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36280 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29281 }
282
283 // We must write that the profile and metrics service shutdown cleanly,
284 // otherwise on startup we'll think we crashed. So we block until done and
285 // then proceed with normal shutdown.
[email protected]c6032e82010-09-13 20:06:05286#if defined(USE_X11)
287 // Can't run a local loop on linux. Instead create a waitable event.
288 base::WaitableEvent done_writing(false, false);
[email protected]d04e7662010-10-10 22:24:48289 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]c6032e82010-09-13 20:06:05290 NewRunnableFunction(Signal, &done_writing));
291 done_writing.TimedWait(
292 base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds));
293#elif defined(OS_WIN)
[email protected]d04e7662010-10-10 22:24:48294 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57295 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29296 MessageLoop::current()->Run();
[email protected]c6032e82010-09-13 20:06:05297#else
298 NOTIMPLEMENTED();
299#endif
initial.commit09911bf2008-07-26 23:55:29300}
301
[email protected]e13ad79b2010-07-22 21:36:50302ResourceDispatcherHost* BrowserProcessImpl::resource_dispatcher_host() {
303 DCHECK(CalledOnValidThread());
304 if (!created_resource_dispatcher_host_)
305 CreateResourceDispatcherHost();
306 return resource_dispatcher_host_.get();
307}
308
309MetricsService* BrowserProcessImpl::metrics_service() {
310 DCHECK(CalledOnValidThread());
311 if (!created_metrics_service_)
312 CreateMetricsService();
313 return metrics_service_.get();
314}
315
316IOThread* BrowserProcessImpl::io_thread() {
317 DCHECK(CalledOnValidThread());
318 if (!created_io_thread_)
319 CreateIOThread();
320 return io_thread_.get();
321}
322
323base::Thread* BrowserProcessImpl::file_thread() {
324 DCHECK(CalledOnValidThread());
325 if (!created_file_thread_)
326 CreateFileThread();
327 return file_thread_.get();
328}
329
330base::Thread* BrowserProcessImpl::db_thread() {
331 DCHECK(CalledOnValidThread());
332 if (!created_db_thread_)
333 CreateDBThread();
334 return db_thread_.get();
335}
336
337base::Thread* BrowserProcessImpl::process_launcher_thread() {
338 DCHECK(CalledOnValidThread());
339 if (!created_process_launcher_thread_)
340 CreateProcessLauncherThread();
341 return process_launcher_thread_.get();
342}
343
344base::Thread* BrowserProcessImpl::cache_thread() {
345 DCHECK(CalledOnValidThread());
346 if (!created_cache_thread_)
347 CreateCacheThread();
348 return cache_thread_.get();
349}
350
351#if defined(USE_X11)
352base::Thread* BrowserProcessImpl::background_x11_thread() {
353 DCHECK(CalledOnValidThread());
354 // The BACKGROUND_X11 thread is created when the IO thread is created.
355 if (!created_io_thread_)
356 CreateIOThread();
357 return background_x11_thread_.get();
358}
359#endif
360
361ProfileManager* BrowserProcessImpl::profile_manager() {
362 DCHECK(CalledOnValidThread());
363 if (!created_profile_manager_)
364 CreateProfileManager();
365 return profile_manager_.get();
366}
367
368PrefService* BrowserProcessImpl::local_state() {
369 DCHECK(CalledOnValidThread());
370 if (!created_local_state_)
371 CreateLocalState();
372 return local_state_.get();
373}
374
375DevToolsManager* BrowserProcessImpl::devtools_manager() {
376 DCHECK(CalledOnValidThread());
377 if (!created_devtools_manager_)
378 CreateDevToolsManager();
379 return devtools_manager_.get();
380}
381
[email protected]8b8e7c92010-08-19 18:05:56382SidebarManager* BrowserProcessImpl::sidebar_manager() {
383 DCHECK(CalledOnValidThread());
384 if (!created_sidebar_manager_)
385 CreateSidebarManager();
386 return sidebar_manager_.get();
387}
388
[email protected]e13ad79b2010-07-22 21:36:50389Clipboard* BrowserProcessImpl::clipboard() {
390 DCHECK(CalledOnValidThread());
391 return clipboard_.get();
392}
393
394NotificationUIManager* BrowserProcessImpl::notification_ui_manager() {
395 DCHECK(CalledOnValidThread());
396 if (!created_notification_ui_manager_)
397 CreateNotificationUIManager();
398 return notification_ui_manager_.get();
399}
400
[email protected]e13ad79b2010-07-22 21:36:50401IconManager* BrowserProcessImpl::icon_manager() {
402 DCHECK(CalledOnValidThread());
403 if (!created_icon_manager_)
404 CreateIconManager();
405 return icon_manager_.get();
406}
407
408ThumbnailGenerator* BrowserProcessImpl::GetThumbnailGenerator() {
409 return &thumbnail_generator_;
410}
411
412AutomationProviderList* BrowserProcessImpl::InitAutomationProviderList() {
413 DCHECK(CalledOnValidThread());
414 if (automation_provider_list_.get() == NULL) {
415 automation_provider_list_.reset(AutomationProviderList::GetInstance());
416 }
417 return automation_provider_list_.get();
418}
419
420void BrowserProcessImpl::InitDebuggerWrapper(int port, bool useHttp) {
421 DCHECK(CalledOnValidThread());
422 if (!created_debugger_wrapper_)
423 CreateDebuggerWrapper(port, useHttp);
424}
425
426bool BrowserProcessImpl::IsShuttingDown() {
427 DCHECK(CalledOnValidThread());
428 return did_start_ && 0 == module_ref_count_;
429}
430
initial.commit09911bf2008-07-26 23:55:29431printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
432 // TODO(abarth): DCHECK(CalledOnValidThread());
[email protected]d8922f22010-05-07 00:57:27433 // http://code.google.com/p/chromium/issues/detail?id=6828
initial.commit09911bf2008-07-26 23:55:29434 // print_job_manager_ is initialized in the constructor and destroyed in the
435 // destructor, so it should always be valid.
436 DCHECK(print_job_manager_.get());
437 return print_job_manager_.get();
438}
439
[email protected]dbeebd52010-11-16 20:34:16440printing::PrintPreviewTabController*
441 BrowserProcessImpl::print_preview_tab_controller() {
442 DCHECK(CalledOnValidThread());
443 if (!print_preview_tab_controller_.get())
444 CreatePrintPreviewTabController();
445 return print_preview_tab_controller_.get();
446}
447
[email protected]e13ad79b2010-07-22 21:36:50448GoogleURLTracker* BrowserProcessImpl::google_url_tracker() {
449 DCHECK(CalledOnValidThread());
450 if (!google_url_tracker_.get())
451 CreateGoogleURLTracker();
452 return google_url_tracker_.get();
453}
454
455IntranetRedirectDetector* BrowserProcessImpl::intranet_redirect_detector() {
456 DCHECK(CalledOnValidThread());
457 if (!intranet_redirect_detector_.get())
458 CreateIntranetRedirectDetector();
459 return intranet_redirect_detector_.get();
460}
461
462const std::string& BrowserProcessImpl::GetApplicationLocale() {
463 DCHECK(!locale_.empty());
464 return locale_;
465}
466
467void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) {
468 locale_ = locale;
469 extension_l10n_util::SetProcessLocale(locale);
470}
471
[email protected]073ed7b2010-09-27 09:20:02472DownloadStatusUpdater* BrowserProcessImpl::download_status_updater() {
473 return &download_status_updater_;
474}
475
[email protected]e13ad79b2010-07-22 21:36:50476base::WaitableEvent* BrowserProcessImpl::shutdown_event() {
477 return shutdown_event_.get();
478}
479
480TabCloseableStateWatcher* BrowserProcessImpl::tab_closeable_state_watcher() {
481 DCHECK(CalledOnValidThread());
482 if (!tab_closeable_state_watcher_.get())
483 CreateTabCloseableStateWatcher();
484 return tab_closeable_state_watcher_.get();
485}
486
487void BrowserProcessImpl::CheckForInspectorFiles() {
488 file_thread()->message_loop()->PostTask
489 (FROM_HERE,
490 NewRunnableMethod(this, &BrowserProcessImpl::DoInspectorFilesCheck));
491}
492
493#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
494void BrowserProcessImpl::StartAutoupdateTimer() {
495 autoupdate_timer_.Start(
496 base::TimeDelta::FromHours(kUpdateCheckIntervalHours),
497 this,
498 &BrowserProcessImpl::OnAutoupdateTimer);
499}
500#endif
501
502bool BrowserProcessImpl::have_inspector_files() const {
503 return have_inspector_files_;
504}
505
[email protected]4ef795df2010-02-03 02:35:08506void BrowserProcessImpl::ClearLocalState(const FilePath& profile_path) {
507 SQLitePersistentCookieStore::ClearLocalState(profile_path.Append(
508 chrome::kCookieFilename));
[email protected]72cfd90f2010-02-06 03:08:04509 webkit_database::DatabaseTracker::ClearLocalState(profile_path);
[email protected]10c934052010-02-04 19:58:31510 ChromeAppCacheService::ClearLocalState(profile_path);
[email protected]4ef795df2010-02-03 02:35:08511}
512
513bool BrowserProcessImpl::ShouldClearLocalState(FilePath* profile_path) {
514 FilePath user_data_dir;
515 Profile* profile;
516
[email protected]09584972010-07-03 00:00:59517 // Check for the existence of a profile manager. When quitting early,
[email protected]ed0cf562010-02-05 22:50:41518 // e.g. because another chrome instance is running, or when invoked with
519 // options such as --uninstall or --try-chrome-again=0, the profile manager
520 // does not exist yet.
521 if (!profile_manager_.get())
522 return false;
523
[email protected]4ef795df2010-02-03 02:35:08524 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
525 profile = profile_manager_->GetDefaultProfile(user_data_dir);
[email protected]7bfc674322010-03-05 00:20:57526 if (!profile)
527 return false;
[email protected]4ef795df2010-02-03 02:35:08528 *profile_path = profile->GetPath();
529 return profile->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit);
530}
531
initial.commit09911bf2008-07-26 23:55:29532void BrowserProcessImpl::CreateResourceDispatcherHost() {
533 DCHECK(!created_resource_dispatcher_host_ &&
534 resource_dispatcher_host_.get() == NULL);
535 created_resource_dispatcher_host_ = true;
536
[email protected]79084c2d2009-11-03 23:12:42537 resource_dispatcher_host_.reset(new ResourceDispatcherHost());
initial.commit09911bf2008-07-26 23:55:29538 resource_dispatcher_host_->Initialize();
539}
540
541void BrowserProcessImpl::CreateMetricsService() {
542 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
543 created_metrics_service_ = true;
544
545 metrics_service_.reset(new MetricsService);
546}
547
548void BrowserProcessImpl::CreateIOThread() {
549 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
550 created_io_thread_ = true;
551
552 // Prior to starting the io thread, we create the plugin service as
553 // it is predominantly used from the io thread, but must be created
554 // on the main thread. The service ctor is inexpensive and does not
555 // invoke the io_thread() accessor.
556 PluginService::GetInstance();
557
[email protected]753efc42010-03-09 19:52:16558#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06559 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
560 // we start it now.
561 scoped_ptr<base::Thread> background_x11_thread(
[email protected]d04e7662010-10-10 22:24:48562 new BrowserProcessSubThread(BrowserThread::BACKGROUND_X11));
[email protected]4c3cd7412009-04-22 17:56:06563 if (!background_x11_thread->Start())
564 return;
565 background_x11_thread_.swap(background_x11_thread);
566#endif
567
[email protected]b2fcd0e2010-12-01 15:19:40568 scoped_ptr<IOThread> thread(new IOThread(local_state(), net_log_.get()));
[email protected]ab820df2008-08-26 05:55:10569 base::Thread::Options options;
570 options.message_loop_type = MessageLoop::TYPE_IO;
571 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29572 return;
573 io_thread_.swap(thread);
574}
575
576void BrowserProcessImpl::CreateFileThread() {
577 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
578 created_file_thread_ = true;
579
[email protected]ab820df2008-08-26 05:55:10580 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48581 new BrowserProcessSubThread(BrowserThread::FILE));
[email protected]a1db3842008-09-17 22:04:06582 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39583#if defined(OS_WIN)
584 // On Windows, the FILE thread needs to be have a UI message loop which pumps
585 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06586 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39587#else
588 options.message_loop_type = MessageLoop::TYPE_IO;
589#endif
[email protected]a1db3842008-09-17 22:04:06590 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29591 return;
592 file_thread_.swap(thread);
593}
594
595void BrowserProcessImpl::CreateDBThread() {
596 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
597 created_db_thread_ = true;
598
[email protected]ab820df2008-08-26 05:55:10599 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48600 new BrowserProcessSubThread(BrowserThread::DB));
initial.commit09911bf2008-07-26 23:55:29601 if (!thread->Start())
602 return;
603 db_thread_.swap(thread);
604}
605
[email protected]914511712009-11-23 19:42:33606void BrowserProcessImpl::CreateProcessLauncherThread() {
607 DCHECK(!created_process_launcher_thread_ && !process_launcher_thread_.get());
608 created_process_launcher_thread_ = true;
609
610 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48611 new BrowserProcessSubThread(BrowserThread::PROCESS_LAUNCHER));
[email protected]914511712009-11-23 19:42:33612 if (!thread->Start())
613 return;
614 process_launcher_thread_.swap(thread);
615}
616
[email protected]875ee822010-05-18 20:58:01617void BrowserProcessImpl::CreateCacheThread() {
618 DCHECK(!created_cache_thread_ && !cache_thread_.get());
619 created_cache_thread_ = true;
620
621 scoped_ptr<base::Thread> thread(
[email protected]75096332010-11-09 03:28:13622 new BrowserThread(BrowserThread::CACHE));
[email protected]875ee822010-05-18 20:58:01623 base::Thread::Options options;
624 options.message_loop_type = MessageLoop::TYPE_IO;
625 if (!thread->StartWithOptions(options))
626 return;
627 cache_thread_.swap(thread);
628}
629
initial.commit09911bf2008-07-26 23:55:29630void BrowserProcessImpl::CreateProfileManager() {
631 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
632 created_profile_manager_ = true;
633
634 profile_manager_.reset(new ProfileManager());
635}
636
637void BrowserProcessImpl::CreateLocalState() {
638 DCHECK(!created_local_state_ && local_state_.get() == NULL);
639 created_local_state_ = true;
640
[email protected]b9636002009-03-04 00:05:25641 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29642 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]a9c23a52010-08-04 09:13:44643 local_state_.reset(PrefService::CreatePrefService(local_state_path, NULL));
[email protected]492d2142010-09-10 13:55:18644
[email protected]2a7e7c152010-10-01 20:12:09645 pref_change_registrar_.Init(local_state_.get());
646
[email protected]492d2142010-09-10 13:55:18647 // Make sure the the plugin updater gets notifications of changes
648 // in the plugin blacklist.
649 local_state_->RegisterListPref(prefs::kPluginsPluginsBlacklist);
[email protected]2a7e7c152010-10-01 20:12:09650 pref_change_registrar_.Add(prefs::kPluginsPluginsBlacklist,
651 PluginUpdater::GetPluginUpdater());
652
653 // Initialize and set up notifications for the printing enabled
654 // preference.
655 local_state_->RegisterBooleanPref(prefs::kPrintingEnabled, true);
656 bool printing_enabled =
657 local_state_->GetBoolean(prefs::kPrintingEnabled);
658 print_job_manager_->set_printing_enabled(printing_enabled);
659 pref_change_registrar_.Add(prefs::kPrintingEnabled,
660 print_job_manager_.get());
[email protected]fd6159a2010-09-03 09:38:39661}
initial.commit09911bf2008-07-26 23:55:29662
initial.commit09911bf2008-07-26 23:55:29663void BrowserProcessImpl::CreateIconManager() {
664 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
665 created_icon_manager_ = true;
666 icon_manager_.reset(new IconManager);
667}
668
[email protected]a9bf67982010-07-06 10:13:00669void BrowserProcessImpl::CreateDebuggerWrapper(int port, bool useHttp) {
initial.commit09911bf2008-07-26 23:55:29670 DCHECK(debugger_wrapper_.get() == NULL);
671 created_debugger_wrapper_ = true;
672
[email protected]a9bf67982010-07-06 10:13:00673 debugger_wrapper_ = new DebuggerWrapper(port, useHttp);
initial.commit09911bf2008-07-26 23:55:29674}
675
[email protected]40ecc902009-03-16 13:42:47676void BrowserProcessImpl::CreateDevToolsManager() {
[email protected]73ee01522009-06-05 10:13:44677 DCHECK(devtools_manager_.get() == NULL);
[email protected]40ecc902009-03-16 13:42:47678 created_devtools_manager_ = true;
[email protected]73ee01522009-06-05 10:13:44679 devtools_manager_ = new DevToolsManager();
[email protected]40ecc902009-03-16 13:42:47680}
681
[email protected]8b8e7c92010-08-19 18:05:56682void BrowserProcessImpl::CreateSidebarManager() {
683 DCHECK(sidebar_manager_.get() == NULL);
684 created_sidebar_manager_ = true;
685 sidebar_manager_ = new SidebarManager();
686}
687
initial.commit09911bf2008-07-26 23:55:29688void BrowserProcessImpl::CreateGoogleURLTracker() {
689 DCHECK(google_url_tracker_.get() == NULL);
690 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
691 google_url_tracker_.swap(google_url_tracker);
692}
[email protected]6641bf662009-08-21 00:34:09693
[email protected]c4ff4952010-01-08 19:12:47694void BrowserProcessImpl::CreateIntranetRedirectDetector() {
695 DCHECK(intranet_redirect_detector_.get() == NULL);
696 scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector(
697 new IntranetRedirectDetector);
698 intranet_redirect_detector_.swap(intranet_redirect_detector);
699}
700
[email protected]29672ab2009-10-30 03:44:03701void BrowserProcessImpl::CreateNotificationUIManager() {
702 DCHECK(notification_ui_manager_.get() == NULL);
703 notification_ui_manager_.reset(NotificationUIManager::Create());
704 created_notification_ui_manager_ = true;
705}
706
[email protected]0b4d3382010-07-14 16:13:04707void BrowserProcessImpl::CreateTabCloseableStateWatcher() {
708 DCHECK(tab_closeable_state_watcher_.get() == NULL);
709 tab_closeable_state_watcher_.reset(TabCloseableStateWatcher::Create());
710}
711
[email protected]dbeebd52010-11-16 20:34:16712void BrowserProcessImpl::CreatePrintPreviewTabController() {
713 DCHECK(print_preview_tab_controller_.get() == NULL);
714 print_preview_tab_controller_ = new printing::PrintPreviewTabController();
715}
716
[email protected]6641bf662009-08-21 00:34:09717// The BrowserProcess object must outlive the file thread so we use traits
718// which don't do any management.
[email protected]c56428f22010-06-16 02:17:23719DISABLE_RUNNABLE_METHOD_REFCOUNT(BrowserProcessImpl);
[email protected]6641bf662009-08-21 00:34:09720
[email protected]d55aaa132009-09-28 21:08:04721#if defined(IPC_MESSAGE_LOG_ENABLED)
722
723void BrowserProcessImpl::SetIPCLoggingEnabled(bool enable) {
724 // First enable myself.
725 if (enable)
726 IPC::Logging::current()->Enable();
727 else
728 IPC::Logging::current()->Disable();
729
730 // Now tell subprocesses. Messages to ChildProcess-derived
731 // processes must be done on the IO thread.
732 io_thread()->message_loop()->PostTask
733 (FROM_HERE,
734 NewRunnableMethod(
735 this,
736 &BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses,
737 enable));
738
739 // Finally, tell the renderers which don't derive from ChildProcess.
740 // Messages to the renderers must be done on the UI (main) thread.
[email protected]019191a2009-10-02 20:37:27741 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
742 !i.IsAtEnd(); i.Advance())
743 i.GetCurrentValue()->Send(new ViewMsg_SetIPCLoggingEnabled(enable));
[email protected]d55aaa132009-09-28 21:08:04744}
745
746// Helper for SetIPCLoggingEnabled.
747void BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses(bool enabled) {
[email protected]d04e7662010-10-10 22:24:48748 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]d55aaa132009-09-28 21:08:04749
[email protected]d27893f62010-07-03 05:47:42750 BrowserChildProcessHost::Iterator i; // default constr references a singleton
[email protected]34cf97d2009-09-29 22:46:11751 while (!i.Done()) {
752 i->Send(new PluginProcessMsg_SetIPCLoggingEnabled(enabled));
753 ++i;
[email protected]d55aaa132009-09-28 21:08:04754 }
755}
756
757#endif // IPC_MESSAGE_LOG_ENABLED
758
[email protected]6641bf662009-08-21 00:34:09759void BrowserProcessImpl::DoInspectorFilesCheck() {
760 // Runs on FILE thread.
761 DCHECK(file_thread_->message_loop() == MessageLoop::current());
762 bool result = false;
763
764 FilePath inspector_dir;
765 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) {
766 result = file_util::PathExists(inspector_dir);
767 }
768
[email protected]308080d2009-11-11 19:19:02769 have_inspector_files_ = result;
[email protected]6641bf662009-08-21 00:34:09770}
[email protected]bd48c2b02010-04-09 20:32:42771
[email protected]3cdacd42010-04-30 18:55:53772// Mac is currently not supported.
773#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
[email protected]bd48c2b02010-04-09 20:32:42774
775bool BrowserProcessImpl::CanAutorestartForUpdate() const {
776 // Check if browser is in the background and if it needs to be restarted to
777 // apply a pending update.
[email protected]c10fa3952010-08-24 16:52:53778 return BrowserList::size() == 0 && BrowserList::WillKeepAlive() &&
[email protected]c984d9f2010-07-20 20:52:20779 Upgrade::IsUpdatePendingRestart();
[email protected]bd48c2b02010-04-09 20:32:42780}
781
[email protected]5c08f222010-09-22 09:37:21782// Switches to add when auto-restarting Chrome.
[email protected]6c10c972010-08-11 04:15:47783const char* const kSwitchesToAddOnAutorestart[] = {
[email protected]f9a06842010-08-21 00:32:40784 switches::kNoStartupWindow
[email protected]bd48c2b02010-04-09 20:32:42785};
786
787void BrowserProcessImpl::RestartPersistentInstance() {
788 CommandLine* old_cl = CommandLine::ForCurrentProcess();
[email protected]3cdacd42010-04-30 18:55:53789 scoped_ptr<CommandLine> new_cl(new CommandLine(old_cl->GetProgram()));
[email protected]bd48c2b02010-04-09 20:32:42790
791 std::map<std::string, CommandLine::StringType> switches =
792 old_cl->GetSwitches();
793
[email protected]5c08f222010-09-22 09:37:21794 switches::RemoveSwitchesForAutostart(&switches);
[email protected]bd48c2b02010-04-09 20:32:42795
796 // Append the rest of the switches (along with their values, if any)
797 // to the new command line
798 for (std::map<std::string, CommandLine::StringType>::const_iterator i =
799 switches.begin(); i != switches.end(); ++i) {
800 CommandLine::StringType switch_value = i->second;
801 if (switch_value.length() > 0) {
[email protected]05076ba22010-07-30 05:59:57802 new_cl->AppendSwitchNative(i->first, i->second);
[email protected]bd48c2b02010-04-09 20:32:42803 } else {
[email protected]3cdacd42010-04-30 18:55:53804 new_cl->AppendSwitch(i->first);
[email protected]bd48c2b02010-04-09 20:32:42805 }
806 }
807
[email protected]6c10c972010-08-11 04:15:47808 // Ensure that our desired switches are set on the new process.
[email protected]5c08f222010-09-22 09:37:21809 for (size_t i = 0; i < arraysize(kSwitchesToAddOnAutorestart); ++i) {
[email protected]6c10c972010-08-11 04:15:47810 if (!new_cl->HasSwitch(kSwitchesToAddOnAutorestart[i]))
811 new_cl->AppendSwitch(kSwitchesToAddOnAutorestart[i]);
812 }
[email protected]bd48c2b02010-04-09 20:32:42813
[email protected]3cdacd42010-04-30 18:55:53814 DLOG(WARNING) << "Shutting down current instance of the browser.";
815 BrowserList::CloseAllBrowsersAndExit();
816
817 // Transfer ownership to Upgrade.
818 Upgrade::SetNewCommandLine(new_cl.release());
[email protected]bd48c2b02010-04-09 20:32:42819}
820
821void BrowserProcessImpl::OnAutoupdateTimer() {
822 if (CanAutorestartForUpdate()) {
[email protected]3cdacd42010-04-30 18:55:53823 DLOG(WARNING) << "Detected update. Restarting browser.";
[email protected]bd48c2b02010-04-09 20:32:42824 RestartPersistentInstance();
825 }
826}
827
[email protected]3cdacd42010-04-30 18:55:53828#endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)