blob: f84b3f30ffc63c5c6852fb2c35a1835ed33eae28 [file] [log] [blame]
[email protected]c4ff4952010-01-08 19:12:471// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/browser_process_impl.h"
6
[email protected]3cdacd42010-04-30 18:55:537#include <map>
8
[email protected]90f39902009-10-03 04:25:379#include "app/clipboard/clipboard.h"
[email protected]a92b8642009-05-05 23:38:5610#include "app/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/command_line.h"
[email protected]6641bf662009-08-21 00:34:0912#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/path_service.h"
[email protected]ac262c9f2008-10-19 17:45:2114#include "base/thread.h"
[email protected]1c4947f2009-01-15 22:25:1115#include "base/waitable_event.h"
[email protected]0082d7e0d2010-02-27 14:34:1216#include "chrome/browser/appcache/chrome_appcache_service.h"
[email protected]d27893f62010-07-03 05:47:4217#include "chrome/browser/browser_child_process_host.h"
[email protected]bd48c2b02010-04-09 20:32:4218#include "chrome/browser/browser_list.h"
[email protected]b443cb042009-12-15 22:05:0919#include "chrome/browser/browser_main.h"
[email protected]0ac83682010-01-22 17:46:2720#include "chrome/browser/browser_process_sub_thread.h"
[email protected]ac262c9f2008-10-19 17:45:2121#include "chrome/browser/browser_trial.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/browser/chrome_thread.h"
[email protected]4ab4b0f2009-02-10 18:54:5023#include "chrome/browser/debugger/debugger_wrapper.h"
[email protected]40ecc902009-03-16 13:42:4724#include "chrome/browser/debugger/devtools_manager.h"
[email protected]b7f05882009-02-22 01:21:5625#include "chrome/browser/download/download_file.h"
[email protected]5ba0a2c2009-02-19 01:19:3426#include "chrome/browser/download/save_file_manager.h"
[email protected]bd48c2b02010-04-09 20:32:4227#include "chrome/browser/first_run.h"
initial.commit09911bf2008-07-26 23:55:2928#include "chrome/browser/google_url_tracker.h"
[email protected]dcefa302009-05-20 00:24:3929#include "chrome/browser/icon_manager.h"
[email protected]4ef795df2010-02-03 02:35:0830#include "chrome/browser/in_process_webkit/dom_storage_context.h"
[email protected]c4ff4952010-01-08 19:12:4731#include "chrome/browser/intranet_redirect_detector.h"
[email protected]0ac83682010-01-22 17:46:2732#include "chrome/browser/io_thread.h"
[email protected]dc6f4962009-02-13 01:25:5033#include "chrome/browser/metrics/metrics_service.h"
[email protected]3530cd92010-06-27 06:22:0134#include "chrome/browser/net/predictor_api.h"
[email protected]d393a0fd2009-05-13 23:32:0135#include "chrome/browser/net/sdch_dictionary_fetcher.h"
[email protected]4ef795df2010-02-03 02:35:0836#include "chrome/browser/net/sqlite_persistent_cookie_store.h"
[email protected]29672ab2009-10-30 03:44:0337#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]fd49e2d2009-02-20 17:21:3038#include "chrome/browser/plugin_service.h"
[email protected]052313b2010-02-19 09:43:0839#include "chrome/browser/pref_service.h"
[email protected]e06131d2010-02-10 18:40:3340#include "chrome/browser/printing/print_job_manager.h"
initial.commit09911bf2008-07-26 23:55:2941#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2642#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]81218f42009-02-05 18:48:0843#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
44#include "chrome/browser/safe_browsing/safe_browsing_service.h"
[email protected]ccb55cf52010-03-06 22:02:0445#include "chrome/browser/status_icons/status_tray_manager.h"
[email protected]0b4d3382010-07-14 16:13:0446#include "chrome/browser/tab_closeable_state_watcher.h"
[email protected]4ef795df2010-02-03 02:35:0847#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2948#include "chrome/common/chrome_paths.h"
49#include "chrome/common/chrome_switches.h"
[email protected]27fbccc2010-03-25 03:00:4950#include "chrome/common/extensions/extension_resource.h"
[email protected]f1b6de22010-03-06 12:13:4751#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]ea587b02010-05-21 15:01:3552#include "chrome/common/json_pref_store.h"
initial.commit09911bf2008-07-26 23:55:2953#include "chrome/common/notification_service.h"
54#include "chrome/common/pref_names.h"
[email protected]4ef795df2010-02-03 02:35:0855#include "chrome/common/url_constants.h"
[email protected]bd48c2b02010-04-09 20:32:4256#include "chrome/installer/util/google_update_constants.h"
[email protected]d55aaa132009-09-28 21:08:0457#include "ipc/ipc_logging.h"
[email protected]4ef795df2010-02-03 02:35:0858#include "webkit/database/database_tracker.h"
[email protected]b112a4c2009-02-01 20:24:0159
60#if defined(OS_WIN)
[email protected]2362e4f2009-05-08 00:34:0561#include "views/focus/view_storage.h"
[email protected]86230b92009-11-23 20:38:3862#endif
63
[email protected]d55aaa132009-09-28 21:08:0464#if defined(IPC_MESSAGE_LOG_ENABLED)
65#include "chrome/common/plugin_messages.h"
66#include "chrome/common/render_messages.h"
67#endif
68
[email protected]3cdacd42010-04-30 18:55:5369#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
70// How often to check if the persistent instance of Chrome needs to restart
71// to install an update.
72static const int kUpdateCheckIntervalHours = 6;
73#endif
74
[email protected]bb975362009-01-21 01:00:2275BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
initial.commit09911bf2008-07-26 23:55:2976 : created_resource_dispatcher_host_(false),
77 created_metrics_service_(false),
78 created_io_thread_(false),
79 created_file_thread_(false),
80 created_db_thread_(false),
[email protected]914511712009-11-23 19:42:3381 created_process_launcher_thread_(false),
[email protected]875ee822010-05-18 20:58:0182 created_cache_thread_(false),
initial.commit09911bf2008-07-26 23:55:2983 created_profile_manager_(false),
84 created_local_state_(false),
[email protected]b112a4c2009-02-01 20:24:0185 created_icon_manager_(false),
86 created_debugger_wrapper_(false),
[email protected]40ecc902009-03-16 13:42:4787 created_devtools_manager_(false),
[email protected]29672ab2009-10-30 03:44:0388 created_notification_ui_manager_(false),
initial.commit09911bf2008-07-26 23:55:2989 module_ref_count_(0),
[email protected]afd20c022010-06-10 00:48:2090 did_start_(false),
[email protected]1b2db1a2008-08-08 17:46:1391 checked_for_new_frames_(false),
[email protected]6641bf662009-08-21 00:34:0992 using_new_frames_(false),
93 have_inspector_files_(true) {
initial.commit09911bf2008-07-26 23:55:2994 g_browser_process = this;
[email protected]1b8d02f12009-05-05 04:14:1195 clipboard_.reset(new Clipboard);
initial.commit09911bf2008-07-26 23:55:2996 main_notification_service_.reset(new NotificationService);
97
98 // Must be created after the NotificationService.
99 print_job_manager_.reset(new printing::PrintJobManager);
100
[email protected]b797e152009-01-23 16:06:14101 shutdown_event_.reset(new base::WaitableEvent(true, false));
initial.commit09911bf2008-07-26 23:55:29102}
103
104BrowserProcessImpl::~BrowserProcessImpl() {
[email protected]4ef795df2010-02-03 02:35:08105 FilePath profile_path;
106 bool clear_local_state_on_exit;
107
108 // Store the profile path for clearing local state data on exit.
109 clear_local_state_on_exit = ShouldClearLocalState(&profile_path);
110
initial.commit09911bf2008-07-26 23:55:29111 // Delete the AutomationProviderList before NotificationService,
112 // since it may try to unregister notifications
113 // Both NotificationService and AutomationProvider are singleton instances in
114 // the BrowserProcess. Since AutomationProvider may have some active
115 // notification observers, it is essential that it gets destroyed before the
116 // NotificationService. NotificationService won't be destroyed until after
117 // this destructor is run.
118 automation_provider_list_.reset();
119
[email protected]d393a0fd2009-05-13 23:32:01120 // We need to shutdown the SdchDictionaryFetcher as it regularly holds
121 // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do
122 // a PostDelayedTask onto the IO thread. This shutdown call will both discard
123 // any pending URLFetchers, and avoid creating any more.
124 SdchDictionaryFetcher::Shutdown();
125
[email protected]c4ff4952010-01-08 19:12:47126 // We need to destroy the MetricsService, GoogleURLTracker, and
127 // IntranetRedirectDetector before the io_thread_ gets destroyed, since their
128 // destructors can call the URLFetcher destructor, which does a
129 // PostDelayedTask operation on the IO thread. (The IO thread will handle
130 // that URLFetcher operation before going away.)
initial.commit09911bf2008-07-26 23:55:29131 metrics_service_.reset();
132 google_url_tracker_.reset();
[email protected]c4ff4952010-01-08 19:12:47133 intranet_redirect_detector_.reset();
initial.commit09911bf2008-07-26 23:55:29134
135 // Need to clear profiles (download managers) before the io_thread_.
136 profile_manager_.reset();
137
138 // Debugger must be cleaned up before IO thread and NotificationService.
139 debugger_wrapper_ = NULL;
140
141 if (resource_dispatcher_host_.get()) {
142 // Need to tell Safe Browsing Service that the IO thread is going away
143 // since it cached a pointer to it.
144 if (resource_dispatcher_host()->safe_browsing_service())
145 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
146
147 // Cancel pending requests and prevent new requests.
148 resource_dispatcher_host()->Shutdown();
149 }
150
[email protected]753efc42010-03-09 19:52:16151#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06152 // The IO thread must outlive the BACKGROUND_X11 thread.
153 background_x11_thread_.reset();
154#endif
155
initial.commit09911bf2008-07-26 23:55:29156 // Need to stop io_thread_ before resource_dispatcher_host_, since
157 // io_thread_ may still deref ResourceDispatcherHost and handle resource
158 // request before going away.
[email protected]0ac83682010-01-22 17:46:27159 io_thread_.reset();
initial.commit09911bf2008-07-26 23:55:29160
[email protected]875ee822010-05-18 20:58:01161 // The IO thread was the only user of this thread.
162 cache_thread_.reset();
163
[email protected]914511712009-11-23 19:42:33164 // Stop the process launcher thread after the IO thread, in case the IO thread
165 // posted a task to terminate a process on the process launcher thread.
166 process_launcher_thread_.reset();
167
initial.commit09911bf2008-07-26 23:55:29168 // Clean up state that lives on the file_thread_ before it goes away.
169 if (resource_dispatcher_host_.get()) {
170 resource_dispatcher_host()->download_file_manager()->Shutdown();
171 resource_dispatcher_host()->save_file_manager()->Shutdown();
172 }
173
174 // Need to stop the file_thread_ here to force it to process messages in its
175 // message loop from the previous call to shutdown the DownloadFileManager,
176 // SaveFileManager and SessionService.
177 file_thread_.reset();
178
179 // With the file_thread_ flushed, we can release any icon resources.
180 icon_manager_.reset();
181
182 // Need to destroy ResourceDispatcherHost before PluginService and
[email protected]49f28bc72010-02-04 00:10:01183 // SafeBrowsingService, since it caches a pointer to it. This also
184 // causes the webkit thread to terminate.
initial.commit09911bf2008-07-26 23:55:29185 resource_dispatcher_host_.reset();
186
187 // Wait for the pending print jobs to finish.
188 print_job_manager_->OnQuit();
189 print_job_manager_.reset();
190
[email protected]0b4d3382010-07-14 16:13:04191 // Destroy TabCloseableStateWatcher before NotificationService since the
192 // former registers for notifications.
193 tab_closeable_state_watcher_.reset();
194
initial.commit09911bf2008-07-26 23:55:29195 // Now OK to destroy NotificationService.
196 main_notification_service_.reset();
197
[email protected]49f28bc72010-02-04 00:10:01198 // Prior to clearing local state, we want to complete tasks pending
199 // on the db thread too.
200 db_thread_.reset();
201
202 // At this point, no render process exist and the file, io, db, and
203 // webkit threads in this process have all terminated, so it's safe
204 // to access local state data such as cookies, database, or local storage.
205 if (clear_local_state_on_exit)
206 ClearLocalState(profile_path);
207
initial.commit09911bf2008-07-26 23:55:29208 g_browser_process = NULL;
209}
210
[email protected]295039bd2008-08-15 04:32:57211// Send a QuitTask to the given MessageLoop.
212static void PostQuit(MessageLoop* message_loop) {
213 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
214}
initial.commit09911bf2008-07-26 23:55:29215
[email protected]b443cb042009-12-15 22:05:09216unsigned int BrowserProcessImpl::AddRefModule() {
217 DCHECK(CalledOnValidThread());
[email protected]afd20c022010-06-10 00:48:20218 did_start_ = true;
[email protected]b443cb042009-12-15 22:05:09219 module_ref_count_++;
220 return module_ref_count_;
221}
222
223unsigned int BrowserProcessImpl::ReleaseModule() {
224 DCHECK(CalledOnValidThread());
[email protected]760d970a2010-05-18 00:39:18225 DCHECK_NE(0u, module_ref_count_);
[email protected]b443cb042009-12-15 22:05:09226 module_ref_count_--;
227 if (0 == module_ref_count_) {
228 MessageLoop::current()->PostTask(
[email protected]34f73fb2010-03-24 20:50:34229 FROM_HERE, NewRunnableFunction(DidEndMainMessageLoop));
[email protected]b443cb042009-12-15 22:05:09230 MessageLoop::current()->Quit();
231 }
232 return module_ref_count_;
233}
234
initial.commit09911bf2008-07-26 23:55:29235void BrowserProcessImpl::EndSession() {
[email protected]b112a4c2009-02-01 20:24:01236#if defined(OS_WIN)
[email protected]d65cab7a2008-08-12 01:25:41237 // Notify we are going away.
[email protected]b797e152009-01-23 16:06:14238 ::SetEvent(shutdown_event_->handle());
[email protected]b112a4c2009-02-01 20:24:01239#endif
[email protected]d65cab7a2008-08-12 01:25:41240
initial.commit09911bf2008-07-26 23:55:29241 // Mark all the profiles as clean.
242 ProfileManager* pm = profile_manager();
243 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
244 (*i)->MarkAsCleanShutdown();
245
246 // Tell the metrics service it was cleanly shutdown.
247 MetricsService* metrics = g_browser_process->metrics_service();
248 if (metrics && local_state()) {
249 metrics->RecordCleanShutdown();
250
251 metrics->RecordStartOfSessionEnd();
252
253 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36254 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29255 }
256
257 // We must write that the profile and metrics service shutdown cleanly,
258 // otherwise on startup we'll think we crashed. So we block until done and
259 // then proceed with normal shutdown.
260 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57261 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29262 MessageLoop::current()->Run();
263}
264
265printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
266 // TODO(abarth): DCHECK(CalledOnValidThread());
[email protected]d8922f22010-05-07 00:57:27267 // http://code.google.com/p/chromium/issues/detail?id=6828
initial.commit09911bf2008-07-26 23:55:29268 // print_job_manager_ is initialized in the constructor and destroyed in the
269 // destructor, so it should always be valid.
270 DCHECK(print_job_manager_.get());
271 return print_job_manager_.get();
272}
273
[email protected]4ef795df2010-02-03 02:35:08274void BrowserProcessImpl::ClearLocalState(const FilePath& profile_path) {
275 SQLitePersistentCookieStore::ClearLocalState(profile_path.Append(
276 chrome::kCookieFilename));
277 DOMStorageContext::ClearLocalState(profile_path, chrome::kExtensionScheme);
[email protected]72cfd90f2010-02-06 03:08:04278 webkit_database::DatabaseTracker::ClearLocalState(profile_path);
[email protected]10c934052010-02-04 19:58:31279 ChromeAppCacheService::ClearLocalState(profile_path);
[email protected]4ef795df2010-02-03 02:35:08280}
281
282bool BrowserProcessImpl::ShouldClearLocalState(FilePath* profile_path) {
283 FilePath user_data_dir;
284 Profile* profile;
285
[email protected]09584972010-07-03 00:00:59286 // Check for the existence of a profile manager. When quitting early,
[email protected]ed0cf562010-02-05 22:50:41287 // e.g. because another chrome instance is running, or when invoked with
288 // options such as --uninstall or --try-chrome-again=0, the profile manager
289 // does not exist yet.
290 if (!profile_manager_.get())
291 return false;
292
[email protected]4ef795df2010-02-03 02:35:08293 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
294 profile = profile_manager_->GetDefaultProfile(user_data_dir);
[email protected]7bfc674322010-03-05 00:20:57295 if (!profile)
296 return false;
[email protected]4ef795df2010-02-03 02:35:08297 *profile_path = profile->GetPath();
298 return profile->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit);
299}
300
initial.commit09911bf2008-07-26 23:55:29301void BrowserProcessImpl::CreateResourceDispatcherHost() {
302 DCHECK(!created_resource_dispatcher_host_ &&
303 resource_dispatcher_host_.get() == NULL);
304 created_resource_dispatcher_host_ = true;
305
[email protected]79084c2d2009-11-03 23:12:42306 resource_dispatcher_host_.reset(new ResourceDispatcherHost());
initial.commit09911bf2008-07-26 23:55:29307 resource_dispatcher_host_->Initialize();
308}
309
310void BrowserProcessImpl::CreateMetricsService() {
311 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
312 created_metrics_service_ = true;
313
314 metrics_service_.reset(new MetricsService);
315}
316
317void BrowserProcessImpl::CreateIOThread() {
318 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
319 created_io_thread_ = true;
320
321 // Prior to starting the io thread, we create the plugin service as
322 // it is predominantly used from the io thread, but must be created
323 // on the main thread. The service ctor is inexpensive and does not
324 // invoke the io_thread() accessor.
325 PluginService::GetInstance();
326
[email protected]753efc42010-03-09 19:52:16327#if defined(USE_X11)
[email protected]4c3cd7412009-04-22 17:56:06328 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
329 // we start it now.
330 scoped_ptr<base::Thread> background_x11_thread(
331 new BrowserProcessSubThread(ChromeThread::BACKGROUND_X11));
332 if (!background_x11_thread->Start())
333 return;
334 background_x11_thread_.swap(background_x11_thread);
335#endif
336
[email protected]0ac83682010-01-22 17:46:27337 scoped_ptr<IOThread> thread(new IOThread);
[email protected]ab820df2008-08-26 05:55:10338 base::Thread::Options options;
339 options.message_loop_type = MessageLoop::TYPE_IO;
340 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29341 return;
342 io_thread_.swap(thread);
343}
344
345void BrowserProcessImpl::CreateFileThread() {
346 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
347 created_file_thread_ = true;
348
[email protected]ab820df2008-08-26 05:55:10349 scoped_ptr<base::Thread> thread(
350 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06351 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39352#if defined(OS_WIN)
353 // On Windows, the FILE thread needs to be have a UI message loop which pumps
354 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06355 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39356#else
357 options.message_loop_type = MessageLoop::TYPE_IO;
358#endif
[email protected]a1db3842008-09-17 22:04:06359 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29360 return;
361 file_thread_.swap(thread);
[email protected]27fbccc2010-03-25 03:00:49362
363 // ExtensionResource is in chrome/common, so it cannot depend on
364 // chrome/browser, which means it cannot lookup what the File thread is.
365 // We therefore store the thread ID from here so we can validate the proper
366 // thread usage in the ExtensionResource class.
367 ExtensionResource::set_file_thread_id(file_thread_->thread_id());
initial.commit09911bf2008-07-26 23:55:29368}
369
370void BrowserProcessImpl::CreateDBThread() {
371 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
372 created_db_thread_ = true;
373
[email protected]ab820df2008-08-26 05:55:10374 scoped_ptr<base::Thread> thread(
375 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29376 if (!thread->Start())
377 return;
378 db_thread_.swap(thread);
379}
380
[email protected]914511712009-11-23 19:42:33381void BrowserProcessImpl::CreateProcessLauncherThread() {
382 DCHECK(!created_process_launcher_thread_ && !process_launcher_thread_.get());
383 created_process_launcher_thread_ = true;
384
385 scoped_ptr<base::Thread> thread(
386 new BrowserProcessSubThread(ChromeThread::PROCESS_LAUNCHER));
387 if (!thread->Start())
388 return;
389 process_launcher_thread_.swap(thread);
390}
391
[email protected]875ee822010-05-18 20:58:01392void BrowserProcessImpl::CreateCacheThread() {
393 DCHECK(!created_cache_thread_ && !cache_thread_.get());
394 created_cache_thread_ = true;
395
396 scoped_ptr<base::Thread> thread(
397 new BrowserProcessSubThread(ChromeThread::CACHE));
398 base::Thread::Options options;
399 options.message_loop_type = MessageLoop::TYPE_IO;
400 if (!thread->StartWithOptions(options))
401 return;
402 cache_thread_.swap(thread);
403}
404
initial.commit09911bf2008-07-26 23:55:29405void BrowserProcessImpl::CreateProfileManager() {
406 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
407 created_profile_manager_ = true;
408
409 profile_manager_.reset(new ProfileManager());
410}
411
412void BrowserProcessImpl::CreateLocalState() {
413 DCHECK(!created_local_state_ && local_state_.get() == NULL);
414 created_local_state_ = true;
415
[email protected]b9636002009-03-04 00:05:25416 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29417 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]d8b08c92010-06-07 13:13:28418 local_state_.reset(PrefService::CreatePrefService(local_state_path));
419 }
initial.commit09911bf2008-07-26 23:55:29420
initial.commit09911bf2008-07-26 23:55:29421void BrowserProcessImpl::CreateIconManager() {
422 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
423 created_icon_manager_ = true;
424 icon_manager_.reset(new IconManager);
425}
426
[email protected]a9bf67982010-07-06 10:13:00427void BrowserProcessImpl::CreateDebuggerWrapper(int port, bool useHttp) {
initial.commit09911bf2008-07-26 23:55:29428 DCHECK(debugger_wrapper_.get() == NULL);
429 created_debugger_wrapper_ = true;
430
[email protected]a9bf67982010-07-06 10:13:00431 debugger_wrapper_ = new DebuggerWrapper(port, useHttp);
initial.commit09911bf2008-07-26 23:55:29432}
433
[email protected]40ecc902009-03-16 13:42:47434void BrowserProcessImpl::CreateDevToolsManager() {
[email protected]73ee01522009-06-05 10:13:44435 DCHECK(devtools_manager_.get() == NULL);
[email protected]40ecc902009-03-16 13:42:47436 created_devtools_manager_ = true;
[email protected]73ee01522009-06-05 10:13:44437 devtools_manager_ = new DevToolsManager();
[email protected]40ecc902009-03-16 13:42:47438}
439
initial.commit09911bf2008-07-26 23:55:29440void BrowserProcessImpl::CreateGoogleURLTracker() {
441 DCHECK(google_url_tracker_.get() == NULL);
442 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
443 google_url_tracker_.swap(google_url_tracker);
444}
[email protected]6641bf662009-08-21 00:34:09445
[email protected]c4ff4952010-01-08 19:12:47446void BrowserProcessImpl::CreateIntranetRedirectDetector() {
447 DCHECK(intranet_redirect_detector_.get() == NULL);
448 scoped_ptr<IntranetRedirectDetector> intranet_redirect_detector(
449 new IntranetRedirectDetector);
450 intranet_redirect_detector_.swap(intranet_redirect_detector);
451}
452
[email protected]29672ab2009-10-30 03:44:03453void BrowserProcessImpl::CreateNotificationUIManager() {
454 DCHECK(notification_ui_manager_.get() == NULL);
455 notification_ui_manager_.reset(NotificationUIManager::Create());
456 created_notification_ui_manager_ = true;
457}
458
[email protected]f1b6de22010-03-06 12:13:47459void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) {
460 locale_ = locale;
461 extension_l10n_util::SetProcessLocale(locale);
462}
463
[email protected]ccb55cf52010-03-06 22:02:04464void BrowserProcessImpl::CreateStatusTrayManager() {
465 DCHECK(status_tray_manager_.get() == NULL);
466 status_tray_manager_.reset(new StatusTrayManager());
467}
468
[email protected]0b4d3382010-07-14 16:13:04469void BrowserProcessImpl::CreateTabCloseableStateWatcher() {
470 DCHECK(tab_closeable_state_watcher_.get() == NULL);
471 tab_closeable_state_watcher_.reset(TabCloseableStateWatcher::Create());
472}
473
[email protected]6641bf662009-08-21 00:34:09474// The BrowserProcess object must outlive the file thread so we use traits
475// which don't do any management.
[email protected]c56428f22010-06-16 02:17:23476DISABLE_RUNNABLE_METHOD_REFCOUNT(BrowserProcessImpl);
[email protected]6641bf662009-08-21 00:34:09477
478void BrowserProcessImpl::CheckForInspectorFiles() {
479 file_thread()->message_loop()->PostTask
480 (FROM_HERE,
481 NewRunnableMethod(this, &BrowserProcessImpl::DoInspectorFilesCheck));
482}
483
[email protected]3cdacd42010-04-30 18:55:53484#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
[email protected]bd48c2b02010-04-09 20:32:42485void BrowserProcessImpl::StartAutoupdateTimer() {
486 autoupdate_timer_.Start(
[email protected]760d970a2010-05-18 00:39:18487 base::TimeDelta::FromHours(kUpdateCheckIntervalHours),
[email protected]bd48c2b02010-04-09 20:32:42488 this,
489 &BrowserProcessImpl::OnAutoupdateTimer);
490}
[email protected]3cdacd42010-04-30 18:55:53491#endif
[email protected]bd48c2b02010-04-09 20:32:42492
[email protected]d55aaa132009-09-28 21:08:04493#if defined(IPC_MESSAGE_LOG_ENABLED)
494
495void BrowserProcessImpl::SetIPCLoggingEnabled(bool enable) {
496 // First enable myself.
497 if (enable)
498 IPC::Logging::current()->Enable();
499 else
500 IPC::Logging::current()->Disable();
501
502 // Now tell subprocesses. Messages to ChildProcess-derived
503 // processes must be done on the IO thread.
504 io_thread()->message_loop()->PostTask
505 (FROM_HERE,
506 NewRunnableMethod(
507 this,
508 &BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses,
509 enable));
510
511 // Finally, tell the renderers which don't derive from ChildProcess.
512 // Messages to the renderers must be done on the UI (main) thread.
[email protected]019191a2009-10-02 20:37:27513 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
514 !i.IsAtEnd(); i.Advance())
515 i.GetCurrentValue()->Send(new ViewMsg_SetIPCLoggingEnabled(enable));
[email protected]d55aaa132009-09-28 21:08:04516}
517
518// Helper for SetIPCLoggingEnabled.
519void BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses(bool enabled) {
[email protected]d85cf072009-10-27 03:59:31520 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
[email protected]d55aaa132009-09-28 21:08:04521
[email protected]d27893f62010-07-03 05:47:42522 BrowserChildProcessHost::Iterator i; // default constr references a singleton
[email protected]34cf97d2009-09-29 22:46:11523 while (!i.Done()) {
524 i->Send(new PluginProcessMsg_SetIPCLoggingEnabled(enabled));
525 ++i;
[email protected]d55aaa132009-09-28 21:08:04526 }
527}
528
529#endif // IPC_MESSAGE_LOG_ENABLED
530
[email protected]6641bf662009-08-21 00:34:09531void BrowserProcessImpl::DoInspectorFilesCheck() {
532 // Runs on FILE thread.
533 DCHECK(file_thread_->message_loop() == MessageLoop::current());
534 bool result = false;
535
536 FilePath inspector_dir;
537 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) {
538 result = file_util::PathExists(inspector_dir);
539 }
540
[email protected]308080d2009-11-11 19:19:02541 have_inspector_files_ = result;
[email protected]6641bf662009-08-21 00:34:09542}
[email protected]bd48c2b02010-04-09 20:32:42543
[email protected]3cdacd42010-04-30 18:55:53544// Mac is currently not supported.
545#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
[email protected]bd48c2b02010-04-09 20:32:42546
547bool BrowserProcessImpl::CanAutorestartForUpdate() const {
548 // Check if browser is in the background and if it needs to be restarted to
549 // apply a pending update.
550 return BrowserList::IsInPersistentMode() && Upgrade::IsUpdatePendingRestart();
551}
552
553// Switches enumerated here will be removed when a background instance of
554// Chrome restarts itself. If your key is designed to only be used once,
555// or if it does not make sense when restarting a background instance to
556// pick up an automatic update, be sure to add it to this list.
557const char* const kSwitchesToRemoveOnAutorestart[] = {
558 switches::kApp,
559 switches::kFirstRun,
560 switches::kImport,
561 switches::kImportFromFile,
562 switches::kMakeDefaultBrowser
563};
564
565void BrowserProcessImpl::RestartPersistentInstance() {
566 CommandLine* old_cl = CommandLine::ForCurrentProcess();
[email protected]3cdacd42010-04-30 18:55:53567 scoped_ptr<CommandLine> new_cl(new CommandLine(old_cl->GetProgram()));
[email protected]bd48c2b02010-04-09 20:32:42568
569 std::map<std::string, CommandLine::StringType> switches =
570 old_cl->GetSwitches();
571
572 // Remove the keys that we shouldn't pass through during restart.
[email protected]3cdacd42010-04-30 18:55:53573 for (size_t i = 0; i < arraysize(kSwitchesToRemoveOnAutorestart); i++) {
[email protected]bd48c2b02010-04-09 20:32:42574 switches.erase(kSwitchesToRemoveOnAutorestart[i]);
575 }
576
577 // Append the rest of the switches (along with their values, if any)
578 // to the new command line
579 for (std::map<std::string, CommandLine::StringType>::const_iterator i =
580 switches.begin(); i != switches.end(); ++i) {
581 CommandLine::StringType switch_value = i->second;
582 if (switch_value.length() > 0) {
[email protected]3cdacd42010-04-30 18:55:53583 new_cl->AppendSwitchWithValue(i->first, i->second);
[email protected]bd48c2b02010-04-09 20:32:42584 } else {
[email protected]3cdacd42010-04-30 18:55:53585 new_cl->AppendSwitch(i->first);
[email protected]bd48c2b02010-04-09 20:32:42586 }
587 }
588
[email protected]398206c2010-06-21 01:46:08589 if (!new_cl->HasSwitch(switches::kRestoreBackgroundContents))
590 new_cl->AppendSwitch(switches::kRestoreBackgroundContents);
[email protected]bd48c2b02010-04-09 20:32:42591
[email protected]3cdacd42010-04-30 18:55:53592 DLOG(WARNING) << "Shutting down current instance of the browser.";
593 BrowserList::CloseAllBrowsersAndExit();
594
595 // Transfer ownership to Upgrade.
596 Upgrade::SetNewCommandLine(new_cl.release());
[email protected]bd48c2b02010-04-09 20:32:42597}
598
599void BrowserProcessImpl::OnAutoupdateTimer() {
600 if (CanAutorestartForUpdate()) {
[email protected]3cdacd42010-04-30 18:55:53601 DLOG(WARNING) << "Detected update. Restarting browser.";
[email protected]bd48c2b02010-04-09 20:32:42602 RestartPersistentInstance();
603 }
604}
605
[email protected]3cdacd42010-04-30 18:55:53606#endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)