blob: 99d3325f9f4b0a98f9dd5e0e2287adc7a286a693 [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
[email protected]b112a4c2009-02-01 20:24:01199 // TODO(port): remove this completely from BrowserProcessImpl, it has no
200 // business being here.
201#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29202 // The ViewStorage needs to go before the NotificationService.
[email protected]c2dacc92008-10-16 23:51:38203 views::ViewStorage::DeleteSharedInstance();
[email protected]b112a4c2009-02-01 20:24:01204#endif
initial.commit09911bf2008-07-26 23:55:29205
206 // 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.
236 local_state()->SavePersistentPrefs(file_thread());
237 }
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());
initial.commit09911bf2008-07-26 23:55:29258 if (locale_.empty()) {
259 locale_ = l10n_util::GetApplicationLocale(local_state()->GetString(
260 prefs::kApplicationLocale));
261 }
initial.commit09911bf2008-07-26 23:55:29262 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]ab820df2008-08-26 05:55:10292 scoped_ptr<base::Thread> thread(
293 new BrowserProcessSubThread(ChromeThread::IO));
294 base::Thread::Options options;
295 options.message_loop_type = MessageLoop::TYPE_IO;
296 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29297 return;
298 io_thread_.swap(thread);
299}
300
301void BrowserProcessImpl::CreateFileThread() {
302 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
303 created_file_thread_ = true;
304
[email protected]ab820df2008-08-26 05:55:10305 scoped_ptr<base::Thread> thread(
306 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06307 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39308#if defined(OS_WIN)
309 // On Windows, the FILE thread needs to be have a UI message loop which pumps
310 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06311 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39312#else
313 options.message_loop_type = MessageLoop::TYPE_IO;
314#endif
[email protected]a1db3842008-09-17 22:04:06315 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29316 return;
317 file_thread_.swap(thread);
318}
319
320void BrowserProcessImpl::CreateDBThread() {
321 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
322 created_db_thread_ = true;
323
[email protected]ab820df2008-08-26 05:55:10324 scoped_ptr<base::Thread> thread(
325 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29326 if (!thread->Start())
327 return;
328 db_thread_.swap(thread);
329}
330
331void BrowserProcessImpl::CreateProfileManager() {
332 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
333 created_profile_manager_ = true;
334
335 profile_manager_.reset(new ProfileManager());
336}
337
338void BrowserProcessImpl::CreateLocalState() {
339 DCHECK(!created_local_state_ && local_state_.get() == NULL);
340 created_local_state_ = true;
341
[email protected]b9636002009-03-04 00:05:25342 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29343 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
344 local_state_.reset(new PrefService(local_state_path));
345}
346
347void BrowserProcessImpl::InitBrokerServices(
348 sandbox::BrokerServices* broker_services) {
349 DCHECK(!initialized_broker_services_ && broker_services_ == NULL);
350 broker_services->Init();
351 initialized_broker_services_ = true;
352 broker_services_ = broker_services;
353}
354
355void BrowserProcessImpl::CreateIconManager() {
356 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
357 created_icon_manager_ = true;
358 icon_manager_.reset(new IconManager);
359}
360
361void BrowserProcessImpl::CreateDebuggerWrapper(int port) {
362 DCHECK(debugger_wrapper_.get() == NULL);
363 created_debugger_wrapper_ = true;
364
365 debugger_wrapper_ = new DebuggerWrapper(port);
366}
367
368void BrowserProcessImpl::CreateAcceleratorHandler() {
[email protected]b112a4c2009-02-01 20:24:01369#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29370 DCHECK(accelerator_handler_.get() == NULL);
[email protected]c2dacc92008-10-16 23:51:38371 scoped_ptr<views::AcceleratorHandler> accelerator_handler(
372 new views::AcceleratorHandler);
initial.commit09911bf2008-07-26 23:55:29373 accelerator_handler_.swap(accelerator_handler);
[email protected]b112a4c2009-02-01 20:24:01374#else
375 // TODO(port): remove this completely, it has no business being here.
376#endif
initial.commit09911bf2008-07-26 23:55:29377}
378
379void BrowserProcessImpl::CreateGoogleURLTracker() {
380 DCHECK(google_url_tracker_.get() == NULL);
381 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
382 google_url_tracker_.swap(google_url_tracker);
383}
license.botbf09a502008-08-24 00:55:55384