blob: 552e171622f42fcb6f89941626cccfe630ac46f5 [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]dcefa302009-05-20 00:24:3920#include "chrome/browser/icon_manager.h"
[email protected]dc6f4962009-02-13 01:25:5021#include "chrome/browser/metrics/metrics_service.h"
[email protected]1933eb202009-02-19 18:23:2522#include "chrome/browser/net/dns_global.h"
[email protected]d393a0fd2009-05-13 23:32:0123#include "chrome/browser/net/sdch_dictionary_fetcher.h"
[email protected]fd49e2d2009-02-20 17:21:3024#include "chrome/browser/plugin_service.h"
initial.commit09911bf2008-07-26 23:55:2925#include "chrome/browser/profile_manager.h"
[email protected]8c8657d62009-01-16 18:31:2626#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]81218f42009-02-05 18:48:0827#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
28#include "chrome/browser/safe_browsing/safe_browsing_service.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/common/chrome_paths.h"
30#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2931#include "chrome/common/notification_service.h"
32#include "chrome/common/pref_names.h"
33#include "chrome/common/pref_service.h"
[email protected]b112a4c2009-02-01 20:24:0134
35#if defined(OS_WIN)
36#include "chrome/browser/automation/automation_provider_list.h"
[email protected]b112a4c2009-02-01 20:24:0137#include "chrome/browser/printing/print_job_manager.h"
[email protected]2362e4f2009-05-08 00:34:0538#include "views/focus/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;
[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
[email protected]d393a0fd2009-05-13 23:32:01146 // We need to shutdown the SdchDictionaryFetcher as it regularly holds
147 // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do
148 // a PostDelayedTask onto the IO thread. This shutdown call will both discard
149 // any pending URLFetchers, and avoid creating any more.
150 SdchDictionaryFetcher::Shutdown();
151
initial.commit09911bf2008-07-26 23:55:29152 // We need to destroy the MetricsService and GoogleURLTracker before the
153 // io_thread_ gets destroyed, since both destructors can call the URLFetcher
[email protected]d393a0fd2009-05-13 23:32:01154 // destructor, which does an PostDelayedTask operation on the IO thread. (The
155 // IO thread will handle that URLFetcher operation before going away.)
initial.commit09911bf2008-07-26 23:55:29156 metrics_service_.reset();
157 google_url_tracker_.reset();
158
159 // Need to clear profiles (download managers) before the io_thread_.
160 profile_manager_.reset();
161
162 // Debugger must be cleaned up before IO thread and NotificationService.
163 debugger_wrapper_ = NULL;
164
165 if (resource_dispatcher_host_.get()) {
166 // Need to tell Safe Browsing Service that the IO thread is going away
167 // since it cached a pointer to it.
168 if (resource_dispatcher_host()->safe_browsing_service())
169 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
170
171 // Cancel pending requests and prevent new requests.
172 resource_dispatcher_host()->Shutdown();
173 }
174
[email protected]1933eb202009-02-19 18:23:25175 // Shutdown DNS prefetching now to ensure that network stack objects
176 // living on the IO thread get destroyed before the IO thread goes away.
[email protected]1a3861a2009-05-06 17:15:42177 if (io_thread_.get()) {
178 io_thread_->message_loop()->PostTask(FROM_HERE,
179 NewRunnableFunction(chrome_browser_net::EnsureDnsPrefetchShutdown));
180 }
[email protected]1933eb202009-02-19 18:23:25181
[email protected]4c3cd7412009-04-22 17:56:06182#if defined(OS_LINUX)
183 // The IO thread must outlive the BACKGROUND_X11 thread.
184 background_x11_thread_.reset();
185#endif
186
initial.commit09911bf2008-07-26 23:55:29187 // Need to stop io_thread_ before resource_dispatcher_host_, since
188 // io_thread_ may still deref ResourceDispatcherHost and handle resource
189 // request before going away.
190 io_thread_.reset();
191
192 // Clean up state that lives on the file_thread_ before it goes away.
193 if (resource_dispatcher_host_.get()) {
194 resource_dispatcher_host()->download_file_manager()->Shutdown();
195 resource_dispatcher_host()->save_file_manager()->Shutdown();
196 }
197
198 // Need to stop the file_thread_ here to force it to process messages in its
199 // message loop from the previous call to shutdown the DownloadFileManager,
200 // SaveFileManager and SessionService.
201 file_thread_.reset();
202
203 // With the file_thread_ flushed, we can release any icon resources.
204 icon_manager_.reset();
205
206 // Need to destroy ResourceDispatcherHost before PluginService and
207 // SafeBrowsingService, since it caches a pointer to it.
208 resource_dispatcher_host_.reset();
209
210 // Wait for the pending print jobs to finish.
211 print_job_manager_->OnQuit();
212 print_job_manager_.reset();
213
initial.commit09911bf2008-07-26 23:55:29214 // Now OK to destroy NotificationService.
215 main_notification_service_.reset();
216
217 g_browser_process = NULL;
218}
219
[email protected]295039bd2008-08-15 04:32:57220// Send a QuitTask to the given MessageLoop.
221static void PostQuit(MessageLoop* message_loop) {
222 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
223}
initial.commit09911bf2008-07-26 23:55:29224
225void BrowserProcessImpl::EndSession() {
[email protected]b112a4c2009-02-01 20:24:01226#if defined(OS_WIN)
[email protected]d65cab7a2008-08-12 01:25:41227 // Notify we are going away.
[email protected]b797e152009-01-23 16:06:14228 ::SetEvent(shutdown_event_->handle());
[email protected]b112a4c2009-02-01 20:24:01229#endif
[email protected]d65cab7a2008-08-12 01:25:41230
initial.commit09911bf2008-07-26 23:55:29231 // Mark all the profiles as clean.
232 ProfileManager* pm = profile_manager();
233 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
234 (*i)->MarkAsCleanShutdown();
235
236 // Tell the metrics service it was cleanly shutdown.
237 MetricsService* metrics = g_browser_process->metrics_service();
238 if (metrics && local_state()) {
239 metrics->RecordCleanShutdown();
240
241 metrics->RecordStartOfSessionEnd();
242
243 // MetricsService lazily writes to prefs, force it to write now.
[email protected]6faa0e0d2009-04-28 06:50:36244 local_state()->SavePersistentPrefs();
initial.commit09911bf2008-07-26 23:55:29245 }
246
247 // We must write that the profile and metrics service shutdown cleanly,
248 // otherwise on startup we'll think we crashed. So we block until done and
249 // then proceed with normal shutdown.
250 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57251 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29252 MessageLoop::current()->Run();
253}
254
255printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
256 // TODO(abarth): DCHECK(CalledOnValidThread());
257 // See <http://b/1287209>.
258 // print_job_manager_ is initialized in the constructor and destroyed in the
259 // destructor, so it should always be valid.
260 DCHECK(print_job_manager_.get());
261 return print_job_manager_.get();
262}
263
[email protected]d70539de2009-06-24 22:17:06264const std::string& BrowserProcessImpl::GetApplicationLocale() {
initial.commit09911bf2008-07-26 23:55:29265 DCHECK(CalledOnValidThread());
266 if (locale_.empty()) {
[email protected]d70539de2009-06-24 22:17:06267 locale_ = l10n_util::GetApplicationLocale(
268 local_state()->GetString(prefs::kApplicationLocale));
initial.commit09911bf2008-07-26 23:55:29269 }
270 return locale_;
271}
272
273void BrowserProcessImpl::CreateResourceDispatcherHost() {
274 DCHECK(!created_resource_dispatcher_host_ &&
275 resource_dispatcher_host_.get() == NULL);
276 created_resource_dispatcher_host_ = true;
277
278 resource_dispatcher_host_.reset(
279 new ResourceDispatcherHost(io_thread()->message_loop()));
280 resource_dispatcher_host_->Initialize();
281}
282
283void BrowserProcessImpl::CreateMetricsService() {
284 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
285 created_metrics_service_ = true;
286
287 metrics_service_.reset(new MetricsService);
288}
289
290void BrowserProcessImpl::CreateIOThread() {
291 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
292 created_io_thread_ = true;
293
294 // Prior to starting the io thread, we create the plugin service as
295 // it is predominantly used from the io thread, but must be created
296 // on the main thread. The service ctor is inexpensive and does not
297 // invoke the io_thread() accessor.
298 PluginService::GetInstance();
299
[email protected]4c3cd7412009-04-22 17:56:06300#if defined(OS_LINUX)
301 // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so
302 // we start it now.
303 scoped_ptr<base::Thread> background_x11_thread(
304 new BrowserProcessSubThread(ChromeThread::BACKGROUND_X11));
305 if (!background_x11_thread->Start())
306 return;
307 background_x11_thread_.swap(background_x11_thread);
308#endif
309
[email protected]ab820df2008-08-26 05:55:10310 scoped_ptr<base::Thread> thread(
311 new BrowserProcessSubThread(ChromeThread::IO));
312 base::Thread::Options options;
313 options.message_loop_type = MessageLoop::TYPE_IO;
314 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29315 return;
316 io_thread_.swap(thread);
317}
318
319void BrowserProcessImpl::CreateFileThread() {
320 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
321 created_file_thread_ = true;
322
[email protected]ab820df2008-08-26 05:55:10323 scoped_ptr<base::Thread> thread(
324 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06325 base::Thread::Options options;
[email protected]9e549b582009-02-05 21:13:39326#if defined(OS_WIN)
327 // On Windows, the FILE thread needs to be have a UI message loop which pumps
328 // messages in such a way that Google Update can communicate back to us.
[email protected]a1db3842008-09-17 22:04:06329 options.message_loop_type = MessageLoop::TYPE_UI;
[email protected]9e549b582009-02-05 21:13:39330#else
331 options.message_loop_type = MessageLoop::TYPE_IO;
332#endif
[email protected]a1db3842008-09-17 22:04:06333 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29334 return;
335 file_thread_.swap(thread);
336}
337
338void BrowserProcessImpl::CreateDBThread() {
339 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
340 created_db_thread_ = true;
341
[email protected]ab820df2008-08-26 05:55:10342 scoped_ptr<base::Thread> thread(
343 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29344 if (!thread->Start())
345 return;
346 db_thread_.swap(thread);
347}
348
349void BrowserProcessImpl::CreateProfileManager() {
350 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
351 created_profile_manager_ = true;
352
353 profile_manager_.reset(new ProfileManager());
354}
355
356void BrowserProcessImpl::CreateLocalState() {
357 DCHECK(!created_local_state_ && local_state_.get() == NULL);
358 created_local_state_ = true;
359
[email protected]b9636002009-03-04 00:05:25360 FilePath local_state_path;
initial.commit09911bf2008-07-26 23:55:29361 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
[email protected]6faa0e0d2009-04-28 06:50:36362 local_state_.reset(new PrefService(local_state_path, file_thread()));
initial.commit09911bf2008-07-26 23:55:29363}
364
365void BrowserProcessImpl::InitBrokerServices(
366 sandbox::BrokerServices* broker_services) {
367 DCHECK(!initialized_broker_services_ && broker_services_ == NULL);
368 broker_services->Init();
369 initialized_broker_services_ = true;
370 broker_services_ = broker_services;
371}
372
373void BrowserProcessImpl::CreateIconManager() {
374 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
375 created_icon_manager_ = true;
376 icon_manager_.reset(new IconManager);
377}
378
379void BrowserProcessImpl::CreateDebuggerWrapper(int port) {
380 DCHECK(debugger_wrapper_.get() == NULL);
381 created_debugger_wrapper_ = true;
382
383 debugger_wrapper_ = new DebuggerWrapper(port);
384}
385
[email protected]40ecc902009-03-16 13:42:47386void BrowserProcessImpl::CreateDevToolsManager() {
[email protected]73ee01522009-06-05 10:13:44387 DCHECK(devtools_manager_.get() == NULL);
[email protected]40ecc902009-03-16 13:42:47388 created_devtools_manager_ = true;
[email protected]73ee01522009-06-05 10:13:44389 devtools_manager_ = new DevToolsManager();
[email protected]40ecc902009-03-16 13:42:47390}
391
initial.commit09911bf2008-07-26 23:55:29392void BrowserProcessImpl::CreateGoogleURLTracker() {
393 DCHECK(google_url_tracker_.get() == NULL);
394 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
395 google_url_tracker_.swap(google_url_tracker);
396}