blob: 756aa6ca1b5211650ea183eb65225a56162855c3 [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]7d038c32010-12-14 00:40:0016#include "base/thread_restrictions.h"
[email protected]1c4947f2009-01-15 22:25:1117#include "base/waitable_event.h"
[email protected]0082d7e0d2010-02-27 14:34:1218#include "chrome/browser/appcache/chrome_appcache_service.h"
[email protected]e13ad79b2010-07-22 21:36:5019#include "chrome/browser/automation/automation_provider_list.h"
[email protected]d27893f62010-07-03 05:47:4220#include "chrome/browser/browser_child_process_host.h"
[email protected]bd48c2b02010-04-09 20:32:4221#include "chrome/browser/browser_list.h"
[email protected]b443cb042009-12-15 22:05:0922#include "chrome/browser/browser_main.h"
[email protected]0ac83682010-01-22 17:46:2723#include "chrome/browser/browser_process_sub_thread.h"
[email protected]ed7e6dd2010-10-12 02:02:4524#include "chrome/browser/browser_thread.h"
[email protected]ac262c9f2008-10-19 17:45:2125#include "chrome/browser/browser_trial.h"
[email protected]4ab4b0f2009-02-10 18:54:5026#include "chrome/browser/debugger/debugger_wrapper.h"
[email protected]40ecc902009-03-16 13:42:4727#include "chrome/browser/debugger/devtools_manager.h"
[email protected]6c69796d2010-07-16 21:41:1628#include "chrome/browser/download/download_file_manager.h"
[email protected]5ba0a2c2009-02-19 01:19:3429#include "chrome/browser/download/save_file_manager.h"
[email protected]820735792010-07-29 23:40:0130#include "chrome/browser/first_run/first_run.h"
[email protected]f7578f52010-08-30 22:22:4931#include "chrome/browser/google/google_url_tracker.h"
[email protected]dcefa302009-05-20 00:24:3932#include "chrome/browser/icon_manager.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]b2fcd0e2010-12-01 15:19:4036#include "chrome/browser/net/chrome_net_log.h"
[email protected]3530cd92010-06-27 06:22:0137#include "chrome/browser/net/predictor_api.h"
[email protected]d393a0fd2009-05-13 23:32:0138#include "chrome/browser/net/sdch_dictionary_fetcher.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"
[email protected]a7a5e992010-12-09 23:39:5148#include "chrome/browser/safe_browsing/client_side_detection_service.h"
[email protected]81218f42009-02-05 18:48:0849#include "chrome/browser/safe_browsing/safe_browsing_service.h"
[email protected]8b8e7c92010-08-19 18:05:5650#include "chrome/browser/sidebar/sidebar_manager.h"
[email protected]0b4d3382010-07-14 16:13:0451#include "chrome/browser/tab_closeable_state_watcher.h"
[email protected]4ef795df2010-02-03 02:35:0852#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2953#include "chrome/common/chrome_paths.h"
54#include "chrome/common/chrome_switches.h"
[email protected]27fbccc2010-03-25 03:00:4955#include "chrome/common/extensions/extension_resource.h"
[email protected]f1b6de22010-03-06 12:13:4756#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]ea587b02010-05-21 15:01:3557#include "chrome/common/json_pref_store.h"
initial.commit09911bf2008-07-26 23:55:2958#include "chrome/common/notification_service.h"
59#include "chrome/common/pref_names.h"
[email protected]4ef795df2010-02-03 02:35:0860#include "chrome/common/url_constants.h"
[email protected]5c08f222010-09-22 09:37:2161#include "chrome/common/switch_utils.h"
[email protected]bd48c2b02010-04-09 20:32:4262#include "chrome/installer/util/google_update_constants.h"
[email protected]d55aaa132009-09-28 21:08:0463#include "ipc/ipc_logging.h"
[email protected]4ef795df2010-02-03 02:35:0864#include "webkit/database/database_tracker.h"
[email protected]b112a4c2009-02-01 20:24:0165
66#if defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0567#include "views/focus/view_storage.h"
[email protected]86230b92009-11-23 20:38:3868#endif
69
[email protected]d55aaa132009-09-28 21:08:0470#if defined(IPC_MESSAGE_LOG_ENABLED)
71#include "chrome/common/plugin_messages.h"
72#include "chrome/common/render_messages.h"
73#endif
74
[email protected]3cdacd42010-04-30 18:55:5375#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
76// How often to check if the persistent instance of Chrome needs to restart
77// to install an update.
78static const int kUpdateCheckIntervalHours = 6;
79#endif
80
[email protected]c6032e82010-09-13 20:06:0581#if defined(USE_X11)
82// How long to wait for the File thread to complete during EndSession, on
83// Linux. We have a timeout here because we're unable to run the UI messageloop
84// and there's some deadlock risk. Our only option is to exit anyway.
85static const int kEndSessionTimeoutSeconds = 10;
86#endif
87
[email protected]bb975362009-01-21 01:00:2288BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
initial.commit09911bf2008-07-26 23:55:2989 : created_resource_dispatcher_host_(false),
90 created_metrics_service_(false),
91 created_io_thread_(false),
92 created_file_thread_(false),
93 created_db_thread_(false),
[email protected]914511712009-11-23 19:42:3394 created_process_launcher_thread_(false),
[email protected]875ee822010-05-18 20:58:0195 created_cache_thread_(false),
initial.commit09911bf2008-07-26 23:55:2996 created_profile_manager_(false),
97 created_local_state_(false),
[email protected]b112a4c2009-02-01 20:24:0198 created_icon_manager_(false),
99 created_debugger_wrapper_(false),
[email protected]40ecc902009-03-16 13:42:47100 created_devtools_manager_(false),
[email protected]8b8e7c92010-08-19 18:05:56101 created_sidebar_manager_(false),
[email protected]29672ab2009-10-30 03:44:03102 created_notification_ui_manager_(false),
[email protected]a7a5e992010-12-09 23:39:51103 created_safe_browsing_detection_service_(false),
initial.commit09911bf2008-07-26 23:55:29104 module_ref_count_(0),
[email protected]afd20c022010-06-10 00:48:20105 did_start_(false),
[email protected]1b2db1a2008-08-08 17:46:13106 checked_for_new_frames_(false),
[email protected]6641bf662009-08-21 00:34:09107 using_new_frames_(false),
108 have_inspector_files_(true) {
initial.commit09911bf2008-07-26 23:55:29109 g_browser_process = this;
[email protected]1b8d02f12009-05-05 04:14:11110 clipboard_.reset(new Clipboard);
initial.commit09911bf2008-07-26 23:55:29111 main_notification_service_.reset(new NotificationService);
112
113 // Must be created after the NotificationService.
114 print_job_manager_.reset(new printing::PrintJobManager);
115
[email protected]b797e152009-01-23 16:06:14116 shutdown_event_.reset(new base::WaitableEvent(true, false));
[email protected]b2fcd0e2010-12-01 15:19:40117
118 net_log_.reset(new ChromeNetLog);
initial.commit09911bf2008-07-26 23:55:29119}
120
121BrowserProcessImpl::~BrowserProcessImpl() {
[email protected]4ef795df2010-02-03 02:35:08122 FilePath profile_path;
123 bool clear_local_state_on_exit;
124
125 // Store the profile path for clearing local state data on exit.
126 clear_local_state_on_exit = ShouldClearLocalState(&profile_path);
127
initial.commit09911bf2008-07-26 23:55:29128 // Delete the AutomationProviderList before NotificationService,
129 // since it may try to unregister notifications
130 // Both NotificationService and AutomationProvider are singleton instances in
131 // the BrowserProcess. Since AutomationProvider may have some active
132 // notification observers, it is essential that it gets destroyed before the
133 // NotificationService. NotificationService won't be destroyed until after
134 // this destructor is run.
135 automation_provider_list_.reset();
136
[email protected]d393a0fd2009-05-13 23:32:01137 // We need to shutdown the SdchDictionaryFetcher as it regularly holds
138 // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do
139 // a PostDelayedTask onto the IO thread. This shutdown call will both discard
140 // any pending URLFetchers, and avoid creating any more.
141 SdchDictionaryFetcher::Shutdown();
142
[email protected]c4ff4952010-01-08 19:12:47143 // We need to destroy the MetricsService, GoogleURLTracker, and
144 // IntranetRedirectDetector before the io_thread_ gets destroyed, since their
145 // destructors can call the URLFetcher destructor, which does a
146 // PostDelayedTask operation on the IO thread. (The IO thread will handle
147 // that URLFetcher operation before going away.)
initial.commit09911bf2008-07-26 23:55:29148 metrics_service_.reset();
149 google_url_tracker_.reset();
[email protected]c4ff4952010-01-08 19:12:47150 intranet_redirect_detector_.reset();
initial.commit09911bf2008-07-26 23:55:29151
[email protected]d8a899c2010-09-21 04:50:33152 // Need to clear the desktop notification balloons before the io_thread_ and
153 // before the profiles, since if there are any still showing we will access
154 // those things during teardown.
155 notification_ui_manager_.reset();
156
initial.commit09911bf2008-07-26 23:55:29157 // Need to clear profiles (download managers) before the io_thread_.
158 profile_manager_.reset();
159
160 // Debugger must be cleaned up before IO thread and NotificationService.
161 debugger_wrapper_ = NULL;
162
163 if (resource_dispatcher_host_.get()) {
164 // Need to tell Safe Browsing Service that the IO thread is going away
165 // since it cached a pointer to it.
166 if (resource_dispatcher_host()->safe_browsing_service())
167 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
168
169 // Cancel pending requests and prevent new requests.
170 resource_dispatcher_host()->Shutdown();
171 }
172
[email protected]753efc42010-03-09 19:52:16173#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06174 // The IO thread must outlive the BACKGROUND_X11 thread.
175 background_x11_thread_.reset();
176#endif
177
initial.commit09911bf2008-07-26 23:55:29178 // Need to stop io_thread_ before resource_dispatcher_host_, since
179 // io_thread_ may still deref ResourceDispatcherHost and handle resource
180 // request before going away.
[email protected]0ac83682010-01-22 17:46:27181 io_thread_.reset();
initial.commit09911bf2008-07-26 23:55:29182
[email protected]875ee822010-05-18 20:58:01183 // The IO thread was the only user of this thread.
184 cache_thread_.reset();
185
[email protected]914511712009-11-23 19:42:33186 // Stop the process launcher thread after the IO thread, in case the IO thread
187 // posted a task to terminate a process on the process launcher thread.
188 process_launcher_thread_.reset();
189
initial.commit09911bf2008-07-26 23:55:29190 // Clean up state that lives on the file_thread_ before it goes away.
191 if (resource_dispatcher_host_.get()) {
192 resource_dispatcher_host()->download_file_manager()->Shutdown();
193 resource_dispatcher_host()->save_file_manager()->Shutdown();
194 }
195
196 // Need to stop the file_thread_ here to force it to process messages in its
197 // message loop from the previous call to shutdown the DownloadFileManager,
198 // SaveFileManager and SessionService.
199 file_thread_.reset();
200
201 // With the file_thread_ flushed, we can release any icon resources.
202 icon_manager_.reset();
203
204 // Need to destroy ResourceDispatcherHost before PluginService and
[email protected]49f28bc72010-02-04 00:10:01205 // SafeBrowsingService, since it caches a pointer to it. This also
206 // causes the webkit thread to terminate.
initial.commit09911bf2008-07-26 23:55:29207 resource_dispatcher_host_.reset();
208
209 // Wait for the pending print jobs to finish.
210 print_job_manager_->OnQuit();
211 print_job_manager_.reset();
212
[email protected]0b4d3382010-07-14 16:13:04213 // Destroy TabCloseableStateWatcher before NotificationService since the
214 // former registers for notifications.
215 tab_closeable_state_watcher_.reset();
216
initial.commit09911bf2008-07-26 23:55:29217 // Now OK to destroy NotificationService.
218 main_notification_service_.reset();
219
[email protected]49f28bc72010-02-04 00:10:01220 // Prior to clearing local state, we want to complete tasks pending
221 // on the db thread too.
222 db_thread_.reset();
223
224 // At this point, no render process exist and the file, io, db, and
225 // webkit threads in this process have all terminated, so it's safe
226 // to access local state data such as cookies, database, or local storage.
227 if (clear_local_state_on_exit)
228 ClearLocalState(profile_path);
229
initial.commit09911bf2008-07-26 23:55:29230 g_browser_process = NULL;
231}
232
[email protected]c6032e82010-09-13 20:06:05233#if defined(OS_WIN)
[email protected]295039bd2008-08-15 04:32:57234// Send a QuitTask to the given MessageLoop.
235static void PostQuit(MessageLoop* message_loop) {
236 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
237}
[email protected]c6032e82010-09-13 20:06:05238#elif defined(USE_X11)
239static void Signal(base::WaitableEvent* event) {
240 event->Signal();
241}
242#endif
initial.commit09911bf2008-07-26 23:55:29243
[email protected]b443cb042009-12-15 22:05:09244unsigned int BrowserProcessImpl::AddRefModule() {
245 DCHECK(CalledOnValidThread());
[email protected]afd20c022010-06-10 00:48:20246 did_start_ = true;
[email protected]b443cb042009-12-15 22:05:09247 module_ref_count_++;
248 return module_ref_count_;
249}
250
251unsigned int BrowserProcessImpl::ReleaseModule() {
252 DCHECK(CalledOnValidThread());
[email protected]760d970a2010-05-18 00:39:18253 DCHECK_NE(0u, module_ref_count_);
[email protected]b443cb042009-12-15 22:05:09254 module_ref_count_--;
255 if (0 == module_ref_count_) {
[email protected]7d038c32010-12-14 00:40:00256 // Allow UI and IO threads to do blocking IO on shutdown, since we do a lot
257 // of it on shutdown for valid reasons.
258 base::ThreadRestrictions::SetIOAllowed(true);
259 io_thread()->message_loop()->PostTask(
260 FROM_HERE,
261 NewRunnableFunction(&base::ThreadRestrictions::SetIOAllowed, true));
[email protected]b443cb042009-12-15 22:05:09262 MessageLoop::current()->PostTask(
[email protected]34f73fb2010-03-24 20:50:34263 FROM_HERE, NewRunnableFunction(DidEndMainMessageLoop));
[email protected]b443cb042009-12-15 22:05:09264 MessageLoop::current()->Quit();
265 }
266 return module_ref_count_;
267}
268
initial.commit09911bf2008-07-26 23:55:29269void BrowserProcessImpl::EndSession() {
[email protected]c6032e82010-09-13 20:06:05270#if defined(OS_WIN) || defined(USE_X11)
[email protected]d65cab7a2008-08-12 01:25:41271 // Notify we are going away.
[email protected]c6032e82010-09-13 20:06:05272 shutdown_event_->Signal();
[email protected]b112a4c2009-02-01 20:24:01273#endif
[email protected]d65cab7a2008-08-12 01:25:41274
initial.commit09911bf2008-07-26 23:55:29275 // Mark all the profiles as clean.
276 ProfileManager* pm = profile_manager();
277 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
278 (*i)->MarkAsCleanShutdown();
279
280 // Tell the metrics service it was cleanly shutdown.
281 MetricsService* metrics = g_browser_process->metrics_service();
282 if (metrics && local_state()) {
283 metrics->RecordCleanShutdown();
284
285 metrics->RecordStartOfSessionEnd();
286
287 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36288 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29289 }
290
291 // We must write that the profile and metrics service shutdown cleanly,
292 // otherwise on startup we'll think we crashed. So we block until done and
293 // then proceed with normal shutdown.
[email protected]c6032e82010-09-13 20:06:05294#if defined(USE_X11)
295 // Can't run a local loop on linux. Instead create a waitable event.
296 base::WaitableEvent done_writing(false, false);
[email protected]d04e7662010-10-10 22:24:48297 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]c6032e82010-09-13 20:06:05298 NewRunnableFunction(Signal, &done_writing));
299 done_writing.TimedWait(
300 base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds));
301#elif defined(OS_WIN)
[email protected]d04e7662010-10-10 22:24:48302 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57303 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29304 MessageLoop::current()->Run();
[email protected]c6032e82010-09-13 20:06:05305#else
306 NOTIMPLEMENTED();
307#endif
initial.commit09911bf2008-07-26 23:55:29308}
309
[email protected]e13ad79b2010-07-22 21:36:50310ResourceDispatcherHost* BrowserProcessImpl::resource_dispatcher_host() {
311 DCHECK(CalledOnValidThread());
312 if (!created_resource_dispatcher_host_)
313 CreateResourceDispatcherHost();
314 return resource_dispatcher_host_.get();
315}
316
317MetricsService* BrowserProcessImpl::metrics_service() {
318 DCHECK(CalledOnValidThread());
319 if (!created_metrics_service_)
320 CreateMetricsService();
321 return metrics_service_.get();
322}
323
324IOThread* BrowserProcessImpl::io_thread() {
325 DCHECK(CalledOnValidThread());
326 if (!created_io_thread_)
327 CreateIOThread();
328 return io_thread_.get();
329}
330
331base::Thread* BrowserProcessImpl::file_thread() {
332 DCHECK(CalledOnValidThread());
333 if (!created_file_thread_)
334 CreateFileThread();
335 return file_thread_.get();
336}
337
338base::Thread* BrowserProcessImpl::db_thread() {
339 DCHECK(CalledOnValidThread());
340 if (!created_db_thread_)
341 CreateDBThread();
342 return db_thread_.get();
343}
344
345base::Thread* BrowserProcessImpl::process_launcher_thread() {
346 DCHECK(CalledOnValidThread());
347 if (!created_process_launcher_thread_)
348 CreateProcessLauncherThread();
349 return process_launcher_thread_.get();
350}
351
352base::Thread* BrowserProcessImpl::cache_thread() {
353 DCHECK(CalledOnValidThread());
354 if (!created_cache_thread_)
355 CreateCacheThread();
356 return cache_thread_.get();
357}
358
359#if defined(USE_X11)
360base::Thread* BrowserProcessImpl::background_x11_thread() {
361 DCHECK(CalledOnValidThread());
362 // The BACKGROUND_X11 thread is created when the IO thread is created.
363 if (!created_io_thread_)
364 CreateIOThread();
365 return background_x11_thread_.get();
366}
367#endif
368
369ProfileManager* BrowserProcessImpl::profile_manager() {
370 DCHECK(CalledOnValidThread());
371 if (!created_profile_manager_)
372 CreateProfileManager();
373 return profile_manager_.get();
374}
375
376PrefService* BrowserProcessImpl::local_state() {
377 DCHECK(CalledOnValidThread());
378 if (!created_local_state_)
379 CreateLocalState();
380 return local_state_.get();
381}
382
383DevToolsManager* BrowserProcessImpl::devtools_manager() {
384 DCHECK(CalledOnValidThread());
385 if (!created_devtools_manager_)
386 CreateDevToolsManager();
387 return devtools_manager_.get();
388}
389
[email protected]8b8e7c92010-08-19 18:05:56390SidebarManager* BrowserProcessImpl::sidebar_manager() {
391 DCHECK(CalledOnValidThread());
392 if (!created_sidebar_manager_)
393 CreateSidebarManager();
394 return sidebar_manager_.get();
395}
396
[email protected]e13ad79b2010-07-22 21:36:50397Clipboard* BrowserProcessImpl::clipboard() {
398 DCHECK(CalledOnValidThread());
399 return clipboard_.get();
400}
401
402NotificationUIManager* BrowserProcessImpl::notification_ui_manager() {
403 DCHECK(CalledOnValidThread());
404 if (!created_notification_ui_manager_)
405 CreateNotificationUIManager();
406 return notification_ui_manager_.get();
407}
408
[email protected]e13ad79b2010-07-22 21:36:50409IconManager* BrowserProcessImpl::icon_manager() {
410 DCHECK(CalledOnValidThread());
411 if (!created_icon_manager_)
412 CreateIconManager();
413 return icon_manager_.get();
414}
415
416ThumbnailGenerator* BrowserProcessImpl::GetThumbnailGenerator() {
417 return &thumbnail_generator_;
418}
419
420AutomationProviderList* BrowserProcessImpl::InitAutomationProviderList() {
421 DCHECK(CalledOnValidThread());
422 if (automation_provider_list_.get() == NULL) {
423 automation_provider_list_.reset(AutomationProviderList::GetInstance());
424 }
425 return automation_provider_list_.get();
426}
427
428void BrowserProcessImpl::InitDebuggerWrapper(int port, bool useHttp) {
429 DCHECK(CalledOnValidThread());
430 if (!created_debugger_wrapper_)
431 CreateDebuggerWrapper(port, useHttp);
432}
433
434bool BrowserProcessImpl::IsShuttingDown() {
435 DCHECK(CalledOnValidThread());
436 return did_start_ && 0 == module_ref_count_;
437}
438
initial.commit09911bf2008-07-26 23:55:29439printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
440 // TODO(abarth): DCHECK(CalledOnValidThread());
[email protected]d8922f22010-05-07 00:57:27441 // http://code.google.com/p/chromium/issues/detail?id=6828
initial.commit09911bf2008-07-26 23:55:29442 // print_job_manager_ is initialized in the constructor and destroyed in the
443 // destructor, so it should always be valid.
444 DCHECK(print_job_manager_.get());
445 return print_job_manager_.get();
446}
447
[email protected]dbeebd52010-11-16 20:34:16448printing::PrintPreviewTabController*
449 BrowserProcessImpl::print_preview_tab_controller() {
450 DCHECK(CalledOnValidThread());
451 if (!print_preview_tab_controller_.get())
452 CreatePrintPreviewTabController();
453 return print_preview_tab_controller_.get();
454}
455
[email protected]e13ad79b2010-07-22 21:36:50456GoogleURLTracker* BrowserProcessImpl::google_url_tracker() {
457 DCHECK(CalledOnValidThread());
458 if (!google_url_tracker_.get())
459 CreateGoogleURLTracker();
460 return google_url_tracker_.get();
461}
462
463IntranetRedirectDetector* BrowserProcessImpl::intranet_redirect_detector() {
464 DCHECK(CalledOnValidThread());
465 if (!intranet_redirect_detector_.get())
466 CreateIntranetRedirectDetector();
467 return intranet_redirect_detector_.get();
468}
469
470const std::string& BrowserProcessImpl::GetApplicationLocale() {
471 DCHECK(!locale_.empty());
472 return locale_;
473}
474
475void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) {
476 locale_ = locale;
477 extension_l10n_util::SetProcessLocale(locale);
478}
479
[email protected]073ed7b2010-09-27 09:20:02480DownloadStatusUpdater* BrowserProcessImpl::download_status_updater() {
481 return &download_status_updater_;
482}
483
[email protected]e13ad79b2010-07-22 21:36:50484base::WaitableEvent* BrowserProcessImpl::shutdown_event() {
485 return shutdown_event_.get();
486}
487
488TabCloseableStateWatcher* BrowserProcessImpl::tab_closeable_state_watcher() {
489 DCHECK(CalledOnValidThread());
490 if (!tab_closeable_state_watcher_.get())
491 CreateTabCloseableStateWatcher();
492 return tab_closeable_state_watcher_.get();
493}
494
[email protected]a7a5e992010-12-09 23:39:51495safe_browsing::ClientSideDetectionService*
496 BrowserProcessImpl::safe_browsing_detection_service() {
497 DCHECK(CalledOnValidThread());
498 if (!created_safe_browsing_detection_service_) {
499 CreateSafeBrowsingDetectionService();
500 }
501 return safe_browsing_detection_service_.get();
502}
503
[email protected]e13ad79b2010-07-22 21:36:50504void BrowserProcessImpl::CheckForInspectorFiles() {
505 file_thread()->message_loop()->PostTask
506 (FROM_HERE,
507 NewRunnableMethod(this, &BrowserProcessImpl::DoInspectorFilesCheck));
508}
509
510#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
511void BrowserProcessImpl::StartAutoupdateTimer() {
512 autoupdate_timer_.Start(
513 base::TimeDelta::FromHours(kUpdateCheckIntervalHours),
514 this,
515 &BrowserProcessImpl::OnAutoupdateTimer);
516}
517#endif
518
519bool BrowserProcessImpl::have_inspector_files() const {
520 return have_inspector_files_;
521}
522
[email protected]4ef795df2010-02-03 02:35:08523void BrowserProcessImpl::ClearLocalState(const FilePath& profile_path) {
[email protected]72cfd90f2010-02-06 03:08:04524 webkit_database::DatabaseTracker::ClearLocalState(profile_path);
[email protected]10c934052010-02-04 19:58:31525 ChromeAppCacheService::ClearLocalState(profile_path);
[email protected]4ef795df2010-02-03 02:35:08526}
527
528bool BrowserProcessImpl::ShouldClearLocalState(FilePath* profile_path) {
529 FilePath user_data_dir;
530 Profile* profile;
531
[email protected]09584972010-07-03 00:00:59532 // Check for the existence of a profile manager. When quitting early,
[email protected]ed0cf562010-02-05 22:50:41533 // e.g. because another chrome instance is running, or when invoked with
534 // options such as --uninstall or --try-chrome-again=0, the profile manager
535 // does not exist yet.
536 if (!profile_manager_.get())
537 return false;
538
[email protected]4ef795df2010-02-03 02:35:08539 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
540 profile = profile_manager_->GetDefaultProfile(user_data_dir);
[email protected]7bfc674322010-03-05 00:20:57541 if (!profile)
542 return false;
[email protected]4ef795df2010-02-03 02:35:08543 *profile_path = profile->GetPath();
544 return profile->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit);
545}
546
initial.commit09911bf2008-07-26 23:55:29547void BrowserProcessImpl::CreateResourceDispatcherHost() {
548 DCHECK(!created_resource_dispatcher_host_ &&
549 resource_dispatcher_host_.get() == NULL);
550 created_resource_dispatcher_host_ = true;
551
[email protected]79084c2d2009-11-03 23:12:42552 resource_dispatcher_host_.reset(new ResourceDispatcherHost());
initial.commit09911bf2008-07-26 23:55:29553 resource_dispatcher_host_->Initialize();
554}
555
556void BrowserProcessImpl::CreateMetricsService() {
557 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
558 created_metrics_service_ = true;
559
560 metrics_service_.reset(new MetricsService);
561}
562
563void BrowserProcessImpl::CreateIOThread() {
564 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
565 created_io_thread_ = true;
566
567 // Prior to starting the io thread, we create the plugin service as
568 // it is predominantly used from the io thread, but must be created
569 // on the main thread. The service ctor is inexpensive and does not
570 // invoke the io_thread() accessor.
571 PluginService::GetInstance();
572
[email protected]753efc42010-03-09 19:52:16573#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06574 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
575 // we start it now.
576 scoped_ptr<base::Thread> background_x11_thread(
[email protected]d04e7662010-10-10 22:24:48577 new BrowserProcessSubThread(BrowserThread::BACKGROUND_X11));
[email protected]4c3cd7412009-04-22 17:56:06578 if (!background_x11_thread->Start())
579 return;
580 background_x11_thread_.swap(background_x11_thread);
581#endif
582
[email protected]b2fcd0e2010-12-01 15:19:40583 scoped_ptr<IOThread> thread(new IOThread(local_state(), net_log_.get()));
[email protected]ab820df2008-08-26 05:55:10584 base::Thread::Options options;
585 options.message_loop_type = MessageLoop::TYPE_IO;
586 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29587 return;
588 io_thread_.swap(thread);
589}
590
591void BrowserProcessImpl::CreateFileThread() {
592 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
593 created_file_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::FILE));
[email protected]a1db3842008-09-17 22:04:06597 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39598#if defined(OS_WIN)
599 // On Windows, the FILE thread needs to be have a UI message loop which pumps
600 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06601 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39602#else
603 options.message_loop_type = MessageLoop::TYPE_IO;
604#endif
[email protected]a1db3842008-09-17 22:04:06605 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29606 return;
607 file_thread_.swap(thread);
608}
609
610void BrowserProcessImpl::CreateDBThread() {
611 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
612 created_db_thread_ = true;
613
[email protected]ab820df2008-08-26 05:55:10614 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48615 new BrowserProcessSubThread(BrowserThread::DB));
initial.commit09911bf2008-07-26 23:55:29616 if (!thread->Start())
617 return;
618 db_thread_.swap(thread);
619}
620
[email protected]914511712009-11-23 19:42:33621void BrowserProcessImpl::CreateProcessLauncherThread() {
622 DCHECK(!created_process_launcher_thread_ && !process_launcher_thread_.get());
623 created_process_launcher_thread_ = true;
624
625 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48626 new BrowserProcessSubThread(BrowserThread::PROCESS_LAUNCHER));
[email protected]914511712009-11-23 19:42:33627 if (!thread->Start())
628 return;
629 process_launcher_thread_.swap(thread);
630}
631
[email protected]875ee822010-05-18 20:58:01632void BrowserProcessImpl::CreateCacheThread() {
633 DCHECK(!created_cache_thread_ && !cache_thread_.get());
634 created_cache_thread_ = true;
635
636 scoped_ptr<base::Thread> thread(
[email protected]75096332010-11-09 03:28:13637 new BrowserThread(BrowserThread::CACHE));
[email protected]875ee822010-05-18 20:58:01638 base::Thread::Options options;
639 options.message_loop_type = MessageLoop::TYPE_IO;
640 if (!thread->StartWithOptions(options))
641 return;
642 cache_thread_.swap(thread);
643}
644
initial.commit09911bf2008-07-26 23:55:29645void BrowserProcessImpl::CreateProfileManager() {
646 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
647 created_profile_manager_ = true;
648
649 profile_manager_.reset(new ProfileManager());
650}
651
652void BrowserProcessImpl::CreateLocalState() {
653 DCHECK(!created_local_state_ && local_state_.get() == NULL);
654 created_local_state_ = true;
655
[email protected]b9636002009-03-04 00:05:25656 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29657 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]f2d1f612010-12-09 15:10:17658 local_state_.reset(
659 PrefService::CreatePrefService(local_state_path, NULL, NULL));
[email protected]492d2142010-09-10 13:55:18660
[email protected]2a7e7c152010-10-01 20:12:09661 pref_change_registrar_.Init(local_state_.get());
662
[email protected]492d2142010-09-10 13:55:18663 // Make sure the the plugin updater gets notifications of changes
664 // in the plugin blacklist.
665 local_state_->RegisterListPref(prefs::kPluginsPluginsBlacklist);
[email protected]2a7e7c152010-10-01 20:12:09666 pref_change_registrar_.Add(prefs::kPluginsPluginsBlacklist,
[email protected]8e8bb6d2010-12-13 08:18:55667 PluginUpdater::GetInstance());
[email protected]2a7e7c152010-10-01 20:12:09668
669 // Initialize and set up notifications for the printing enabled
670 // preference.
671 local_state_->RegisterBooleanPref(prefs::kPrintingEnabled, true);
672 bool printing_enabled =
673 local_state_->GetBoolean(prefs::kPrintingEnabled);
674 print_job_manager_->set_printing_enabled(printing_enabled);
675 pref_change_registrar_.Add(prefs::kPrintingEnabled,
676 print_job_manager_.get());
[email protected]fd6159a2010-09-03 09:38:39677}
initial.commit09911bf2008-07-26 23:55:29678
initial.commit09911bf2008-07-26 23:55:29679void BrowserProcessImpl::CreateIconManager() {
680 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
681 created_icon_manager_ = true;
682 icon_manager_.reset(new IconManager);
683}
684
[email protected]a9bf67982010-07-06 10:13:00685void BrowserProcessImpl::CreateDebuggerWrapper(int port, bool useHttp) {
initial.commit09911bf2008-07-26 23:55:29686 DCHECK(debugger_wrapper_.get() == NULL);
687 created_debugger_wrapper_ = true;
688
[email protected]a9bf67982010-07-06 10:13:00689 debugger_wrapper_ = new DebuggerWrapper(port, useHttp);
initial.commit09911bf2008-07-26 23:55:29690}
691
[email protected]40ecc902009-03-16 13:42:47692void BrowserProcessImpl::CreateDevToolsManager() {
[email protected]73ee01522009-06-05 10:13:44693 DCHECK(devtools_manager_.get() == NULL);
[email protected]40ecc902009-03-16 13:42:47694 created_devtools_manager_ = true;
[email protected]73ee01522009-06-05 10:13:44695 devtools_manager_ = new DevToolsManager();
[email protected]40ecc902009-03-16 13:42:47696}
697
[email protected]8b8e7c92010-08-19 18:05:56698void BrowserProcessImpl::CreateSidebarManager() {
699 DCHECK(sidebar_manager_.get() == NULL);
700 created_sidebar_manager_ = true;
701 sidebar_manager_ = new SidebarManager();
702}
703
initial.commit09911bf2008-07-26 23:55:29704void BrowserProcessImpl::CreateGoogleURLTracker() {
705 DCHECK(google_url_tracker_.get() == NULL);
706 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
707 google_url_tracker_.swap(google_url_tracker);
708}
[email protected]6641bf662009-08-21 00:34:09709
[email protected]c4ff4952010-01-08 19:12:47710void BrowserProcessImpl::CreateIntranetRedirectDetector() {
711 DCHECK(intranet_redirect_detector_.get() == NULL);
712 scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector(
713 new IntranetRedirectDetector);
714 intranet_redirect_detector_.swap(intranet_redirect_detector);
715}
716
[email protected]29672ab2009-10-30 03:44:03717void BrowserProcessImpl::CreateNotificationUIManager() {
718 DCHECK(notification_ui_manager_.get() == NULL);
719 notification_ui_manager_.reset(NotificationUIManager::Create());
720 created_notification_ui_manager_ = true;
721}
722
[email protected]0b4d3382010-07-14 16:13:04723void BrowserProcessImpl::CreateTabCloseableStateWatcher() {
724 DCHECK(tab_closeable_state_watcher_.get() == NULL);
725 tab_closeable_state_watcher_.reset(TabCloseableStateWatcher::Create());
726}
727
[email protected]dbeebd52010-11-16 20:34:16728void BrowserProcessImpl::CreatePrintPreviewTabController() {
729 DCHECK(print_preview_tab_controller_.get() == NULL);
730 print_preview_tab_controller_ = new printing::PrintPreviewTabController();
731}
732
[email protected]a7a5e992010-12-09 23:39:51733void BrowserProcessImpl::CreateSafeBrowsingDetectionService() {
734 DCHECK(safe_browsing_detection_service_.get() == NULL);
735 // Set this flag to true so that we don't retry indefinitely to
736 // create the service class if there was an error.
737 created_safe_browsing_detection_service_ = true;
738
739 FilePath model_file_path;
740 Profile* profile = profile_manager() ?
741 profile_manager()->GetDefaultProfile() : NULL;
742 if (IsSafeBrowsingDetectionServiceEnabled() &&
743 PathService::Get(chrome::DIR_USER_DATA, &model_file_path) &&
744 profile && profile->GetRequestContext()) {
745 safe_browsing_detection_service_.reset(
746 safe_browsing::ClientSideDetectionService::Create(
747 model_file_path.Append(chrome::kSafeBrowsingPhishingModelFilename),
748 profile->GetRequestContext()));
749 }
750}
751
752bool BrowserProcessImpl::IsSafeBrowsingDetectionServiceEnabled() {
753 // The safe browsing client-side detection is enabled only if the switch is
754 // enabled, the user has opted in to UMA usage stats and SafeBrowsing
755 // is enabled.
756 Profile* profile = profile_manager() ?
757 profile_manager()->GetDefaultProfile() : NULL;
758 return (CommandLine::ForCurrentProcess()->HasSwitch(
759 switches::kEnableClientSidePhishingDetection) &&
760 metrics_service() && metrics_service()->reporting_active() &&
761 profile && profile->GetPrefs() &&
762 profile->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled));
763}
764
[email protected]6641bf662009-08-21 00:34:09765// The BrowserProcess object must outlive the file thread so we use traits
766// which don't do any management.
[email protected]c56428f22010-06-16 02:17:23767DISABLE_RUNNABLE_METHOD_REFCOUNT(BrowserProcessImpl);
[email protected]6641bf662009-08-21 00:34:09768
[email protected]d55aaa132009-09-28 21:08:04769#if defined(IPC_MESSAGE_LOG_ENABLED)
770
771void BrowserProcessImpl::SetIPCLoggingEnabled(bool enable) {
772 // First enable myself.
773 if (enable)
[email protected]8e8bb6d2010-12-13 08:18:55774 IPC::Logging::GetInstance()->Enable();
[email protected]d55aaa132009-09-28 21:08:04775 else
[email protected]8e8bb6d2010-12-13 08:18:55776 IPC::Logging::GetInstance()->Disable();
[email protected]d55aaa132009-09-28 21:08:04777
778 // Now tell subprocesses. Messages to ChildProcess-derived
779 // processes must be done on the IO thread.
780 io_thread()->message_loop()->PostTask
781 (FROM_HERE,
782 NewRunnableMethod(
783 this,
784 &BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses,
785 enable));
786
787 // Finally, tell the renderers which don't derive from ChildProcess.
788 // Messages to the renderers must be done on the UI (main) thread.
[email protected]019191a2009-10-02 20:37:27789 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
790 !i.IsAtEnd(); i.Advance())
791 i.GetCurrentValue()->Send(new ViewMsg_SetIPCLoggingEnabled(enable));
[email protected]d55aaa132009-09-28 21:08:04792}
793
794// Helper for SetIPCLoggingEnabled.
795void BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses(bool enabled) {
[email protected]d04e7662010-10-10 22:24:48796 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]d55aaa132009-09-28 21:08:04797
[email protected]d27893f62010-07-03 05:47:42798 BrowserChildProcessHost::Iterator i; // default constr references a singleton
[email protected]34cf97d2009-09-29 22:46:11799 while (!i.Done()) {
800 i->Send(new PluginProcessMsg_SetIPCLoggingEnabled(enabled));
801 ++i;
[email protected]d55aaa132009-09-28 21:08:04802 }
803}
804
805#endif // IPC_MESSAGE_LOG_ENABLED
806
[email protected]6641bf662009-08-21 00:34:09807void BrowserProcessImpl::DoInspectorFilesCheck() {
808 // Runs on FILE thread.
809 DCHECK(file_thread_->message_loop() == MessageLoop::current());
810 bool result = false;
811
812 FilePath inspector_dir;
813 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) {
814 result = file_util::PathExists(inspector_dir);
815 }
816
[email protected]308080d2009-11-11 19:19:02817 have_inspector_files_ = result;
[email protected]6641bf662009-08-21 00:34:09818}
[email protected]bd48c2b02010-04-09 20:32:42819
[email protected]3cdacd42010-04-30 18:55:53820// Mac is currently not supported.
821#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
[email protected]bd48c2b02010-04-09 20:32:42822
823bool BrowserProcessImpl::CanAutorestartForUpdate() const {
824 // Check if browser is in the background and if it needs to be restarted to
825 // apply a pending update.
[email protected]c10fa3952010-08-24 16:52:53826 return BrowserList::size() == 0 && BrowserList::WillKeepAlive() &&
[email protected]c984d9f2010-07-20 20:52:20827 Upgrade::IsUpdatePendingRestart();
[email protected]bd48c2b02010-04-09 20:32:42828}
829
[email protected]5c08f222010-09-22 09:37:21830// Switches to add when auto-restarting Chrome.
[email protected]6c10c972010-08-11 04:15:47831const char* const kSwitchesToAddOnAutorestart[] = {
[email protected]f9a06842010-08-21 00:32:40832 switches::kNoStartupWindow
[email protected]bd48c2b02010-04-09 20:32:42833};
834
835void BrowserProcessImpl::RestartPersistentInstance() {
836 CommandLine* old_cl = CommandLine::ForCurrentProcess();
[email protected]3cdacd42010-04-30 18:55:53837 scoped_ptr<CommandLine> new_cl(new CommandLine(old_cl->GetProgram()));
[email protected]bd48c2b02010-04-09 20:32:42838
839 std::map<std::string, CommandLine::StringType> switches =
840 old_cl->GetSwitches();
841
[email protected]5c08f222010-09-22 09:37:21842 switches::RemoveSwitchesForAutostart(&switches);
[email protected]bd48c2b02010-04-09 20:32:42843
844 // Append the rest of the switches (along with their values, if any)
845 // to the new command line
846 for (std::map<std::string, CommandLine::StringType>::const_iterator i =
847 switches.begin(); i != switches.end(); ++i) {
848 CommandLine::StringType switch_value = i->second;
849 if (switch_value.length() > 0) {
[email protected]05076ba22010-07-30 05:59:57850 new_cl->AppendSwitchNative(i->first, i->second);
[email protected]bd48c2b02010-04-09 20:32:42851 } else {
[email protected]3cdacd42010-04-30 18:55:53852 new_cl->AppendSwitch(i->first);
[email protected]bd48c2b02010-04-09 20:32:42853 }
854 }
855
[email protected]6c10c972010-08-11 04:15:47856 // Ensure that our desired switches are set on the new process.
[email protected]5c08f222010-09-22 09:37:21857 for (size_t i = 0; i < arraysize(kSwitchesToAddOnAutorestart); ++i) {
[email protected]6c10c972010-08-11 04:15:47858 if (!new_cl->HasSwitch(kSwitchesToAddOnAutorestart[i]))
859 new_cl->AppendSwitch(kSwitchesToAddOnAutorestart[i]);
860 }
[email protected]bd48c2b02010-04-09 20:32:42861
[email protected]3cdacd42010-04-30 18:55:53862 DLOG(WARNING) << "Shutting down current instance of the browser.";
863 BrowserList::CloseAllBrowsersAndExit();
864
865 // Transfer ownership to Upgrade.
866 Upgrade::SetNewCommandLine(new_cl.release());
[email protected]bd48c2b02010-04-09 20:32:42867}
868
869void BrowserProcessImpl::OnAutoupdateTimer() {
870 if (CanAutorestartForUpdate()) {
[email protected]3cdacd42010-04-30 18:55:53871 DLOG(WARNING) << "Detected update. Restarting browser.";
[email protected]bd48c2b02010-04-09 20:32:42872 RestartPersistentInstance();
873 }
874}
875
[email protected]3cdacd42010-04-30 18:55:53876#endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)