blob: d11ae1de8af2b63c1642eb58639f406bcec0158a [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]40ecc902009-03-16 13:42:4714#include "chrome/browser/debugger/devtools_manager.h"
[email protected]b7f05882009-02-22 01:21:5615#include "chrome/browser/download/download_file.h"
[email protected]5ba0a2c2009-02-19 01:19:3416#include "chrome/browser/download/save_file_manager.h"
initial.commit09911bf2008-07-26 23:55:2917#include "chrome/browser/google_url_tracker.h"
[email protected]dc6f4962009-02-13 01:25:5018#include "chrome/browser/metrics/metrics_service.h"
[email protected]1933eb202009-02-19 18:23:2519#include "chrome/browser/net/dns_global.h"
[email protected]fd49e2d2009-02-20 17:21:3020#include "chrome/browser/plugin_service.h"
initial.commit09911bf2008-07-26 23:55:2921#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2622#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]81218f42009-02-05 18:48:0823#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
24#include "chrome/browser/safe_browsing/safe_browsing_service.h"
initial.commit09911bf2008-07-26 23:55:2925#include "chrome/common/chrome_paths.h"
26#include "chrome/common/chrome_switches.h"
[email protected]ef3a0f22009-03-07 01:06:3827#include "chrome/common/clipboard_service.h"
[email protected]70dd2b32009-02-13 01:43:5928#include "chrome/common/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/common/notification_service.h"
30#include "chrome/common/pref_names.h"
31#include "chrome/common/pref_service.h"
[email protected]b112a4c2009-02-01 20:24:0132
33#if defined(OS_WIN)
34#include "chrome/browser/automation/automation_provider_list.h"
[email protected]b112a4c2009-02-01 20:24:0135#include "chrome/browser/icon_manager.h"
[email protected]b112a4c2009-02-01 20:24:0136#include "chrome/browser/printing/print_job_manager.h"
initial.commit09911bf2008-07-26 23:55:2937#include "chrome/views/accelerator_handler.h"
38#include "chrome/views/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
61 ~BrowserProcessSubThread() {
62 // 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
95} // namespace
96
[email protected]bb975362009-01-21 01:00:2297BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
initial.commit09911bf2008-07-26 23:55:2998 : created_resource_dispatcher_host_(false),
99 created_metrics_service_(false),
100 created_io_thread_(false),
101 created_file_thread_(false),
102 created_db_thread_(false),
103 created_profile_manager_(false),
104 created_local_state_(false),
initial.commit09911bf2008-07-26 23:55:29105 initialized_broker_services_(false),
initial.commit09911bf2008-07-26 23:55:29106 broker_services_(NULL),
[email protected]b112a4c2009-02-01 20:24:01107 created_icon_manager_(false),
108 created_debugger_wrapper_(false),
[email protected]40ecc902009-03-16 13:42:47109 created_devtools_manager_(false),
initial.commit09911bf2008-07-26 23:55:29110 module_ref_count_(0),
[email protected]1b2db1a2008-08-08 17:46:13111 memory_model_(MEDIUM_MEMORY_MODEL),
112 checked_for_new_frames_(false),
113 using_new_frames_(false) {
initial.commit09911bf2008-07-26 23:55:29114 g_browser_process = this;
115 clipboard_service_.reset(new ClipboardService);
116 main_notification_service_.reset(new NotificationService);
117
118 // Must be created after the NotificationService.
119 print_job_manager_.reset(new printing::PrintJobManager);
120
121 // Configure the browser memory model.
122 if (command_line.HasSwitch(switches::kMemoryModel)) {
123 std::wstring model = command_line.GetSwitchValue(switches::kMemoryModel);
124 if (!model.empty()) {
125 if (model == L"high")
126 memory_model_ = HIGH_MEMORY_MODEL;
127 else if (model == L"low")
128 memory_model_ = LOW_MEMORY_MODEL;
129 else if (model == L"medium")
130 memory_model_ = MEDIUM_MEMORY_MODEL;
131 }
initial.commit09911bf2008-07-26 23:55:29132 }
[email protected]b797e152009-01-23 16:06:14133 shutdown_event_.reset(new base::WaitableEvent(true, false));
initial.commit09911bf2008-07-26 23:55:29134}
135
136BrowserProcessImpl::~BrowserProcessImpl() {
137 // Delete the AutomationProviderList before NotificationService,
138 // since it may try to unregister notifications
139 // Both NotificationService and AutomationProvider are singleton instances in
140 // the BrowserProcess. Since AutomationProvider may have some active
141 // notification observers, it is essential that it gets destroyed before the
142 // NotificationService. NotificationService won't be destroyed until after
143 // this destructor is run.
144 automation_provider_list_.reset();
145
146 // We need to destroy the MetricsService and GoogleURLTracker before the
147 // io_thread_ gets destroyed, since both destructors can call the URLFetcher
148 // destructor, which does an InvokeLater operation on the IO thread. (The IO
149 // thread will handle that URLFetcher operation before going away.)
150 metrics_service_.reset();
151 google_url_tracker_.reset();
152
153 // Need to clear profiles (download managers) before the io_thread_.
154 profile_manager_.reset();
155
156 // Debugger must be cleaned up before IO thread and NotificationService.
157 debugger_wrapper_ = NULL;
158
159 if (resource_dispatcher_host_.get()) {
160 // Need to tell Safe Browsing Service that the IO thread is going away
161 // since it cached a pointer to it.
162 if (resource_dispatcher_host()->safe_browsing_service())
163 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
164
165 // Cancel pending requests and prevent new requests.
166 resource_dispatcher_host()->Shutdown();
167 }
168
[email protected]1933eb202009-02-19 18:23:25169 // Shutdown DNS prefetching now to ensure that network stack objects
170 // living on the IO thread get destroyed before the IO thread goes away.
171 io_thread_->message_loop()->PostTask(FROM_HERE,
[email protected]53c4c622009-02-23 19:22:13172 NewRunnableFunction(chrome_browser_net::EnsureDnsPrefetchShutdown));
[email protected]1933eb202009-02-19 18:23:25173
initial.commit09911bf2008-07-26 23:55:29174 // Need to stop io_thread_ before resource_dispatcher_host_, since
175 // io_thread_ may still deref ResourceDispatcherHost and handle resource
176 // request before going away.
177 io_thread_.reset();
178
179 // Clean up state that lives on the file_thread_ before it goes away.
180 if (resource_dispatcher_host_.get()) {
181 resource_dispatcher_host()->download_file_manager()->Shutdown();
182 resource_dispatcher_host()->save_file_manager()->Shutdown();
183 }
184
185 // Need to stop the file_thread_ here to force it to process messages in its
186 // message loop from the previous call to shutdown the DownloadFileManager,
187 // SaveFileManager and SessionService.
188 file_thread_.reset();
189
190 // With the file_thread_ flushed, we can release any icon resources.
191 icon_manager_.reset();
192
193 // Need to destroy ResourceDispatcherHost before PluginService and
194 // SafeBrowsingService, since it caches a pointer to it.
195 resource_dispatcher_host_.reset();
196
197 // Wait for the pending print jobs to finish.
198 print_job_manager_->OnQuit();
199 print_job_manager_.reset();
200
initial.commit09911bf2008-07-26 23:55:29201 // Now OK to destroy NotificationService.
202 main_notification_service_.reset();
203
204 g_browser_process = NULL;
205}
206
[email protected]295039bd2008-08-15 04:32:57207// Send a QuitTask to the given MessageLoop.
208static void PostQuit(MessageLoop* message_loop) {
209 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
210}
initial.commit09911bf2008-07-26 23:55:29211
212void BrowserProcessImpl::EndSession() {
[email protected]b112a4c2009-02-01 20:24:01213#if defined(OS_WIN)
[email protected]d65cab7a2008-08-12 01:25:41214 // Notify we are going away.
[email protected]b797e152009-01-23 16:06:14215 ::SetEvent(shutdown_event_->handle());
[email protected]b112a4c2009-02-01 20:24:01216#endif
[email protected]d65cab7a2008-08-12 01:25:41217
initial.commit09911bf2008-07-26 23:55:29218 // Mark all the profiles as clean.
219 ProfileManager* pm = profile_manager();
220 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
221 (*i)->MarkAsCleanShutdown();
222
223 // Tell the metrics service it was cleanly shutdown.
224 MetricsService* metrics = g_browser_process->metrics_service();
225 if (metrics && local_state()) {
226 metrics->RecordCleanShutdown();
227
228 metrics->RecordStartOfSessionEnd();
229
230 // MetricsService lazily writes to prefs, force it to write now.
231 local_state()->SavePersistentPrefs(file_thread());
232 }
233
234 // We must write that the profile and metrics service shutdown cleanly,
235 // otherwise on startup we'll think we crashed. So we block until done and
236 // then proceed with normal shutdown.
237 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57238 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29239 MessageLoop::current()->Run();
240}
241
242printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
243 // TODO(abarth): DCHECK(CalledOnValidThread());
244 // See <http://b/1287209>.
245 // print_job_manager_ is initialized in the constructor and destroyed in the
246 // destructor, so it should always be valid.
247 DCHECK(print_job_manager_.get());
248 return print_job_manager_.get();
249}
250
251const std::wstring& BrowserProcessImpl::GetApplicationLocale() {
252 DCHECK(CalledOnValidThread());
initial.commit09911bf2008-07-26 23:55:29253 if (locale_.empty()) {
254 locale_ = l10n_util::GetApplicationLocale(local_state()->GetString(
255 prefs::kApplicationLocale));
256 }
initial.commit09911bf2008-07-26 23:55:29257 return locale_;
258}
259
260void BrowserProcessImpl::CreateResourceDispatcherHost() {
261 DCHECK(!created_resource_dispatcher_host_ &&
262 resource_dispatcher_host_.get() == NULL);
263 created_resource_dispatcher_host_ = true;
264
265 resource_dispatcher_host_.reset(
266 new ResourceDispatcherHost(io_thread()->message_loop()));
267 resource_dispatcher_host_->Initialize();
268}
269
270void BrowserProcessImpl::CreateMetricsService() {
271 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
272 created_metrics_service_ = true;
273
274 metrics_service_.reset(new MetricsService);
275}
276
277void BrowserProcessImpl::CreateIOThread() {
278 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
279 created_io_thread_ = true;
280
281 // Prior to starting the io thread, we create the plugin service as
282 // it is predominantly used from the io thread, but must be created
283 // on the main thread. The service ctor is inexpensive and does not
284 // invoke the io_thread() accessor.
285 PluginService::GetInstance();
286
[email protected]ab820df2008-08-26 05:55:10287 scoped_ptr<base::Thread> thread(
288 new BrowserProcessSubThread(ChromeThread::IO));
289 base::Thread::Options options;
290 options.message_loop_type = MessageLoop::TYPE_IO;
291 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29292 return;
293 io_thread_.swap(thread);
294}
295
296void BrowserProcessImpl::CreateFileThread() {
297 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
298 created_file_thread_ = true;
299
[email protected]ab820df2008-08-26 05:55:10300 scoped_ptr<base::Thread> thread(
301 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06302 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39303#if defined(OS_WIN)
304 // On Windows, the FILE thread needs to be have a UI message loop which pumps
305 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06306 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39307#else
308 options.message_loop_type = MessageLoop::TYPE_IO;
309#endif
[email protected]a1db3842008-09-17 22:04:06310 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29311 return;
312 file_thread_.swap(thread);
313}
314
315void BrowserProcessImpl::CreateDBThread() {
316 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
317 created_db_thread_ = true;
318
[email protected]ab820df2008-08-26 05:55:10319 scoped_ptr<base::Thread> thread(
320 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29321 if (!thread->Start())
322 return;
323 db_thread_.swap(thread);
324}
325
326void BrowserProcessImpl::CreateProfileManager() {
327 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
328 created_profile_manager_ = true;
329
330 profile_manager_.reset(new ProfileManager());
331}
332
333void BrowserProcessImpl::CreateLocalState() {
334 DCHECK(!created_local_state_ && local_state_.get() == NULL);
335 created_local_state_ = true;
336
[email protected]b9636002009-03-04 00:05:25337 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29338 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
339 local_state_.reset(new PrefService(local_state_path));
340}
341
342void BrowserProcessImpl::InitBrokerServices(
343 sandbox::BrokerServices* broker_services) {
344 DCHECK(!initialized_broker_services_ && broker_services_ == NULL);
345 broker_services->Init();
346 initialized_broker_services_ = true;
347 broker_services_ = broker_services;
348}
349
350void BrowserProcessImpl::CreateIconManager() {
351 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
352 created_icon_manager_ = true;
353 icon_manager_.reset(new IconManager);
354}
355
356void BrowserProcessImpl::CreateDebuggerWrapper(int port) {
357 DCHECK(debugger_wrapper_.get() == NULL);
358 created_debugger_wrapper_ = true;
359
360 debugger_wrapper_ = new DebuggerWrapper(port);
361}
362
[email protected]40ecc902009-03-16 13:42:47363void BrowserProcessImpl::CreateDevToolsManager() {
364 DCHECK(!devtools_manager_.get());
365 created_devtools_manager_ = true;
366 devtools_manager_.reset(new DevToolsManager());
367}
368
initial.commit09911bf2008-07-26 23:55:29369void BrowserProcessImpl::CreateAcceleratorHandler() {
[email protected]b112a4c2009-02-01 20:24:01370#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29371 DCHECK(accelerator_handler_.get() == NULL);
[email protected]c2dacc92008-10-16 23:51:38372 scoped_ptr<views::AcceleratorHandler> accelerator_handler(
373 new views::AcceleratorHandler);
initial.commit09911bf2008-07-26 23:55:29374 accelerator_handler_.swap(accelerator_handler);
[email protected]b112a4c2009-02-01 20:24:01375#else
376 // TODO(port): remove this completely, it has no business being here.
377#endif
initial.commit09911bf2008-07-26 23:55:29378}
379
380void BrowserProcessImpl::CreateGoogleURLTracker() {
381 DCHECK(google_url_tracker_.get() == NULL);
382 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
383 google_url_tracker_.swap(google_url_tracker);
384}