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

Add additional global state to ios_global_state.

Expands on the shared initialization to include base::AtExitManager,
net::NetworkChangeNotifier, and base::MessageLoopForUI.

With this change Cronet and ChromeWebView can successfully be used when
built together in the framework created by
//ios/web_view:cronet_web_view_combined.

Bug: 738510
Cq-Include-Trybots: master.tryserver.chromium.android:android_cronet_tester
Change-Id: Iead4ba5d4f2376b09edf3f5c87a13829229b83b4
Reviewed-on: https://chromium-review.googlesource.com/558268
Commit-Queue: Mike Dougherty <michaeldo@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487161}
parent bcf45bcb
......@@ -12,7 +12,6 @@
#include "base/files/file_path.h"
#include "base/files/scoped_file.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/sys_string_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
......
......@@ -6,7 +6,6 @@
#include <utility>
#include "base/at_exit.h"
#include "base/atomicops.h"
#include "base/bind.h"
#include "base/command_line.h"
......@@ -55,11 +54,6 @@
namespace {
base::AtExitManager* g_at_exit_ = nullptr;
net::NetworkChangeNotifier* g_network_change_notifier = nullptr;
// MessageLoop on the main thread.
base::MessageLoop* g_main_message_loop = nullptr;
// Request context getter for Cronet.
class CronetURLRequestContextGetter : public net::URLRequestContextGetter {
public:
......@@ -128,28 +122,16 @@ net::URLRequestContextGetter* CronetEnvironment::GetURLRequestContextGetter()
void CronetEnvironment::Initialize() {
// This method must be called once from the main thread.
DCHECK_EQ([NSThread currentThread], [NSThread mainThread]);
if (!g_at_exit_)
g_at_exit_ = new base::AtExitManager;
// Initializes the statistics recorder system. This needs to be done before
// emitting histograms to prevent memory leaks (crbug.com/707836).
base::StatisticsRecorder::Initialize();
ios_global_state::Create();
ios_global_state::CreateParams create_params;
create_params.install_at_exit_manager = true;
ios_global_state::Create(create_params);
ios_global_state::StartTaskScheduler(/*init_params=*/nullptr);
url::Initialize();
base::CommandLine::Init(0, nullptr);
// Create a message loop on the UI thread.
DCHECK(!base::MessageLoop::current());
DCHECK(!g_main_message_loop);
g_main_message_loop = new base::MessageLoopForUI();
base::MessageLoopForUI::current()->Attach();
// The network change notifier must be initialized so that registered
// delegates will receive callbacks.
DCHECK(!g_network_change_notifier);
g_network_change_notifier = net::NetworkChangeNotifier::Create();
ios_global_state::BuildMessageLoop();
ios_global_state::CreateNetworkChangeNotifier();
}
bool CronetEnvironment::StartNetLog(base::FilePath::StringType file_name,
......@@ -282,7 +264,6 @@ CronetEnvironment::~CronetEnvironment() {
void CronetEnvironment::InitializeOnNetworkThread() {
DCHECK(network_io_thread_->task_runner()->BelongsToCurrentThread());
base::FeatureList::InitializeInstance(std::string(), std::string());
static bool ssl_key_log_file_set = false;
if (!ssl_key_log_file_set && !ssl_key_log_file_name_.empty()) {
......
......@@ -12,15 +12,10 @@
#include "ios/web/public/app/task_scheduler_init_params_callback.h"
namespace base {
class MessageLoop;
class PowerMonitor;
class SystemMonitor;
} // namespace base
namespace net {
class NetworkChangeNotifier;
} // namespace net
namespace web {
class CookieNotificationBridge;
class ServiceManagerContext;
......@@ -73,10 +68,8 @@ class WebMainLoop {
bool created_threads_;
// Members initialized in |MainMessageLoopStart()| ---------------------------
std::unique_ptr<base::MessageLoop> main_message_loop_;
std::unique_ptr<base::SystemMonitor> system_monitor_;
std::unique_ptr<base::PowerMonitor> power_monitor_;
std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_;
// Destroy parts_ before main_message_loop_ (required) and before other
// classes constructed in web (but after main_thread_).
......
......@@ -11,7 +11,6 @@
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram_macros.h"
#include "base/path_service.h"
#include "base/power_monitor/power_monitor.h"
......@@ -24,12 +23,11 @@
#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"
#include "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"
#include "ios/web/webui/url_data_manager_ios.h"
#include "net/base/network_change_notifier.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -46,8 +44,6 @@ WebMainLoop* g_current_web_main_loop = nullptr;
WebMainLoop::WebMainLoop() : result_code_(0), created_threads_(false) {
DCHECK(!g_current_web_main_loop);
g_current_web_main_loop = this;
ios_global_state::Create();
}
WebMainLoop::~WebMainLoop() {
......@@ -71,11 +67,7 @@ void WebMainLoop::MainMessageLoopStart() {
parts_->PreMainMessageLoopStart();
}
// Create a MessageLoop if one does not already exist for the current thread.
if (!base::MessageLoop::current()) {
main_message_loop_.reset(new base::MessageLoopForUI);
}
base::MessageLoopForUI::current()->Attach();
ios_global_state::BuildMessageLoop();
InitializeMainThread();
......@@ -87,7 +79,8 @@ void WebMainLoop::MainMessageLoopStart() {
std::unique_ptr<base::PowerMonitorSource> power_monitor_source(
new base::PowerMonitorDeviceSource());
power_monitor_.reset(new base::PowerMonitor(std::move(power_monitor_source)));
network_change_notifier_.reset(net::NetworkChangeNotifier::Create());
ios_global_state::CreateNetworkChangeNotifier();
if (parts_) {
parts_->PostMainMessageLoopStart();
......
......@@ -4,13 +4,11 @@
#include "ios/web/public/app/web_main_runner.h"
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/i18n/icu_util.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/metrics/statistics_recorder.h"
#include "ios/web/app/web_main_loop.h"
#include "ios/web/public/global_state/ios_global_state.h"
#include "ios/web/public/url_schemes.h"
#import "ios/web/public/web_client.h"
#include "mojo/edk/embedder/embedder.h"
......@@ -43,11 +41,12 @@ class WebMainRunnerImpl : public WebMainRunner {
is_initialized_ = true;
delegate_ = params.delegate;
if (params.register_exit_manager) {
exit_manager_.reset(new base::AtExitManager);
}
ios_global_state::CreateParams create_params;
create_params.install_at_exit_manager = params.register_exit_manager;
create_params.argc = params.argc;
create_params.argv = params.argv;
ios_global_state::Create(create_params);
base::CommandLine::Init(params.argc, params.argv);
if (delegate_) {
delegate_->BasicStartupComplete();
}
......@@ -67,7 +66,6 @@ class WebMainRunnerImpl : public WebMainRunner {
////////////////////////////////////////////////////////////
// BrowserMainRunnerImpl::Initialize()
base::StatisticsRecorder::Initialize();
main_loop_.reset(new WebMainLoop());
main_loop_->Init();
......@@ -99,7 +97,8 @@ class WebMainRunnerImpl : public WebMainRunner {
delegate_->ProcessExiting();
}
exit_manager_.reset(nullptr);
ios_global_state::DestroyAtExitManager();
delegate_ = nullptr;
is_shutdown_ = true;
}
......@@ -120,7 +119,6 @@ class WebMainRunnerImpl : public WebMainRunner {
// Used if the embedder doesn't set one.
WebClient empty_web_client_;
std::unique_ptr<base::AtExitManager> exit_manager_;
std::unique_ptr<WebMainLoop> main_loop_;
DISALLOW_COPY_AND_ASSIGN(WebMainRunnerImpl);
......
......@@ -7,6 +7,7 @@ source_set("global_state") {
deps = [
"//base",
"//net",
]
sources = [
......
......@@ -9,10 +9,38 @@
namespace ios_global_state {
// Contains parameters passed to |Create|.
struct CreateParams {
CreateParams() : install_at_exit_manager(false), argc(0), argv(nullptr) {}
bool install_at_exit_manager;
int argc;
const char** argv;
};
// 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();
//
// An AtExitManager will only be created if |register_exit_manager| is true. If
// |register_exit_manager| is false, an AtExitManager must already exist before
// calling |Create|.
// |argc| and |argv| may be set to the command line options which were passed to
// the application.
//
// Since the initialization will only be performed the first time this method is
// called, the values of all the parameters will be ignored after the first
// call.
void Create(const CreateParams& create_params);
// Creates a message loop for the UI thread and attaches it. It is safe to call
// this method more than once, the initialization will only be performed once.
void BuildMessageLoop();
// Creates a network change notifier. It is safe to call this method more than
// once, the initialization will only be performed once.
void CreateNetworkChangeNotifier();
// 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,
......@@ -20,6 +48,12 @@ void Create();
// once, the task scheduler will only be started once.
void StartTaskScheduler(base::TaskScheduler::InitParams* init_params);
// Destroys the AtExitManager if one was created in |Create|. It is safe to call
// this method even if |install_at_exit_manager| was false in the CreateParams
// passed to |Create|. It is safe to call this method more than once, the
// AtExitManager will be destroyed on the first call.
void DestroyAtExitManager();
} // namespace ios_global_state
#endif // IOS_WEB_PUBLIC_GLOBAL_STATE_IOS_GLOBAL_STATE_H_
......@@ -4,8 +4,13 @@
#include "ios/web/public/global_state/ios_global_state.h"
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/statistics_recorder.h"
#include "base/task_scheduler/initialization_util.h"
#include "net/base/network_change_notifier.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -13,6 +18,8 @@
namespace {
base::AtExitManager* g_exit_manager = nullptr;
base::TaskScheduler::InitParams GetDefaultTaskSchedulerInitParams() {
using StandbyThreadPolicy =
base::SchedulerWorkerPoolParams::StandbyThreadPolicy;
......@@ -39,12 +46,43 @@ base::TaskScheduler::InitParams GetDefaultTaskSchedulerInitParams() {
namespace ios_global_state {
void Create() {
void Create(const CreateParams& create_params) {
static dispatch_once_t once_token;
dispatch_once(&once_token, ^{
if (create_params.install_at_exit_manager) {
g_exit_manager = new base::AtExitManager();
}
// Use an empty string as TaskScheduler name to match the suffix of browser
// process TaskScheduler histograms.
base::TaskScheduler::Create("");
base::CommandLine::Init(create_params.argc, create_params.argv);
base::StatisticsRecorder::Initialize();
});
}
void BuildMessageLoop() {
static std::unique_ptr<base::MessageLoopForUI> main_message_loop;
static dispatch_once_t once_token;
dispatch_once(&once_token, ^{
// Create a MessageLoop if one does not already exist for the current
// thread.
if (!base::MessageLoop::current()) {
main_message_loop = base::MakeUnique<base::MessageLoopForUI>();
}
base::MessageLoopForUI::current()->Attach();
});
}
void CreateNetworkChangeNotifier() {
static std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier;
static dispatch_once_t once_token;
dispatch_once(&once_token, ^{
network_change_notifier.reset(net::NetworkChangeNotifier::Create());
});
}
......@@ -56,4 +94,9 @@ void StartTaskScheduler(base::TaskScheduler::InitParams* params) {
});
}
void DestroyAtExitManager() {
delete g_exit_manager;
g_exit_manager = nullptr;
}
} // namespace ios_global_state
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