blob: 1e0d956f93adf59fd556ada5a10d00fe6c072335 [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
7#include "base/command_line.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/path_service.h"
[email protected]ac262c9f2008-10-19 17:45:219#include "base/thread.h"
[email protected]1c4947f2009-01-15 22:25:1110#include "base/waitable_event.h"
[email protected]ac262c9f2008-10-19 17:45:2111#include "chrome/browser/browser_trial.h"
initial.commit09911bf2008-07-26 23:55:2912#include "chrome/browser/chrome_thread.h"
[email protected]4ab4b0f2009-02-10 18:54:5013#include "chrome/browser/debugger/debugger_wrapper.h"
[email protected]b7f05882009-02-22 01:21:5614#include "chrome/browser/download/download_file.h"
[email protected]5ba0a2c2009-02-19 01:19:3415#include "chrome/browser/download/save_file_manager.h"
initial.commit09911bf2008-07-26 23:55:2916#include "chrome/browser/google_url_tracker.h"
[email protected]dc6f4962009-02-13 01:25:5017#include "chrome/browser/metrics/metrics_service.h"
[email protected]1933eb202009-02-19 18:23:2518#include "chrome/browser/net/dns_global.h"
[email protected]fd49e2d2009-02-20 17:21:3019#include "chrome/browser/plugin_service.h"
initial.commit09911bf2008-07-26 23:55:2920#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2621#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]81218f42009-02-05 18:48:0822#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
23#include "chrome/browser/safe_browsing/safe_browsing_service.h"
initial.commit09911bf2008-07-26 23:55:2924#include "chrome/common/chrome_paths.h"
25#include "chrome/common/chrome_switches.h"
[email protected]70dd2b32009-02-13 01:43:5926#include "chrome/common/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2927#include "chrome/common/notification_service.h"
28#include "chrome/common/pref_names.h"
29#include "chrome/common/pref_service.h"
[email protected]b112a4c2009-02-01 20:24:0130
31#if defined(OS_WIN)
32#include "chrome/browser/automation/automation_provider_list.h"
[email protected]b112a4c2009-02-01 20:24:0133#include "chrome/browser/icon_manager.h"
[email protected]b112a4c2009-02-01 20:24:0134#include "chrome/browser/printing/print_job_manager.h"
[email protected]b112a4c2009-02-01 20:24:0135#include "chrome/common/clipboard_service.h"
initial.commit09911bf2008-07-26 23:55:2936#include "chrome/views/accelerator_handler.h"
37#include "chrome/views/view_storage.h"
[email protected]81218f42009-02-05 18:48:0838#elif defined(OS_POSIX)
39// TODO(port): Remove the temporary scaffolding as we port the above headers.
40#include "chrome/common/temp_scaffolding_stubs.h"
[email protected]b112a4c2009-02-01 20:24:0141#endif
initial.commit09911bf2008-07-26 23:55:2942
43namespace {
44
45// ----------------------------------------------------------------------------
46// BrowserProcessSubThread
47//
48// This simple thread object is used for the specialized threads that the
49// BrowserProcess spins up.
50//
51// Applications must initialize the COM library before they can call
52// COM library functions other than CoGetMalloc and memory allocation
53// functions, so this class initializes COM for those users.
54class BrowserProcessSubThread : public ChromeThread {
55 public:
56 explicit BrowserProcessSubThread(ChromeThread::ID identifier)
57 : ChromeThread(identifier) {
58 }
59
60 ~BrowserProcessSubThread() {
61 // We cannot rely on our base class to stop the thread since we want our
62 // CleanUp function to run.
63 Stop();
64 }
65
66 protected:
67 virtual void Init() {
[email protected]b112a4c2009-02-01 20:24:0168#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2969 // Initializes the COM library on the current thread.
70 CoInitialize(NULL);
[email protected]b112a4c2009-02-01 20:24:0171#endif
initial.commit09911bf2008-07-26 23:55:2972
73 notification_service_ = new NotificationService;
74 }
75
76 virtual void CleanUp() {
77 delete notification_service_;
78 notification_service_ = NULL;
79
[email protected]b112a4c2009-02-01 20:24:0180#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2981 // Closes the COM library on the current thread. CoInitialize must
82 // be balanced by a corresponding call to CoUninitialize.
83 CoUninitialize();
[email protected]b112a4c2009-02-01 20:24:0184#endif
initial.commit09911bf2008-07-26 23:55:2985 }
86
87 private:
88 // Each specialized thread has its own notification service.
89 // Note: We don't use scoped_ptr because the destructor runs on the wrong
90 // thread.
91 NotificationService* notification_service_;
92};
93
94} // namespace
95
[email protected]bb975362009-01-21 01:00:2296BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
initial.commit09911bf2008-07-26 23:55:2997 : created_resource_dispatcher_host_(false),
98 created_metrics_service_(false),
99 created_io_thread_(false),
100 created_file_thread_(false),
101 created_db_thread_(false),
102 created_profile_manager_(false),
103 created_local_state_(false),
initial.commit09911bf2008-07-26 23:55:29104 initialized_broker_services_(false),
initial.commit09911bf2008-07-26 23:55:29105 broker_services_(NULL),
[email protected]b112a4c2009-02-01 20:24:01106 created_icon_manager_(false),
107 created_debugger_wrapper_(false),
initial.commit09911bf2008-07-26 23:55:29108 module_ref_count_(0),
[email protected]1b2db1a2008-08-08 17:46:13109 memory_model_(MEDIUM_MEMORY_MODEL),
110 checked_for_new_frames_(false),
111 using_new_frames_(false) {
initial.commit09911bf2008-07-26 23:55:29112 g_browser_process = this;
113 clipboard_service_.reset(new ClipboardService);
114 main_notification_service_.reset(new NotificationService);
115
116 // Must be created after the NotificationService.
117 print_job_manager_.reset(new printing::PrintJobManager);
118
119 // Configure the browser memory model.
120 if (command_line.HasSwitch(switches::kMemoryModel)) {
121 std::wstring model = command_line.GetSwitchValue(switches::kMemoryModel);
122 if (!model.empty()) {
123 if (model == L"high")
124 memory_model_ = HIGH_MEMORY_MODEL;
125 else if (model == L"low")
126 memory_model_ = LOW_MEMORY_MODEL;
127 else if (model == L"medium")
128 memory_model_ = MEDIUM_MEMORY_MODEL;
129 }
[email protected]ac262c9f2008-10-19 17:45:21130 } else {
131 // Randomly choose what memory model to use.
132 const double probability = 0.5;
133 FieldTrial* trial(new FieldTrial(BrowserTrial::kMemoryModelFieldTrial,
134 probability));
135 DCHECK(FieldTrialList::Find(BrowserTrial::kMemoryModelFieldTrial) == trial);
136 if (trial->boolean_value())
137 memory_model_ = HIGH_MEMORY_MODEL;
138 else
139 memory_model_ = MEDIUM_MEMORY_MODEL;
initial.commit09911bf2008-07-26 23:55:29140 }
141
[email protected]b797e152009-01-23 16:06:14142 shutdown_event_.reset(new base::WaitableEvent(true, false));
initial.commit09911bf2008-07-26 23:55:29143}
144
145BrowserProcessImpl::~BrowserProcessImpl() {
146 // Delete the AutomationProviderList before NotificationService,
147 // since it may try to unregister notifications
148 // Both NotificationService and AutomationProvider are singleton instances in
149 // the BrowserProcess. Since AutomationProvider may have some active
150 // notification observers, it is essential that it gets destroyed before the
151 // NotificationService. NotificationService won't be destroyed until after
152 // this destructor is run.
153 automation_provider_list_.reset();
154
155 // We need to destroy the MetricsService and GoogleURLTracker before the
156 // io_thread_ gets destroyed, since both destructors can call the URLFetcher
157 // destructor, which does an InvokeLater operation on the IO thread. (The IO
158 // thread will handle that URLFetcher operation before going away.)
159 metrics_service_.reset();
160 google_url_tracker_.reset();
161
162 // Need to clear profiles (download managers) before the io_thread_.
163 profile_manager_.reset();
164
165 // Debugger must be cleaned up before IO thread and NotificationService.
166 debugger_wrapper_ = NULL;
167
168 if (resource_dispatcher_host_.get()) {
169 // Need to tell Safe Browsing Service that the IO thread is going away
170 // since it cached a pointer to it.
171 if (resource_dispatcher_host()->safe_browsing_service())
172 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
173
174 // Cancel pending requests and prevent new requests.
175 resource_dispatcher_host()->Shutdown();
176 }
177
[email protected]1933eb202009-02-19 18:23:25178 // Shutdown DNS prefetching now to ensure that network stack objects
179 // living on the IO thread get destroyed before the IO thread goes away.
180 io_thread_->message_loop()->PostTask(FROM_HERE,
[email protected]53c4c622009-02-23 19:22:13181 NewRunnableFunction(chrome_browser_net::EnsureDnsPrefetchShutdown));
[email protected]1933eb202009-02-19 18:23:25182
initial.commit09911bf2008-07-26 23:55:29183 // Need to stop io_thread_ before resource_dispatcher_host_, since
184 // io_thread_ may still deref ResourceDispatcherHost and handle resource
185 // request before going away.
186 io_thread_.reset();
187
188 // Clean up state that lives on the file_thread_ before it goes away.
189 if (resource_dispatcher_host_.get()) {
190 resource_dispatcher_host()->download_file_manager()->Shutdown();
191 resource_dispatcher_host()->save_file_manager()->Shutdown();
192 }
193
194 // Need to stop the file_thread_ here to force it to process messages in its
195 // message loop from the previous call to shutdown the DownloadFileManager,
196 // SaveFileManager and SessionService.
197 file_thread_.reset();
198
199 // With the file_thread_ flushed, we can release any icon resources.
200 icon_manager_.reset();
201
202 // Need to destroy ResourceDispatcherHost before PluginService and
203 // SafeBrowsingService, since it caches a pointer to it.
204 resource_dispatcher_host_.reset();
205
206 // Wait for the pending print jobs to finish.
207 print_job_manager_->OnQuit();
208 print_job_manager_.reset();
209
[email protected]b112a4c2009-02-01 20:24:01210 // TODO(port): remove this completely from BrowserProcessImpl, it has no
211 // business being here.
212#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29213 // The ViewStorage needs to go before the NotificationService.
[email protected]c2dacc92008-10-16 23:51:38214 views::ViewStorage::DeleteSharedInstance();
[email protected]b112a4c2009-02-01 20:24:01215#endif
initial.commit09911bf2008-07-26 23:55:29216
217 // Now OK to destroy NotificationService.
218 main_notification_service_.reset();
219
220 g_browser_process = NULL;
221}
222
[email protected]295039bd2008-08-15 04:32:57223// Send a QuitTask to the given MessageLoop.
224static void PostQuit(MessageLoop* message_loop) {
225 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
226}
initial.commit09911bf2008-07-26 23:55:29227
228void BrowserProcessImpl::EndSession() {
[email protected]b112a4c2009-02-01 20:24:01229#if defined(OS_WIN)
[email protected]d65cab7a2008-08-12 01:25:41230 // Notify we are going away.
[email protected]b797e152009-01-23 16:06:14231 ::SetEvent(shutdown_event_->handle());
[email protected]b112a4c2009-02-01 20:24:01232#endif
[email protected]d65cab7a2008-08-12 01:25:41233
initial.commit09911bf2008-07-26 23:55:29234 // Mark all the profiles as clean.
235 ProfileManager* pm = profile_manager();
236 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
237 (*i)->MarkAsCleanShutdown();
238
239 // Tell the metrics service it was cleanly shutdown.
240 MetricsService* metrics = g_browser_process->metrics_service();
241 if (metrics && local_state()) {
242 metrics->RecordCleanShutdown();
243
244 metrics->RecordStartOfSessionEnd();
245
246 // MetricsService lazily writes to prefs, force it to write now.
247 local_state()->SavePersistentPrefs(file_thread());
248 }
249
250 // We must write that the profile and metrics service shutdown cleanly,
251 // otherwise on startup we'll think we crashed. So we block until done and
252 // then proceed with normal shutdown.
253 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57254 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29255 MessageLoop::current()->Run();
256}
257
258printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
259 // TODO(abarth): DCHECK(CalledOnValidThread());
260 // See <http://b/1287209>.
261 // print_job_manager_ is initialized in the constructor and destroyed in the
262 // destructor, so it should always be valid.
263 DCHECK(print_job_manager_.get());
264 return print_job_manager_.get();
265}
266
267const std::wstring& BrowserProcessImpl::GetApplicationLocale() {
268 DCHECK(CalledOnValidThread());
initial.commit09911bf2008-07-26 23:55:29269 if (locale_.empty()) {
270 locale_ = l10n_util::GetApplicationLocale(local_state()->GetString(
271 prefs::kApplicationLocale));
272 }
initial.commit09911bf2008-07-26 23:55:29273 return locale_;
274}
275
276void BrowserProcessImpl::CreateResourceDispatcherHost() {
277 DCHECK(!created_resource_dispatcher_host_ &&
278 resource_dispatcher_host_.get() == NULL);
279 created_resource_dispatcher_host_ = true;
280
281 resource_dispatcher_host_.reset(
282 new ResourceDispatcherHost(io_thread()->message_loop()));
283 resource_dispatcher_host_->Initialize();
284}
285
286void BrowserProcessImpl::CreateMetricsService() {
287 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
288 created_metrics_service_ = true;
289
290 metrics_service_.reset(new MetricsService);
291}
292
293void BrowserProcessImpl::CreateIOThread() {
294 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
295 created_io_thread_ = true;
296
297 // Prior to starting the io thread, we create the plugin service as
298 // it is predominantly used from the io thread, but must be created
299 // on the main thread. The service ctor is inexpensive and does not
300 // invoke the io_thread() accessor.
301 PluginService::GetInstance();
302
[email protected]ab820df2008-08-26 05:55:10303 scoped_ptr<base::Thread> thread(
304 new BrowserProcessSubThread(ChromeThread::IO));
305 base::Thread::Options options;
306 options.message_loop_type = MessageLoop::TYPE_IO;
307 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29308 return;
309 io_thread_.swap(thread);
310}
311
312void BrowserProcessImpl::CreateFileThread() {
313 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
314 created_file_thread_ = true;
315
[email protected]ab820df2008-08-26 05:55:10316 scoped_ptr<base::Thread> thread(
317 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06318 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39319#if defined(OS_WIN)
320 // On Windows, the FILE thread needs to be have a UI message loop which pumps
321 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06322 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39323#else
324 options.message_loop_type = MessageLoop::TYPE_IO;
325#endif
[email protected]a1db3842008-09-17 22:04:06326 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29327 return;
328 file_thread_.swap(thread);
329}
330
331void BrowserProcessImpl::CreateDBThread() {
332 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
333 created_db_thread_ = true;
334
[email protected]ab820df2008-08-26 05:55:10335 scoped_ptr<base::Thread> thread(
336 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29337 if (!thread->Start())
338 return;
339 db_thread_.swap(thread);
340}
341
342void BrowserProcessImpl::CreateProfileManager() {
343 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
344 created_profile_manager_ = true;
345
346 profile_manager_.reset(new ProfileManager());
347}
348
349void BrowserProcessImpl::CreateLocalState() {
350 DCHECK(!created_local_state_ && local_state_.get() == NULL);
351 created_local_state_ = true;
352
353 std::wstring local_state_path;
354 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
355 local_state_.reset(new PrefService(local_state_path));
356}
357
358void BrowserProcessImpl::InitBrokerServices(
359 sandbox::BrokerServices* broker_services) {
360 DCHECK(!initialized_broker_services_ && broker_services_ == NULL);
361 broker_services->Init();
362 initialized_broker_services_ = true;
363 broker_services_ = broker_services;
364}
365
366void BrowserProcessImpl::CreateIconManager() {
367 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
368 created_icon_manager_ = true;
369 icon_manager_.reset(new IconManager);
370}
371
372void BrowserProcessImpl::CreateDebuggerWrapper(int port) {
373 DCHECK(debugger_wrapper_.get() == NULL);
374 created_debugger_wrapper_ = true;
375
376 debugger_wrapper_ = new DebuggerWrapper(port);
377}
378
379void BrowserProcessImpl::CreateAcceleratorHandler() {
[email protected]b112a4c2009-02-01 20:24:01380#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29381 DCHECK(accelerator_handler_.get() == NULL);
[email protected]c2dacc92008-10-16 23:51:38382 scoped_ptr<views::AcceleratorHandler> accelerator_handler(
383 new views::AcceleratorHandler);
initial.commit09911bf2008-07-26 23:55:29384 accelerator_handler_.swap(accelerator_handler);
[email protected]b112a4c2009-02-01 20:24:01385#else
386 // TODO(port): remove this completely, it has no business being here.
387#endif
initial.commit09911bf2008-07-26 23:55:29388}
389
390void BrowserProcessImpl::CreateGoogleURLTracker() {
391 DCHECK(google_url_tracker_.get() == NULL);
392 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
393 google_url_tracker_.swap(google_url_tracker);
394}
license.botbf09a502008-08-24 00:55:55395