license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 1 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 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" |
| 12 | #include "chrome/browser/download_file.h" |
| 13 | #include "chrome/browser/google_url_tracker.h" |
| 14 | #include "chrome/browser/icon_manager.h" |
| 15 | #include "chrome/browser/metrics_service.h" |
| 16 | #include "chrome/browser/plugin_service.h" |
| 17 | #include "chrome/browser/printing/print_job_manager.h" |
| 18 | #include "chrome/browser/profile_manager.h" |
| 19 | #include "chrome/browser/render_process_host.h" |
| 20 | #include "chrome/browser/resource_dispatcher_host.h" |
| 21 | #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 22 | #include "chrome/browser/save_file_manager.h" |
| 23 | #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 | |
| 35 | namespace { |
| 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. |
| 46 | class 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 | |
| 84 | BrowserProcessImpl::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] | 1b2db1a | 2008-08-08 17:46:13 | [diff] [blame] | 97 | memory_model_(MEDIUM_MEMORY_MODEL), |
| 98 | checked_for_new_frames_(false), |
| 99 | using_new_frames_(false) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | 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] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 121 | |
| 122 | shutdown_event_ = ::CreateEvent(NULL, TRUE, FALSE, NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | BrowserProcessImpl::~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] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 194 | // Send a QuitTask to the given MessageLoop. |
| 195 | static void PostQuit(MessageLoop* message_loop) { |
| 196 | message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 197 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 198 | |
| 199 | void BrowserProcessImpl::EndSession() { |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 200 | // Notify we are going away. |
| 201 | ::SetEvent(shutdown_event_); |
| 202 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 203 | // 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] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 223 | NewRunnableFunction(PostQuit, MessageLoop::current())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 224 | MessageLoop::current()->Run(); |
| 225 | } |
| 226 | |
| 227 | printing::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 | |
| 236 | const 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 | |
| 245 | void 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 | |
| 255 | void 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 | |
| 262 | void 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 | |
| 272 | scoped_ptr<Thread> thread(new BrowserProcessSubThread(ChromeThread::IO)); |
| 273 | if (!thread->Start()) |
| 274 | return; |
| 275 | io_thread_.swap(thread); |
| 276 | } |
| 277 | |
| 278 | void BrowserProcessImpl::CreateFileThread() { |
| 279 | DCHECK(!created_file_thread_ && file_thread_.get() == NULL); |
| 280 | created_file_thread_ = true; |
| 281 | |
| 282 | scoped_ptr<Thread> thread(new BrowserProcessSubThread(ChromeThread::FILE)); |
| 283 | if (!thread->Start()) |
| 284 | return; |
| 285 | file_thread_.swap(thread); |
| 286 | } |
| 287 | |
| 288 | void BrowserProcessImpl::CreateDBThread() { |
| 289 | DCHECK(!created_db_thread_ && db_thread_.get() == NULL); |
| 290 | created_db_thread_ = true; |
| 291 | |
| 292 | scoped_ptr<Thread> thread(new BrowserProcessSubThread(ChromeThread::DB)); |
| 293 | if (!thread->Start()) |
| 294 | return; |
| 295 | db_thread_.swap(thread); |
| 296 | } |
| 297 | |
| 298 | void BrowserProcessImpl::CreateProfileManager() { |
| 299 | DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL); |
| 300 | created_profile_manager_ = true; |
| 301 | |
| 302 | profile_manager_.reset(new ProfileManager()); |
| 303 | } |
| 304 | |
| 305 | void BrowserProcessImpl::CreateLocalState() { |
| 306 | DCHECK(!created_local_state_ && local_state_.get() == NULL); |
| 307 | created_local_state_ = true; |
| 308 | |
| 309 | std::wstring local_state_path; |
| 310 | PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path); |
| 311 | local_state_.reset(new PrefService(local_state_path)); |
| 312 | } |
| 313 | |
| 314 | void BrowserProcessImpl::InitBrokerServices( |
| 315 | sandbox::BrokerServices* broker_services) { |
| 316 | DCHECK(!initialized_broker_services_ && broker_services_ == NULL); |
| 317 | broker_services->Init(); |
| 318 | initialized_broker_services_ = true; |
| 319 | broker_services_ = broker_services; |
| 320 | } |
| 321 | |
| 322 | void BrowserProcessImpl::CreateIconManager() { |
| 323 | DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL); |
| 324 | created_icon_manager_ = true; |
| 325 | icon_manager_.reset(new IconManager); |
| 326 | } |
| 327 | |
| 328 | void BrowserProcessImpl::CreateDebuggerWrapper(int port) { |
| 329 | DCHECK(debugger_wrapper_.get() == NULL); |
| 330 | created_debugger_wrapper_ = true; |
| 331 | |
| 332 | debugger_wrapper_ = new DebuggerWrapper(port); |
| 333 | } |
| 334 | |
| 335 | void BrowserProcessImpl::CreateAcceleratorHandler() { |
| 336 | DCHECK(accelerator_handler_.get() == NULL); |
| 337 | scoped_ptr<ChromeViews::AcceleratorHandler> accelerator_handler( |
| 338 | new ChromeViews::AcceleratorHandler); |
| 339 | accelerator_handler_.swap(accelerator_handler); |
| 340 | } |
| 341 | |
| 342 | void BrowserProcessImpl::CreateGoogleURLTracker() { |
| 343 | DCHECK(google_url_tracker_.get() == NULL); |
| 344 | scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker); |
| 345 | google_url_tracker_.swap(google_url_tracker); |
| 346 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 347 | |