blob: 4730c932bd8efbc3b1393eb1a951149e1c88ad1a [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]ef3a0f22009-03-07 01:06:3826#include "chrome/common/clipboard_service.h"
[email protected]70dd2b32009-02-13 01:43:5927#include "chrome/common/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2928#include "chrome/common/notification_service.h"
29#include "chrome/common/pref_names.h"
30#include "chrome/common/pref_service.h"
[email protected]b112a4c2009-02-01 20:24:0131
32#if defined(OS_WIN)
33#include "chrome/browser/automation/automation_provider_list.h"
[email protected]b112a4c2009-02-01 20:24:0134#include "chrome/browser/icon_manager.h"
[email protected]b112a4c2009-02-01 20:24:0135#include "chrome/browser/printing/print_job_manager.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 }
initial.commit09911bf2008-07-26 23:55:29130 }
[email protected]b797e152009-01-23 16:06:14131 shutdown_event_.reset(new base::WaitableEvent(true, false));
initial.commit09911bf2008-07-26 23:55:29132}
133
134BrowserProcessImpl::~BrowserProcessImpl() {
135 // Delete the AutomationProviderList before NotificationService,
136 // since it may try to unregister notifications
137 // Both NotificationService and AutomationProvider are singleton instances in
138 // the BrowserProcess. Since AutomationProvider may have some active
139 // notification observers, it is essential that it gets destroyed before the
140 // NotificationService. NotificationService won't be destroyed until after
141 // this destructor is run.
142 automation_provider_list_.reset();
143
144 // We need to destroy the MetricsService and GoogleURLTracker before the
145 // io_thread_ gets destroyed, since both destructors can call the URLFetcher
146 // destructor, which does an InvokeLater operation on the IO thread. (The IO
147 // thread will handle that URLFetcher operation before going away.)
148 metrics_service_.reset();
149 google_url_tracker_.reset();
150
151 // Need to clear profiles (download managers) before the io_thread_.
152 profile_manager_.reset();
153
154 // Debugger must be cleaned up before IO thread and NotificationService.
155 debugger_wrapper_ = NULL;
156
157 if (resource_dispatcher_host_.get()) {
158 // Need to tell Safe Browsing Service that the IO thread is going away
159 // since it cached a pointer to it.
160 if (resource_dispatcher_host()->safe_browsing_service())
161 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
162
163 // Cancel pending requests and prevent new requests.
164 resource_dispatcher_host()->Shutdown();
165 }
166
[email protected]1933eb202009-02-19 18:23:25167 // Shutdown DNS prefetching now to ensure that network stack objects
168 // living on the IO thread get destroyed before the IO thread goes away.
169 io_thread_->message_loop()->PostTask(FROM_HERE,
[email protected]53c4c622009-02-23 19:22:13170 NewRunnableFunction(chrome_browser_net::EnsureDnsPrefetchShutdown));
[email protected]1933eb202009-02-19 18:23:25171
initial.commit09911bf2008-07-26 23:55:29172 // Need to stop io_thread_ before resource_dispatcher_host_, since
173 // io_thread_ may still deref ResourceDispatcherHost and handle resource
174 // request before going away.
175 io_thread_.reset();
176
177 // Clean up state that lives on the file_thread_ before it goes away.
178 if (resource_dispatcher_host_.get()) {
179 resource_dispatcher_host()->download_file_manager()->Shutdown();
180 resource_dispatcher_host()->save_file_manager()->Shutdown();
181 }
182
183 // Need to stop the file_thread_ here to force it to process messages in its
184 // message loop from the previous call to shutdown the DownloadFileManager,
185 // SaveFileManager and SessionService.
186 file_thread_.reset();
187
188 // With the file_thread_ flushed, we can release any icon resources.
189 icon_manager_.reset();
190
191 // Need to destroy ResourceDispatcherHost before PluginService and
192 // SafeBrowsingService, since it caches a pointer to it.
193 resource_dispatcher_host_.reset();
194
195 // Wait for the pending print jobs to finish.
196 print_job_manager_->OnQuit();
197 print_job_manager_.reset();
198
initial.commit09911bf2008-07-26 23:55:29199 // Now OK to destroy NotificationService.
200 main_notification_service_.reset();
201
202 g_browser_process = NULL;
203}
204
[email protected]295039bd2008-08-15 04:32:57205// Send a QuitTask to the given MessageLoop.
206static void PostQuit(MessageLoop* message_loop) {
207 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
208}
initial.commit09911bf2008-07-26 23:55:29209
210void BrowserProcessImpl::EndSession() {
[email protected]b112a4c2009-02-01 20:24:01211#if defined(OS_WIN)
[email protected]d65cab7a2008-08-12 01:25:41212 // Notify we are going away.
[email protected]b797e152009-01-23 16:06:14213 ::SetEvent(shutdown_event_->handle());
[email protected]b112a4c2009-02-01 20:24:01214#endif
[email protected]d65cab7a2008-08-12 01:25:41215
initial.commit09911bf2008-07-26 23:55:29216 // Mark all the profiles as clean.
217 ProfileManager* pm = profile_manager();
218 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
219 (*i)->MarkAsCleanShutdown();
220
221 // Tell the metrics service it was cleanly shutdown.
222 MetricsService* metrics = g_browser_process->metrics_service();
223 if (metrics && local_state()) {
224 metrics->RecordCleanShutdown();
225
226 metrics->RecordStartOfSessionEnd();
227
228 // MetricsService lazily writes to prefs, force it to write now.
229 local_state()->SavePersistentPrefs(file_thread());
230 }
231
232 // We must write that the profile and metrics service shutdown cleanly,
233 // otherwise on startup we'll think we crashed. So we block until done and
234 // then proceed with normal shutdown.
235 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57236 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29237 MessageLoop::current()->Run();
238}
239
240printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
241 // TODO(abarth): DCHECK(CalledOnValidThread());
242 // See <http://b/1287209>.
243 // print_job_manager_ is initialized in the constructor and destroyed in the
244 // destructor, so it should always be valid.
245 DCHECK(print_job_manager_.get());
246 return print_job_manager_.get();
247}
248
249const std::wstring& BrowserProcessImpl::GetApplicationLocale() {
250 DCHECK(CalledOnValidThread());
initial.commit09911bf2008-07-26 23:55:29251 if (locale_.empty()) {
252 locale_ = l10n_util::GetApplicationLocale(local_state()->GetString(
253 prefs::kApplicationLocale));
254 }
initial.commit09911bf2008-07-26 23:55:29255 return locale_;
256}
257
258void BrowserProcessImpl::CreateResourceDispatcherHost() {
259 DCHECK(!created_resource_dispatcher_host_ &&
260 resource_dispatcher_host_.get() == NULL);
261 created_resource_dispatcher_host_ = true;
262
263 resource_dispatcher_host_.reset(
264 new ResourceDispatcherHost(io_thread()->message_loop()));
265 resource_dispatcher_host_->Initialize();
266}
267
268void BrowserProcessImpl::CreateMetricsService() {
269 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
270 created_metrics_service_ = true;
271
272 metrics_service_.reset(new MetricsService);
273}
274
275void BrowserProcessImpl::CreateIOThread() {
276 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
277 created_io_thread_ = true;
278
279 // Prior to starting the io thread, we create the plugin service as
280 // it is predominantly used from the io thread, but must be created
281 // on the main thread. The service ctor is inexpensive and does not
282 // invoke the io_thread() accessor.
283 PluginService::GetInstance();
284
[email protected]ab820df2008-08-26 05:55:10285 scoped_ptr<base::Thread> thread(
286 new BrowserProcessSubThread(ChromeThread::IO));
287 base::Thread::Options options;
288 options.message_loop_type = MessageLoop::TYPE_IO;
289 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29290 return;
291 io_thread_.swap(thread);
292}
293
294void BrowserProcessImpl::CreateFileThread() {
295 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
296 created_file_thread_ = true;
297
[email protected]ab820df2008-08-26 05:55:10298 scoped_ptr<base::Thread> thread(
299 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06300 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39301#if defined(OS_WIN)
302 // On Windows, the FILE thread needs to be have a UI message loop which pumps
303 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06304 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39305#else
306 options.message_loop_type = MessageLoop::TYPE_IO;
307#endif
[email protected]a1db3842008-09-17 22:04:06308 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29309 return;
310 file_thread_.swap(thread);
311}
312
313void BrowserProcessImpl::CreateDBThread() {
314 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
315 created_db_thread_ = true;
316
[email protected]ab820df2008-08-26 05:55:10317 scoped_ptr<base::Thread> thread(
318 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29319 if (!thread->Start())
320 return;
321 db_thread_.swap(thread);
322}
323
324void BrowserProcessImpl::CreateProfileManager() {
325 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
326 created_profile_manager_ = true;
327
328 profile_manager_.reset(new ProfileManager());
329}
330
331void BrowserProcessImpl::CreateLocalState() {
332 DCHECK(!created_local_state_ && local_state_.get() == NULL);
333 created_local_state_ = true;
334
[email protected]b9636002009-03-04 00:05:25335 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29336 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
337 local_state_.reset(new PrefService(local_state_path));
338}
339
340void BrowserProcessImpl::InitBrokerServices(
341 sandbox::BrokerServices* broker_services) {
342 DCHECK(!initialized_broker_services_ && broker_services_ == NULL);
343 broker_services->Init();
344 initialized_broker_services_ = true;
345 broker_services_ = broker_services;
346}
347
348void BrowserProcessImpl::CreateIconManager() {
349 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
350 created_icon_manager_ = true;
351 icon_manager_.reset(new IconManager);
352}
353
354void BrowserProcessImpl::CreateDebuggerWrapper(int port) {
355 DCHECK(debugger_wrapper_.get() == NULL);
356 created_debugger_wrapper_ = true;
357
358 debugger_wrapper_ = new DebuggerWrapper(port);
359}
360
361void BrowserProcessImpl::CreateAcceleratorHandler() {
[email protected]b112a4c2009-02-01 20:24:01362#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29363 DCHECK(accelerator_handler_.get() == NULL);
[email protected]c2dacc92008-10-16 23:51:38364 scoped_ptr<views::AcceleratorHandler> accelerator_handler(
365 new views::AcceleratorHandler);
initial.commit09911bf2008-07-26 23:55:29366 accelerator_handler_.swap(accelerator_handler);
[email protected]b112a4c2009-02-01 20:24:01367#else
368 // TODO(port): remove this completely, it has no business being here.
369#endif
initial.commit09911bf2008-07-26 23:55:29370}
371
372void BrowserProcessImpl::CreateGoogleURLTracker() {
373 DCHECK(google_url_tracker_.get() == NULL);
374 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
375 google_url_tracker_.swap(google_url_tracker);
376}