blob: a0313746122cf00a0d82c6c6e644bcea058f4123 [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]2362e4f2009-05-08 00:34:0537#include "views/focus/view_storage.h"
38#include "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.
[email protected]1a3861a2009-05-06 17:15:42171 if (io_thread_.get()) {
172 io_thread_->message_loop()->PostTask(FROM_HERE,
173 NewRunnableFunction(chrome_browser_net::EnsureDnsPrefetchShutdown));
174 }
[email protected]1933eb202009-02-19 18:23:25175
[email protected]4c3cd7412009-04-22 17:56:06176#if defined(OS_LINUX)
177 // The IO thread must outlive the BACKGROUND_X11 thread.
178 background_x11_thread_.reset();
179#endif
180
initial.commit09911bf2008-07-26 23:55:29181 // Need to stop io_thread_ before resource_dispatcher_host_, since
182 // io_thread_ may still deref ResourceDispatcherHost and handle resource
183 // request before going away.
184 io_thread_.reset();
185
186 // Clean up state that lives on the file_thread_ before it goes away.
187 if (resource_dispatcher_host_.get()) {
188 resource_dispatcher_host()->download_file_manager()->Shutdown();
189 resource_dispatcher_host()->save_file_manager()->Shutdown();
190 }
191
192 // Need to stop the file_thread_ here to force it to process messages in its
193 // message loop from the previous call to shutdown the DownloadFileManager,
194 // SaveFileManager and SessionService.
195 file_thread_.reset();
196
197 // With the file_thread_ flushed, we can release any icon resources.
198 icon_manager_.reset();
199
200 // Need to destroy ResourceDispatcherHost before PluginService and
201 // SafeBrowsingService, since it caches a pointer to it.
202 resource_dispatcher_host_.reset();
203
204 // Wait for the pending print jobs to finish.
205 print_job_manager_->OnQuit();
206 print_job_manager_.reset();
207
initial.commit09911bf2008-07-26 23:55:29208 // Now OK to destroy NotificationService.
209 main_notification_service_.reset();
210
211 g_browser_process = NULL;
212}
213
[email protected]295039bd2008-08-15 04:32:57214// Send a QuitTask to the given MessageLoop.
215static void PostQuit(MessageLoop* message_loop) {
216 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
217}
initial.commit09911bf2008-07-26 23:55:29218
219void BrowserProcessImpl::EndSession() {
[email protected]b112a4c2009-02-01 20:24:01220#if defined(OS_WIN)
[email protected]d65cab7a2008-08-12 01:25:41221 // Notify we are going away.
[email protected]b797e152009-01-23 16:06:14222 ::SetEvent(shutdown_event_->handle());
[email protected]b112a4c2009-02-01 20:24:01223#endif
[email protected]d65cab7a2008-08-12 01:25:41224
initial.commit09911bf2008-07-26 23:55:29225 // Mark all the profiles as clean.
226 ProfileManager* pm = profile_manager();
227 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
228 (*i)->MarkAsCleanShutdown();
229
230 // Tell the metrics service it was cleanly shutdown.
231 MetricsService* metrics = g_browser_process->metrics_service();
232 if (metrics && local_state()) {
233 metrics->RecordCleanShutdown();
234
235 metrics->RecordStartOfSessionEnd();
236
237 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36238 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29239 }
240
241 // We must write that the profile and metrics service shutdown cleanly,
242 // otherwise on startup we'll think we crashed. So we block until done and
243 // then proceed with normal shutdown.
244 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57245 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29246 MessageLoop::current()->Run();
247}
248
249printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
250 // TODO(abarth): DCHECK(CalledOnValidThread());
251 // See <http://b/1287209>.
252 // print_job_manager_ is initialized in the constructor and destroyed in the
253 // destructor, so it should always be valid.
254 DCHECK(print_job_manager_.get());
255 return print_job_manager_.get();
256}
257
258const std::wstring& BrowserProcessImpl::GetApplicationLocale() {
259 DCHECK(CalledOnValidThread());
260 if (locale_.empty()) {
261 locale_ = l10n_util::GetApplicationLocale(local_state()->GetString(
262 prefs::kApplicationLocale));
263 }
264 return locale_;
265}
266
267void BrowserProcessImpl::CreateResourceDispatcherHost() {
268 DCHECK(!created_resource_dispatcher_host_ &&
269 resource_dispatcher_host_.get() == NULL);
270 created_resource_dispatcher_host_ = true;
271
272 resource_dispatcher_host_.reset(
273 new ResourceDispatcherHost(io_thread()->message_loop()));
274 resource_dispatcher_host_->Initialize();
275}
276
277void BrowserProcessImpl::CreateMetricsService() {
278 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
279 created_metrics_service_ = true;
280
281 metrics_service_.reset(new MetricsService);
282}
283
284void BrowserProcessImpl::CreateIOThread() {
285 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
286 created_io_thread_ = true;
287
288 // Prior to starting the io thread, we create the plugin service as
289 // it is predominantly used from the io thread, but must be created
290 // on the main thread. The service ctor is inexpensive and does not
291 // invoke the io_thread() accessor.
292 PluginService::GetInstance();
293
[email protected]4c3cd7412009-04-22 17:56:06294#if defined(OS_LINUX)
295 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
296 // we start it now.
297 scoped_ptr<base::Thread> background_x11_thread(
298 new BrowserProcessSubThread(ChromeThread::BACKGROUND_X11));
299 if (!background_x11_thread->Start())
300 return;
301 background_x11_thread_.swap(background_x11_thread);
302#endif
303
[email protected]ab820df2008-08-26 05:55:10304 scoped_ptr<base::Thread> thread(
305 new BrowserProcessSubThread(ChromeThread::IO));
306 base::Thread::Options options;
307 options.message_loop_type = MessageLoop::TYPE_IO;
308 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29309 return;
310 io_thread_.swap(thread);
311}
312
313void BrowserProcessImpl::CreateFileThread() {
314 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
315 created_file_thread_ = true;
316
[email protected]ab820df2008-08-26 05:55:10317 scoped_ptr<base::Thread> thread(
318 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06319 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39320#if defined(OS_WIN)
321 // On Windows, the FILE thread needs to be have a UI message loop which pumps
322 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06323 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39324#else
325 options.message_loop_type = MessageLoop::TYPE_IO;
326#endif
[email protected]a1db3842008-09-17 22:04:06327 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29328 return;
329 file_thread_.swap(thread);
330}
331
332void BrowserProcessImpl::CreateDBThread() {
333 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
334 created_db_thread_ = true;
335
[email protected]ab820df2008-08-26 05:55:10336 scoped_ptr<base::Thread> thread(
337 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29338 if (!thread->Start())
339 return;
340 db_thread_.swap(thread);
341}
342
343void BrowserProcessImpl::CreateProfileManager() {
344 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
345 created_profile_manager_ = true;
346
347 profile_manager_.reset(new ProfileManager());
348}
349
350void BrowserProcessImpl::CreateLocalState() {
351 DCHECK(!created_local_state_ && local_state_.get() == NULL);
352 created_local_state_ = true;
353
[email protected]b9636002009-03-04 00:05:25354 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29355 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]6faa0e0d2009-04-28 06:50:36356 local_state_.reset(new PrefService(local_state_path, file_thread()));
initial.commit09911bf2008-07-26 23:55:29357}
358
359void BrowserProcessImpl::InitBrokerServices(
360 sandbox::BrokerServices* broker_services) {
361 DCHECK(!initialized_broker_services_ && broker_services_ == NULL);
362 broker_services->Init();
363 initialized_broker_services_ = true;
364 broker_services_ = broker_services;
365}
366
367void BrowserProcessImpl::CreateIconManager() {
368 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
369 created_icon_manager_ = true;
370 icon_manager_.reset(new IconManager);
371}
372
373void BrowserProcessImpl::CreateDebuggerWrapper(int port) {
374 DCHECK(debugger_wrapper_.get() == NULL);
375 created_debugger_wrapper_ = true;
376
377 debugger_wrapper_ = new DebuggerWrapper(port);
378}
379
[email protected]40ecc902009-03-16 13:42:47380void BrowserProcessImpl::CreateDevToolsManager() {
381 DCHECK(!devtools_manager_.get());
382 created_devtools_manager_ = true;
[email protected]7aa27fd2009-03-23 10:43:58383 devtools_manager_.reset(new DevToolsManager());
[email protected]40ecc902009-03-16 13:42:47384}
385
initial.commit09911bf2008-07-26 23:55:29386void BrowserProcessImpl::CreateAcceleratorHandler() {
[email protected]b112a4c2009-02-01 20:24:01387#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29388 DCHECK(accelerator_handler_.get() == NULL);
[email protected]c2dacc92008-10-16 23:51:38389 scoped_ptr<views::AcceleratorHandler> accelerator_handler(
390 new views::AcceleratorHandler);
initial.commit09911bf2008-07-26 23:55:29391 accelerator_handler_.swap(accelerator_handler);
[email protected]b112a4c2009-02-01 20:24:01392#else
393 // TODO(port): remove this completely, it has no business being here.
394#endif
initial.commit09911bf2008-07-26 23:55:29395}
396
397void BrowserProcessImpl::CreateGoogleURLTracker() {
398 DCHECK(google_url_tracker_.get() == NULL);
399 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
400 google_url_tracker_.swap(google_url_tracker);
401}