1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/cronet/cronet_global_state.h"
6
7 #include <tuple>
8
9 #include "base/at_exit.h"
10 #include "base/feature_list.h"
11 #include "base/task/sequenced_task_runner.h"
12 #include "base/task/single_thread_task_runner.h"
13 #include "base/task/thread_pool.h"
14 #include "base/task/thread_pool/thread_pool_instance.h"
15 #include "net/proxy_resolution/configured_proxy_resolution_service.h"
16 #include "net/proxy_resolution/proxy_config_service.h"
17
18 // This file provides minimal "stub" implementations of the Cronet global-state
19 // functions for the native library build, sufficient to have cronet_tests and
20 // cronet_unittests build.
21
22 namespace cronet {
23
24 namespace {
25
InitializeAndCreateTaskRunner()26 scoped_refptr<base::SingleThreadTaskRunner> InitializeAndCreateTaskRunner() {
27 // Cronet tests sets AtExitManager as part of TestSuite, so statically linked
28 // library is not allowed to set its own.
29 #if !defined(CRONET_TESTS_IMPLEMENTATION)
30 std::ignore = new base::AtExitManager;
31 #endif
32
33 base::FeatureList::InitInstance(std::string(), std::string());
34
35 // Note that in component builds this ThreadPoolInstance will be shared with
36 // the calling process, if it also depends on //base. In particular this means
37 // that the Cronet test binaries must avoid initializing or shutting-down the
38 // ThreadPoolInstance themselves.
39 base::ThreadPoolInstance::CreateAndStartWithDefaultParams("cronet");
40
41 return base::ThreadPool::CreateSingleThreadTaskRunner({});
42 }
43
InitTaskRunner()44 base::SingleThreadTaskRunner* InitTaskRunner() {
45 static scoped_refptr<base::SingleThreadTaskRunner> init_task_runner =
46 InitializeAndCreateTaskRunner();
47 return init_task_runner.get();
48 }
49
50 } // namespace
51
EnsureInitialized()52 void EnsureInitialized() {
53 std::ignore = InitTaskRunner();
54 }
55
OnInitThread()56 bool OnInitThread() {
57 return InitTaskRunner()->BelongsToCurrentThread();
58 }
59
PostTaskToInitThread(const base::Location & posted_from,base::OnceClosure task)60 void PostTaskToInitThread(const base::Location& posted_from,
61 base::OnceClosure task) {
62 InitTaskRunner()->PostTask(posted_from, std::move(task));
63 }
64
CreateProxyConfigService(const scoped_refptr<base::SequencedTaskRunner> & io_task_runner)65 std::unique_ptr<net::ProxyConfigService> CreateProxyConfigService(
66 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) {
67 return net::ProxyConfigService::CreateSystemProxyConfigService(
68 io_task_runner);
69 }
70
CreateProxyResolutionService(std::unique_ptr<net::ProxyConfigService> proxy_config_service,net::NetLog * net_log)71 std::unique_ptr<net::ProxyResolutionService> CreateProxyResolutionService(
72 std::unique_ptr<net::ProxyConfigService> proxy_config_service,
73 net::NetLog* net_log) {
74 return net::ConfiguredProxyResolutionService::CreateUsingSystemProxyResolver(
75 std::move(proxy_config_service), net_log, /*quick_check_enabled=*/true);
76 }
77
CreateDefaultUserAgent(const std::string & partial_user_agent)78 std::string CreateDefaultUserAgent(const std::string& partial_user_agent) {
79 return partial_user_agent;
80 }
81
SetNetworkThreadPriorityOnNetworkThread(double priority)82 void SetNetworkThreadPriorityOnNetworkThread(double priority) {
83 NOTIMPLEMENTED();
84 }
85
86 } // namespace cronet
87