blob: 24a3098ecac2db4716b7ae65904795cd188d8a0e [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"
8#include "base/thread.h"
9#include "base/path_service.h"
10#include "chrome/browser/automation/automation_provider_list.h"
11#include "chrome/browser/chrome_thread.h"
[email protected]cdaa8652008-09-13 02:48:5912#include "chrome/browser/download/download_file.h"
[email protected]c0264d42008-09-14 01:43:2413#include "chrome/browser/download/save_file_manager.h"
initial.commit09911bf2008-07-26 23:55:2914#include "chrome/browser/google_url_tracker.h"
15#include "chrome/browser/icon_manager.h"
16#include "chrome/browser/metrics_service.h"
17#include "chrome/browser/plugin_service.h"
18#include "chrome/browser/printing/print_job_manager.h"
19#include "chrome/browser/profile_manager.h"
20#include "chrome/browser/render_process_host.h"
21#include "chrome/browser/resource_dispatcher_host.h"
22#include "chrome/browser/safe_browsing/safe_browsing_service.h"
initial.commit09911bf2008-07-26 23:55:2923#include "chrome/browser/debugger/debugger_wrapper.h"
24#include "chrome/browser/suspend_controller.h"
25#include "chrome/common/chrome_paths.h"
26#include "chrome/common/chrome_switches.h"
27#include "chrome/common/clipboard_service.h"
28#include "chrome/common/l10n_util.h"
29#include "chrome/common/notification_service.h"
30#include "chrome/common/pref_names.h"
31#include "chrome/common/pref_service.h"
32#include "chrome/views/accelerator_handler.h"
33#include "chrome/views/view_storage.h"
34
35namespace {
36
37// ----------------------------------------------------------------------------
38// BrowserProcessSubThread
39//
40// This simple thread object is used for the specialized threads that the
41// BrowserProcess spins up.
42//
43// Applications must initialize the COM library before they can call
44// COM library functions other than CoGetMalloc and memory allocation
45// functions, so this class initializes COM for those users.
46class BrowserProcessSubThread : public ChromeThread {
47 public:
48 explicit BrowserProcessSubThread(ChromeThread::ID identifier)
49 : ChromeThread(identifier) {
50 }
51
52 ~BrowserProcessSubThread() {
53 // We cannot rely on our base class to stop the thread since we want our
54 // CleanUp function to run.
55 Stop();
56 }
57
58 protected:
59 virtual void Init() {
60 // Initializes the COM library on the current thread.
61 CoInitialize(NULL);
62
63 notification_service_ = new NotificationService;
64 }
65
66 virtual void CleanUp() {
67 delete notification_service_;
68 notification_service_ = NULL;
69
70 // Closes the COM library on the current thread. CoInitialize must
71 // be balanced by a corresponding call to CoUninitialize.
72 CoUninitialize();
73 }
74
75 private:
76 // Each specialized thread has its own notification service.
77 // Note: We don't use scoped_ptr because the destructor runs on the wrong
78 // thread.
79 NotificationService* notification_service_;
80};
81
82} // namespace
83
84BrowserProcessImpl::BrowserProcessImpl(CommandLine& command_line)
85 : created_resource_dispatcher_host_(false),
86 created_metrics_service_(false),
87 created_io_thread_(false),
88 created_file_thread_(false),
89 created_db_thread_(false),
90 created_profile_manager_(false),
91 created_local_state_(false),
92 created_icon_manager_(false),
93 initialized_broker_services_(false),
94 created_debugger_wrapper_(false),
95 broker_services_(NULL),
96 module_ref_count_(0),
[email protected]1b2db1a2008-08-08 17:46:1397 memory_model_(MEDIUM_MEMORY_MODEL),
98 checked_for_new_frames_(false),
99 using_new_frames_(false) {
initial.commit09911bf2008-07-26 23:55:29100 g_browser_process = this;
101 clipboard_service_.reset(new ClipboardService);
102 main_notification_service_.reset(new NotificationService);
103
104 // Must be created after the NotificationService.
105 print_job_manager_.reset(new printing::PrintJobManager);
106
107 // Configure the browser memory model.
108 if (command_line.HasSwitch(switches::kMemoryModel)) {
109 std::wstring model = command_line.GetSwitchValue(switches::kMemoryModel);
110 if (!model.empty()) {
111 if (model == L"high")
112 memory_model_ = HIGH_MEMORY_MODEL;
113 else if (model == L"low")
114 memory_model_ = LOW_MEMORY_MODEL;
115 else if (model == L"medium")
116 memory_model_ = MEDIUM_MEMORY_MODEL;
117 }
118 }
119
120 suspend_controller_ = new SuspendController();
[email protected]d65cab7a2008-08-12 01:25:41121
122 shutdown_event_ = ::CreateEvent(NULL, TRUE, FALSE, NULL);
initial.commit09911bf2008-07-26 23:55:29123}
124
125BrowserProcessImpl::~BrowserProcessImpl() {
126 // Delete the AutomationProviderList before NotificationService,
127 // since it may try to unregister notifications
128 // Both NotificationService and AutomationProvider are singleton instances in
129 // the BrowserProcess. Since AutomationProvider may have some active
130 // notification observers, it is essential that it gets destroyed before the
131 // NotificationService. NotificationService won't be destroyed until after
132 // this destructor is run.
133 automation_provider_list_.reset();
134
135 // We need to destroy the MetricsService and GoogleURLTracker before the
136 // io_thread_ gets destroyed, since both destructors can call the URLFetcher
137 // destructor, which does an InvokeLater operation on the IO thread. (The IO
138 // thread will handle that URLFetcher operation before going away.)
139 metrics_service_.reset();
140 google_url_tracker_.reset();
141
142 // Need to clear profiles (download managers) before the io_thread_.
143 profile_manager_.reset();
144
145 // Debugger must be cleaned up before IO thread and NotificationService.
146 debugger_wrapper_ = NULL;
147
148 if (resource_dispatcher_host_.get()) {
149 // Need to tell Safe Browsing Service that the IO thread is going away
150 // since it cached a pointer to it.
151 if (resource_dispatcher_host()->safe_browsing_service())
152 resource_dispatcher_host()->safe_browsing_service()->ShutDown();
153
154 // Cancel pending requests and prevent new requests.
155 resource_dispatcher_host()->Shutdown();
156 }
157
158 // Need to stop io_thread_ before resource_dispatcher_host_, since
159 // io_thread_ may still deref ResourceDispatcherHost and handle resource
160 // request before going away.
161 io_thread_.reset();
162
163 // Clean up state that lives on the file_thread_ before it goes away.
164 if (resource_dispatcher_host_.get()) {
165 resource_dispatcher_host()->download_file_manager()->Shutdown();
166 resource_dispatcher_host()->save_file_manager()->Shutdown();
167 }
168
169 // Need to stop the file_thread_ here to force it to process messages in its
170 // message loop from the previous call to shutdown the DownloadFileManager,
171 // SaveFileManager and SessionService.
172 file_thread_.reset();
173
174 // With the file_thread_ flushed, we can release any icon resources.
175 icon_manager_.reset();
176
177 // Need to destroy ResourceDispatcherHost before PluginService and
178 // SafeBrowsingService, since it caches a pointer to it.
179 resource_dispatcher_host_.reset();
180
181 // Wait for the pending print jobs to finish.
182 print_job_manager_->OnQuit();
183 print_job_manager_.reset();
184
185 // The ViewStorage needs to go before the NotificationService.
186 ChromeViews::ViewStorage::DeleteSharedInstance();
187
188 // Now OK to destroy NotificationService.
189 main_notification_service_.reset();
190
191 g_browser_process = NULL;
192}
193
[email protected]295039bd2008-08-15 04:32:57194// Send a QuitTask to the given MessageLoop.
195static void PostQuit(MessageLoop* message_loop) {
196 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
197}
initial.commit09911bf2008-07-26 23:55:29198
199void BrowserProcessImpl::EndSession() {
[email protected]d65cab7a2008-08-12 01:25:41200 // Notify we are going away.
201 ::SetEvent(shutdown_event_);
202
initial.commit09911bf2008-07-26 23:55:29203 // Mark all the profiles as clean.
204 ProfileManager* pm = profile_manager();
205 for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i)
206 (*i)->MarkAsCleanShutdown();
207
208 // Tell the metrics service it was cleanly shutdown.
209 MetricsService* metrics = g_browser_process->metrics_service();
210 if (metrics && local_state()) {
211 metrics->RecordCleanShutdown();
212
213 metrics->RecordStartOfSessionEnd();
214
215 // MetricsService lazily writes to prefs, force it to write now.
216 local_state()->SavePersistentPrefs(file_thread());
217 }
218
219 // We must write that the profile and metrics service shutdown cleanly,
220 // otherwise on startup we'll think we crashed. So we block until done and
221 // then proceed with normal shutdown.
222 g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]295039bd2008-08-15 04:32:57223 NewRunnableFunction(PostQuit, MessageLoop::current()));
initial.commit09911bf2008-07-26 23:55:29224 MessageLoop::current()->Run();
225}
226
227printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
228 // TODO(abarth): DCHECK(CalledOnValidThread());
229 // See <http://b/1287209>.
230 // print_job_manager_ is initialized in the constructor and destroyed in the
231 // destructor, so it should always be valid.
232 DCHECK(print_job_manager_.get());
233 return print_job_manager_.get();
234}
235
236const std::wstring& BrowserProcessImpl::GetApplicationLocale() {
237 DCHECK(CalledOnValidThread());
238 if (locale_.empty()) {
239 locale_ = l10n_util::GetApplicationLocale(local_state()->GetString(
240 prefs::kApplicationLocale));
241 }
242 return locale_;
243}
244
245void BrowserProcessImpl::CreateResourceDispatcherHost() {
246 DCHECK(!created_resource_dispatcher_host_ &&
247 resource_dispatcher_host_.get() == NULL);
248 created_resource_dispatcher_host_ = true;
249
250 resource_dispatcher_host_.reset(
251 new ResourceDispatcherHost(io_thread()->message_loop()));
252 resource_dispatcher_host_->Initialize();
253}
254
255void BrowserProcessImpl::CreateMetricsService() {
256 DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL);
257 created_metrics_service_ = true;
258
259 metrics_service_.reset(new MetricsService);
260}
261
262void BrowserProcessImpl::CreateIOThread() {
263 DCHECK(!created_io_thread_ && io_thread_.get() == NULL);
264 created_io_thread_ = true;
265
266 // Prior to starting the io thread, we create the plugin service as
267 // it is predominantly used from the io thread, but must be created
268 // on the main thread. The service ctor is inexpensive and does not
269 // invoke the io_thread() accessor.
270 PluginService::GetInstance();
271
[email protected]ab820df2008-08-26 05:55:10272 scoped_ptr<base::Thread> thread(
273 new BrowserProcessSubThread(ChromeThread::IO));
274 base::Thread::Options options;
275 options.message_loop_type = MessageLoop::TYPE_IO;
276 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29277 return;
278 io_thread_.swap(thread);
279}
280
281void BrowserProcessImpl::CreateFileThread() {
282 DCHECK(!created_file_thread_ && file_thread_.get() == NULL);
283 created_file_thread_ = true;
284
[email protected]ab820df2008-08-26 05:55:10285 scoped_ptr<base::Thread> thread(
286 new BrowserProcessSubThread(ChromeThread::FILE));
[email protected]a1db3842008-09-17 22:04:06287 base::Thread::Options options;
288 options.message_loop_type = MessageLoop::TYPE_UI;
289 if (!thread->StartWithOptions(options))
initial.commit09911bf2008-07-26 23:55:29290 return;
291 file_thread_.swap(thread);
292}
293
294void BrowserProcessImpl::CreateDBThread() {
295 DCHECK(!created_db_thread_ && db_thread_.get() == NULL);
296 created_db_thread_ = true;
297
[email protected]ab820df2008-08-26 05:55:10298 scoped_ptr<base::Thread> thread(
299 new BrowserProcessSubThread(ChromeThread::DB));
initial.commit09911bf2008-07-26 23:55:29300 if (!thread->Start())
301 return;
302 db_thread_.swap(thread);
303}
304
305void BrowserProcessImpl::CreateProfileManager() {
306 DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL);
307 created_profile_manager_ = true;
308
309 profile_manager_.reset(new ProfileManager());
310}
311
312void BrowserProcessImpl::CreateLocalState() {
313 DCHECK(!created_local_state_ && local_state_.get() == NULL);
314 created_local_state_ = true;
315
316 std::wstring local_state_path;
317 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
318 local_state_.reset(new PrefService(local_state_path));
319}
320
321void BrowserProcessImpl::InitBrokerServices(
322 sandbox::BrokerServices* broker_services) {
323 DCHECK(!initialized_broker_services_ && broker_services_ == NULL);
324 broker_services->Init();
325 initialized_broker_services_ = true;
326 broker_services_ = broker_services;
327}
328
329void BrowserProcessImpl::CreateIconManager() {
330 DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL);
331 created_icon_manager_ = true;
332 icon_manager_.reset(new IconManager);
333}
334
335void BrowserProcessImpl::CreateDebuggerWrapper(int port) {
336 DCHECK(debugger_wrapper_.get() == NULL);
337 created_debugger_wrapper_ = true;
338
339 debugger_wrapper_ = new DebuggerWrapper(port);
340}
341
342void BrowserProcessImpl::CreateAcceleratorHandler() {
343 DCHECK(accelerator_handler_.get() == NULL);
344 scoped_ptr<ChromeViews::AcceleratorHandler> accelerator_handler(
345 new ChromeViews::AcceleratorHandler);
346 accelerator_handler_.swap(accelerator_handler);
347}
348
349void BrowserProcessImpl::CreateGoogleURLTracker() {
350 DCHECK(google_url_tracker_.get() == NULL);
351 scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker);
352 google_url_tracker_.swap(google_url_tracker);
353}
license.botbf09a502008-08-24 00:55:55354