[email protected] | 019191a | 2009-10-02 20:37:27 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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 | |
[email protected] | 90f3990 | 2009-10-03 04:25:37 | [diff] [blame^] | 7 | #include "app/clipboard/clipboard.h" |
[email protected] | a92b864 | 2009-05-05 23:38:56 | [diff] [blame] | 8 | #include "app/l10n_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 10 | #include "base/file_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "base/path_service.h" |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 12 | #include "base/thread.h" |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 13 | #include "base/waitable_event.h" |
[email protected] | ac262c9f | 2008-10-19 17:45:21 | [diff] [blame] | 14 | #include "chrome/browser/browser_trial.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "chrome/browser/chrome_thread.h" |
[email protected] | 4ab4b0f | 2009-02-10 18:54:50 | [diff] [blame] | 16 | #include "chrome/browser/debugger/debugger_wrapper.h" |
[email protected] | 40ecc90 | 2009-03-16 13:42:47 | [diff] [blame] | 17 | #include "chrome/browser/debugger/devtools_manager.h" |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 18 | #include "chrome/browser/download/download_file.h" |
[email protected] | 5ba0a2c | 2009-02-19 01:19:34 | [diff] [blame] | 19 | #include "chrome/browser/download/save_file_manager.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | #include "chrome/browser/google_url_tracker.h" |
[email protected] | dcefa30 | 2009-05-20 00:24:39 | [diff] [blame] | 21 | #include "chrome/browser/icon_manager.h" |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 22 | #include "chrome/browser/metrics/metrics_service.h" |
[email protected] | 1933eb20 | 2009-02-19 18:23:25 | [diff] [blame] | 23 | #include "chrome/browser/net/dns_global.h" |
[email protected] | d393a0fd | 2009-05-13 23:32:01 | [diff] [blame] | 24 | #include "chrome/browser/net/sdch_dictionary_fetcher.h" |
[email protected] | fd49e2d | 2009-02-20 17:21:30 | [diff] [blame] | 25 | #include "chrome/browser/plugin_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | #include "chrome/browser/profile_manager.h" |
[email protected] | 8c8657d6 | 2009-01-16 18:31:26 | [diff] [blame] | 27 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | 81218f4 | 2009-02-05 18:48:08 | [diff] [blame] | 28 | #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 29 | #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | #include "chrome/common/chrome_paths.h" |
| 31 | #include "chrome/common/chrome_switches.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | #include "chrome/common/notification_service.h" |
| 33 | #include "chrome/common/pref_names.h" |
| 34 | #include "chrome/common/pref_service.h" |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 35 | #include "ipc/ipc_logging.h" |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 36 | |
| 37 | #if defined(OS_WIN) |
| 38 | #include "chrome/browser/automation/automation_provider_list.h" |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 39 | #include "chrome/browser/printing/print_job_manager.h" |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 40 | #include "views/focus/view_storage.h" |
[email protected] | 81218f4 | 2009-02-05 18:48:08 | [diff] [blame] | 41 | #elif defined(OS_POSIX) |
| 42 | // TODO(port): Remove the temporary scaffolding as we port the above headers. |
| 43 | #include "chrome/common/temp_scaffolding_stubs.h" |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 44 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 46 | #if defined(IPC_MESSAGE_LOG_ENABLED) |
| 47 | #include "chrome/common/plugin_messages.h" |
| 48 | #include "chrome/common/render_messages.h" |
| 49 | #endif |
| 50 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | namespace { |
| 52 | |
| 53 | // ---------------------------------------------------------------------------- |
| 54 | // BrowserProcessSubThread |
| 55 | // |
| 56 | // This simple thread object is used for the specialized threads that the |
| 57 | // BrowserProcess spins up. |
| 58 | // |
| 59 | // Applications must initialize the COM library before they can call |
| 60 | // COM library functions other than CoGetMalloc and memory allocation |
| 61 | // functions, so this class initializes COM for those users. |
| 62 | class BrowserProcessSubThread : public ChromeThread { |
| 63 | public: |
| 64 | explicit BrowserProcessSubThread(ChromeThread::ID identifier) |
| 65 | : ChromeThread(identifier) { |
| 66 | } |
| 67 | |
[email protected] | 48ca901 | 2009-08-11 21:38:54 | [diff] [blame] | 68 | virtual ~BrowserProcessSubThread() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | // We cannot rely on our base class to stop the thread since we want our |
| 70 | // CleanUp function to run. |
| 71 | Stop(); |
| 72 | } |
| 73 | |
| 74 | protected: |
| 75 | virtual void Init() { |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 76 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | // Initializes the COM library on the current thread. |
| 78 | CoInitialize(NULL); |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 79 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | |
| 81 | notification_service_ = new NotificationService; |
| 82 | } |
| 83 | |
| 84 | virtual void CleanUp() { |
| 85 | delete notification_service_; |
| 86 | notification_service_ = NULL; |
| 87 | |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 88 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | // Closes the COM library on the current thread. CoInitialize must |
| 90 | // be balanced by a corresponding call to CoUninitialize. |
| 91 | CoUninitialize(); |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 92 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private: |
| 96 | // Each specialized thread has its own notification service. |
| 97 | // Note: We don't use scoped_ptr because the destructor runs on the wrong |
| 98 | // thread. |
| 99 | NotificationService* notification_service_; |
| 100 | }; |
| 101 | |
[email protected] | 48ca901 | 2009-08-11 21:38:54 | [diff] [blame] | 102 | class IOThread : public BrowserProcessSubThread { |
| 103 | public: |
| 104 | IOThread() : BrowserProcessSubThread(ChromeThread::IO) {} |
| 105 | |
| 106 | virtual ~IOThread() { |
| 107 | // We cannot rely on our base class to stop the thread since we want our |
| 108 | // CleanUp function to run. |
| 109 | Stop(); |
| 110 | } |
| 111 | |
| 112 | protected: |
| 113 | virtual void CleanUp() { |
| 114 | // URLFetcher and URLRequest instances must NOT outlive the IO thread. |
[email protected] | 03a69dc | 2009-09-22 00:04:59 | [diff] [blame] | 115 | // |
| 116 | // Strictly speaking, URLFetcher's CheckForLeaks() should be done on the |
| 117 | // UI thread. However, since there _shouldn't_ be any instances left |
| 118 | // at this point, it shouldn't be a race. |
| 119 | // |
| 120 | // We check URLFetcher first, since if it has leaked then an associated |
| 121 | // URLRequest will also have leaked. However it is more useful to |
| 122 | // crash showing the callstack of URLFetcher's allocation than its |
| 123 | // URLRequest member. |
[email protected] | 48ca901 | 2009-08-11 21:38:54 | [diff] [blame] | 124 | base::LeakTracker<URLFetcher>::CheckForLeaks(); |
[email protected] | 03a69dc | 2009-09-22 00:04:59 | [diff] [blame] | 125 | base::LeakTracker<URLRequest>::CheckForLeaks(); |
[email protected] | 48ca901 | 2009-08-11 21:38:54 | [diff] [blame] | 126 | |
| 127 | BrowserProcessSubThread::CleanUp(); |
| 128 | } |
| 129 | }; |
| 130 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | } // namespace |
| 132 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 133 | BrowserProcessImpl::BrowserProcessImpl(const CommandLine& command_line) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | : created_resource_dispatcher_host_(false), |
| 135 | created_metrics_service_(false), |
| 136 | created_io_thread_(false), |
| 137 | created_file_thread_(false), |
| 138 | created_db_thread_(false), |
| 139 | created_profile_manager_(false), |
| 140 | created_local_state_(false), |
[email protected] | f3a4f30 | 2009-08-21 22:35:29 | [diff] [blame] | 141 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 142 | initialized_broker_services_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | broker_services_(NULL), |
[email protected] | f3a4f30 | 2009-08-21 22:35:29 | [diff] [blame] | 144 | #endif // defined(OS_WIN) |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 145 | created_icon_manager_(false), |
| 146 | created_debugger_wrapper_(false), |
[email protected] | 40ecc90 | 2009-03-16 13:42:47 | [diff] [blame] | 147 | created_devtools_manager_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | module_ref_count_(0), |
[email protected] | 1b2db1a | 2008-08-08 17:46:13 | [diff] [blame] | 149 | checked_for_new_frames_(false), |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 150 | using_new_frames_(false), |
| 151 | have_inspector_files_(true) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | g_browser_process = this; |
[email protected] | 1b8d02f1 | 2009-05-05 04:14:11 | [diff] [blame] | 153 | clipboard_.reset(new Clipboard); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | main_notification_service_.reset(new NotificationService); |
| 155 | |
| 156 | // Must be created after the NotificationService. |
| 157 | print_job_manager_.reset(new printing::PrintJobManager); |
| 158 | |
[email protected] | b797e15 | 2009-01-23 16:06:14 | [diff] [blame] | 159 | shutdown_event_.reset(new base::WaitableEvent(true, false)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | BrowserProcessImpl::~BrowserProcessImpl() { |
| 163 | // Delete the AutomationProviderList before NotificationService, |
| 164 | // since it may try to unregister notifications |
| 165 | // Both NotificationService and AutomationProvider are singleton instances in |
| 166 | // the BrowserProcess. Since AutomationProvider may have some active |
| 167 | // notification observers, it is essential that it gets destroyed before the |
| 168 | // NotificationService. NotificationService won't be destroyed until after |
| 169 | // this destructor is run. |
| 170 | automation_provider_list_.reset(); |
| 171 | |
[email protected] | d393a0fd | 2009-05-13 23:32:01 | [diff] [blame] | 172 | // We need to shutdown the SdchDictionaryFetcher as it regularly holds |
| 173 | // a pointer to a URLFetcher, and that URLFetcher (upon destruction) will do |
| 174 | // a PostDelayedTask onto the IO thread. This shutdown call will both discard |
| 175 | // any pending URLFetchers, and avoid creating any more. |
| 176 | SdchDictionaryFetcher::Shutdown(); |
| 177 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 178 | // We need to destroy the MetricsService and GoogleURLTracker before the |
| 179 | // io_thread_ gets destroyed, since both destructors can call the URLFetcher |
[email protected] | d393a0fd | 2009-05-13 23:32:01 | [diff] [blame] | 180 | // destructor, which does an PostDelayedTask operation on the IO thread. (The |
| 181 | // IO thread will handle that URLFetcher operation before going away.) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 182 | metrics_service_.reset(); |
| 183 | google_url_tracker_.reset(); |
| 184 | |
| 185 | // Need to clear profiles (download managers) before the io_thread_. |
| 186 | profile_manager_.reset(); |
| 187 | |
| 188 | // Debugger must be cleaned up before IO thread and NotificationService. |
| 189 | debugger_wrapper_ = NULL; |
| 190 | |
| 191 | if (resource_dispatcher_host_.get()) { |
| 192 | // Need to tell Safe Browsing Service that the IO thread is going away |
| 193 | // since it cached a pointer to it. |
| 194 | if (resource_dispatcher_host()->safe_browsing_service()) |
| 195 | resource_dispatcher_host()->safe_browsing_service()->ShutDown(); |
| 196 | |
| 197 | // Cancel pending requests and prevent new requests. |
| 198 | resource_dispatcher_host()->Shutdown(); |
| 199 | } |
| 200 | |
[email protected] | 4c3cd741 | 2009-04-22 17:56:06 | [diff] [blame] | 201 | #if defined(OS_LINUX) |
| 202 | // The IO thread must outlive the BACKGROUND_X11 thread. |
| 203 | background_x11_thread_.reset(); |
| 204 | #endif |
| 205 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | // Need to stop io_thread_ before resource_dispatcher_host_, since |
| 207 | // io_thread_ may still deref ResourceDispatcherHost and handle resource |
| 208 | // request before going away. |
[email protected] | 48ca901 | 2009-08-11 21:38:54 | [diff] [blame] | 209 | ResetIOThread(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | |
| 211 | // Clean up state that lives on the file_thread_ before it goes away. |
| 212 | if (resource_dispatcher_host_.get()) { |
| 213 | resource_dispatcher_host()->download_file_manager()->Shutdown(); |
| 214 | resource_dispatcher_host()->save_file_manager()->Shutdown(); |
| 215 | } |
| 216 | |
| 217 | // Need to stop the file_thread_ here to force it to process messages in its |
| 218 | // message loop from the previous call to shutdown the DownloadFileManager, |
| 219 | // SaveFileManager and SessionService. |
| 220 | file_thread_.reset(); |
| 221 | |
| 222 | // With the file_thread_ flushed, we can release any icon resources. |
| 223 | icon_manager_.reset(); |
| 224 | |
| 225 | // Need to destroy ResourceDispatcherHost before PluginService and |
| 226 | // SafeBrowsingService, since it caches a pointer to it. |
| 227 | resource_dispatcher_host_.reset(); |
| 228 | |
| 229 | // Wait for the pending print jobs to finish. |
| 230 | print_job_manager_->OnQuit(); |
| 231 | print_job_manager_.reset(); |
| 232 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | // Now OK to destroy NotificationService. |
| 234 | main_notification_service_.reset(); |
| 235 | |
| 236 | g_browser_process = NULL; |
| 237 | } |
| 238 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 239 | // Send a QuitTask to the given MessageLoop. |
| 240 | static void PostQuit(MessageLoop* message_loop) { |
| 241 | message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 242 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 243 | |
| 244 | void BrowserProcessImpl::EndSession() { |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 245 | #if defined(OS_WIN) |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 246 | // Notify we are going away. |
[email protected] | b797e15 | 2009-01-23 16:06:14 | [diff] [blame] | 247 | ::SetEvent(shutdown_event_->handle()); |
[email protected] | b112a4c | 2009-02-01 20:24:01 | [diff] [blame] | 248 | #endif |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 249 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | // Mark all the profiles as clean. |
| 251 | ProfileManager* pm = profile_manager(); |
| 252 | for (ProfileManager::const_iterator i = pm->begin(); i != pm->end(); ++i) |
| 253 | (*i)->MarkAsCleanShutdown(); |
| 254 | |
| 255 | // Tell the metrics service it was cleanly shutdown. |
| 256 | MetricsService* metrics = g_browser_process->metrics_service(); |
| 257 | if (metrics && local_state()) { |
| 258 | metrics->RecordCleanShutdown(); |
| 259 | |
| 260 | metrics->RecordStartOfSessionEnd(); |
| 261 | |
| 262 | // MetricsService lazily writes to prefs, force it to write now. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 263 | local_state()->SavePersistentPrefs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // We must write that the profile and metrics service shutdown cleanly, |
| 267 | // otherwise on startup we'll think we crashed. So we block until done and |
| 268 | // then proceed with normal shutdown. |
| 269 | g_browser_process->file_thread()->message_loop()->PostTask(FROM_HERE, |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 270 | NewRunnableFunction(PostQuit, MessageLoop::current())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 271 | MessageLoop::current()->Run(); |
| 272 | } |
| 273 | |
| 274 | printing::PrintJobManager* BrowserProcessImpl::print_job_manager() { |
| 275 | // TODO(abarth): DCHECK(CalledOnValidThread()); |
| 276 | // See <http://b/1287209>. |
| 277 | // print_job_manager_ is initialized in the constructor and destroyed in the |
| 278 | // destructor, so it should always be valid. |
| 279 | DCHECK(print_job_manager_.get()); |
| 280 | return print_job_manager_.get(); |
| 281 | } |
| 282 | |
[email protected] | d70539de | 2009-06-24 22:17:06 | [diff] [blame] | 283 | const std::string& BrowserProcessImpl::GetApplicationLocale() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 284 | DCHECK(CalledOnValidThread()); |
| 285 | if (locale_.empty()) { |
[email protected] | d70539de | 2009-06-24 22:17:06 | [diff] [blame] | 286 | locale_ = l10n_util::GetApplicationLocale( |
| 287 | local_state()->GetString(prefs::kApplicationLocale)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 288 | } |
| 289 | return locale_; |
| 290 | } |
| 291 | |
| 292 | void BrowserProcessImpl::CreateResourceDispatcherHost() { |
| 293 | DCHECK(!created_resource_dispatcher_host_ && |
| 294 | resource_dispatcher_host_.get() == NULL); |
| 295 | created_resource_dispatcher_host_ = true; |
| 296 | |
| 297 | resource_dispatcher_host_.reset( |
| 298 | new ResourceDispatcherHost(io_thread()->message_loop())); |
| 299 | resource_dispatcher_host_->Initialize(); |
| 300 | } |
| 301 | |
| 302 | void BrowserProcessImpl::CreateMetricsService() { |
| 303 | DCHECK(!created_metrics_service_ && metrics_service_.get() == NULL); |
| 304 | created_metrics_service_ = true; |
| 305 | |
| 306 | metrics_service_.reset(new MetricsService); |
| 307 | } |
| 308 | |
| 309 | void BrowserProcessImpl::CreateIOThread() { |
| 310 | DCHECK(!created_io_thread_ && io_thread_.get() == NULL); |
| 311 | created_io_thread_ = true; |
| 312 | |
| 313 | // Prior to starting the io thread, we create the plugin service as |
| 314 | // it is predominantly used from the io thread, but must be created |
| 315 | // on the main thread. The service ctor is inexpensive and does not |
| 316 | // invoke the io_thread() accessor. |
| 317 | PluginService::GetInstance(); |
| 318 | |
[email protected] | 4c3cd741 | 2009-04-22 17:56:06 | [diff] [blame] | 319 | #if defined(OS_LINUX) |
| 320 | // The lifetime of the BACKGROUND_X11 thread is a subset of the IO thread so |
| 321 | // we start it now. |
| 322 | scoped_ptr<base::Thread> background_x11_thread( |
| 323 | new BrowserProcessSubThread(ChromeThread::BACKGROUND_X11)); |
| 324 | if (!background_x11_thread->Start()) |
| 325 | return; |
| 326 | background_x11_thread_.swap(background_x11_thread); |
| 327 | #endif |
| 328 | |
[email protected] | 48ca901 | 2009-08-11 21:38:54 | [diff] [blame] | 329 | scoped_ptr<base::Thread> thread(new IOThread); |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 330 | base::Thread::Options options; |
| 331 | options.message_loop_type = MessageLoop::TYPE_IO; |
| 332 | if (!thread->StartWithOptions(options)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 333 | return; |
| 334 | io_thread_.swap(thread); |
| 335 | } |
| 336 | |
[email protected] | 48ca901 | 2009-08-11 21:38:54 | [diff] [blame] | 337 | void BrowserProcessImpl::ResetIOThread() { |
| 338 | if (io_thread_.get()) { |
| 339 | io_thread_->message_loop()->PostTask(FROM_HERE, |
| 340 | NewRunnableFunction(CleanupOnIOThread)); |
| 341 | } |
| 342 | io_thread_.reset(); |
| 343 | } |
| 344 | |
| 345 | // static |
| 346 | void BrowserProcessImpl::CleanupOnIOThread() { |
| 347 | // Shutdown DNS prefetching now to ensure that network stack objects |
| 348 | // living on the IO thread get destroyed before the IO thread goes away. |
| 349 | chrome_browser_net::EnsureDnsPrefetchShutdown(); |
| 350 | // TODO(eroman): can this be merged into IOThread::CleanUp() ? |
| 351 | } |
| 352 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | void BrowserProcessImpl::CreateFileThread() { |
| 354 | DCHECK(!created_file_thread_ && file_thread_.get() == NULL); |
| 355 | created_file_thread_ = true; |
| 356 | |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 357 | scoped_ptr<base::Thread> thread( |
| 358 | new BrowserProcessSubThread(ChromeThread::FILE)); |
[email protected] | a1db384 | 2008-09-17 22:04:06 | [diff] [blame] | 359 | base::Thread::Options options; |
[email protected] | 9e549b58 | 2009-02-05 21:13:39 | [diff] [blame] | 360 | #if defined(OS_WIN) |
| 361 | // On Windows, the FILE thread needs to be have a UI message loop which pumps |
| 362 | // messages in such a way that Google Update can communicate back to us. |
[email protected] | a1db384 | 2008-09-17 22:04:06 | [diff] [blame] | 363 | options.message_loop_type = MessageLoop::TYPE_UI; |
[email protected] | 9e549b58 | 2009-02-05 21:13:39 | [diff] [blame] | 364 | #else |
| 365 | options.message_loop_type = MessageLoop::TYPE_IO; |
| 366 | #endif |
[email protected] | a1db384 | 2008-09-17 22:04:06 | [diff] [blame] | 367 | if (!thread->StartWithOptions(options)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 368 | return; |
| 369 | file_thread_.swap(thread); |
| 370 | } |
| 371 | |
| 372 | void BrowserProcessImpl::CreateDBThread() { |
| 373 | DCHECK(!created_db_thread_ && db_thread_.get() == NULL); |
| 374 | created_db_thread_ = true; |
| 375 | |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 376 | scoped_ptr<base::Thread> thread( |
| 377 | new BrowserProcessSubThread(ChromeThread::DB)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 378 | if (!thread->Start()) |
| 379 | return; |
| 380 | db_thread_.swap(thread); |
| 381 | } |
| 382 | |
| 383 | void BrowserProcessImpl::CreateProfileManager() { |
| 384 | DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL); |
| 385 | created_profile_manager_ = true; |
| 386 | |
| 387 | profile_manager_.reset(new ProfileManager()); |
| 388 | } |
| 389 | |
| 390 | void BrowserProcessImpl::CreateLocalState() { |
| 391 | DCHECK(!created_local_state_ && local_state_.get() == NULL); |
| 392 | created_local_state_ = true; |
| 393 | |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 394 | FilePath local_state_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 395 | PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 396 | local_state_.reset(new PrefService(local_state_path, file_thread())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 397 | } |
| 398 | |
[email protected] | f3a4f30 | 2009-08-21 22:35:29 | [diff] [blame] | 399 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 400 | void BrowserProcessImpl::InitBrokerServices( |
| 401 | sandbox::BrokerServices* broker_services) { |
| 402 | DCHECK(!initialized_broker_services_ && broker_services_ == NULL); |
| 403 | broker_services->Init(); |
| 404 | initialized_broker_services_ = true; |
| 405 | broker_services_ = broker_services; |
| 406 | } |
[email protected] | f3a4f30 | 2009-08-21 22:35:29 | [diff] [blame] | 407 | #endif // defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 408 | |
| 409 | void BrowserProcessImpl::CreateIconManager() { |
| 410 | DCHECK(!created_icon_manager_ && icon_manager_.get() == NULL); |
| 411 | created_icon_manager_ = true; |
| 412 | icon_manager_.reset(new IconManager); |
| 413 | } |
| 414 | |
| 415 | void BrowserProcessImpl::CreateDebuggerWrapper(int port) { |
| 416 | DCHECK(debugger_wrapper_.get() == NULL); |
| 417 | created_debugger_wrapper_ = true; |
| 418 | |
| 419 | debugger_wrapper_ = new DebuggerWrapper(port); |
| 420 | } |
| 421 | |
[email protected] | 40ecc90 | 2009-03-16 13:42:47 | [diff] [blame] | 422 | void BrowserProcessImpl::CreateDevToolsManager() { |
[email protected] | 73ee0152 | 2009-06-05 10:13:44 | [diff] [blame] | 423 | DCHECK(devtools_manager_.get() == NULL); |
[email protected] | 40ecc90 | 2009-03-16 13:42:47 | [diff] [blame] | 424 | created_devtools_manager_ = true; |
[email protected] | 73ee0152 | 2009-06-05 10:13:44 | [diff] [blame] | 425 | devtools_manager_ = new DevToolsManager(); |
[email protected] | 40ecc90 | 2009-03-16 13:42:47 | [diff] [blame] | 426 | } |
| 427 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 428 | void BrowserProcessImpl::CreateGoogleURLTracker() { |
| 429 | DCHECK(google_url_tracker_.get() == NULL); |
| 430 | scoped_ptr<GoogleURLTracker> google_url_tracker(new GoogleURLTracker); |
| 431 | google_url_tracker_.swap(google_url_tracker); |
| 432 | } |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 433 | |
| 434 | // The BrowserProcess object must outlive the file thread so we use traits |
| 435 | // which don't do any management. |
| 436 | template <> |
| 437 | struct RunnableMethodTraits<BrowserProcessImpl> { |
[email protected] | 39fe32a | 2009-09-30 04:29:20 | [diff] [blame] | 438 | void RetainCallee(BrowserProcessImpl*) {} |
| 439 | void ReleaseCallee(BrowserProcessImpl*) {} |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 440 | }; |
| 441 | |
| 442 | void BrowserProcessImpl::CheckForInspectorFiles() { |
| 443 | file_thread()->message_loop()->PostTask |
| 444 | (FROM_HERE, |
| 445 | NewRunnableMethod(this, &BrowserProcessImpl::DoInspectorFilesCheck)); |
| 446 | } |
| 447 | |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 448 | #if defined(IPC_MESSAGE_LOG_ENABLED) |
| 449 | |
| 450 | void BrowserProcessImpl::SetIPCLoggingEnabled(bool enable) { |
| 451 | // First enable myself. |
| 452 | if (enable) |
| 453 | IPC::Logging::current()->Enable(); |
| 454 | else |
| 455 | IPC::Logging::current()->Disable(); |
| 456 | |
| 457 | // Now tell subprocesses. Messages to ChildProcess-derived |
| 458 | // processes must be done on the IO thread. |
| 459 | io_thread()->message_loop()->PostTask |
| 460 | (FROM_HERE, |
| 461 | NewRunnableMethod( |
| 462 | this, |
| 463 | &BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses, |
| 464 | enable)); |
| 465 | |
| 466 | // Finally, tell the renderers which don't derive from ChildProcess. |
| 467 | // Messages to the renderers must be done on the UI (main) thread. |
[email protected] | 019191a | 2009-10-02 20:37:27 | [diff] [blame] | 468 | for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 469 | !i.IsAtEnd(); i.Advance()) |
| 470 | i.GetCurrentValue()->Send(new ViewMsg_SetIPCLoggingEnabled(enable)); |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | // Helper for SetIPCLoggingEnabled. |
| 474 | void BrowserProcessImpl::SetIPCLoggingEnabledForChildProcesses(bool enabled) { |
| 475 | DCHECK(MessageLoop::current() == |
| 476 | ChromeThread::GetMessageLoop(ChromeThread::IO)); |
| 477 | |
| 478 | ChildProcessHost::Iterator i; // default constr references a singleton |
[email protected] | 34cf97d | 2009-09-29 22:46:11 | [diff] [blame] | 479 | while (!i.Done()) { |
| 480 | i->Send(new PluginProcessMsg_SetIPCLoggingEnabled(enabled)); |
| 481 | ++i; |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | #endif // IPC_MESSAGE_LOG_ENABLED |
| 486 | |
[email protected] | 6641bf66 | 2009-08-21 00:34:09 | [diff] [blame] | 487 | void BrowserProcessImpl::DoInspectorFilesCheck() { |
| 488 | // Runs on FILE thread. |
| 489 | DCHECK(file_thread_->message_loop() == MessageLoop::current()); |
| 490 | bool result = false; |
| 491 | |
| 492 | FilePath inspector_dir; |
| 493 | if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) { |
| 494 | result = file_util::PathExists(inspector_dir); |
| 495 | } |
| 496 | |
| 497 | have_inspector_files_ = result; |
| 498 | } |