blob: 4007cf6ce9ef91fd1d0ed6af152d40bc4f3e0bb6 [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"
initial.commit09911bf2008-07-26 23:55:2910#include "base/path_service.h"
[email protected]ac262c9f2008-10-19 17:45:2111#include "base/thread.h"
[email protected]1c4947f2009-01-15 22:25:1112#include "base/waitable_event.h"
[email protected]ac262c9f2008-10-19 17:45:2113#include "chrome/browser/browser_trial.h"
initial.commit09911bf2008-07-26 23:55:2914#include "chrome/browser/chrome_thread.h"
[email protected]4ab4b0f2009-02-10 18:54:5015#include "chrome/browser/debugger/debugger_wrapper.h"
[email protected]40ecc902009-03-16 13:42:4716#include "chrome/browser/debugger/devtools_manager.h"
[email protected]b7f05882009-02-22 01:21:5617#include "chrome/browser/download/download_file.h"
[email protected]5ba0a2c2009-02-19 01:19:3418#include "chrome/browser/download/save_file_manager.h"
initial.commit09911bf2008-07-26 23:55:2919#include "chrome/browser/google_url_tracker.h"
[email protected]dcefa302009-05-20 00:24:3920#include "chrome/browser/icon_manager.h"
[email protected]dc6f4962009-02-13 01:25:5021#include "chrome/browser/metrics/metrics_service.h"
[email protected]1933eb202009-02-19 18:23:2522#include "chrome/browser/net/dns_global.h"
[email protected]d393a0fd2009-05-13 23:32:0123#include "chrome/browser/net/sdch_dictionary_fetcher.h"
[email protected]fd49e2d2009-02-20 17:21:3024#include "chrome/browser/plugin_service.h"
initial.commit09911bf2008-07-26 23:55:2925#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2626#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]81218f42009-02-05 18:48:0827#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
28#include "chrome/browser/safe_browsing/safe_browsing_service.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/common/chrome_paths.h"
30#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2931#include "chrome/common/notification_service.h"
32#include "chrome/common/pref_names.h"
33#include "chrome/common/pref_service.h"
[email protected]b112a4c2009-02-01 20:24:0134
35#if defined(OS_WIN)
36#include "chrome/browser/automation/automation_provider_list.h"
[email protected]b112a4c2009-02-01 20:24:0137#include "chrome/browser/printing/print_job_manager.h"
[email protected]2362e4f2009-05-08 00:34:0538#include "views/focus/view_storage.h"
[email protected]81218f42009-02-05 18:48:0839#elif defined(OS_POSIX)
40// TODO(port): Remove the temporary scaffolding as we port the above headers.
41#include "chrome/common/temp_scaffolding_stubs.h"
[email protected]b112a4c2009-02-01 20:24:0142#endif
initial.commit09911bf2008-07-26 23:55:2943
44namespace {
45
46// ----------------------------------------------------------------------------
47// BrowserProcessSubThread
48//
49// This simple thread object is used for the specialized threads that the
50// BrowserProcess spins up.
51//
52// Applications must initialize the COM library before they can call
53// COM library functions other than CoGetMalloc and memory allocation
54// functions, so this class initializes COM for those users.
55class BrowserProcessSubThread : public ChromeThread {
56 public:
57 explicit BrowserProcessSubThread(ChromeThread::ID identifier)
58 : ChromeThread(identifier) {
59 }
60
[email protected]48ca9012009-08-11 21:38:5461 virtual ~BrowserProcessSubThread() {
initial.commit09911bf2008-07-26 23:55:2962 // We cannot rely on our base class to stop the thread since we want our
63 // CleanUp function to run.
64 Stop();
65 }
66
67 protected:
68 virtual void Init() {
[email protected]b112a4c2009-02-01 20:24:0169#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2970 // Initializes the COM library on the current thread.
71 CoInitialize(NULL);
[email protected]b112a4c2009-02-01 20:24:0172#endif
initial.commit09911bf2008-07-26 23:55:2973
74 notification_service_ = new NotificationService;
75 }
76
77 virtual void CleanUp() {
78 delete notification_service_;
79 notification_service_ = NULL;
80
[email protected]b112a4c2009-02-01 20:24:0181#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2982 // Closes the COM library on the current thread. CoInitialize must
83 // be balanced by a corresponding call to CoUninitialize.
84 CoUninitialize();
[email protected]b112a4c2009-02-01 20:24:0185#endif
initial.commit09911bf2008-07-26 23:55:2986 }
87
88 private:
89 // Each specialized thread has its own notification service.
90 // Note: We don't use scoped_ptr because the destructor runs on the wrong
91 // thread.
92 NotificationService* notification_service_;
93};
94
[email protected]48ca9012009-08-11 21:38:5495class IOThread : public BrowserProcessSubThread {
96 public:
97 IOThread() : BrowserProcessSubThread(ChromeThread::IO) {}
98
99 virtual ~IOThread() {
100 // We cannot rely on our base class to stop the thread since we want our
101 // CleanUp function to run.
102 Stop();
103 }
104
105 protected:
106 virtual void CleanUp() {
107 // URLFetcher and URLRequest instances must NOT outlive the IO thread.
108 base::LeakTracker<URLRequest>::CheckForLeaks();
109 base::LeakTracker<URLFetcher>::CheckForLeaks();
110
111 BrowserProcessSubThread::CleanUp();
112 }
113};
114
initial.commit09911bf2008-07-26 23:55:29115} // namespace
116
[email protected]bb975362009-01-21 01:00:22117BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
initial.commit09911bf2008-07-26 23:55:29118 : created_resource_dispatcher_host_(false),
119 created_metrics_service_(false),
120 created_io_thread_(false),
121 created_file_thread_(false),
122 created_db_thread_(false),
123 created_profile_manager_(false),
124 created_local_state_(false),
initial.commit09911bf2008-07-26 23:55:29125 initialized_broker_services_(false),
initial.commit09911bf2008-07-26 23:55:29126 broker_services_(NULL),
[email protected]b112a4c2009-02-01 20:24:01127 created_icon_manager_(false),
128 created_debugger_wrapper_(false),
[email protected]40ecc902009-03-16 13:42:47129 created_devtools_manager_(false),
initial.commit09911bf2008-07-26 23:55:29130 module_ref_count_(0),
[email protected]1b2db1a2008-08-08 17:46:13131 memory_model_(MEDIUM_MEMORY_MODEL),
132 checked_for_new_frames_(false),
133 using_new_frames_(false) {
initial.commit09911bf2008-07-26 23:55:29134 g_browser_process = this;
[email protected]1b8d02f12009-05-05 04:14:11135 clipboard_.reset(new Clipboard);
initial.commit09911bf2008-07-26 23:55:29136 main_notification_service_.reset(new NotificationService);
137
138 // Must be created after the NotificationService.
139 print_job_manager_.reset(new printing::PrintJobManager);
140
141 // Configure the browser memory model.
142 if (command_line.HasSwitch(switches::kMemoryModel)) {
143 std::wstring model = command_line.GetSwitchValue(switches::kMemoryModel);
144 if (!model.empty()) {
145 if (model == L"high")
146 memory_model_ = HIGH_MEMORY_MODEL;
147 else if (model == L"low")
148 memory_model_ = LOW_MEMORY_MODEL;
149 else if (model == L"medium")
150 memory_model_ = MEDIUM_MEMORY_MODEL;
151 }
152 }
[email protected]b797e152009-01-23 16:06:14153 shutdown_event_.reset(new base::WaitableEvent(true, false));
initial.commit09911bf2008-07-26 23:55:29154}
155
156BrowserProcessImpl::~BrowserProcessImpl() {
157 // Delete the AutomationProviderList before NotificationService,
158 // since it may try to unregister notifications
159 // Both NotificationService and AutomationProvider are singleton instances in
160 // the BrowserProcess. Since AutomationProvider may have some active
161 // notification observers, it is essential that it gets destroyed before the
162 // NotificationService. NotificationService won't be destroyed until after
163 // this destructor is run.
164 automation_provider_list_.reset();
165
[email protected]d393a0fd2009-05-13 23:32:01166 // We need to shutdown the SdchDictionaryFetcher as it regularly holds
167 // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do
168 // a PostDelayedTask onto the IO thread. This shutdown call will both discard
169 // any pending URLFetchers, and avoid creating any more.
170 SdchDictionaryFetcher::Shutdown();
171
initial.commit09911bf2008-07-26 23:55:29172 // We need to destroy the MetricsService and GoogleURLTracker before the
173 // io_thread_ gets destroyed, since both destructors can call the URLFetcher
[email protected]d393a0fd2009-05-13 23:32:01174 // destructor, which does an PostDelayedTask operation on the IO thread. (The
175 // IO thread will handle that URLFetcher operation before going away.)
initial.commit09911bf2008-07-26 23:55:29176 metrics_service_.reset();
177 google_url_tracker_.reset();
178
179 // Need to clear profiles (download managers) before the io_thread_.
180 profile_manager_.reset();
181
182 // Debugger must be cleaned up before IO thread and NotificationService.
183 debugger_wrapper_ = NULL;
184
185 if (resource_dispatcher_host_.get()) {
186 // Need to tell Safe Browsing Service that the IO thread is going away
187 // since it cached a pointer to it.
188 if (resource_dispatcher_host()->safe_browsing_service())
189 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
190
191 // Cancel pending requests and prevent new requests.
192 resource_dispatcher_host()->Shutdown();
193 }
194
[email protected]4c3cd7412009-04-22 17:56:06195#if defined(OS_LINUX)
196 // The IO thread must outlive the BACKGROUND_X11 thread.
197 background_x11_thread_.reset();
198#endif
199
initial.commit09911bf2008-07-26 23:55:29200 // Need to stop io_thread_ before resource_dispatcher_host_, since
201 // io_thread_ may still deref ResourceDispatcherHost and handle resource
202 // request before going away.
[email protected]48ca9012009-08-11 21:38:54203 ResetIOThread();
initial.commit09911bf2008-07-26 23:55:29204
205 // Clean up state that lives on the file_thread_ before it goes away.
206 if (resource_dispatcher_host_.get()) {
207 resource_dispatcher_host()->download_file_manager()->Shutdown();
208 resource_dispatcher_host()->save_file_manager()->Shutdown();
209 }
210
211 // Need to stop the file_thread_ here to force it to process messages in its
212 // message loop from the previous call to shutdown the DownloadFileManager,
213 // SaveFileManager and SessionService.
214 file_thread_.reset();
215
216 // With the file_thread_ flushed, we can release any icon resources.
217 icon_manager_.reset();
218
219 // Need to destroy ResourceDispatcherHost before PluginService and
220 // SafeBrowsingService, since it caches a pointer to it.
221 resource_dispatcher_host_.reset();
222
223 // Wait for the pending print jobs to finish.
224 print_job_manager_->OnQuit();
225 print_job_manager_.reset();
226
initial.commit09911bf2008-07-26 23:55:29227 // Now OK to destroy NotificationService.
228 main_notification_service_.reset();
229
230 g_browser_process = NULL;
231}
232
[email protected]295039bd2008-08-15 04:32:57233// Send a QuitTask to the given MessageLoop.
234static void PostQuit(MessageLoop* message_loop) {
235 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
236}
initial.commit09911bf2008-07-26 23:55:29237
238void BrowserProcessImpl::EndSession() {
[email protected]b112a4c2009-02-01 20:24:01239#if defined(OS_WIN)
[email protected]d65cab7a2008-08-12 01:25:41240 // Notify we are going away.
[email protected]b797e152009-01-23 16:06:14241 ::SetEvent(shutdown_event_->handle());
[email protected]b112a4c2009-02-01 20:24:01242#endif
[email protected]d65cab7a2008-08-12 01:25:41243
initial.commit09911bf2008-07-26 23:55:29244 // Mark all the profiles as clean.
245 ProfileManager* pm = profile_manager();
246 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
247 (*i)->MarkAsCleanShutdown();
248
249 // Tell the metrics service it was cleanly shutdown.
250 MetricsService* metrics = g_browser_process->metrics_service();
251 if (metrics && local_state()) {
252 metrics->RecordCleanShutdown();
253
254 metrics->RecordStartOfSessionEnd();
255
256 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36257 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29258 }
259
260 // We must write that the profile and metrics service shutdown cleanly,
261 // otherwise on startup we'll think we crashed. So we block until done and
262 // then proceed with normal shutdown.
263 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57264 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29265 MessageLoop::current()->Run();
266}
267
268printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
269 // TODO(abarth): DCHECK(CalledOnValidThread());
270 // See <http://b/1287209>.
271 // print_job_manager_ is initialized in the constructor and destroyed in the
272 // destructor, so it should always be valid.
273 DCHECK(print_job_manager_.get());
274 return print_job_manager_.get();
275}
276
[email protected]d70539de2009-06-24 22:17:06277const std::string& BrowserProcessImpl::GetApplicationLocale() {
initial.commit09911bf2008-07-26 23:55:29278 DCHECK(CalledOnValidThread());
279 if (locale_.empty()) {
[email protected]d70539de2009-06-24 22:17:06280 locale_ = l10n_util::GetApplicationLocale(
281 local_state()->GetString(prefs::kApplicationLocale));
initial.commit09911bf2008-07-26 23:55:29282 }
283 return locale_;
284}
285
286void BrowserProcessImpl::CreateResourceDispatcherHost() {
287 DCHECK(!created_resource_dispatcher_host_ &&
288 resource_dispatcher_host_.get() == NULL);
289 created_resource_dispatcher_host_ = true;
290
291 resource_dispatcher_host_.reset(
292 new ResourceDispatcherHost(io_thread()->message_loop()));
293 resource_dispatcher_host_->Initialize();
294}
295
296void BrowserProcessImpl::CreateMetricsService() {
297 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
298 created_metrics_service_ = true;
299
300 metrics_service_.reset(new MetricsService);
301}
302
303void BrowserProcessImpl::CreateIOThread() {
304 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
305 created_io_thread_ = true;
306
307 // Prior to starting the io thread, we create the plugin service as
308 // it is predominantly used from the io thread, but must be created
309 // on the main thread. The service ctor is inexpensive and does not
310 // invoke the io_thread() accessor.
311 PluginService::GetInstance();
312
[email protected]4c3cd7412009-04-22 17:56:06313#if defined(OS_LINUX)
314 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
315 // we start it now.
316 scoped_ptr<base::Thread> background_x11_thread(
317 new BrowserProcessSubThread(ChromeThread::BACKGROUND_X11));
318 if (!background_x11_thread->Start())
319 return;
320 background_x11_thread_.swap(background_x11_thread);
321#endif
322
[email protected]48ca9012009-08-11 21:38:54323 scoped_ptr<base::Thread> thread(new IOThread);
[email protected]ab820df2008-08-26 05:55:10324 base::Thread::Options options;
325 options.message_loop_type = MessageLoop::TYPE_IO;
326 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29327 return;
328 io_thread_.swap(thread);
329}
330
[email protected]48ca9012009-08-11 21:38:54331void BrowserProcessImpl::ResetIOThread() {
332 if (io_thread_.get()) {
333 io_thread_->message_loop()->PostTask(FROM_HERE,
334 NewRunnableFunction(CleanupOnIOThread));
335 }
336 io_thread_.reset();
337}
338
339// static
340void BrowserProcessImpl::CleanupOnIOThread() {
341 // Shutdown DNS prefetching now to ensure that network stack objects
342 // living on the IO thread get destroyed before the IO thread goes away.
343 chrome_browser_net::EnsureDnsPrefetchShutdown();
344 // TODO(eroman): can this be merged into IOThread::CleanUp() ?
345}
346
initial.commit09911bf2008-07-26 23:55:29347void BrowserProcessImpl::CreateFileThread() {
348 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
349 created_file_thread_ = true;
350
[email protected]ab820df2008-08-26 05:55:10351 scoped_ptr<base::Thread> thread(
352 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06353 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39354#if defined(OS_WIN)
355 // On Windows, the FILE thread needs to be have a UI message loop which pumps
356 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06357 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39358#else
359 options.message_loop_type = MessageLoop::TYPE_IO;
360#endif
[email protected]a1db3842008-09-17 22:04:06361 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29362 return;
363 file_thread_.swap(thread);
364}
365
366void BrowserProcessImpl::CreateDBThread() {
367 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
368 created_db_thread_ = true;
369
[email protected]ab820df2008-08-26 05:55:10370 scoped_ptr<base::Thread> thread(
371 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29372 if (!thread->Start())
373 return;
374 db_thread_.swap(thread);
375}
376
377void BrowserProcessImpl::CreateProfileManager() {
378 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
379 created_profile_manager_ = true;
380
381 profile_manager_.reset(new ProfileManager());
382}
383
384void BrowserProcessImpl::CreateLocalState() {
385 DCHECK(!created_local_state_ && local_state_.get() == NULL);
386 created_local_state_ = true;
387
[email protected]b9636002009-03-04 00:05:25388 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29389 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]6faa0e0d2009-04-28 06:50:36390 local_state_.reset(new PrefService(local_state_path, file_thread()));
initial.commit09911bf2008-07-26 23:55:29391}
392
393void BrowserProcessImpl::InitBrokerServices(
394 sandbox::BrokerServices* broker_services) {
395 DCHECK(!initialized_broker_services_ && broker_services_ == NULL);
396 broker_services->Init();
397 initialized_broker_services_ = true;
398 broker_services_ = broker_services;
399}
400
401void BrowserProcessImpl::CreateIconManager() {
402 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
403 created_icon_manager_ = true;
404 icon_manager_.reset(new IconManager);
405}
406
407void BrowserProcessImpl::CreateDebuggerWrapper(int port) {
408 DCHECK(debugger_wrapper_.get() == NULL);
409 created_debugger_wrapper_ = true;
410
411 debugger_wrapper_ = new DebuggerWrapper(port);
412}
413
[email protected]40ecc902009-03-16 13:42:47414void BrowserProcessImpl::CreateDevToolsManager() {
[email protected]73ee01522009-06-05 10:13:44415 DCHECK(devtools_manager_.get() == NULL);
[email protected]40ecc902009-03-16 13:42:47416 created_devtools_manager_ = true;
[email protected]73ee01522009-06-05 10:13:44417 devtools_manager_ = new DevToolsManager();
[email protected]40ecc902009-03-16 13:42:47418}
419
initial.commit09911bf2008-07-26 23:55:29420void BrowserProcessImpl::CreateGoogleURLTracker() {
421 DCHECK(google_url_tracker_.get() == NULL);
422 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
423 google_url_tracker_.swap(google_url_tracker);
424}