blob: f971e8070af27051d77f628ff80d7c57d8545640 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// 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]a92b8642009-05-05 23:38:567#include "app/l10n_util.h"
[email protected]1b8d02f12009-05-05 04:14:118#include "base/clipboard.h"
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]ac262c9f2008-10-19 17:45:2112#include "base/thread.h"
[email protected]1c4947f2009-01-15 22:25:1113#include "base/waitable_event.h"
[email protected]ac262c9f2008-10-19 17:45:2114#include "chrome/browser/browser_trial.h"
initial.commit09911bf2008-07-26 23:55:2915#include "chrome/browser/chrome_thread.h"
[email protected]4ab4b0f2009-02-10 18:54:5016#include "chrome/browser/debugger/debugger_wrapper.h"
[email protected]40ecc902009-03-16 13:42:4717#include "chrome/browser/debugger/devtools_manager.h"
[email protected]b7f05882009-02-22 01:21:5618#include "chrome/browser/download/download_file.h"
[email protected]5ba0a2c2009-02-19 01:19:3419#include "chrome/browser/download/save_file_manager.h"
initial.commit09911bf2008-07-26 23:55:2920#include "chrome/browser/google_url_tracker.h"
[email protected]dcefa302009-05-20 00:24:3921#include "chrome/browser/icon_manager.h"
[email protected]dc6f4962009-02-13 01:25:5022#include "chrome/browser/metrics/metrics_service.h"
[email protected]1933eb202009-02-19 18:23:2523#include "chrome/browser/net/dns_global.h"
[email protected]d393a0fd2009-05-13 23:32:0124#include "chrome/browser/net/sdch_dictionary_fetcher.h"
[email protected]fd49e2d2009-02-20 17:21:3025#include "chrome/browser/plugin_service.h"
initial.commit09911bf2008-07-26 23:55:2926#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2627#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]81218f42009-02-05 18:48:0828#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
29#include "chrome/browser/safe_browsing/safe_browsing_service.h"
initial.commit09911bf2008-07-26 23:55:2930#include "chrome/common/chrome_paths.h"
31#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2932#include "chrome/common/notification_service.h"
33#include "chrome/common/pref_names.h"
34#include "chrome/common/pref_service.h"
[email protected]b112a4c2009-02-01 20:24:0135
36#if defined(OS_WIN)
37#include "chrome/browser/automation/automation_provider_list.h"
[email protected]b112a4c2009-02-01 20:24:0138#include "chrome/browser/printing/print_job_manager.h"
[email protected]2362e4f2009-05-08 00:34:0539#include "views/focus/view_storage.h"
[email protected]81218f42009-02-05 18:48:0840#elif defined(OS_POSIX)
41// TODO(port): Remove the temporary scaffolding as we port the above headers.
42#include "chrome/common/temp_scaffolding_stubs.h"
[email protected]b112a4c2009-02-01 20:24:0143#endif
initial.commit09911bf2008-07-26 23:55:2944
45namespace {
46
47// ----------------------------------------------------------------------------
48// BrowserProcessSubThread
49//
50// This simple thread object is used for the specialized threads that the
51// BrowserProcess spins up.
52//
53// Applications must initialize the COM library before they can call
54// COM library functions other than CoGetMalloc and memory allocation
55// functions, so this class initializes COM for those users.
56class BrowserProcessSubThread : public ChromeThread {
57 public:
58 explicit BrowserProcessSubThread(ChromeThread::ID identifier)
59 : ChromeThread(identifier) {
60 }
61
[email protected]48ca9012009-08-11 21:38:5462 virtual ~BrowserProcessSubThread() {
initial.commit09911bf2008-07-26 23:55:2963 // We cannot rely on our base class to stop the thread since we want our
64 // CleanUp function to run.
65 Stop();
66 }
67
68 protected:
69 virtual void Init() {
[email protected]b112a4c2009-02-01 20:24:0170#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2971 // Initializes the COM library on the current thread.
72 CoInitialize(NULL);
[email protected]b112a4c2009-02-01 20:24:0173#endif
initial.commit09911bf2008-07-26 23:55:2974
75 notification_service_ = new NotificationService;
76 }
77
78 virtual void CleanUp() {
79 delete notification_service_;
80 notification_service_ = NULL;
81
[email protected]b112a4c2009-02-01 20:24:0182#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2983 // Closes the COM library on the current thread. CoInitialize must
84 // be balanced by a corresponding call to CoUninitialize.
85 CoUninitialize();
[email protected]b112a4c2009-02-01 20:24:0186#endif
initial.commit09911bf2008-07-26 23:55:2987 }
88
89 private:
90 // Each specialized thread has its own notification service.
91 // Note: We don't use scoped_ptr because the destructor runs on the wrong
92 // thread.
93 NotificationService* notification_service_;
94};
95
[email protected]48ca9012009-08-11 21:38:5496class IOThread : public BrowserProcessSubThread {
97 public:
98 IOThread() : BrowserProcessSubThread(ChromeThread::IO) {}
99
100 virtual ~IOThread() {
101 // We cannot rely on our base class to stop the thread since we want our
102 // CleanUp function to run.
103 Stop();
104 }
105
106 protected:
107 virtual void CleanUp() {
108 // URLFetcher and URLRequest instances must NOT outlive the IO thread.
[email protected]03a69dc2009-09-22 00:04:59109 //
110 // Strictly speaking, URLFetcher's CheckForLeaks() should be done on the
111 // UI thread. However, since there _shouldn't_ be any instances left
112 // at this point, it shouldn't be a race.
113 //
114 // We check URLFetcher first, since if it has leaked then an associated
115 // URLRequest will also have leaked. However it is more useful to
116 // crash showing the callstack of URLFetcher's allocation than its
117 // URLRequest member.
[email protected]48ca9012009-08-11 21:38:54118 base::LeakTracker<URLFetcher>::CheckForLeaks();
[email protected]03a69dc2009-09-22 00:04:59119 base::LeakTracker<URLRequest>::CheckForLeaks();
[email protected]48ca9012009-08-11 21:38:54120
121 BrowserProcessSubThread::CleanUp();
122 }
123};
124
initial.commit09911bf2008-07-26 23:55:29125} // namespace
126
[email protected]bb975362009-01-21 01:00:22127BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
initial.commit09911bf2008-07-26 23:55:29128 : created_resource_dispatcher_host_(false),
129 created_metrics_service_(false),
130 created_io_thread_(false),
131 created_file_thread_(false),
132 created_db_thread_(false),
133 created_profile_manager_(false),
134 created_local_state_(false),
[email protected]f3a4f302009-08-21 22:35:29135#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29136 initialized_broker_services_(false),
initial.commit09911bf2008-07-26 23:55:29137 broker_services_(NULL),
[email protected]f3a4f302009-08-21 22:35:29138#endif // defined(OS_WIN)
[email protected]b112a4c2009-02-01 20:24:01139 created_icon_manager_(false),
140 created_debugger_wrapper_(false),
[email protected]40ecc902009-03-16 13:42:47141 created_devtools_manager_(false),
initial.commit09911bf2008-07-26 23:55:29142 module_ref_count_(0),
[email protected]ecfa8b392009-08-20 15:44:33143 memory_model_(HIGH_MEMORY_MODEL),
[email protected]1b2db1a2008-08-08 17:46:13144 checked_for_new_frames_(false),
[email protected]6641bf662009-08-21 00:34:09145 using_new_frames_(false),
146 have_inspector_files_(true) {
initial.commit09911bf2008-07-26 23:55:29147 g_browser_process = this;
[email protected]1b8d02f12009-05-05 04:14:11148 clipboard_.reset(new Clipboard);
initial.commit09911bf2008-07-26 23:55:29149 main_notification_service_.reset(new NotificationService);
150
151 // Must be created after the NotificationService.
152 print_job_manager_.reset(new printing::PrintJobManager);
153
154 // Configure the browser memory model.
155 if (command_line.HasSwitch(switches::kMemoryModel)) {
156 std::wstring model = command_line.GetSwitchValue(switches::kMemoryModel);
157 if (!model.empty()) {
158 if (model == L"high")
159 memory_model_ = HIGH_MEMORY_MODEL;
160 else if (model == L"low")
161 memory_model_ = LOW_MEMORY_MODEL;
162 else if (model == L"medium")
163 memory_model_ = MEDIUM_MEMORY_MODEL;
164 }
165 }
[email protected]b797e152009-01-23 16:06:14166 shutdown_event_.reset(new base::WaitableEvent(true, false));
initial.commit09911bf2008-07-26 23:55:29167}
168
169BrowserProcessImpl::~BrowserProcessImpl() {
170 // Delete the AutomationProviderList before NotificationService,
171 // since it may try to unregister notifications
172 // Both NotificationService and AutomationProvider are singleton instances in
173 // the BrowserProcess. Since AutomationProvider may have some active
174 // notification observers, it is essential that it gets destroyed before the
175 // NotificationService. NotificationService won't be destroyed until after
176 // this destructor is run.
177 automation_provider_list_.reset();
178
[email protected]d393a0fd2009-05-13 23:32:01179 // We need to shutdown the SdchDictionaryFetcher as it regularly holds
180 // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do
181 // a PostDelayedTask onto the IO thread. This shutdown call will both discard
182 // any pending URLFetchers, and avoid creating any more.
183 SdchDictionaryFetcher::Shutdown();
184
initial.commit09911bf2008-07-26 23:55:29185 // We need to destroy the MetricsService and GoogleURLTracker before the
186 // io_thread_ gets destroyed, since both destructors can call the URLFetcher
[email protected]d393a0fd2009-05-13 23:32:01187 // destructor, which does an PostDelayedTask operation on the IO thread. (The
188 // IO thread will handle that URLFetcher operation before going away.)
initial.commit09911bf2008-07-26 23:55:29189 metrics_service_.reset();
190 google_url_tracker_.reset();
191
192 // Need to clear profiles (download managers) before the io_thread_.
193 profile_manager_.reset();
194
195 // Debugger must be cleaned up before IO thread and NotificationService.
196 debugger_wrapper_ = NULL;
197
198 if (resource_dispatcher_host_.get()) {
199 // Need to tell Safe Browsing Service that the IO thread is going away
200 // since it cached a pointer to it.
201 if (resource_dispatcher_host()->safe_browsing_service())
202 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
203
204 // Cancel pending requests and prevent new requests.
205 resource_dispatcher_host()->Shutdown();
206 }
207
[email protected]4c3cd7412009-04-22 17:56:06208#if defined(OS_LINUX)
209 // The IO thread must outlive the BACKGROUND_X11 thread.
210 background_x11_thread_.reset();
211#endif
212
initial.commit09911bf2008-07-26 23:55:29213 // Need to stop io_thread_ before resource_dispatcher_host_, since
214 // io_thread_ may still deref ResourceDispatcherHost and handle resource
215 // request before going away.
[email protected]48ca9012009-08-11 21:38:54216 ResetIOThread();
initial.commit09911bf2008-07-26 23:55:29217
218 // Clean up state that lives on the file_thread_ before it goes away.
219 if (resource_dispatcher_host_.get()) {
220 resource_dispatcher_host()->download_file_manager()->Shutdown();
221 resource_dispatcher_host()->save_file_manager()->Shutdown();
222 }
223
224 // Need to stop the file_thread_ here to force it to process messages in its
225 // message loop from the previous call to shutdown the DownloadFileManager,
226 // SaveFileManager and SessionService.
227 file_thread_.reset();
228
229 // With the file_thread_ flushed, we can release any icon resources.
230 icon_manager_.reset();
231
232 // Need to destroy ResourceDispatcherHost before PluginService and
233 // SafeBrowsingService, since it caches a pointer to it.
234 resource_dispatcher_host_.reset();
235
236 // Wait for the pending print jobs to finish.
237 print_job_manager_->OnQuit();
238 print_job_manager_.reset();
239
initial.commit09911bf2008-07-26 23:55:29240 // Now OK to destroy NotificationService.
241 main_notification_service_.reset();
242
243 g_browser_process = NULL;
244}
245
[email protected]295039bd2008-08-15 04:32:57246// Send a QuitTask to the given MessageLoop.
247static void PostQuit(MessageLoop* message_loop) {
248 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
249}
initial.commit09911bf2008-07-26 23:55:29250
251void BrowserProcessImpl::EndSession() {
[email protected]b112a4c2009-02-01 20:24:01252#if defined(OS_WIN)
[email protected]d65cab7a2008-08-12 01:25:41253 // Notify we are going away.
[email protected]b797e152009-01-23 16:06:14254 ::SetEvent(shutdown_event_->handle());
[email protected]b112a4c2009-02-01 20:24:01255#endif
[email protected]d65cab7a2008-08-12 01:25:41256
initial.commit09911bf2008-07-26 23:55:29257 // Mark all the profiles as clean.
258 ProfileManager* pm = profile_manager();
259 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
260 (*i)->MarkAsCleanShutdown();
261
262 // Tell the metrics service it was cleanly shutdown.
263 MetricsService* metrics = g_browser_process->metrics_service();
264 if (metrics && local_state()) {
265 metrics->RecordCleanShutdown();
266
267 metrics->RecordStartOfSessionEnd();
268
269 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36270 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29271 }
272
273 // We must write that the profile and metrics service shutdown cleanly,
274 // otherwise on startup we'll think we crashed. So we block until done and
275 // then proceed with normal shutdown.
276 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57277 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29278 MessageLoop::current()->Run();
279}
280
281printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
282 // TODO(abarth): DCHECK(CalledOnValidThread());
283 // See <http://b/1287209>.
284 // print_job_manager_ is initialized in the constructor and destroyed in the
285 // destructor, so it should always be valid.
286 DCHECK(print_job_manager_.get());
287 return print_job_manager_.get();
288}
289
[email protected]d70539de2009-06-24 22:17:06290const std::string& BrowserProcessImpl::GetApplicationLocale() {
initial.commit09911bf2008-07-26 23:55:29291 DCHECK(CalledOnValidThread());
292 if (locale_.empty()) {
[email protected]d70539de2009-06-24 22:17:06293 locale_ = l10n_util::GetApplicationLocale(
294 local_state()->GetString(prefs::kApplicationLocale));
initial.commit09911bf2008-07-26 23:55:29295 }
296 return locale_;
297}
298
299void BrowserProcessImpl::CreateResourceDispatcherHost() {
300 DCHECK(!created_resource_dispatcher_host_ &&
301 resource_dispatcher_host_.get() == NULL);
302 created_resource_dispatcher_host_ = true;
303
304 resource_dispatcher_host_.reset(
305 new ResourceDispatcherHost(io_thread()->message_loop()));
306 resource_dispatcher_host_->Initialize();
307}
308
309void BrowserProcessImpl::CreateMetricsService() {
310 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
311 created_metrics_service_ = true;
312
313 metrics_service_.reset(new MetricsService);
314}
315
316void BrowserProcessImpl::CreateIOThread() {
317 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
318 created_io_thread_ = true;
319
320 // Prior to starting the io thread, we create the plugin service as
321 // it is predominantly used from the io thread, but must be created
322 // on the main thread. The service ctor is inexpensive and does not
323 // invoke the io_thread() accessor.
324 PluginService::GetInstance();
325
[email protected]4c3cd7412009-04-22 17:56:06326#if defined(OS_LINUX)
327 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
328 // we start it now.
329 scoped_ptr<base::Thread> background_x11_thread(
330 new BrowserProcessSubThread(ChromeThread::BACKGROUND_X11));
331 if (!background_x11_thread->Start())
332 return;
333 background_x11_thread_.swap(background_x11_thread);
334#endif
335
[email protected]48ca9012009-08-11 21:38:54336 scoped_ptr<base::Thread> thread(new IOThread);
[email protected]ab820df2008-08-26 05:55:10337 base::Thread::Options options;
338 options.message_loop_type = MessageLoop::TYPE_IO;
339 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29340 return;
341 io_thread_.swap(thread);
342}
343
[email protected]48ca9012009-08-11 21:38:54344void BrowserProcessImpl::ResetIOThread() {
345 if (io_thread_.get()) {
346 io_thread_->message_loop()->PostTask(FROM_HERE,
347 NewRunnableFunction(CleanupOnIOThread));
348 }
349 io_thread_.reset();
350}
351
352// static
353void BrowserProcessImpl::CleanupOnIOThread() {
354 // Shutdown DNS prefetching now to ensure that network stack objects
355 // living on the IO thread get destroyed before the IO thread goes away.
356 chrome_browser_net::EnsureDnsPrefetchShutdown();
357 // TODO(eroman): can this be merged into IOThread::CleanUp() ?
358}
359
initial.commit09911bf2008-07-26 23:55:29360void BrowserProcessImpl::CreateFileThread() {
361 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
362 created_file_thread_ = true;
363
[email protected]ab820df2008-08-26 05:55:10364 scoped_ptr<base::Thread> thread(
365 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06366 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39367#if defined(OS_WIN)
368 // On Windows, the FILE thread needs to be have a UI message loop which pumps
369 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06370 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39371#else
372 options.message_loop_type = MessageLoop::TYPE_IO;
373#endif
[email protected]a1db3842008-09-17 22:04:06374 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29375 return;
376 file_thread_.swap(thread);
377}
378
379void BrowserProcessImpl::CreateDBThread() {
380 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
381 created_db_thread_ = true;
382
[email protected]ab820df2008-08-26 05:55:10383 scoped_ptr<base::Thread> thread(
384 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29385 if (!thread->Start())
386 return;
387 db_thread_.swap(thread);
388}
389
390void BrowserProcessImpl::CreateProfileManager() {
391 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
392 created_profile_manager_ = true;
393
394 profile_manager_.reset(new ProfileManager());
395}
396
397void BrowserProcessImpl::CreateLocalState() {
398 DCHECK(!created_local_state_ && local_state_.get() == NULL);
399 created_local_state_ = true;
400
[email protected]b9636002009-03-04 00:05:25401 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29402 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]6faa0e0d2009-04-28 06:50:36403 local_state_.reset(new PrefService(local_state_path, file_thread()));
initial.commit09911bf2008-07-26 23:55:29404}
405
[email protected]f3a4f302009-08-21 22:35:29406#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29407void BrowserProcessImpl::InitBrokerServices(
408 sandbox::BrokerServices* broker_services) {
409 DCHECK(!initialized_broker_services_ && broker_services_ == NULL);
410 broker_services->Init();
411 initialized_broker_services_ = true;
412 broker_services_ = broker_services;
413}
[email protected]f3a4f302009-08-21 22:35:29414#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29415
416void BrowserProcessImpl::CreateIconManager() {
417 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
418 created_icon_manager_ = true;
419 icon_manager_.reset(new IconManager);
420}
421
422void BrowserProcessImpl::CreateDebuggerWrapper(int port) {
423 DCHECK(debugger_wrapper_.get() == NULL);
424 created_debugger_wrapper_ = true;
425
426 debugger_wrapper_ = new DebuggerWrapper(port);
427}
428
[email protected]40ecc902009-03-16 13:42:47429void BrowserProcessImpl::CreateDevToolsManager() {
[email protected]73ee01522009-06-05 10:13:44430 DCHECK(devtools_manager_.get() == NULL);
[email protected]40ecc902009-03-16 13:42:47431 created_devtools_manager_ = true;
[email protected]73ee01522009-06-05 10:13:44432 devtools_manager_ = new DevToolsManager();
[email protected]40ecc902009-03-16 13:42:47433}
434
initial.commit09911bf2008-07-26 23:55:29435void BrowserProcessImpl::CreateGoogleURLTracker() {
436 DCHECK(google_url_tracker_.get() == NULL);
437 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
438 google_url_tracker_.swap(google_url_tracker);
439}
[email protected]6641bf662009-08-21 00:34:09440
441// The BrowserProcess object must outlive the file thread so we use traits
442// which don't do any management.
443template <>
444struct RunnableMethodTraits<BrowserProcessImpl> {
445 static void RetainCallee(BrowserProcessImpl*) {}
446 static void ReleaseCallee(BrowserProcessImpl*) {}
447};
448
449void BrowserProcessImpl::CheckForInspectorFiles() {
450 file_thread()->message_loop()->PostTask
451 (FROM_HERE,
452 NewRunnableMethod(this, &BrowserProcessImpl::DoInspectorFilesCheck));
453}
454
455void BrowserProcessImpl::DoInspectorFilesCheck() {
456 // Runs on FILE thread.
457 DCHECK(file_thread_->message_loop() == MessageLoop::current());
458 bool result = false;
459
460 FilePath inspector_dir;
461 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) {
462 result = file_util::PathExists(inspector_dir);
463 }
464
465 have_inspector_files_ = result;
466}