blob: 7a0932027026522988c25eea1cef28e50b3ddd78 [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]dc6f4962009-02-13 01:25:5020#include "chrome/browser/metrics/metrics_service.h"
[email protected]1933eb202009-02-19 18:23:2521#include "chrome/browser/net/dns_global.h"
[email protected]fd49e2d2009-02-20 17:21:3022#include "chrome/browser/plugin_service.h"
initial.commit09911bf2008-07-26 23:55:2923#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2624#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]81218f42009-02-05 18:48:0825#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
26#include "chrome/browser/safe_browsing/safe_browsing_service.h"
initial.commit09911bf2008-07-26 23:55:2927#include "chrome/common/chrome_paths.h"
28#include "chrome/common/chrome_switches.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"
[email protected]08682a92009-03-17 02:54:0237#include "chrome/views/focus/view_storage.h"
[email protected]2a2576e92009-03-17 00:36:1238#include "chrome/views/widget/accelerator_handler.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;
[email protected]1b8d02f12009-05-05 04:14:11115 clipboard_.reset(new Clipboard);
initial.commit09911bf2008-07-26 23:55:29116 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 }
132 }
[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
[email protected]4c3cd7412009-04-22 17:56:06174#if defined(OS_LINUX)
175 // The IO thread must outlive the BACKGROUND_X11 thread.
176 background_x11_thread_.reset();
177#endif
178
initial.commit09911bf2008-07-26 23:55:29179 // Need to stop io_thread_ before resource_dispatcher_host_, since
180 // io_thread_ may still deref ResourceDispatcherHost and handle resource
181 // request before going away.
182 io_thread_.reset();
183
184 // Clean up state that lives on the file_thread_ before it goes away.
185 if (resource_dispatcher_host_.get()) {
186 resource_dispatcher_host()->download_file_manager()->Shutdown();
187 resource_dispatcher_host()->save_file_manager()->Shutdown();
188 }
189
190 // Need to stop the file_thread_ here to force it to process messages in its
191 // message loop from the previous call to shutdown the DownloadFileManager,
192 // SaveFileManager and SessionService.
193 file_thread_.reset();
194
195 // With the file_thread_ flushed, we can release any icon resources.
196 icon_manager_.reset();
197
198 // Need to destroy ResourceDispatcherHost before PluginService and
199 // SafeBrowsingService, since it caches a pointer to it.
200 resource_dispatcher_host_.reset();
201
202 // Wait for the pending print jobs to finish.
203 print_job_manager_->OnQuit();
204 print_job_manager_.reset();
205
initial.commit09911bf2008-07-26 23:55:29206 // Now OK to destroy NotificationService.
207 main_notification_service_.reset();
208
209 g_browser_process = NULL;
210}
211
[email protected]295039bd2008-08-15 04:32:57212// Send a QuitTask to the given MessageLoop.
213static void PostQuit(MessageLoop* message_loop) {
214 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
215}
initial.commit09911bf2008-07-26 23:55:29216
217void BrowserProcessImpl::EndSession() {
[email protected]b112a4c2009-02-01 20:24:01218#if defined(OS_WIN)
[email protected]d65cab7a2008-08-12 01:25:41219 // Notify we are going away.
[email protected]b797e152009-01-23 16:06:14220 ::SetEvent(shutdown_event_->handle());
[email protected]b112a4c2009-02-01 20:24:01221#endif
[email protected]d65cab7a2008-08-12 01:25:41222
initial.commit09911bf2008-07-26 23:55:29223 // Mark all the profiles as clean.
224 ProfileManager* pm = profile_manager();
225 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
226 (*i)->MarkAsCleanShutdown();
227
228 // Tell the metrics service it was cleanly shutdown.
229 MetricsService* metrics = g_browser_process->metrics_service();
230 if (metrics && local_state()) {
231 metrics->RecordCleanShutdown();
232
233 metrics->RecordStartOfSessionEnd();
234
235 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36236 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29237 }
238
239 // We must write that the profile and metrics service shutdown cleanly,
240 // otherwise on startup we'll think we crashed. So we block until done and
241 // then proceed with normal shutdown.
242 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57243 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29244 MessageLoop::current()->Run();
245}
246
247printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
248 // TODO(abarth): DCHECK(CalledOnValidThread());
249 // See <http://b/1287209>.
250 // print_job_manager_ is initialized in the constructor and destroyed in the
251 // destructor, so it should always be valid.
252 DCHECK(print_job_manager_.get());
253 return print_job_manager_.get();
254}
255
256const std::wstring& BrowserProcessImpl::GetApplicationLocale() {
257 DCHECK(CalledOnValidThread());
258 if (locale_.empty()) {
259 locale_ = l10n_util::GetApplicationLocale(local_state()->GetString(
260 prefs::kApplicationLocale));
261 }
262 return locale_;
263}
264
265void BrowserProcessImpl::CreateResourceDispatcherHost() {
266 DCHECK(!created_resource_dispatcher_host_ &&
267 resource_dispatcher_host_.get() == NULL);
268 created_resource_dispatcher_host_ = true;
269
270 resource_dispatcher_host_.reset(
271 new ResourceDispatcherHost(io_thread()->message_loop()));
272 resource_dispatcher_host_->Initialize();
273}
274
275void BrowserProcessImpl::CreateMetricsService() {
276 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
277 created_metrics_service_ = true;
278
279 metrics_service_.reset(new MetricsService);
280}
281
282void BrowserProcessImpl::CreateIOThread() {
283 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
284 created_io_thread_ = true;
285
286 // Prior to starting the io thread, we create the plugin service as
287 // it is predominantly used from the io thread, but must be created
288 // on the main thread. The service ctor is inexpensive and does not
289 // invoke the io_thread() accessor.
290 PluginService::GetInstance();
291
[email protected]4c3cd7412009-04-22 17:56:06292#if defined(OS_LINUX)
293 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
294 // we start it now.
295 scoped_ptr<base::Thread> background_x11_thread(
296 new BrowserProcessSubThread(ChromeThread::BACKGROUND_X11));
297 if (!background_x11_thread->Start())
298 return;
299 background_x11_thread_.swap(background_x11_thread);
300#endif
301
[email protected]ab820df2008-08-26 05:55:10302 scoped_ptr<base::Thread> thread(
303 new BrowserProcessSubThread(ChromeThread::IO));
304 base::Thread::Options options;
305 options.message_loop_type = MessageLoop::TYPE_IO;
306 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29307 return;
308 io_thread_.swap(thread);
309}
310
311void BrowserProcessImpl::CreateFileThread() {
312 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
313 created_file_thread_ = true;
314
[email protected]ab820df2008-08-26 05:55:10315 scoped_ptr<base::Thread> thread(
316 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06317 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39318#if defined(OS_WIN)
319 // On Windows, the FILE thread needs to be have a UI message loop which pumps
320 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06321 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39322#else
323 options.message_loop_type = MessageLoop::TYPE_IO;
324#endif
[email protected]a1db3842008-09-17 22:04:06325 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29326 return;
327 file_thread_.swap(thread);
328}
329
330void BrowserProcessImpl::CreateDBThread() {
331 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
332 created_db_thread_ = true;
333
[email protected]ab820df2008-08-26 05:55:10334 scoped_ptr<base::Thread> thread(
335 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29336 if (!thread->Start())
337 return;
338 db_thread_.swap(thread);
339}
340
341void BrowserProcessImpl::CreateProfileManager() {
342 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
343 created_profile_manager_ = true;
344
345 profile_manager_.reset(new ProfileManager());
346}
347
348void BrowserProcessImpl::CreateLocalState() {
349 DCHECK(!created_local_state_ && local_state_.get() == NULL);
350 created_local_state_ = true;
351
[email protected]b9636002009-03-04 00:05:25352 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29353 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]6faa0e0d2009-04-28 06:50:36354 local_state_.reset(new PrefService(local_state_path, file_thread()));
initial.commit09911bf2008-07-26 23:55:29355}
356
357void BrowserProcessImpl::InitBrokerServices(
358 sandbox::BrokerServices* broker_services) {
359 DCHECK(!initialized_broker_services_ && broker_services_ == NULL);
360 broker_services->Init();
361 initialized_broker_services_ = true;
362 broker_services_ = broker_services;
363}
364
365void BrowserProcessImpl::CreateIconManager() {
366 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
367 created_icon_manager_ = true;
368 icon_manager_.reset(new IconManager);
369}
370
371void BrowserProcessImpl::CreateDebuggerWrapper(int port) {
372 DCHECK(debugger_wrapper_.get() == NULL);
373 created_debugger_wrapper_ = true;
374
375 debugger_wrapper_ = new DebuggerWrapper(port);
376}
377
[email protected]40ecc902009-03-16 13:42:47378void BrowserProcessImpl::CreateDevToolsManager() {
379 DCHECK(!devtools_manager_.get());
380 created_devtools_manager_ = true;
[email protected]7aa27fd2009-03-23 10:43:58381 devtools_manager_.reset(new DevToolsManager());
[email protected]40ecc902009-03-16 13:42:47382}
383
initial.commit09911bf2008-07-26 23:55:29384void BrowserProcessImpl::CreateAcceleratorHandler() {
[email protected]b112a4c2009-02-01 20:24:01385#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29386 DCHECK(accelerator_handler_.get() == NULL);
[email protected]c2dacc92008-10-16 23:51:38387 scoped_ptr<views::AcceleratorHandler> accelerator_handler(
388 new views::AcceleratorHandler);
initial.commit09911bf2008-07-26 23:55:29389 accelerator_handler_.swap(accelerator_handler);
[email protected]b112a4c2009-02-01 20:24:01390#else
391 // TODO(port): remove this completely, it has no business being here.
392#endif
initial.commit09911bf2008-07-26 23:55:29393}
394
395void BrowserProcessImpl::CreateGoogleURLTracker() {
396 DCHECK(google_url_tracker_.get() == NULL);
397 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
398 google_url_tracker_.swap(google_url_tracker);
399}