Commit e1071718 authored by Mike Dougherty's avatar Mike Dougherty Committed by Commit Bot

Share task scheduler initialization between ios/web_view and cronet.

Bug: 738510
Cq-Include-Trybots: master.tryserver.chromium.android:android_cronet_tester
Change-Id: I0095d6f8d4959d1fa797f64ff45885348780bed8
Reviewed-on: https://chromium-review.googlesource.com/552763Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Reviewed-by: default avatarMisha Efimov <mef@chromium.org>
Commit-Queue: Mike Dougherty <michaeldo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#486018}
parent c605569b
......@@ -59,6 +59,7 @@ _cronet_deps = [
"//components/prefs:prefs",
"//ios/net:net",
"//ios/web:user_agent",
"//ios/web/public/global_state",
"//net",
"//url",
]
......
include_rules = [
"+ios/net",
"+ios/web",
"+ios/web/public",
]
......@@ -22,12 +22,12 @@
#include "base/path_service.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/waitable_event.h"
#include "base/task_scheduler/task_scheduler.h"
#include "components/cronet/histogram_manager.h"
#include "components/cronet/ios/version.h"
#include "components/prefs/json_pref_store.h"
#include "components/prefs/pref_filter.h"
#include "ios/net/cookies/cookie_store_ios.h"
#include "ios/web/public/global_state/ios_global_state.h"
#include "ios/web/public/user_agent.h"
#include "net/base/network_change_notifier.h"
#include "net/cert/cert_verifier.h"
......@@ -118,7 +118,8 @@ void CronetEnvironment::Initialize() {
if (!g_at_exit_)
g_at_exit_ = new base::AtExitManager;
base::TaskScheduler::CreateAndStartWithDefaultParams("CronetIos");
ios_global_state::Create();
ios_global_state::StartTaskScheduler(/*init_params=*/nullptr);
url::Initialize();
base::CommandLine::Init(0, nullptr);
......
......@@ -17,6 +17,7 @@ include_rules = [
"+components/reading_list/core",
"+components/signin/core/browser",
"+components/suggestions",
"+components/task_scheduler_util",
"+components/url_formatter",
"+components/web_resource",
"+ios/chrome/browser",
......
......@@ -24,6 +24,7 @@ source_set("startup_basic") {
deps = [
"//base",
"//components/crash/core/common",
"//components/task_scheduler_util/browser",
"//ios/chrome/browser:chrome_paths",
"//ios/web/public/app",
"//skia",
......
......@@ -8,9 +8,11 @@
#include <vector>
#include "base/bind.h"
#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "base/time/time.h"
#include "components/task_scheduler_util/browser/initialization.h"
#include "ios/web/public/app/web_main_runner.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
......@@ -24,7 +26,7 @@ base::Time* g_start_time;
IOSChromeMain::IOSChromeMain() {
web_main_runner_.reset(web::WebMainRunner::Create());
web::WebMainParams main_params = web::WebMainParams(&main_delegate_);
web::WebMainParams main_params(&main_delegate_);
// Copy NSProcessInfo arguments into WebMainParams in debug only, since
// command line should be meaningless outside of developer builds.
#if !defined(NDEBUG)
......@@ -47,10 +49,12 @@ IOSChromeMain::IOSChromeMain() {
main_params.argv = argv;
#endif
main_params.get_task_scheduler_init_params_callback = base::Bind(
&task_scheduler_util::GetBrowserTaskSchedulerInitParamsFromVariations);
// Chrome registers an AtExitManager in main in order to initialize breakpad
// early, so prevent a second registration by WebMainRunner.
main_params.register_exit_manager = false;
web_main_runner_->Initialize(main_params);
web_main_runner_->Initialize(std::move(main_params));
}
IOSChromeMain::~IOSChromeMain() {
......
......@@ -44,8 +44,6 @@ class ChromeWebClient : public web::WebClient {
const GURL& request_url,
bool overridable,
const base::Callback<void(bool)>& callback) override;
std::unique_ptr<base::TaskScheduler::InitParams> GetTaskSchedulerInitParams()
override;
bool IsSlimNavigationManagerEnabled() const override;
private:
......
......@@ -15,7 +15,6 @@
#include "components/payments/core/features.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "components/task_scheduler_util/browser/initialization.h"
#include "components/version_info/version_info.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/browser_about_rewriter.h"
......@@ -187,11 +186,6 @@ void ChromeWebClient::AllowCertificateError(
overridable, callback);
}
std::unique_ptr<base::TaskScheduler::InitParams>
ChromeWebClient::GetTaskSchedulerInitParams() {
return task_scheduler_util::GetBrowserTaskSchedulerInitParamsFromVariations();
}
bool ChromeWebClient::IsSlimNavigationManagerEnabled() const {
return experimental_flags::IsSlimNavigationManagerEnabled();
}
......@@ -19,6 +19,7 @@ source_set("app") {
"//base:i18n",
"//crypto",
"//ios/web",
"//ios/web/public/global_state",
"//mojo/edk/system",
"//net",
"//ui/base",
......
......@@ -11,9 +11,24 @@
namespace web {
WebMain::WebMain(const WebMainParams& params) {
WebMainParams::WebMainParams() : WebMainParams(nullptr) {}
WebMainParams::WebMainParams(WebMainDelegate* delegate)
: delegate(delegate),
register_exit_manager(true),
get_task_scheduler_init_params_callback(nullptr),
argc(0),
argv(nullptr) {}
WebMainParams::~WebMainParams() = default;
WebMainParams::WebMainParams(WebMainParams&& other) = default;
WebMainParams& WebMainParams::operator=(web::WebMainParams&& other) = default;
WebMain::WebMain(WebMainParams params) {
web_main_runner_.reset(WebMainRunner::Create());
web_main_runner_->Initialize(params);
web_main_runner_->Initialize(std::move(params));
}
WebMain::~WebMain() {
......
......@@ -9,6 +9,7 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "ios/web/public/app/task_scheduler_init_params_callback.h"
namespace base {
class MessageLoop;
......@@ -38,8 +39,10 @@ class WebMainLoop {
void EarlyInitialization();
void MainMessageLoopStart();
// Creates and starts running the tasks needed to complete startup.
void CreateStartupTasks();
// Creates and starts running the tasks needed to complete startup. The
// |init_params_callback| may be null or supply InitParams to be used to start
// the global TaskScheduler instead of using the defaults.
void CreateStartupTasks(TaskSchedulerInitParamsCallback init_params_callback);
// Performs the shutdown sequence, starting with PostMainMessageLoopRun
// through stopping threads to PostDestroyThreads.
......@@ -53,8 +56,10 @@ class WebMainLoop {
// Called just before creating the threads
int PreCreateThreads();
// Creates all secondary threads.
int CreateThreads();
// Creates all secondary threads. The |init_params_callback| may be null or
// supply InitParams to be used to start the global TaskScheduler instead of
// using the defaults.
int CreateThreads(TaskSchedulerInitParamsCallback init_params_callback);
// Called right after the web threads have been started.
int WebThreadsStarted();
......
......@@ -18,13 +18,13 @@
#include "base/power_monitor/power_monitor_device_source.h"
#include "base/process/process_metrics.h"
#include "base/system_monitor/system_monitor.h"
#include "base/task_scheduler/initialization_util.h"
#include "base/task_scheduler/scheduler_worker_pool_params.h"
#include "base/task_scheduler/task_scheduler.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/thread_restrictions.h"
#import "ios/web/net/cookie_notification_bridge.h"
#include "ios/web/public/app/web_main_parts.h"
#import "ios/web/public/global_state/ios_global_state.h"
#import "ios/web/public/web_client.h"
#include "ios/web/service_manager_context.h"
#include "ios/web/web_thread_impl.h"
......@@ -37,33 +37,6 @@
namespace web {
namespace {
std::unique_ptr<base::TaskScheduler::InitParams>
GetDefaultTaskSchedulerInitParams() {
using StandbyThreadPolicy =
base::SchedulerWorkerPoolParams::StandbyThreadPolicy;
return base::MakeUnique<base::TaskScheduler::InitParams>(
base::SchedulerWorkerPoolParams(
StandbyThreadPolicy::ONE,
base::RecommendedMaxNumberOfThreadsInPool(2, 8, 0.1, 0),
base::TimeDelta::FromSeconds(30)),
base::SchedulerWorkerPoolParams(
StandbyThreadPolicy::ONE,
base::RecommendedMaxNumberOfThreadsInPool(2, 8, 0.1, 0),
base::TimeDelta::FromSeconds(30)),
base::SchedulerWorkerPoolParams(
StandbyThreadPolicy::ONE,
base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.3, 0),
base::TimeDelta::FromSeconds(30)),
base::SchedulerWorkerPoolParams(
StandbyThreadPolicy::ONE,
base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.3, 0),
base::TimeDelta::FromSeconds(60)));
}
} // namespace
// The currently-running WebMainLoop. There can be one or zero.
// TODO(rohitrao): Desktop uses this to implement
// ImmediateShutdownAndExitProcess. If we don't need that functionality, we can
......@@ -74,9 +47,7 @@ WebMainLoop::WebMainLoop() : result_code_(0), created_threads_(false) {
DCHECK(!g_current_web_main_loop);
g_current_web_main_loop = this;
// Use an empty string as TaskScheduler name to match the suffix of browser
// process TaskScheduler histograms.
base::TaskScheduler::Create("");
ios_global_state::Create();
}
WebMainLoop::~WebMainLoop() {
......@@ -123,13 +94,14 @@ void WebMainLoop::MainMessageLoopStart() {
}
}
void WebMainLoop::CreateStartupTasks() {
void WebMainLoop::CreateStartupTasks(
TaskSchedulerInitParamsCallback init_params_callback) {
int result = 0;
result = PreCreateThreads();
if (result > 0)
return;
result = CreateThreads();
result = CreateThreads(std::move(init_params_callback));
if (result > 0)
return;
......@@ -150,16 +122,13 @@ int WebMainLoop::PreCreateThreads() {
return result_code_;
}
int WebMainLoop::CreateThreads() {
{
auto task_scheduler_init_params =
GetWebClient()->GetTaskSchedulerInitParams();
if (!task_scheduler_init_params)
task_scheduler_init_params = GetDefaultTaskSchedulerInitParams();
DCHECK(task_scheduler_init_params);
base::TaskScheduler::GetInstance()->Start(
*task_scheduler_init_params.get());
int WebMainLoop::CreateThreads(
TaskSchedulerInitParamsCallback init_params_callback) {
std::unique_ptr<base::TaskScheduler::InitParams> init_params;
if (!init_params_callback.is_null()) {
init_params = std::move(init_params_callback).Run();
}
ios_global_state::StartTaskScheduler(init_params.get());
base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess();
......
......@@ -36,7 +36,7 @@ class WebMainRunnerImpl : public WebMainRunner {
}
}
int Initialize(const WebMainParams& params) override {
int Initialize(WebMainParams params) override {
////////////////////////////////////////////////////////////////////////
// ContentMainRunnerImpl::Initialize()
//
......@@ -73,7 +73,8 @@ class WebMainRunnerImpl : public WebMainRunner {
main_loop_->Init();
main_loop_->EarlyInitialization();
main_loop_->MainMessageLoopStart();
main_loop_->CreateStartupTasks();
main_loop_->CreateStartupTasks(
std::move(params.get_task_scheduler_init_params_callback));
int result_code = main_loop_->GetResultCode();
if (result_code > 0)
return result_code;
......
......@@ -6,6 +6,7 @@ import("//services/service_manager/public/service_manifest.gni")
source_set("app") {
sources = [
"task_scheduler_init_params_callback.h",
"web_main.h",
"web_main_delegate.h",
"web_main_parts.h",
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_PUBLIC_GLOBAL_STATE_TASK_SCHEDULER_INIT_PARAMS_CALLBACK_H_
#define IOS_WEB_PUBLIC_GLOBAL_STATE_TASK_SCHEDULER_INIT_PARAMS_CALLBACK_H_
#include "base/callback_forward.h"
#include "base/task_scheduler/task_scheduler.h"
namespace web {
// Callback which returns a pointer to InitParams for base::TaskScheduler.
typedef base::OnceCallback<std::unique_ptr<base::TaskScheduler::InitParams>()>
TaskSchedulerInitParamsCallback;
} // namespace web
#endif // IOS_WEB_PUBLIC_GLOBAL_STATE_TASK_SCHEDULER_INIT_PARAMS_CALLBACK_H_
......@@ -7,6 +7,8 @@
#include <memory>
#include "base/macros.h"
#include "ios/web/public/app/task_scheduler_init_params_callback.h"
#include "ios/web/public/app/web_main_delegate.h"
namespace web {
......@@ -14,18 +16,23 @@ class WebMainRunner;
// Contains parameters passed to WebMain.
struct WebMainParams {
explicit WebMainParams(WebMainDelegate* delegate)
: delegate(delegate),
register_exit_manager(true),
argc(0),
argv(nullptr) {}
WebMainParams();
explicit WebMainParams(WebMainDelegate* delegate);
~WebMainParams();
// WebMainParams is moveable.
WebMainParams(WebMainParams&& other);
WebMainParams& operator=(WebMainParams&& other);
WebMainDelegate* delegate;
bool register_exit_manager;
TaskSchedulerInitParamsCallback get_task_scheduler_init_params_callback;
int argc;
const char** argv;
DISALLOW_COPY_AND_ASSIGN(WebMainParams);
};
// Encapsulates any setup and initialization that is needed by common
......@@ -37,7 +44,7 @@ struct WebMainParams {
// in WebMainDelegate and WebMainParts.
class WebMain {
public:
explicit WebMain(const WebMainParams& params);
explicit WebMain(WebMainParams params);
~WebMain();
private:
......
......@@ -18,7 +18,7 @@ class WebMainRunner {
static WebMainRunner* Create();
// Initialize all necessary web state.
virtual int Initialize(const WebMainParams& params) = 0;
virtual int Initialize(WebMainParams params) = 0;
// Shut down the web state.
virtual void ShutDown() = 0;
......
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
source_set("global_state") {
configs += [ "//build/config/compiler:enable_arc" ]
deps = [
"//base",
]
sources = [
"ios_global_state.h",
"ios_global_state.mm",
]
}
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_PUBLIC_GLOBAL_STATE_IOS_GLOBAL_STATE_H_
#define IOS_WEB_PUBLIC_GLOBAL_STATE_IOS_GLOBAL_STATE_H_
#include "base/task_scheduler/task_scheduler.h"
namespace ios_global_state {
// Creates global state for iOS. This should be called as early as possible in
// the application lifecycle. It is safe to call this method more than once, the
// initialization will only be performed once.
void Create();
// Starts a global base::TaskScheduler. This method must be called to start
// the Task Scheduler that is created in |Create|. If |init_params| is null,
// default InitParams will be used. It is safe to call this method more than
// once, the task scheduler will only be started once.
void StartTaskScheduler(base::TaskScheduler::InitParams* init_params);
} // namespace ios_global_state
#endif // IOS_WEB_PUBLIC_GLOBAL_STATE_IOS_GLOBAL_STATE_H_
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/web/public/global_state/ios_global_state.h"
#include "base/memory/ptr_util.h"
#include "base/task_scheduler/initialization_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
base::TaskScheduler::InitParams GetDefaultTaskSchedulerInitParams() {
using StandbyThreadPolicy =
base::SchedulerWorkerPoolParams::StandbyThreadPolicy;
return base::TaskScheduler::InitParams(
base::SchedulerWorkerPoolParams(
StandbyThreadPolicy::ONE,
base::RecommendedMaxNumberOfThreadsInPool(2, 8, 0.1, 0),
base::TimeDelta::FromSeconds(30)),
base::SchedulerWorkerPoolParams(
StandbyThreadPolicy::ONE,
base::RecommendedMaxNumberOfThreadsInPool(2, 8, 0.1, 0),
base::TimeDelta::FromSeconds(30)),
base::SchedulerWorkerPoolParams(
StandbyThreadPolicy::ONE,
base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.3, 0),
base::TimeDelta::FromSeconds(30)),
base::SchedulerWorkerPoolParams(
StandbyThreadPolicy::ONE,
base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.3, 0),
base::TimeDelta::FromSeconds(60)));
}
} // namespace
namespace ios_global_state {
void Create() {
static dispatch_once_t once_token;
dispatch_once(&once_token, ^{
// Use an empty string as TaskScheduler name to match the suffix of browser
// process TaskScheduler histograms.
base::TaskScheduler::Create("");
});
}
void StartTaskScheduler(base::TaskScheduler::InitParams* params) {
static dispatch_once_t once_token;
dispatch_once(&once_token, ^{
auto init_params = params ? *params : GetDefaultTaskSchedulerInitParams();
base::TaskScheduler::GetInstance()->Start(init_params);
});
}
} // namespace ios_global_state
......@@ -141,11 +141,6 @@ class WebClient {
bool overridable,
const base::Callback<void(bool)>& callback);
// Provides parameters for initializing the global task scheduler. Default
// params are used if this returns nullptr.
virtual std::unique_ptr<base::TaskScheduler::InitParams>
GetTaskSchedulerInitParams();
// Allows upper layers to inject experimental flags to the web layer.
// TODO(crbug.com/734150): Clean up this flag after experiment. If need for a
// second flag arises before clean up, consider generalizing to an experiment
......
......@@ -91,11 +91,6 @@ void WebClient::AllowCertificateError(
callback.Run(false);
}
std::unique_ptr<base::TaskScheduler::InitParams>
WebClient::GetTaskSchedulerInitParams() {
return nullptr;
}
bool WebClient::IsSlimNavigationManagerEnabled() const {
return false;
}
......
......@@ -7,6 +7,7 @@
#include <memory>
#import "base/mac/scoped_nsobject.h"
#include "base/memory/ptr_util.h"
#include "ios/web/public/app/web_main.h"
#import "ios/web/public/web_client.h"
#import "ios/web/public/web_state/web_state.h"
......@@ -36,8 +37,9 @@
self.window.backgroundColor = [UIColor whiteColor];
_delegate.reset(new web::ShellMainDelegate());
web::WebMainParams params(_delegate.get());
_webMain.reset(new web::WebMain(params));
_webMain = base::MakeUnique<web::WebMain>(std::move(params));
web::ShellWebClient* client =
static_cast<web::ShellWebClient*>(web::GetWebClient());
......
......@@ -24,7 +24,7 @@ void InitializeGlobalState() {
web_main_delegate =
base::MakeUnique<ios_web_view::WebViewWebMainDelegate>();
web::WebMainParams params(web_main_delegate.get());
web_main = base::MakeUnique<web::WebMain>(params);
web_main = base::MakeUnique<web::WebMain>(std::move(params));
});
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment