| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ios/web/app/web_main_loop.h" | 5 #include "ios/web/app/web_main_loop.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/power_monitor/power_monitor.h" | 17 #include "base/power_monitor/power_monitor.h" |
| 18 #include "base/power_monitor/power_monitor_device_source.h" | 18 #include "base/power_monitor/power_monitor_device_source.h" |
| 19 #include "base/process/process_metrics.h" | 19 #include "base/process/process_metrics.h" |
| 20 #include "base/system_monitor/system_monitor.h" | 20 #include "base/system_monitor/system_monitor.h" |
| 21 #include "base/task_scheduler/initialization_util.h" |
| 22 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
| 23 #include "base/task_scheduler/task_scheduler.h" |
| 24 #include "base/task_scheduler/task_traits.h" |
| 21 #include "base/threading/thread_restrictions.h" | 25 #include "base/threading/thread_restrictions.h" |
| 22 #include "ios/web/net/cookie_notification_bridge.h" | 26 #include "ios/web/net/cookie_notification_bridge.h" |
| 23 #include "ios/web/public/app/web_main_parts.h" | 27 #include "ios/web/public/app/web_main_parts.h" |
| 24 #include "ios/web/public/web_client.h" | 28 #include "ios/web/public/web_client.h" |
| 25 #include "ios/web/web_thread_impl.h" | 29 #include "ios/web/web_thread_impl.h" |
| 26 #include "ios/web/webui/url_data_manager_ios.h" | 30 #include "ios/web/webui/url_data_manager_ios.h" |
| 27 #include "net/base/network_change_notifier.h" | 31 #include "net/base/network_change_notifier.h" |
| 28 | 32 |
| 29 #if !defined(__has_feature) || !__has_feature(objc_arc) | 33 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 30 #error "This file requires ARC support." | 34 #error "This file requires ARC support." |
| 31 #endif | 35 #endif |
| 32 | 36 |
| 33 namespace web { | 37 namespace web { |
| 34 | 38 |
| 39 namespace { |
| 40 |
| 41 enum WorkerPoolType : size_t { |
| 42 BACKGROUND = 0, |
| 43 BACKGROUND_FILE_IO, |
| 44 FOREGROUND, |
| 45 FOREGROUND_FILE_IO, |
| 46 WORKER_POOL_COUNT // Always last. |
| 47 }; |
| 48 |
| 49 std::vector<base::SchedulerWorkerPoolParams> |
| 50 GetDefaultSchedulerWorkerPoolParams() { |
| 51 using StandbyThreadPolicy = |
| 52 base::SchedulerWorkerPoolParams::StandbyThreadPolicy; |
| 53 using ThreadPriority = base::ThreadPriority; |
| 54 std::vector<base::SchedulerWorkerPoolParams> params_vector; |
| 55 params_vector.emplace_back( |
| 56 "Background", ThreadPriority::BACKGROUND, StandbyThreadPolicy::ONE, |
| 57 base::RecommendedMaxNumberOfThreadsInPool(2, 8, 0.1, 0), |
| 58 base::TimeDelta::FromSeconds(30)); |
| 59 params_vector.emplace_back( |
| 60 "BackgroundFileIO", ThreadPriority::BACKGROUND, StandbyThreadPolicy::ONE, |
| 61 base::RecommendedMaxNumberOfThreadsInPool(2, 8, 0.1, 0), |
| 62 base::TimeDelta::FromSeconds(30)); |
| 63 params_vector.emplace_back( |
| 64 "Foreground", ThreadPriority::NORMAL, StandbyThreadPolicy::ONE, |
| 65 base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.3, 0), |
| 66 base::TimeDelta::FromSeconds(30)); |
| 67 params_vector.emplace_back( |
| 68 "ForegroundFileIO", ThreadPriority::NORMAL, StandbyThreadPolicy::ONE, |
| 69 base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.1, 0), |
| 70 base::TimeDelta::FromSeconds(30)); |
| 71 DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size()); |
| 72 return params_vector; |
| 73 } |
| 74 |
| 75 // Returns the worker pool index for |traits| defaulting to FOREGROUND or |
| 76 // FOREGROUND_FILE_IO on any other priorities based off of worker pools defined |
| 77 // in GetDefaultSchedulerWorkerPoolParams(). |
| 78 size_t DefaultBrowserWorkerPoolIndexForTraits(const base::TaskTraits& traits) { |
| 79 const bool is_background = |
| 80 traits.priority() == base::TaskPriority::BACKGROUND; |
| 81 if (traits.with_file_io()) |
| 82 return is_background ? BACKGROUND_FILE_IO : FOREGROUND_FILE_IO; |
| 83 |
| 84 return is_background ? BACKGROUND : FOREGROUND; |
| 85 } |
| 86 |
| 87 } // namespace |
| 88 |
| 35 // The currently-running WebMainLoop. There can be one or zero. | 89 // The currently-running WebMainLoop. There can be one or zero. |
| 36 // TODO(rohitrao): Desktop uses this to implement | 90 // TODO(rohitrao): Desktop uses this to implement |
| 37 // ImmediateShutdownAndExitProcess. If we don't need that functionality, we can | 91 // ImmediateShutdownAndExitProcess. If we don't need that functionality, we can |
| 38 // remove this. | 92 // remove this. |
| 39 WebMainLoop* g_current_web_main_loop = nullptr; | 93 WebMainLoop* g_current_web_main_loop = nullptr; |
| 40 | 94 |
| 41 WebMainLoop::WebMainLoop() : result_code_(0), created_threads_(false) { | 95 WebMainLoop::WebMainLoop() : result_code_(0), created_threads_(false) { |
| 42 DCHECK(!g_current_web_main_loop); | 96 DCHECK(!g_current_web_main_loop); |
| 43 g_current_web_main_loop = this; | 97 g_current_web_main_loop = this; |
| 44 } | 98 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 | 162 |
| 109 int WebMainLoop::PreCreateThreads() { | 163 int WebMainLoop::PreCreateThreads() { |
| 110 if (parts_) { | 164 if (parts_) { |
| 111 parts_->PreCreateThreads(); | 165 parts_->PreCreateThreads(); |
| 112 } | 166 } |
| 113 | 167 |
| 114 return result_code_; | 168 return result_code_; |
| 115 } | 169 } |
| 116 | 170 |
| 117 int WebMainLoop::CreateThreads() { | 171 int WebMainLoop::CreateThreads() { |
| 172 std::vector<base::SchedulerWorkerPoolParams> params_vector; |
| 173 base::TaskScheduler::WorkerPoolIndexForTraitsCallback |
| 174 index_to_traits_callback; |
| 175 GetWebClient()->GetTaskSchedulerInitializationParams( |
| 176 ¶ms_vector, &index_to_traits_callback); |
| 177 |
| 178 if (params_vector.empty() || index_to_traits_callback.is_null()) { |
| 179 params_vector = GetDefaultSchedulerWorkerPoolParams(); |
| 180 index_to_traits_callback = |
| 181 base::Bind(&DefaultBrowserWorkerPoolIndexForTraits); |
| 182 } |
| 183 |
| 184 base::TaskScheduler::CreateAndSetDefaultTaskScheduler( |
| 185 params_vector, index_to_traits_callback); |
| 186 |
| 187 GetWebClient()->PerformExperimentalTaskSchedulerRedirections(); |
| 188 |
| 118 base::Thread::Options io_message_loop_options; | 189 base::Thread::Options io_message_loop_options; |
| 119 io_message_loop_options.message_loop_type = base::MessageLoop::TYPE_IO; | 190 io_message_loop_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 120 | 191 |
| 121 // Start threads in the order they occur in the WebThread::ID | 192 // Start threads in the order they occur in the WebThread::ID |
| 122 // enumeration, except for WebThread::UI which is the main | 193 // enumeration, except for WebThread::UI which is the main |
| 123 // thread. | 194 // thread. |
| 124 // | 195 // |
| 125 // Must be size_t so we can increment it. | 196 // Must be size_t so we can increment it. |
| 126 for (size_t thread_id = WebThread::UI + 1; thread_id < WebThread::ID_COUNT; | 197 for (size_t thread_id = WebThread::UI + 1; thread_id < WebThread::ID_COUNT; |
| 127 ++thread_id) { | 198 ++thread_id) { |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 } | 313 } |
| 243 } | 314 } |
| 244 | 315 |
| 245 // Close the blocking I/O pool after the other threads. Other threads such | 316 // Close the blocking I/O pool after the other threads. Other threads such |
| 246 // as the I/O thread may need to schedule work like closing files or flushing | 317 // as the I/O thread may need to schedule work like closing files or flushing |
| 247 // data during shutdown, so the blocking pool needs to be available. There | 318 // data during shutdown, so the blocking pool needs to be available. There |
| 248 // may also be slow operations pending that will block shutdown, so closing | 319 // may also be slow operations pending that will block shutdown, so closing |
| 249 // it here (which will block until required operations are complete) gives | 320 // it here (which will block until required operations are complete) gives |
| 250 // more head start for those operations to finish. | 321 // more head start for those operations to finish. |
| 251 WebThreadImpl::ShutdownThreadPool(); | 322 WebThreadImpl::ShutdownThreadPool(); |
| 323 base::TaskScheduler::GetInstance()->Shutdown(); |
| 252 | 324 |
| 253 URLDataManagerIOS::DeleteDataSources(); | 325 URLDataManagerIOS::DeleteDataSources(); |
| 254 | 326 |
| 255 if (parts_) { | 327 if (parts_) { |
| 256 parts_->PostDestroyThreads(); | 328 parts_->PostDestroyThreads(); |
| 257 } | 329 } |
| 258 } | 330 } |
| 259 | 331 |
| 260 void WebMainLoop::InitializeMainThread() { | 332 void WebMainLoop::InitializeMainThread() { |
| 261 base::PlatformThread::SetName("CrWebMain"); | 333 base::PlatformThread::SetName("CrWebMain"); |
| 262 | 334 |
| 263 // Register the main thread by instantiating it, but don't call any methods. | 335 // Register the main thread by instantiating it, but don't call any methods. |
| 264 main_thread_.reset( | 336 main_thread_.reset( |
| 265 new WebThreadImpl(WebThread::UI, base::MessageLoop::current())); | 337 new WebThreadImpl(WebThread::UI, base::MessageLoop::current())); |
| 266 } | 338 } |
| 267 | 339 |
| 268 int WebMainLoop::WebThreadsStarted() { | 340 int WebMainLoop::WebThreadsStarted() { |
| 269 cookie_notification_bridge_.reset(new CookieNotificationBridge); | 341 cookie_notification_bridge_.reset(new CookieNotificationBridge); |
| 270 return result_code_; | 342 return result_code_; |
| 271 } | 343 } |
| 272 | 344 |
| 273 } // namespace web | 345 } // namespace web |
| OLD | NEW |