blob: 70f321e22a3486f9fae59137510175c4174efd16 [file] [log] [blame]
[email protected]f2a893c2011-01-05 09:38:071// Copyright (c) 2011 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
initial.commit09911bf2008-07-26 23:55:299#include "base/command_line.h"
[email protected]6641bf662009-08-21 00:34:0910#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/path_service.h"
[email protected]985655a2011-02-23 09:54:2512#include "base/synchronization/waitable_event.h"
[email protected]c6032e82010-09-13 20:06:0513#include "base/task.h"
[email protected]34b99632011-01-01 01:01:0614#include "base/threading/thread.h"
15#include "base/threading/thread_restrictions.h"
[email protected]e13ad79b2010-07-22 21:36:5016#include "chrome/browser/automation/automation_provider_list.h"
[email protected]bd48c2b02010-04-09 20:32:4217#include "chrome/browser/browser_list.h"
[email protected]b443cb042009-12-15 22:05:0918#include "chrome/browser/browser_main.h"
[email protected]0ac83682010-01-22 17:46:2719#include "chrome/browser/browser_process_sub_thread.h"
[email protected]ac262c9f2008-10-19 17:45:2120#include "chrome/browser/browser_trial.h"
[email protected]9678cb42011-01-31 18:49:4221#include "chrome/browser/debugger/browser_list_tabcontents_provider.h"
[email protected]5613126e2011-01-31 15:27:5522#include "chrome/browser/debugger/devtools_http_protocol_handler.h"
[email protected]40ecc902009-03-16 13:42:4723#include "chrome/browser/debugger/devtools_manager.h"
[email protected]5613126e2011-01-31 15:27:5524#include "chrome/browser/debugger/devtools_protocol_handler.h"
[email protected]6c69796d2010-07-16 21:41:1625#include "chrome/browser/download/download_file_manager.h"
[email protected]5ba0a2c2009-02-19 01:19:3426#include "chrome/browser/download/save_file_manager.h"
[email protected]3ce02412011-03-01 12:01:1527#include "chrome/browser/extensions/extension_event_router_forwarder.h"
[email protected]fc4facd2011-03-22 23:18:5028#include "chrome/browser/extensions/extension_tab_id_map.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]7a31f7c2011-03-21 23:22:0431#include "chrome/browser/gpu_process_host_ui_shim.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]0b565182011-03-02 18:11:1536#include "chrome/browser/metrics/thread_watcher.h"
[email protected]b2fcd0e2010-12-01 15:19:4037#include "chrome/browser/net/chrome_net_log.h"
[email protected]3530cd92010-06-27 06:22:0138#include "chrome/browser/net/predictor_api.h"
[email protected]d393a0fd2009-05-13 23:32:0139#include "chrome/browser/net/sdch_dictionary_fetcher.h"
[email protected]29672ab2009-10-30 03:44:0340#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]6618d1d2010-12-15 21:18:4741#include "chrome/browser/plugin_data_remover.h"
[email protected]492d2142010-09-10 13:55:1842#include "chrome/browser/plugin_updater.h"
[email protected]985655a2011-02-23 09:54:2543#include "chrome/browser/policy/browser_policy_connector.h"
[email protected]37858e52010-08-26 00:22:0244#include "chrome/browser/prefs/pref_service.h"
[email protected]e06131d2010-02-10 18:40:3345#include "chrome/browser/printing/print_job_manager.h"
[email protected]dbeebd52010-11-16 20:34:1646#include "chrome/browser/printing/print_preview_tab_controller.h"
[email protected]8ecad5e2010-12-02 21:18:3347#include "chrome/browser/profiles/profile_manager.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]e450fa62011-02-01 12:52:5650#include "chrome/browser/shell_integration.h"
[email protected]8b8e7c92010-08-19 18:05:5651#include "chrome/browser/sidebar/sidebar_manager.h"
[email protected]0b4d3382010-07-14 16:13:0452#include "chrome/browser/tab_closeable_state_watcher.h"
[email protected]4ef795df2010-02-03 02:35:0853#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2954#include "chrome/common/chrome_paths.h"
55#include "chrome/common/chrome_switches.h"
[email protected]f1b6de22010-03-06 12:13:4756#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]985655a2011-02-23 09:54:2557#include "chrome/common/extensions/extension_resource.h"
[email protected]ea587b02010-05-21 15:01:3558#include "chrome/common/json_pref_store.h"
initial.commit09911bf2008-07-26 23:55:2959#include "chrome/common/pref_names.h"
[email protected]5c08f222010-09-22 09:37:2160#include "chrome/common/switch_utils.h"
[email protected]985655a2011-02-23 09:54:2561#include "chrome/common/url_constants.h"
[email protected]bd48c2b02010-04-09 20:32:4262#include "chrome/installer/util/google_update_constants.h"
[email protected]a01efd22011-03-01 00:38:3263#include "content/browser/browser_child_process_host.h"
64#include "content/browser/browser_thread.h"
65#include "content/browser/plugin_service.h"
66#include "content/browser/renderer_host/render_process_host.h"
67#include "content/browser/renderer_host/resource_dispatcher_host.h"
[email protected]f20d7332011-03-08 21:11:5368#include "content/common/notification_service.h"
[email protected]d55aaa132009-09-28 21:08:0469#include "ipc/ipc_logging.h"
[email protected]abe2c032011-03-31 18:49:3470#include "net/url_request/url_request_context_getter.h"
[email protected]2dfeaf92011-01-10 21:08:2171#include "ui/base/clipboard/clipboard.h"
[email protected]c051a1b2011-01-21 23:30:1772#include "ui/base/l10n/l10n_util.h"
[email protected]4ef795df2010-02-03 02:35:0873#include "webkit/database/database_tracker.h"
[email protected]b112a4c2009-02-01 20:24:0174
75#if defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0576#include "views/focus/view_storage.h"
[email protected]86230b92009-11-23 20:38:3877#endif
78
[email protected]d55aaa132009-09-28 21:08:0479#if defined(IPC_MESSAGE_LOG_ENABLED)
[email protected]ff47b2962011-03-07 23:51:4980#include "content/common/child_process_messages.h"
[email protected]d55aaa132009-09-28 21:08:0481#endif
82
[email protected]db0e86dd2011-03-16 14:47:2183#if defined(OS_CHROMEOS)
84#include "chrome/browser/chromeos/proxy_config_service_impl.h"
85#endif // defined(OS_CHROMEOS)
86
[email protected]3cdacd42010-04-30 18:55:5387#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
88// How often to check if the persistent instance of Chrome needs to restart
89// to install an update.
90static const int kUpdateCheckIntervalHours = 6;
91#endif
92
[email protected]c6032e82010-09-13 20:06:0593#if defined(USE_X11)
94// How long to wait for the File thread to complete during EndSession, on
95// Linux. We have a timeout here because we're unable to run the UI messageloop
96// and there's some deadlock risk. Our only option is to exit anyway.
97static const int kEndSessionTimeoutSeconds = 10;
98#endif
99
[email protected]bb975362009-01-21 01:00:22100BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
initial.commit09911bf2008-07-26 23:55:29101 : created_resource_dispatcher_host_(false),
102 created_metrics_service_(false),
103 created_io_thread_(false),
104 created_file_thread_(false),
105 created_db_thread_(false),
[email protected]914511712009-11-23 19:42:33106 created_process_launcher_thread_(false),
[email protected]875ee822010-05-18 20:58:01107 created_cache_thread_(false),
[email protected]7a31f7c2011-03-21 23:22:04108 created_gpu_thread_(false),
[email protected]0b565182011-03-02 18:11:15109 created_watchdog_thread_(false),
initial.commit09911bf2008-07-26 23:55:29110 created_profile_manager_(false),
111 created_local_state_(false),
[email protected]b112a4c2009-02-01 20:24:01112 created_icon_manager_(false),
[email protected]40ecc902009-03-16 13:42:47113 created_devtools_manager_(false),
[email protected]8b8e7c92010-08-19 18:05:56114 created_sidebar_manager_(false),
[email protected]985655a2011-02-23 09:54:25115 created_browser_policy_connector_(false),
[email protected]29672ab2009-10-30 03:44:03116 created_notification_ui_manager_(false),
[email protected]a7a5e992010-12-09 23:39:51117 created_safe_browsing_detection_service_(false),
initial.commit09911bf2008-07-26 23:55:29118 module_ref_count_(0),
[email protected]afd20c022010-06-10 00:48:20119 did_start_(false),
[email protected]1b2db1a2008-08-08 17:46:13120 checked_for_new_frames_(false),
[email protected]4747caf2011-03-23 20:09:21121 using_new_frames_(false) {
initial.commit09911bf2008-07-26 23:55:29122 g_browser_process = this;
[email protected]2dfeaf92011-01-10 21:08:21123 clipboard_.reset(new ui::Clipboard);
initial.commit09911bf2008-07-26 23:55:29124 main_notification_service_.reset(new NotificationService);
125
[email protected]6618d1d2010-12-15 21:18:47126 notification_registrar_.Add(this,
127 NotificationType::APP_TERMINATING,
128 NotificationService::AllSources());
129
initial.commit09911bf2008-07-26 23:55:29130 // Must be created after the NotificationService.
131 print_job_manager_.reset(new printing::PrintJobManager);
132
[email protected]b797e152009-01-23 16:06:14133 shutdown_event_.reset(new base::WaitableEvent(true, false));
[email protected]b2fcd0e2010-12-01 15:19:40134
135 net_log_.reset(new ChromeNetLog);
[email protected]3ce02412011-03-01 12:01:15136
137 extension_event_router_forwarder_ = new ExtensionEventRouterForwarder;
[email protected]fc4facd2011-03-22 23:18:50138
139 ExtensionTabIdMap::GetInstance()->Init();
initial.commit09911bf2008-07-26 23:55:29140}
141
142BrowserProcessImpl::~BrowserProcessImpl() {
[email protected]4ef795df2010-02-03 02:35:08143 FilePath profile_path;
144 bool clear_local_state_on_exit;
145
146 // Store the profile path for clearing local state data on exit.
147 clear_local_state_on_exit = ShouldClearLocalState(&profile_path);
148
initial.commit09911bf2008-07-26 23:55:29149 // Delete the AutomationProviderList before NotificationService,
150 // since it may try to unregister notifications
151 // Both NotificationService and AutomationProvider are singleton instances in
152 // the BrowserProcess. Since AutomationProvider may have some active
153 // notification observers, it is essential that it gets destroyed before the
154 // NotificationService. NotificationService won't be destroyed until after
155 // this destructor is run.
156 automation_provider_list_.reset();
157
[email protected]d393a0fd2009-05-13 23:32:01158 // We need to shutdown the SdchDictionaryFetcher as it regularly holds
159 // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do
160 // a PostDelayedTask onto the IO thread. This shutdown call will both discard
161 // any pending URLFetchers, and avoid creating any more.
162 SdchDictionaryFetcher::Shutdown();
163
[email protected]51ac040762011-01-20 02:01:04164 // We need to destroy the MetricsService, GoogleURLTracker,
165 // IntranetRedirectDetector, and SafeBrowsing ClientSideDetectionService
166 // before the io_thread_ gets destroyed, since their destructors can call the
167 // URLFetcher destructor, which does a PostDelayedTask operation on the IO
168 // thread. (The IO thread will handle that URLFetcher operation before going
169 // away.)
initial.commit09911bf2008-07-26 23:55:29170 metrics_service_.reset();
171 google_url_tracker_.reset();
[email protected]c4ff4952010-01-08 19:12:47172 intranet_redirect_detector_.reset();
[email protected]51ac040762011-01-20 02:01:04173 safe_browsing_detection_service_.reset();
initial.commit09911bf2008-07-26 23:55:29174
[email protected]d8a899c2010-09-21 04:50:33175 // Need to clear the desktop notification balloons before the io_thread_ and
176 // before the profiles, since if there are any still showing we will access
177 // those things during teardown.
178 notification_ui_manager_.reset();
179
initial.commit09911bf2008-07-26 23:55:29180 // Need to clear profiles (download managers) before the io_thread_.
181 profile_manager_.reset();
182
183 // Debugger must be cleaned up before IO thread and NotificationService.
[email protected]5613126e2011-01-31 15:27:55184 if (devtools_http_handler_.get()) {
185 devtools_http_handler_->Stop();
186 devtools_http_handler_ = NULL;
187 }
188 if (devtools_legacy_handler_.get()) {
189 devtools_legacy_handler_->Stop();
190 devtools_legacy_handler_ = NULL;
191 }
initial.commit09911bf2008-07-26 23:55:29192
193 if (resource_dispatcher_host_.get()) {
194 // Need to tell Safe Browsing Service that the IO thread is going away
195 // since it cached a pointer to it.
196 if (resource_dispatcher_host()->safe_browsing_service())
197 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
198
199 // Cancel pending requests and prevent new requests.
200 resource_dispatcher_host()->Shutdown();
201 }
202
[email protected]fc4facd2011-03-22 23:18:50203 ExtensionTabIdMap::GetInstance()->Shutdown();
204
[email protected]985655a2011-02-23 09:54:25205 // The policy providers managed by |browser_policy_connector_| need to shut
206 // down while the IO and FILE threads are still alive.
207 browser_policy_connector_.reset();
[email protected]f2a893c2011-01-05 09:38:07208
[email protected]753efc42010-03-09 19:52:16209#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06210 // The IO thread must outlive the BACKGROUND_X11 thread.
211 background_x11_thread_.reset();
212#endif
213
[email protected]6618d1d2010-12-15 21:18:47214 // Wait for removing plugin data to finish before shutting down the IO thread.
215 WaitForPluginDataRemoverToFinish();
216
[email protected]7a31f7c2011-03-21 23:22:04217 // Destroying the GpuProcessHostUIShims on the UI thread posts a task to
218 // delete related objects on the GPU thread. This must be done before
219 // stopping the GPU thread. The GPU thread will close IPC channels to renderer
220 // processes so this has to happen before stopping the IO thread.
221 GpuProcessHostUIShim::DestroyAll();
222 gpu_thread_.reset();
223
initial.commit09911bf2008-07-26 23:55:29224 // Need to stop io_thread_ before resource_dispatcher_host_, since
225 // io_thread_ may still deref ResourceDispatcherHost and handle resource
226 // request before going away.
[email protected]0ac83682010-01-22 17:46:27227 io_thread_.reset();
initial.commit09911bf2008-07-26 23:55:29228
[email protected]875ee822010-05-18 20:58:01229 // The IO thread was the only user of this thread.
230 cache_thread_.reset();
231
[email protected]914511712009-11-23 19:42:33232 // Stop the process launcher thread after the IO thread, in case the IO thread
233 // posted a task to terminate a process on the process launcher thread.
234 process_launcher_thread_.reset();
235
initial.commit09911bf2008-07-26 23:55:29236 // Clean up state that lives on the file_thread_ before it goes away.
237 if (resource_dispatcher_host_.get()) {
238 resource_dispatcher_host()->download_file_manager()->Shutdown();
239 resource_dispatcher_host()->save_file_manager()->Shutdown();
240 }
241
242 // Need to stop the file_thread_ here to force it to process messages in its
243 // message loop from the previous call to shutdown the DownloadFileManager,
244 // SaveFileManager and SessionService.
245 file_thread_.reset();
246
247 // With the file_thread_ flushed, we can release any icon resources.
248 icon_manager_.reset();
249
250 // Need to destroy ResourceDispatcherHost before PluginService and
[email protected]49f28bc72010-02-04 00:10:01251 // SafeBrowsingService, since it caches a pointer to it. This also
252 // causes the webkit thread to terminate.
initial.commit09911bf2008-07-26 23:55:29253 resource_dispatcher_host_.reset();
254
255 // Wait for the pending print jobs to finish.
256 print_job_manager_->OnQuit();
257 print_job_manager_.reset();
258
[email protected]0b4d3382010-07-14 16:13:04259 // Destroy TabCloseableStateWatcher before NotificationService since the
260 // former registers for notifications.
261 tab_closeable_state_watcher_.reset();
262
initial.commit09911bf2008-07-26 23:55:29263 // Now OK to destroy NotificationService.
264 main_notification_service_.reset();
265
[email protected]49f28bc72010-02-04 00:10:01266 // Prior to clearing local state, we want to complete tasks pending
267 // on the db thread too.
268 db_thread_.reset();
269
[email protected]0b565182011-03-02 18:11:15270 // Stop the watchdog thread after stopping other threads.
271 watchdog_thread_.reset();
272
[email protected]49f28bc72010-02-04 00:10:01273 // At this point, no render process exist and the file, io, db, and
274 // webkit threads in this process have all terminated, so it's safe
275 // to access local state data such as cookies, database, or local storage.
276 if (clear_local_state_on_exit)
277 ClearLocalState(profile_path);
278
initial.commit09911bf2008-07-26 23:55:29279 g_browser_process = NULL;
280}
281
[email protected]c6032e82010-09-13 20:06:05282#if defined(OS_WIN)
[email protected]295039bd2008-08-15 04:32:57283// Send a QuitTask to the given MessageLoop.
284static void PostQuit(MessageLoop* message_loop) {
285 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
286}
[email protected]c6032e82010-09-13 20:06:05287#elif defined(USE_X11)
288static void Signal(base::WaitableEvent* event) {
289 event->Signal();
290}
291#endif
initial.commit09911bf2008-07-26 23:55:29292
[email protected]b443cb042009-12-15 22:05:09293unsigned int BrowserProcessImpl::AddRefModule() {
294 DCHECK(CalledOnValidThread());
[email protected]afd20c022010-06-10 00:48:20295 did_start_ = true;
[email protected]b443cb042009-12-15 22:05:09296 module_ref_count_++;
297 return module_ref_count_;
298}
299
300unsigned int BrowserProcessImpl::ReleaseModule() {
301 DCHECK(CalledOnValidThread());
[email protected]760d970a2010-05-18 00:39:18302 DCHECK_NE(0u, module_ref_count_);
[email protected]b443cb042009-12-15 22:05:09303 module_ref_count_--;
304 if (0 == module_ref_count_) {
[email protected]7d038c32010-12-14 00:40:00305 // Allow UI and IO threads to do blocking IO on shutdown, since we do a lot
306 // of it on shutdown for valid reasons.
307 base::ThreadRestrictions::SetIOAllowed(true);
308 io_thread()->message_loop()->PostTask(
309 FROM_HERE,
310 NewRunnableFunction(&base::ThreadRestrictions::SetIOAllowed, true));
[email protected]b443cb042009-12-15 22:05:09311 MessageLoop::current()->PostTask(
[email protected]34f73fb2010-03-24 20:50:34312 FROM_HERE, NewRunnableFunction(DidEndMainMessageLoop));
[email protected]b443cb042009-12-15 22:05:09313 MessageLoop::current()->Quit();
314 }
315 return module_ref_count_;
316}
317
initial.commit09911bf2008-07-26 23:55:29318void BrowserProcessImpl::EndSession() {
[email protected]c6032e82010-09-13 20:06:05319#if defined(OS_WIN) || defined(USE_X11)
[email protected]d65cab7a2008-08-12 01:25:41320 // Notify we are going away.
[email protected]c6032e82010-09-13 20:06:05321 shutdown_event_->Signal();
[email protected]b112a4c2009-02-01 20:24:01322#endif
[email protected]d65cab7a2008-08-12 01:25:41323
initial.commit09911bf2008-07-26 23:55:29324 // Mark all the profiles as clean.
325 ProfileManager* pm = profile_manager();
326 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
327 (*i)->MarkAsCleanShutdown();
328
329 // Tell the metrics service it was cleanly shutdown.
330 MetricsService* metrics = g_browser_process->metrics_service();
331 if (metrics && local_state()) {
initial.commit09911bf2008-07-26 23:55:29332 metrics->RecordStartOfSessionEnd();
333
334 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36335 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29336 }
337
338 // We must write that the profile and metrics service shutdown cleanly,
339 // otherwise on startup we'll think we crashed. So we block until done and
340 // then proceed with normal shutdown.
[email protected]c6032e82010-09-13 20:06:05341#if defined(USE_X11)
342 // Can't run a local loop on linux. Instead create a waitable event.
343 base::WaitableEvent done_writing(false, false);
[email protected]d04e7662010-10-10 22:24:48344 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]c6032e82010-09-13 20:06:05345 NewRunnableFunction(Signal, &done_writing));
346 done_writing.TimedWait(
347 base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds));
348#elif defined(OS_WIN)
[email protected]d04e7662010-10-10 22:24:48349 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57350 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29351 MessageLoop::current()->Run();
[email protected]c6032e82010-09-13 20:06:05352#else
353 NOTIMPLEMENTED();
354#endif
initial.commit09911bf2008-07-26 23:55:29355}
356
[email protected]e13ad79b2010-07-22 21:36:50357ResourceDispatcherHost* BrowserProcessImpl::resource_dispatcher_host() {
358 DCHECK(CalledOnValidThread());
359 if (!created_resource_dispatcher_host_)
360 CreateResourceDispatcherHost();
361 return resource_dispatcher_host_.get();
362}
363
364MetricsService* BrowserProcessImpl::metrics_service() {
365 DCHECK(CalledOnValidThread());
366 if (!created_metrics_service_)
367 CreateMetricsService();
368 return metrics_service_.get();
369}
370
371IOThread* BrowserProcessImpl::io_thread() {
372 DCHECK(CalledOnValidThread());
373 if (!created_io_thread_)
374 CreateIOThread();
375 return io_thread_.get();
376}
377
378base::Thread* BrowserProcessImpl::file_thread() {
379 DCHECK(CalledOnValidThread());
380 if (!created_file_thread_)
381 CreateFileThread();
382 return file_thread_.get();
383}
384
385base::Thread* BrowserProcessImpl::db_thread() {
386 DCHECK(CalledOnValidThread());
387 if (!created_db_thread_)
388 CreateDBThread();
389 return db_thread_.get();
390}
391
392base::Thread* BrowserProcessImpl::process_launcher_thread() {
393 DCHECK(CalledOnValidThread());
394 if (!created_process_launcher_thread_)
395 CreateProcessLauncherThread();
396 return process_launcher_thread_.get();
397}
398
399base::Thread* BrowserProcessImpl::cache_thread() {
400 DCHECK(CalledOnValidThread());
401 if (!created_cache_thread_)
402 CreateCacheThread();
403 return cache_thread_.get();
404}
405
[email protected]7a31f7c2011-03-21 23:22:04406base::Thread* BrowserProcessImpl::gpu_thread() {
407 DCHECK(CalledOnValidThread());
408 if (!created_gpu_thread_)
409 CreateGpuThread();
410 return gpu_thread_.get();
411}
412
[email protected]e13ad79b2010-07-22 21:36:50413#if defined(USE_X11)
414base::Thread* BrowserProcessImpl::background_x11_thread() {
415 DCHECK(CalledOnValidThread());
416 // The BACKGROUND_X11 thread is created when the IO thread is created.
417 if (!created_io_thread_)
418 CreateIOThread();
419 return background_x11_thread_.get();
420}
421#endif
422
[email protected]0b565182011-03-02 18:11:15423WatchDogThread* BrowserProcessImpl::watchdog_thread() {
424 DCHECK(CalledOnValidThread());
425 if (!created_watchdog_thread_)
426 CreateWatchdogThread();
427 DCHECK(watchdog_thread_.get() != NULL);
428 return watchdog_thread_.get();
429}
430
[email protected]e13ad79b2010-07-22 21:36:50431ProfileManager* BrowserProcessImpl::profile_manager() {
432 DCHECK(CalledOnValidThread());
433 if (!created_profile_manager_)
434 CreateProfileManager();
435 return profile_manager_.get();
436}
437
438PrefService* BrowserProcessImpl::local_state() {
439 DCHECK(CalledOnValidThread());
440 if (!created_local_state_)
441 CreateLocalState();
442 return local_state_.get();
443}
444
445DevToolsManager* BrowserProcessImpl::devtools_manager() {
446 DCHECK(CalledOnValidThread());
447 if (!created_devtools_manager_)
448 CreateDevToolsManager();
449 return devtools_manager_.get();
450}
451
[email protected]8b8e7c92010-08-19 18:05:56452SidebarManager* BrowserProcessImpl::sidebar_manager() {
453 DCHECK(CalledOnValidThread());
454 if (!created_sidebar_manager_)
455 CreateSidebarManager();
456 return sidebar_manager_.get();
457}
458
[email protected]2dfeaf92011-01-10 21:08:21459ui::Clipboard* BrowserProcessImpl::clipboard() {
[email protected]e13ad79b2010-07-22 21:36:50460 DCHECK(CalledOnValidThread());
461 return clipboard_.get();
462}
463
[email protected]abe2c032011-03-31 18:49:34464net::URLRequestContextGetter* BrowserProcessImpl::system_request_context() {
[email protected]db0e86dd2011-03-16 14:47:21465 DCHECK(CalledOnValidThread());
466 return io_thread()->system_url_request_context_getter();
467}
468
469#if defined(OS_CHROMEOS)
470chromeos::ProxyConfigServiceImpl*
471BrowserProcessImpl::chromeos_proxy_config_service_impl() {
472 DCHECK(CalledOnValidThread());
473 if (!chromeos_proxy_config_service_impl_) {
474 chromeos_proxy_config_service_impl_ =
475 new chromeos::ProxyConfigServiceImpl();
476 }
477 return chromeos_proxy_config_service_impl_;
478}
479#endif // defined(OS_CHROMEOS)
480
[email protected]3ce02412011-03-01 12:01:15481ExtensionEventRouterForwarder*
482BrowserProcessImpl::extension_event_router_forwarder() {
483 return extension_event_router_forwarder_.get();
484}
485
[email protected]e13ad79b2010-07-22 21:36:50486NotificationUIManager* BrowserProcessImpl::notification_ui_manager() {
487 DCHECK(CalledOnValidThread());
488 if (!created_notification_ui_manager_)
489 CreateNotificationUIManager();
490 return notification_ui_manager_.get();
491}
492
[email protected]985655a2011-02-23 09:54:25493policy::BrowserPolicyConnector* BrowserProcessImpl::browser_policy_connector() {
[email protected]f2a893c2011-01-05 09:38:07494 DCHECK(CalledOnValidThread());
[email protected]985655a2011-02-23 09:54:25495 if (!created_browser_policy_connector_) {
496 DCHECK(browser_policy_connector_.get() == NULL);
497 created_browser_policy_connector_ = true;
498 browser_policy_connector_.reset(new policy::BrowserPolicyConnector());
[email protected]f2a893c2011-01-05 09:38:07499 }
[email protected]985655a2011-02-23 09:54:25500 return browser_policy_connector_.get();
[email protected]f2a893c2011-01-05 09:38:07501}
502
[email protected]e13ad79b2010-07-22 21:36:50503IconManager* BrowserProcessImpl::icon_manager() {
504 DCHECK(CalledOnValidThread());
505 if (!created_icon_manager_)
506 CreateIconManager();
507 return icon_manager_.get();
508}
509
510ThumbnailGenerator* BrowserProcessImpl::GetThumbnailGenerator() {
511 return &thumbnail_generator_;
512}
513
514AutomationProviderList* BrowserProcessImpl::InitAutomationProviderList() {
515 DCHECK(CalledOnValidThread());
516 if (automation_provider_list_.get() == NULL) {
517 automation_provider_list_.reset(AutomationProviderList::GetInstance());
518 }
519 return automation_provider_list_.get();
520}
521
[email protected]5613126e2011-01-31 15:27:55522void BrowserProcessImpl::InitDevToolsHttpProtocolHandler(
[email protected]01000cf12011-02-04 11:39:22523 const std::string& ip,
[email protected]5613126e2011-01-31 15:27:55524 int port,
525 const std::string& frontend_url) {
[email protected]e13ad79b2010-07-22 21:36:50526 DCHECK(CalledOnValidThread());
[email protected]5613126e2011-01-31 15:27:55527 devtools_http_handler_ =
[email protected]01000cf12011-02-04 11:39:22528 DevToolsHttpProtocolHandler::Start(ip,
529 port,
[email protected]9678cb42011-01-31 18:49:42530 frontend_url,
531 new BrowserListTabContentsProvider());
[email protected]5613126e2011-01-31 15:27:55532}
533
534void BrowserProcessImpl::InitDevToolsLegacyProtocolHandler(int port) {
535 DCHECK(CalledOnValidThread());
536 devtools_legacy_handler_ = DevToolsProtocolHandler::Start(port);
[email protected]e13ad79b2010-07-22 21:36:50537}
538
539bool BrowserProcessImpl::IsShuttingDown() {
540 DCHECK(CalledOnValidThread());
541 return did_start_ && 0 == module_ref_count_;
542}
543
initial.commit09911bf2008-07-26 23:55:29544printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
545 // TODO(abarth): DCHECK(CalledOnValidThread());
[email protected]d8922f22010-05-07 00:57:27546 // http://code.google.com/p/chromium/issues/detail?id=6828
initial.commit09911bf2008-07-26 23:55:29547 // print_job_manager_ is initialized in the constructor and destroyed in the
548 // destructor, so it should always be valid.
549 DCHECK(print_job_manager_.get());
550 return print_job_manager_.get();
551}
552
[email protected]dbeebd52010-11-16 20:34:16553printing::PrintPreviewTabController*
554 BrowserProcessImpl::print_preview_tab_controller() {
555 DCHECK(CalledOnValidThread());
556 if (!print_preview_tab_controller_.get())
557 CreatePrintPreviewTabController();
558 return print_preview_tab_controller_.get();
559}
560
[email protected]e13ad79b2010-07-22 21:36:50561GoogleURLTracker* BrowserProcessImpl::google_url_tracker() {
562 DCHECK(CalledOnValidThread());
563 if (!google_url_tracker_.get())
564 CreateGoogleURLTracker();
565 return google_url_tracker_.get();
566}
567
568IntranetRedirectDetector* BrowserProcessImpl::intranet_redirect_detector() {
569 DCHECK(CalledOnValidThread());
570 if (!intranet_redirect_detector_.get())
571 CreateIntranetRedirectDetector();
572 return intranet_redirect_detector_.get();
573}
574
575const std::string& BrowserProcessImpl::GetApplicationLocale() {
576 DCHECK(!locale_.empty());
577 return locale_;
578}
579
580void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) {
581 locale_ = locale;
582 extension_l10n_util::SetProcessLocale(locale);
583}
584
[email protected]073ed7b2010-09-27 09:20:02585DownloadStatusUpdater* BrowserProcessImpl::download_status_updater() {
586 return &download_status_updater_;
587}
588
[email protected]e13ad79b2010-07-22 21:36:50589base::WaitableEvent* BrowserProcessImpl::shutdown_event() {
590 return shutdown_event_.get();
591}
592
593TabCloseableStateWatcher* BrowserProcessImpl::tab_closeable_state_watcher() {
594 DCHECK(CalledOnValidThread());
595 if (!tab_closeable_state_watcher_.get())
596 CreateTabCloseableStateWatcher();
597 return tab_closeable_state_watcher_.get();
598}
599
[email protected]a7a5e992010-12-09 23:39:51600safe_browsing::ClientSideDetectionService*
601 BrowserProcessImpl::safe_browsing_detection_service() {
602 DCHECK(CalledOnValidThread());
603 if (!created_safe_browsing_detection_service_) {
604 CreateSafeBrowsingDetectionService();
605 }
606 return safe_browsing_detection_service_.get();
607}
608
[email protected]8b08a47f2011-02-25 12:36:37609bool BrowserProcessImpl::plugin_finder_disabled() const {
610 return *plugin_finder_disabled_pref_;
611}
612
[email protected]6618d1d2010-12-15 21:18:47613void BrowserProcessImpl::Observe(NotificationType type,
614 const NotificationSource& source,
615 const NotificationDetails& details) {
616 if (type == NotificationType::APP_TERMINATING) {
617 Profile* profile = ProfileManager::GetDefaultProfile();
618 if (profile) {
619 PrefService* prefs = profile->GetPrefs();
[email protected]2ffc3c6c2011-01-14 12:50:24620 if (prefs->GetBoolean(prefs::kClearSiteDataOnExit) &&
[email protected]6618d1d2010-12-15 21:18:47621 local_state()->GetBoolean(prefs::kClearPluginLSODataEnabled)) {
622 plugin_data_remover_ = new PluginDataRemover();
[email protected]c7d58d62011-01-21 10:27:18623 if (!plugin_data_remover_mime_type().empty())
624 plugin_data_remover_->set_mime_type(plugin_data_remover_mime_type());
625 plugin_data_remover_->StartRemoving(base::Time());
[email protected]6618d1d2010-12-15 21:18:47626 }
627 }
[email protected]e450fa62011-02-01 12:52:56628 } else if (type == NotificationType::PREF_CHANGED) {
629 std::string* pref = Details<std::string>(details).ptr();
630 if (*pref == prefs::kDefaultBrowserSettingEnabled) {
631 if (local_state_->GetBoolean(prefs::kDefaultBrowserSettingEnabled))
632 ShellIntegration::SetAsDefaultBrowser();
633 }
[email protected]6618d1d2010-12-15 21:18:47634 } else {
635 NOTREACHED();
636 }
637}
638
639void BrowserProcessImpl::WaitForPluginDataRemoverToFinish() {
[email protected]c7d58d62011-01-21 10:27:18640 if (plugin_data_remover_.get())
641 plugin_data_remover_->Wait();
[email protected]6618d1d2010-12-15 21:18:47642}
643
[email protected]e13ad79b2010-07-22 21:36:50644#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
645void BrowserProcessImpl::StartAutoupdateTimer() {
646 autoupdate_timer_.Start(
647 base::TimeDelta::FromHours(kUpdateCheckIntervalHours),
648 this,
649 &BrowserProcessImpl::OnAutoupdateTimer);
650}
651#endif
652
[email protected]d6f37fc2011-02-13 23:58:41653ChromeNetLog* BrowserProcessImpl::net_log() {
654 return net_log_.get();
655}
656
[email protected]4ef795df2010-02-03 02:35:08657void BrowserProcessImpl::ClearLocalState(const FilePath& profile_path) {
[email protected]72cfd90f2010-02-06 03:08:04658 webkit_database::DatabaseTracker::ClearLocalState(profile_path);
[email protected]4ef795df2010-02-03 02:35:08659}
660
661bool BrowserProcessImpl::ShouldClearLocalState(FilePath* profile_path) {
662 FilePath user_data_dir;
663 Profile* profile;
664
[email protected]09584972010-07-03 00:00:59665 // Check for the existence of a profile manager. When quitting early,
[email protected]ed0cf562010-02-05 22:50:41666 // e.g. because another chrome instance is running, or when invoked with
667 // options such as --uninstall or --try-chrome-again=0, the profile manager
668 // does not exist yet.
669 if (!profile_manager_.get())
670 return false;
671
[email protected]4ef795df2010-02-03 02:35:08672 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
673 profile = profile_manager_->GetDefaultProfile(user_data_dir);
[email protected]7bfc674322010-03-05 00:20:57674 if (!profile)
675 return false;
[email protected]4ef795df2010-02-03 02:35:08676 *profile_path = profile->GetPath();
677 return profile->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit);
678}
679
initial.commit09911bf2008-07-26 23:55:29680void BrowserProcessImpl::CreateResourceDispatcherHost() {
681 DCHECK(!created_resource_dispatcher_host_ &&
682 resource_dispatcher_host_.get() == NULL);
683 created_resource_dispatcher_host_ = true;
684
[email protected]79084c2d2009-11-03 23:12:42685 resource_dispatcher_host_.reset(new ResourceDispatcherHost());
initial.commit09911bf2008-07-26 23:55:29686 resource_dispatcher_host_->Initialize();
687}
688
689void BrowserProcessImpl::CreateMetricsService() {
690 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
691 created_metrics_service_ = true;
692
693 metrics_service_.reset(new MetricsService);
694}
695
696void BrowserProcessImpl::CreateIOThread() {
697 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
698 created_io_thread_ = true;
699
700 // Prior to starting the io thread, we create the plugin service as
701 // it is predominantly used from the io thread, but must be created
702 // on the main thread. The service ctor is inexpensive and does not
703 // invoke the io_thread() accessor.
704 PluginService::GetInstance();
705
[email protected]753efc42010-03-09 19:52:16706#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06707 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
708 // we start it now.
709 scoped_ptr<base::Thread> background_x11_thread(
[email protected]d04e7662010-10-10 22:24:48710 new BrowserProcessSubThread(BrowserThread::BACKGROUND_X11));
[email protected]4c3cd7412009-04-22 17:56:06711 if (!background_x11_thread->Start())
712 return;
713 background_x11_thread_.swap(background_x11_thread);
714#endif
715
[email protected]3ce02412011-03-01 12:01:15716 scoped_ptr<IOThread> thread(new IOThread(
717 local_state(), net_log_.get(), extension_event_router_forwarder_.get()));
[email protected]ab820df2008-08-26 05:55:10718 base::Thread::Options options;
719 options.message_loop_type = MessageLoop::TYPE_IO;
720 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29721 return;
722 io_thread_.swap(thread);
723}
724
725void BrowserProcessImpl::CreateFileThread() {
726 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
727 created_file_thread_ = true;
728
[email protected]ab820df2008-08-26 05:55:10729 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48730 new BrowserProcessSubThread(BrowserThread::FILE));
[email protected]a1db3842008-09-17 22:04:06731 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39732#if defined(OS_WIN)
733 // On Windows, the FILE thread needs to be have a UI message loop which pumps
734 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06735 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39736#else
737 options.message_loop_type = MessageLoop::TYPE_IO;
738#endif
[email protected]a1db3842008-09-17 22:04:06739 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29740 return;
741 file_thread_.swap(thread);
742}
743
744void BrowserProcessImpl::CreateDBThread() {
745 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
746 created_db_thread_ = true;
747
[email protected]ab820df2008-08-26 05:55:10748 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48749 new BrowserProcessSubThread(BrowserThread::DB));
initial.commit09911bf2008-07-26 23:55:29750 if (!thread->Start())
751 return;
752 db_thread_.swap(thread);
753}
754
[email protected]914511712009-11-23 19:42:33755void BrowserProcessImpl::CreateProcessLauncherThread() {
756 DCHECK(!created_process_launcher_thread_ && !process_launcher_thread_.get());
757 created_process_launcher_thread_ = true;
758
759 scoped_ptr<base::Thread> thread(
[email protected]d04e7662010-10-10 22:24:48760 new BrowserProcessSubThread(BrowserThread::PROCESS_LAUNCHER));
[email protected]914511712009-11-23 19:42:33761 if (!thread->Start())
762 return;
763 process_launcher_thread_.swap(thread);
764}
765
[email protected]875ee822010-05-18 20:58:01766void BrowserProcessImpl::CreateCacheThread() {
767 DCHECK(!created_cache_thread_ && !cache_thread_.get());
768 created_cache_thread_ = true;
769
770 scoped_ptr<base::Thread> thread(
[email protected]75096332010-11-09 03:28:13771 new BrowserThread(BrowserThread::CACHE));
[email protected]875ee822010-05-18 20:58:01772 base::Thread::Options options;
773 options.message_loop_type = MessageLoop::TYPE_IO;
774 if (!thread->StartWithOptions(options))
775 return;
776 cache_thread_.swap(thread);
777}
778
[email protected]7a31f7c2011-03-21 23:22:04779void BrowserProcessImpl::CreateGpuThread() {
780 DCHECK(!created_gpu_thread_ && !gpu_thread_.get());
781 created_gpu_thread_ = true;
782
783 scoped_ptr<base::Thread> thread(new BrowserThread(BrowserThread::GPU));
784
785 base::Thread::Options options;
786#if defined(OS_WIN)
787 // On Windows the GPU thread needs to pump the compositor child window's
788 // message loop. TODO(apatrick): make this an IO thread if / when we get rid
789 // of this child window. Unfortunately it might always be necessary for
790 // Windows XP because we cannot share the backing store textures between
791 // processes.
792 options.message_loop_type = MessageLoop::TYPE_UI;
793#else
794 options.message_loop_type = MessageLoop::TYPE_IO;
795#endif
796
797 if (!thread->StartWithOptions(options))
798 return;
799 gpu_thread_.swap(thread);
800}
801
[email protected]0b565182011-03-02 18:11:15802void BrowserProcessImpl::CreateWatchdogThread() {
803 DCHECK(!created_watchdog_thread_ && watchdog_thread_.get() == NULL);
804 created_watchdog_thread_ = true;
805
806 scoped_ptr<WatchDogThread> thread(new WatchDogThread());
807 if (!thread->Start())
808 return;
809 watchdog_thread_.swap(thread);
810}
811
initial.commit09911bf2008-07-26 23:55:29812void BrowserProcessImpl::CreateProfileManager() {
813 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
814 created_profile_manager_ = true;
815
816 profile_manager_.reset(new ProfileManager());
817}
818
819void BrowserProcessImpl::CreateLocalState() {
820 DCHECK(!created_local_state_ && local_state_.get() == NULL);
821 created_local_state_ = true;
822
[email protected]b9636002009-03-04 00:05:25823 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29824 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]f2d1f612010-12-09 15:10:17825 local_state_.reset(
826 PrefService::CreatePrefService(local_state_path, NULL, NULL));
[email protected]492d2142010-09-10 13:55:18827
[email protected]2a7e7c152010-10-01 20:12:09828 pref_change_registrar_.Init(local_state_.get());
829
[email protected]492d2142010-09-10 13:55:18830 // Make sure the the plugin updater gets notifications of changes
[email protected]24ff43d72011-03-01 13:28:58831 // in the plugin policy lists.
832 local_state_->RegisterListPref(prefs::kPluginsDisabledPlugins);
833 pref_change_registrar_.Add(prefs::kPluginsDisabledPlugins,
834 PluginUpdater::GetInstance());
835 local_state_->RegisterListPref(prefs::kPluginsDisabledPluginsExceptions);
836 pref_change_registrar_.Add(prefs::kPluginsDisabledPluginsExceptions,
837 PluginUpdater::GetInstance());
838 local_state_->RegisterListPref(prefs::kPluginsEnabledPlugins);
839 pref_change_registrar_.Add(prefs::kPluginsEnabledPlugins,
[email protected]8e8bb6d2010-12-13 08:18:55840 PluginUpdater::GetInstance());
[email protected]2a7e7c152010-10-01 20:12:09841
842 // Initialize and set up notifications for the printing enabled
843 // preference.
844 local_state_->RegisterBooleanPref(prefs::kPrintingEnabled, true);
845 bool printing_enabled =
846 local_state_->GetBoolean(prefs::kPrintingEnabled);
847 print_job_manager_->set_printing_enabled(printing_enabled);
848 pref_change_registrar_.Add(prefs::kPrintingEnabled,
849 print_job_manager_.get());
[email protected]e450fa62011-02-01 12:52:56850
851 // Initialize the notification for the default browser setting policy.
852 local_state_->RegisterBooleanPref(prefs::kDefaultBrowserSettingEnabled,
853 false);
854 if (local_state_->IsManagedPreference(prefs::kDefaultBrowserSettingEnabled)) {
855 if (local_state_->GetBoolean(prefs::kDefaultBrowserSettingEnabled))
856 ShellIntegration::SetAsDefaultBrowser();
857 }
858 pref_change_registrar_.Add(prefs::kDefaultBrowserSettingEnabled, this);
[email protected]8b08a47f2011-02-25 12:36:37859
860 // Initialize the preference for the plugin finder policy.
861 // This preference is only needed on the IO thread so make it available there.
862 local_state_->RegisterBooleanPref(prefs::kDisablePluginFinder, false);
863 plugin_finder_disabled_pref_.Init(prefs::kDisablePluginFinder,
864 local_state_.get(), NULL);
865 plugin_finder_disabled_pref_.MoveToThread(BrowserThread::IO);
[email protected]fd6159a2010-09-03 09:38:39866}
initial.commit09911bf2008-07-26 23:55:29867
initial.commit09911bf2008-07-26 23:55:29868void BrowserProcessImpl::CreateIconManager() {
869 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
870 created_icon_manager_ = true;
871 icon_manager_.reset(new IconManager);
872}
873
[email protected]40ecc902009-03-16 13:42:47874void BrowserProcessImpl::CreateDevToolsManager() {
[email protected]73ee01522009-06-05 10:13:44875 DCHECK(devtools_manager_.get() == NULL);
[email protected]40ecc902009-03-16 13:42:47876 created_devtools_manager_ = true;
[email protected]73ee01522009-06-05 10:13:44877 devtools_manager_ = new DevToolsManager();
[email protected]40ecc902009-03-16 13:42:47878}
879
[email protected]8b8e7c92010-08-19 18:05:56880void BrowserProcessImpl::CreateSidebarManager() {
881 DCHECK(sidebar_manager_.get() == NULL);
882 created_sidebar_manager_ = true;
883 sidebar_manager_ = new SidebarManager();
884}
885
initial.commit09911bf2008-07-26 23:55:29886void BrowserProcessImpl::CreateGoogleURLTracker() {
887 DCHECK(google_url_tracker_.get() == NULL);
888 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
889 google_url_tracker_.swap(google_url_tracker);
890}
[email protected]6641bf662009-08-21 00:34:09891
[email protected]c4ff4952010-01-08 19:12:47892void BrowserProcessImpl::CreateIntranetRedirectDetector() {
893 DCHECK(intranet_redirect_detector_.get() == NULL);
894 scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector(
895 new IntranetRedirectDetector);
896 intranet_redirect_detector_.swap(intranet_redirect_detector);
897}
898
[email protected]29672ab2009-10-30 03:44:03899void BrowserProcessImpl::CreateNotificationUIManager() {
900 DCHECK(notification_ui_manager_.get() == NULL);
[email protected]b0b2a3dd2011-01-06 00:30:05901 notification_ui_manager_.reset(NotificationUIManager::Create(local_state()));
902
[email protected]29672ab2009-10-30 03:44:03903 created_notification_ui_manager_ = true;
904}
905
[email protected]0b4d3382010-07-14 16:13:04906void BrowserProcessImpl::CreateTabCloseableStateWatcher() {
907 DCHECK(tab_closeable_state_watcher_.get() == NULL);
908 tab_closeable_state_watcher_.reset(TabCloseableStateWatcher::Create());
909}
910
[email protected]dbeebd52010-11-16 20:34:16911void BrowserProcessImpl::CreatePrintPreviewTabController() {
912 DCHECK(print_preview_tab_controller_.get() == NULL);
913 print_preview_tab_controller_ = new printing::PrintPreviewTabController();
914}
915
[email protected]a7a5e992010-12-09 23:39:51916void BrowserProcessImpl::CreateSafeBrowsingDetectionService() {
917 DCHECK(safe_browsing_detection_service_.get() == NULL);
918 // Set this flag to true so that we don't retry indefinitely to
919 // create the service class if there was an error.
920 created_safe_browsing_detection_service_ = true;
921
922 FilePath model_file_path;
923 Profile* profile = profile_manager() ?
924 profile_manager()->GetDefaultProfile() : NULL;
925 if (IsSafeBrowsingDetectionServiceEnabled() &&
926 PathService::Get(chrome::DIR_USER_DATA, &model_file_path) &&
927 profile && profile->GetRequestContext()) {
928 safe_browsing_detection_service_.reset(
929 safe_browsing::ClientSideDetectionService::Create(
930 model_file_path.Append(chrome::kSafeBrowsingPhishingModelFilename),
931 profile->GetRequestContext()));
932 }
933}
934
935bool BrowserProcessImpl::IsSafeBrowsingDetectionServiceEnabled() {
936 // The safe browsing client-side detection is enabled only if the switch is
[email protected]42930de42011-01-13 23:26:28937 // enabled and when safe browsing related stats is allowed to be collected.
[email protected]5f700cd2011-03-31 07:55:56938 return CommandLine::ForCurrentProcess()->HasSwitch(
939 switches::kEnableClientSidePhishingDetection) &&
[email protected]42930de42011-01-13 23:26:28940 resource_dispatcher_host()->safe_browsing_service() &&
941 resource_dispatcher_host()->safe_browsing_service()->CanReportStats();
[email protected]a7a5e992010-12-09 23:39:51942}
943
[email protected]6641bf662009-08-21 00:34:09944// The BrowserProcess object must outlive the file thread so we use traits
945// which don't do any management.
[email protected]c56428f22010-06-16 02:17:23946DISABLE_RUNNABLE_METHOD_REFCOUNT(BrowserProcessImpl);
[email protected]6641bf662009-08-21 00:34:09947
[email protected]d55aaa132009-09-28 21:08:04948#if defined(IPC_MESSAGE_LOG_ENABLED)
949
950void BrowserProcessImpl::SetIPCLoggingEnabled(bool enable) {
951 // First enable myself.
952 if (enable)
[email protected]8e8bb6d2010-12-13 08:18:55953 IPC::Logging::GetInstance()->Enable();
[email protected]d55aaa132009-09-28 21:08:04954 else
[email protected]8e8bb6d2010-12-13 08:18:55955 IPC::Logging::GetInstance()->Disable();
[email protected]d55aaa132009-09-28 21:08:04956
957 // Now tell subprocesses. Messages to ChildProcess-derived
958 // processes must be done on the IO thread.
959 io_thread()->message_loop()->PostTask
960 (FROM_HERE,
961 NewRunnableMethod(
962 this,
963 &BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses,
964 enable));
965
966 // Finally, tell the renderers which don't derive from ChildProcess.
967 // Messages to the renderers must be done on the UI (main) thread.
[email protected]019191a2009-10-02 20:37:27968 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
969 !i.IsAtEnd(); i.Advance())
[email protected]ff47b2962011-03-07 23:51:49970 i.GetCurrentValue()->Send(new ChildProcessMsg_SetIPCLoggingEnabled(enable));
[email protected]d55aaa132009-09-28 21:08:04971}
972
973// Helper for SetIPCLoggingEnabled.
974void BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses(bool enabled) {
[email protected]d04e7662010-10-10 22:24:48975 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]d55aaa132009-09-28 21:08:04976
[email protected]d27893f62010-07-03 05:47:42977 BrowserChildProcessHost::Iterator i; // default constr references a singleton
[email protected]34cf97d2009-09-29 22:46:11978 while (!i.Done()) {
[email protected]ff47b2962011-03-07 23:51:49979 i->Send(new ChildProcessMsg_SetIPCLoggingEnabled(enabled));
[email protected]34cf97d2009-09-29 22:46:11980 ++i;
[email protected]d55aaa132009-09-28 21:08:04981 }
982}
983
984#endif // IPC_MESSAGE_LOG_ENABLED
985
[email protected]3cdacd42010-04-30 18:55:53986// Mac is currently not supported.
987#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
[email protected]bd48c2b02010-04-09 20:32:42988
989bool BrowserProcessImpl::CanAutorestartForUpdate() const {
990 // Check if browser is in the background and if it needs to be restarted to
991 // apply a pending update.
[email protected]c10fa3952010-08-24 16:52:53992 return BrowserList::size() == 0 && BrowserList::WillKeepAlive() &&
[email protected]c984d9f2010-07-20 20:52:20993 Upgrade::IsUpdatePendingRestart();
[email protected]bd48c2b02010-04-09 20:32:42994}
995
[email protected]5c08f222010-09-22 09:37:21996// Switches to add when auto-restarting Chrome.
[email protected]6c10c972010-08-11 04:15:47997const char* const kSwitchesToAddOnAutorestart[] = {
[email protected]f9a06842010-08-21 00:32:40998 switches::kNoStartupWindow
[email protected]bd48c2b02010-04-09 20:32:42999};
1000
1001void BrowserProcessImpl::RestartPersistentInstance() {
1002 CommandLine* old_cl = CommandLine::ForCurrentProcess();
[email protected]3cdacd42010-04-30 18:55:531003 scoped_ptr<CommandLine> new_cl(new CommandLine(old_cl->GetProgram()));
[email protected]bd48c2b02010-04-09 20:32:421004
1005 std::map<std::string, CommandLine::StringType> switches =
1006 old_cl->GetSwitches();
1007
[email protected]5c08f222010-09-22 09:37:211008 switches::RemoveSwitchesForAutostart(&switches);
[email protected]bd48c2b02010-04-09 20:32:421009
1010 // Append the rest of the switches (along with their values, if any)
1011 // to the new command line
1012 for (std::map<std::string, CommandLine::StringType>::const_iterator i =
1013 switches.begin(); i != switches.end(); ++i) {
1014 CommandLine::StringType switch_value = i->second;
1015 if (switch_value.length() > 0) {
[email protected]05076ba22010-07-30 05:59:571016 new_cl->AppendSwitchNative(i->first, i->second);
[email protected]bd48c2b02010-04-09 20:32:421017 } else {
[email protected]3cdacd42010-04-30 18:55:531018 new_cl->AppendSwitch(i->first);
[email protected]bd48c2b02010-04-09 20:32:421019 }
1020 }
1021
[email protected]6c10c972010-08-11 04:15:471022 // Ensure that our desired switches are set on the new process.
[email protected]5c08f222010-09-22 09:37:211023 for (size_t i = 0; i < arraysize(kSwitchesToAddOnAutorestart); ++i) {
[email protected]6c10c972010-08-11 04:15:471024 if (!new_cl->HasSwitch(kSwitchesToAddOnAutorestart[i]))
1025 new_cl->AppendSwitch(kSwitchesToAddOnAutorestart[i]);
1026 }
[email protected]bd48c2b02010-04-09 20:32:421027
[email protected]3cdacd42010-04-30 18:55:531028 DLOG(WARNING) << "Shutting down current instance of the browser.";
1029 BrowserList::CloseAllBrowsersAndExit();
1030
1031 // Transfer ownership to Upgrade.
1032 Upgrade::SetNewCommandLine(new_cl.release());
[email protected]bd48c2b02010-04-09 20:32:421033}
1034
1035void BrowserProcessImpl::OnAutoupdateTimer() {
1036 if (CanAutorestartForUpdate()) {
[email protected]3cdacd42010-04-30 18:55:531037 DLOG(WARNING) << "Detected update. Restarting browser.";
[email protected]bd48c2b02010-04-09 20:32:421038 RestartPersistentInstance();
1039 }
1040}
1041
[email protected]3cdacd42010-04-30 18:55:531042#endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)