blob: 7cb57b2ac48f410442b09b2c38f2b1e595881014 [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"
initial.commit09911bf2008-07-26 23:55:2914#include "chrome/browser/google_url_tracker.h"
initial.commit09911bf2008-07-26 23:55:2915#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2616#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]81218f42009-02-05 18:48:0817#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
18#include "chrome/browser/safe_browsing/safe_browsing_service.h"
initial.commit09911bf2008-07-26 23:55:2919#include "chrome/common/chrome_paths.h"
20#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2921#include "chrome/common/notification_service.h"
22#include "chrome/common/pref_names.h"
23#include "chrome/common/pref_service.h"
[email protected]b112a4c2009-02-01 20:24:0124
25#if defined(OS_WIN)
26#include "chrome/browser/automation/automation_provider_list.h"
27#include "chrome/browser/download/download_file.h"
28#include "chrome/browser/download/save_file_manager.h"
29#include "chrome/browser/icon_manager.h"
30#include "chrome/browser/metrics/metrics_service.h"
[email protected]81218f42009-02-05 18:48:0831#include "chrome/browser/plugin_service.h"
[email protected]b112a4c2009-02-01 20:24:0132#include "chrome/browser/printing/print_job_manager.h"
[email protected]b112a4c2009-02-01 20:24:0133#include "chrome/common/clipboard_service.h"
34#include "chrome/common/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2935#include "chrome/views/accelerator_handler.h"
36#include "chrome/views/view_storage.h"
[email protected]81218f42009-02-05 18:48:0837#elif defined(OS_POSIX)
38// TODO(port): Remove the temporary scaffolding as we port the above headers.
39#include "chrome/common/temp_scaffolding_stubs.h"
[email protected]b112a4c2009-02-01 20:24:0140#endif
initial.commit09911bf2008-07-26 23:55:2941
42namespace {
43
44// ----------------------------------------------------------------------------
45// BrowserProcessSubThread
46//
47// This simple thread object is used for the specialized threads that the
48// BrowserProcess spins up.
49//
50// Applications must initialize the COM library before they can call
51// COM library functions other than CoGetMalloc and memory allocation
52// functions, so this class initializes COM for those users.
53class BrowserProcessSubThread : public ChromeThread {
54 public:
55 explicit BrowserProcessSubThread(ChromeThread::ID identifier)
56 : ChromeThread(identifier) {
57 }
58
59 ~BrowserProcessSubThread() {
60 // We cannot rely on our base class to stop the thread since we want our
61 // CleanUp function to run.
62 Stop();
63 }
64
65 protected:
66 virtual void Init() {
[email protected]b112a4c2009-02-01 20:24:0167#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2968 // Initializes the COM library on the current thread.
69 CoInitialize(NULL);
[email protected]b112a4c2009-02-01 20:24:0170#endif
initial.commit09911bf2008-07-26 23:55:2971
72 notification_service_ = new NotificationService;
73 }
74
75 virtual void CleanUp() {
76 delete notification_service_;
77 notification_service_ = NULL;
78
[email protected]b112a4c2009-02-01 20:24:0179#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2980 // Closes the COM library on the current thread. CoInitialize must
81 // be balanced by a corresponding call to CoUninitialize.
82 CoUninitialize();
[email protected]b112a4c2009-02-01 20:24:0183#endif
initial.commit09911bf2008-07-26 23:55:2984 }
85
86 private:
87 // Each specialized thread has its own notification service.
88 // Note: We don't use scoped_ptr because the destructor runs on the wrong
89 // thread.
90 NotificationService* notification_service_;
91};
92
93} // namespace
94
[email protected]bb975362009-01-21 01:00:2295BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line)
initial.commit09911bf2008-07-26 23:55:2996 : created_resource_dispatcher_host_(false),
97 created_metrics_service_(false),
98 created_io_thread_(false),
99 created_file_thread_(false),
100 created_db_thread_(false),
101 created_profile_manager_(false),
102 created_local_state_(false),
initial.commit09911bf2008-07-26 23:55:29103 initialized_broker_services_(false),
initial.commit09911bf2008-07-26 23:55:29104 broker_services_(NULL),
[email protected]b112a4c2009-02-01 20:24:01105 created_icon_manager_(false),
106 created_debugger_wrapper_(false),
initial.commit09911bf2008-07-26 23:55:29107 module_ref_count_(0),
[email protected]1b2db1a2008-08-08 17:46:13108 memory_model_(MEDIUM_MEMORY_MODEL),
109 checked_for_new_frames_(false),
110 using_new_frames_(false) {
initial.commit09911bf2008-07-26 23:55:29111 g_browser_process = this;
112 clipboard_service_.reset(new ClipboardService);
113 main_notification_service_.reset(new NotificationService);
114
115 // Must be created after the NotificationService.
116 print_job_manager_.reset(new printing::PrintJobManager);
117
118 // Configure the browser memory model.
119 if (command_line.HasSwitch(switches::kMemoryModel)) {
120 std::wstring model = command_line.GetSwitchValue(switches::kMemoryModel);
121 if (!model.empty()) {
122 if (model == L"high")
123 memory_model_ = HIGH_MEMORY_MODEL;
124 else if (model == L"low")
125 memory_model_ = LOW_MEMORY_MODEL;
126 else if (model == L"medium")
127 memory_model_ = MEDIUM_MEMORY_MODEL;
128 }
[email protected]ac262c9f2008-10-19 17:45:21129 } else {
130 // Randomly choose what memory model to use.
131 const double probability = 0.5;
132 FieldTrial* trial(new FieldTrial(BrowserTrial::kMemoryModelFieldTrial,
133 probability));
134 DCHECK(FieldTrialList::Find(BrowserTrial::kMemoryModelFieldTrial) == trial);
135 if (trial->boolean_value())
136 memory_model_ = HIGH_MEMORY_MODEL;
137 else
138 memory_model_ = MEDIUM_MEMORY_MODEL;
initial.commit09911bf2008-07-26 23:55:29139 }
140
[email protected]b797e152009-01-23 16:06:14141 shutdown_event_.reset(new base::WaitableEvent(true, false));
initial.commit09911bf2008-07-26 23:55:29142}
143
144BrowserProcessImpl::~BrowserProcessImpl() {
145 // Delete the AutomationProviderList before NotificationService,
146 // since it may try to unregister notifications
147 // Both NotificationService and AutomationProvider are singleton instances in
148 // the BrowserProcess. Since AutomationProvider may have some active
149 // notification observers, it is essential that it gets destroyed before the
150 // NotificationService. NotificationService won't be destroyed until after
151 // this destructor is run.
152 automation_provider_list_.reset();
153
154 // We need to destroy the MetricsService and GoogleURLTracker before the
155 // io_thread_ gets destroyed, since both destructors can call the URLFetcher
156 // destructor, which does an InvokeLater operation on the IO thread. (The IO
157 // thread will handle that URLFetcher operation before going away.)
158 metrics_service_.reset();
159 google_url_tracker_.reset();
160
161 // Need to clear profiles (download managers) before the io_thread_.
162 profile_manager_.reset();
163
164 // Debugger must be cleaned up before IO thread and NotificationService.
165 debugger_wrapper_ = NULL;
166
167 if (resource_dispatcher_host_.get()) {
168 // Need to tell Safe Browsing Service that the IO thread is going away
169 // since it cached a pointer to it.
170 if (resource_dispatcher_host()->safe_browsing_service())
171 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
172
173 // Cancel pending requests and prevent new requests.
174 resource_dispatcher_host()->Shutdown();
175 }
176
177 // Need to stop io_thread_ before resource_dispatcher_host_, since
178 // io_thread_ may still deref ResourceDispatcherHost and handle resource
179 // request before going away.
180 io_thread_.reset();
181
182 // Clean up state that lives on the file_thread_ before it goes away.
183 if (resource_dispatcher_host_.get()) {
184 resource_dispatcher_host()->download_file_manager()->Shutdown();
185 resource_dispatcher_host()->save_file_manager()->Shutdown();
186 }
187
188 // Need to stop the file_thread_ here to force it to process messages in its
189 // message loop from the previous call to shutdown the DownloadFileManager,
190 // SaveFileManager and SessionService.
191 file_thread_.reset();
192
193 // With the file_thread_ flushed, we can release any icon resources.
194 icon_manager_.reset();
195
196 // Need to destroy ResourceDispatcherHost before PluginService and
197 // SafeBrowsingService, since it caches a pointer to it.
198 resource_dispatcher_host_.reset();
199
200 // Wait for the pending print jobs to finish.
201 print_job_manager_->OnQuit();
202 print_job_manager_.reset();
203
[email protected]b112a4c2009-02-01 20:24:01204 // TODO(port): remove this completely from BrowserProcessImpl, it has no
205 // business being here.
206#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29207 // The ViewStorage needs to go before the NotificationService.
[email protected]c2dacc92008-10-16 23:51:38208 views::ViewStorage::DeleteSharedInstance();
[email protected]b112a4c2009-02-01 20:24:01209#endif
initial.commit09911bf2008-07-26 23:55:29210
211 // Now OK to destroy NotificationService.
212 main_notification_service_.reset();
213
214 g_browser_process = NULL;
215}
216
[email protected]295039bd2008-08-15 04:32:57217// Send a QuitTask to the given MessageLoop.
218static void PostQuit(MessageLoop* message_loop) {
219 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
220}
initial.commit09911bf2008-07-26 23:55:29221
222void BrowserProcessImpl::EndSession() {
[email protected]b112a4c2009-02-01 20:24:01223#if defined(OS_WIN)
[email protected]d65cab7a2008-08-12 01:25:41224 // Notify we are going away.
[email protected]b797e152009-01-23 16:06:14225 ::SetEvent(shutdown_event_->handle());
[email protected]b112a4c2009-02-01 20:24:01226#endif
[email protected]d65cab7a2008-08-12 01:25:41227
initial.commit09911bf2008-07-26 23:55:29228 // Mark all the profiles as clean.
229 ProfileManager* pm = profile_manager();
230 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
231 (*i)->MarkAsCleanShutdown();
232
233 // Tell the metrics service it was cleanly shutdown.
234 MetricsService* metrics = g_browser_process->metrics_service();
235 if (metrics && local_state()) {
236 metrics->RecordCleanShutdown();
237
238 metrics->RecordStartOfSessionEnd();
239
240 // MetricsService lazily writes to prefs, force it to write now.
241 local_state()->SavePersistentPrefs(file_thread());
242 }
243
244 // We must write that the profile and metrics service shutdown cleanly,
245 // otherwise on startup we'll think we crashed. So we block until done and
246 // then proceed with normal shutdown.
247 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57248 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29249 MessageLoop::current()->Run();
250}
251
252printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
253 // TODO(abarth): DCHECK(CalledOnValidThread());
254 // See <http://b/1287209>.
255 // print_job_manager_ is initialized in the constructor and destroyed in the
256 // destructor, so it should always be valid.
257 DCHECK(print_job_manager_.get());
258 return print_job_manager_.get();
259}
260
261const std::wstring& BrowserProcessImpl::GetApplicationLocale() {
262 DCHECK(CalledOnValidThread());
[email protected]b112a4c2009-02-01 20:24:01263#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29264 if (locale_.empty()) {
265 locale_ = l10n_util::GetApplicationLocale(local_state()->GetString(
266 prefs::kApplicationLocale));
267 }
[email protected]b112a4c2009-02-01 20:24:01268#else
269 NOTIMPLEMENTED();
270 // TODO(port): port l10n_util
271#endif
initial.commit09911bf2008-07-26 23:55:29272 return locale_;
273}
274
275void BrowserProcessImpl::CreateResourceDispatcherHost() {
276 DCHECK(!created_resource_dispatcher_host_ &&
277 resource_dispatcher_host_.get() == NULL);
278 created_resource_dispatcher_host_ = true;
279
280 resource_dispatcher_host_.reset(
281 new ResourceDispatcherHost(io_thread()->message_loop()));
282 resource_dispatcher_host_->Initialize();
283}
284
285void BrowserProcessImpl::CreateMetricsService() {
286 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
287 created_metrics_service_ = true;
288
289 metrics_service_.reset(new MetricsService);
290}
291
292void BrowserProcessImpl::CreateIOThread() {
293 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
294 created_io_thread_ = true;
295
296 // Prior to starting the io thread, we create the plugin service as
297 // it is predominantly used from the io thread, but must be created
298 // on the main thread. The service ctor is inexpensive and does not
299 // invoke the io_thread() accessor.
300 PluginService::GetInstance();
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
352 std::wstring local_state_path;
353 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
354 local_state_.reset(new PrefService(local_state_path));
355}
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
378void BrowserProcessImpl::CreateAcceleratorHandler() {
[email protected]b112a4c2009-02-01 20:24:01379#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29380 DCHECK(accelerator_handler_.get() == NULL);
[email protected]c2dacc92008-10-16 23:51:38381 scoped_ptr<views::AcceleratorHandler> accelerator_handler(
382 new views::AcceleratorHandler);
initial.commit09911bf2008-07-26 23:55:29383 accelerator_handler_.swap(accelerator_handler);
[email protected]b112a4c2009-02-01 20:24:01384#else
385 // TODO(port): remove this completely, it has no business being here.
386#endif
initial.commit09911bf2008-07-26 23:55:29387}
388
389void BrowserProcessImpl::CreateGoogleURLTracker() {
390 DCHECK(google_url_tracker_.get() == NULL);
391 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
392 google_url_tracker_.swap(google_url_tracker);
393}
license.botbf09a502008-08-24 00:55:55394