mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
mef | 63643db | 2015-02-10 02:18:25 | [diff] [blame] | 5 | #include <jni.h> |
Misha Efimov | 1770a1b | 2018-01-11 15:27:27 | [diff] [blame] | 6 | #include <memory> |
| 7 | #include <string> |
| 8 | #include <utility> |
mef | 63643db | 2015-02-10 02:18:25 | [diff] [blame] | 9 | |
michaelbai | 0c7df61f | 2015-03-10 17:57:04 | [diff] [blame] | 10 | #include "base/android/base_jni_onload.h" |
Paul Jensen | fccbefe | 2018-06-25 16:23:44 | [diff] [blame] | 11 | #include "base/android/build_info.h" |
mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 12 | #include "base/android/jni_android.h" |
| 13 | #include "base/android/jni_registrar.h" |
Yaron Friedman | 8d84b83 | 2017-08-22 17:30:19 | [diff] [blame] | 14 | #include "base/android/jni_string.h" |
xunjieli | 3b8f568 | 2015-02-24 18:22:59 | [diff] [blame] | 15 | #include "base/android/jni_utils.h" |
michaelbai | 0c7df61f | 2015-03-10 17:57:04 | [diff] [blame] | 16 | #include "base/android/library_loader/library_loader_hooks.h" |
Hans Wennborg | df87046c | 2020-04-28 11:06:24 | [diff] [blame] | 17 | #include "base/check_op.h" |
kapishnikov | 3180f53 | 2016-06-29 17:39:50 | [diff] [blame] | 18 | #include "base/feature_list.h" |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 19 | #include "base/message_loop/message_pump_type.h" |
Misha Efimov | d4ab3830 | 2018-01-30 23:56:42 | [diff] [blame] | 20 | #include "base/synchronization/waitable_event.h" |
Carlos Caballero | b25fe847 | 2020-07-17 10:27:17 | [diff] [blame] | 21 | #include "base/task/current_thread.h" |
Alex Clarke | f7fb8a8 | 2019-06-06 15:41:53 | [diff] [blame] | 22 | #include "base/task/single_thread_task_executor.h" |
Gabriel Charette | eadf5886 | 2019-08-29 05:20:27 | [diff] [blame] | 23 | #include "base/task/thread_pool/thread_pool_instance.h" |
Paul Jensen | fccbefe | 2018-06-25 16:23:44 | [diff] [blame] | 24 | #include "build/build_config.h" |
Shenghuai Ji | 9e1d2ed | 2018-07-26 00:51:30 | [diff] [blame] | 25 | #include "components/cronet/android/buildflags.h" |
Mohamed Heikal | bd64131 | 2019-06-22 00:14:37 | [diff] [blame] | 26 | #include "components/cronet/android/cronet_jni_headers/CronetLibraryLoader_jni.h" |
Misha Efimov | 1770a1b | 2018-01-11 15:27:27 | [diff] [blame] | 27 | #include "components/cronet/cronet_global_state.h" |
xunjieli | 6f35690c | 2015-06-04 20:53:23 | [diff] [blame] | 28 | #include "components/cronet/version.h" |
mef | 63643db | 2015-02-10 02:18:25 | [diff] [blame] | 29 | #include "net/android/network_change_notifier_factory_android.h" |
| 30 | #include "net/base/network_change_notifier.h" |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 31 | #include "net/proxy_resolution/configured_proxy_resolution_service.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 32 | #include "net/proxy_resolution/proxy_config_service_android.h" |
Paul Jensen | fccbefe | 2018-06-25 16:23:44 | [diff] [blame] | 33 | #include "third_party/zlib/zlib.h" |
Scott Violet | fd6ee11 | 2019-01-10 19:05:32 | [diff] [blame] | 34 | #include "url/buildflags.h" |
mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 35 | |
torne | d8b7d925 | 2016-08-04 19:41:45 | [diff] [blame] | 36 | #if !BUILDFLAG(USE_PLATFORM_ICU_ALTERNATIVES) |
mgersh | f3e5731 | 2016-06-01 21:55:00 | [diff] [blame] | 37 | #include "base/i18n/icu_util.h" // nogncheck |
mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 38 | #endif |
| 39 | |
Shenghuai Ji | 9e1d2ed | 2018-07-26 00:51:30 | [diff] [blame] | 40 | #if !BUILDFLAG(INTEGRATED_MODE) |
| 41 | #include "components/cronet/android/cronet_jni_registration.h" |
| 42 | #include "components/cronet/android/cronet_library_loader.h" |
| 43 | #endif |
| 44 | |
torne | bc8890f | 2016-08-05 11:48:44 | [diff] [blame] | 45 | using base::android::JavaParamRef; |
| 46 | using base::android::ScopedJavaLocalRef; |
| 47 | |
mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 48 | namespace cronet { |
| 49 | namespace { |
| 50 | |
Alex Clarke | f7fb8a8 | 2019-06-06 15:41:53 | [diff] [blame] | 51 | // SingleThreadTaskExecutor on the init thread, which is where objects that |
| 52 | // receive Java notifications generally live. |
| 53 | base::SingleThreadTaskExecutor* g_init_task_executor = nullptr; |
mef | 63643db | 2015-02-10 02:18:25 | [diff] [blame] | 54 | |
Shenghuai Ji | 9e1d2ed | 2018-07-26 00:51:30 | [diff] [blame] | 55 | #if !BUILDFLAG(INTEGRATED_MODE) |
Erik Anderson | ca163bb9 | 2019-06-18 16:10:48 | [diff] [blame] | 56 | std::unique_ptr<net::NetworkChangeNotifier> g_network_change_notifier; |
Shenghuai Ji | 9e1d2ed | 2018-07-26 00:51:30 | [diff] [blame] | 57 | #endif |
mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 58 | |
Misha Efimov | d4ab3830 | 2018-01-30 23:56:42 | [diff] [blame] | 59 | base::WaitableEvent g_init_thread_init_done( |
| 60 | base::WaitableEvent::ResetPolicy::MANUAL, |
| 61 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 62 | |
| 63 | void NativeInit() { |
Paul Jensen | a6d848cb | 2018-10-17 19:22:47 | [diff] [blame] | 64 | // In integrated mode, ICU and FeatureList has been initialized by the host. |
| 65 | #if !BUILDFLAG(INTEGRATED_MODE) |
| 66 | #if !BUILDFLAG(USE_PLATFORM_ICU_ALTERNATIVES) |
| 67 | base::i18n::InitializeICU(); |
| 68 | #endif |
| 69 | base::FeatureList::InitializeInstance(std::string(), std::string()); |
| 70 | #endif |
| 71 | |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 72 | if (!base::ThreadPoolInstance::Get()) |
| 73 | base::ThreadPoolInstance::CreateAndStartWithDefaultParams("Cronet"); |
michaelbai | 0c7df61f | 2015-03-10 17:57:04 | [diff] [blame] | 74 | } |
| 75 | |
mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 76 | } // namespace |
| 77 | |
pauljensen | 6815aef | 2017-05-05 03:00:06 | [diff] [blame] | 78 | bool OnInitThread() { |
Alex Clarke | f7fb8a8 | 2019-06-06 15:41:53 | [diff] [blame] | 79 | DCHECK(g_init_task_executor); |
| 80 | return g_init_task_executor->task_runner()->RunsTasksInCurrentSequence(); |
pauljensen | 6815aef | 2017-05-05 03:00:06 | [diff] [blame] | 81 | } |
| 82 | |
Shenghuai Ji | 9e1d2ed | 2018-07-26 00:51:30 | [diff] [blame] | 83 | // In integrated mode, Cronet native library is built and loaded together with |
| 84 | // the native library of the host app. |
| 85 | #if !BUILDFLAG(INTEGRATED_MODE) |
mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 86 | // Checks the available version of JNI. Also, caches Java reflection artifacts. |
| 87 | jint CronetOnLoad(JavaVM* vm, void* reserved) { |
tobiasjs | b9e287e | 2017-02-08 11:09:50 | [diff] [blame] | 88 | base::android::InitVM(vm); |
| 89 | JNIEnv* env = base::android::AttachCurrentThread(); |
Yipeng Wang | 158dbc5c | 2017-06-30 18:16:41 | [diff] [blame] | 90 | if (!RegisterMainDexNatives(env) || !RegisterNonMainDexNatives(env)) { |
| 91 | return -1; |
| 92 | } |
Misha Efimov | d4ab3830 | 2018-01-30 23:56:42 | [diff] [blame] | 93 | if (!base::android::OnJNIOnLoadInit()) |
mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 94 | return -1; |
Misha Efimov | d4ab3830 | 2018-01-30 23:56:42 | [diff] [blame] | 95 | NativeInit(); |
mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 96 | return JNI_VERSION_1_6; |
| 97 | } |
| 98 | |
| 99 | void CronetOnUnLoad(JavaVM* jvm, void* reserved) { |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 100 | if (base::ThreadPoolInstance::Get()) |
| 101 | base::ThreadPoolInstance::Get()->Shutdown(); |
Eric Roman | 3e3c14a | 2017-07-07 21:49:10 | [diff] [blame] | 102 | |
michaelbai | 0c7df61f | 2015-03-10 17:57:04 | [diff] [blame] | 103 | base::android::LibraryLoaderExitHook(); |
mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 104 | } |
Shenghuai Ji | 9e1d2ed | 2018-07-26 00:51:30 | [diff] [blame] | 105 | #endif |
mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 106 | |
Aiden Benner | c99e905 | 2018-11-30 20:50:50 | [diff] [blame] | 107 | void JNI_CronetLibraryLoader_CronetInitOnInitThread(JNIEnv* env) { |
Alex Clarke | f7fb8a8 | 2019-06-06 15:41:53 | [diff] [blame] | 108 | // Initialize SingleThreadTaskExecutor for init thread. |
Carlos Caballero | b25fe847 | 2020-07-17 10:27:17 | [diff] [blame] | 109 | DCHECK(!base::CurrentThread::IsSet()); |
Alex Clarke | f7fb8a8 | 2019-06-06 15:41:53 | [diff] [blame] | 110 | DCHECK(!g_init_task_executor); |
| 111 | g_init_task_executor = |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 112 | new base::SingleThreadTaskExecutor(base::MessagePumpType::JAVA); |
Pengcheng | eaf5ae2 | 2018-01-18 01:57:17 | [diff] [blame] | 113 | |
Shenghuai Ji | 9e1d2ed | 2018-07-26 00:51:30 | [diff] [blame] | 114 | // In integrated mode, NetworkChangeNotifier has been initialized by the host. |
| 115 | #if BUILDFLAG(INTEGRATED_MODE) |
Min Qin | 0454521 | 2019-11-06 01:10:49 | [diff] [blame] | 116 | CHECK(!net::NetworkChangeNotifier::CreateIfNeeded()); |
Shenghuai Ji | 9e1d2ed | 2018-07-26 00:51:30 | [diff] [blame] | 117 | #else |
| 118 | DCHECK(!g_network_change_notifier); |
Pengcheng | eaf5ae2 | 2018-01-18 01:57:17 | [diff] [blame] | 119 | if (!net::NetworkChangeNotifier::GetFactory()) { |
| 120 | net::NetworkChangeNotifier::SetFactory( |
| 121 | new net::NetworkChangeNotifierFactoryAndroid()); |
| 122 | } |
Min Qin | 0454521 | 2019-11-06 01:10:49 | [diff] [blame] | 123 | g_network_change_notifier = net::NetworkChangeNotifier::CreateIfNeeded(); |
| 124 | DCHECK(g_network_change_notifier); |
Shenghuai Ji | 9e1d2ed | 2018-07-26 00:51:30 | [diff] [blame] | 125 | #endif |
| 126 | |
Misha Efimov | d4ab3830 | 2018-01-30 23:56:42 | [diff] [blame] | 127 | g_init_thread_init_done.Signal(); |
mef | 63643db | 2015-02-10 02:18:25 | [diff] [blame] | 128 | } |
| 129 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 130 | ScopedJavaLocalRef<jstring> JNI_CronetLibraryLoader_GetCronetVersion( |
Aiden Benner | c99e905 | 2018-11-30 20:50:50 | [diff] [blame] | 131 | JNIEnv* env) { |
Paul Jensen | fccbefe | 2018-06-25 16:23:44 | [diff] [blame] | 132 | #if defined(ARCH_CPU_ARM64) |
| 133 | // Attempt to avoid crashes on some ARM64 Marshmallow devices by |
| 134 | // prompting zlib ARM feature detection early on. https://crbug.com/853725 |
| 135 | if (base::android::BuildInfo::GetInstance()->sdk_int() == |
| 136 | base::android::SDK_VERSION_MARSHMALLOW) { |
| 137 | crc32(0, Z_NULL, 0); |
| 138 | } |
| 139 | #endif |
torne | f71efb3 | 2015-08-26 14:07:32 | [diff] [blame] | 140 | return base::android::ConvertUTF8ToJavaString(env, CRONET_VERSION); |
xunjieli | 6f35690c | 2015-06-04 20:53:23 | [diff] [blame] | 141 | } |
| 142 | |
Misha Efimov | d4ab3830 | 2018-01-30 23:56:42 | [diff] [blame] | 143 | void PostTaskToInitThread(const base::Location& posted_from, |
| 144 | base::OnceClosure task) { |
| 145 | g_init_thread_init_done.Wait(); |
Alex Clarke | f7fb8a8 | 2019-06-06 15:41:53 | [diff] [blame] | 146 | g_init_task_executor->task_runner()->PostTask(posted_from, std::move(task)); |
Misha Efimov | d4ab3830 | 2018-01-30 23:56:42 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void EnsureInitialized() { |
Alex Clarke | f7fb8a8 | 2019-06-06 15:41:53 | [diff] [blame] | 150 | if (g_init_task_executor) { |
Misha Efimov | d4ab3830 | 2018-01-30 23:56:42 | [diff] [blame] | 151 | // Ensure that init is done on the init thread. |
| 152 | g_init_thread_init_done.Wait(); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | // The initialization can only be done once, so static |s_run_once| variable |
| 157 | // is used to do it in the constructor. |
| 158 | static class RunOnce { |
| 159 | public: |
| 160 | RunOnce() { |
| 161 | NativeInit(); |
| 162 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 163 | // Ensure initialized from Java side to properly create Init thread. |
| 164 | cronet::Java_CronetLibraryLoader_ensureInitializedFromNative(env); |
| 165 | } |
| 166 | } s_run_once; |
| 167 | } |
| 168 | |
Misha Efimov | 1770a1b | 2018-01-11 15:27:27 | [diff] [blame] | 169 | std::unique_ptr<net::ProxyConfigService> CreateProxyConfigService( |
| 170 | const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) { |
| 171 | std::unique_ptr<net::ProxyConfigService> service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 172 | net::ConfiguredProxyResolutionService::CreateSystemProxyConfigService( |
| 173 | io_task_runner); |
Misha Efimov | 1770a1b | 2018-01-11 15:27:27 | [diff] [blame] | 174 | // If a PAC URL is present, ignore it and use the address and port of |
| 175 | // Android system's local HTTP proxy server. See: crbug.com/432539. |
| 176 | // TODO(csharrison) Architect the wrapper better so we don't need to cast for |
| 177 | // android ProxyConfigServices. |
| 178 | net::ProxyConfigServiceAndroid* android_proxy_config_service = |
| 179 | static_cast<net::ProxyConfigServiceAndroid*>(service.get()); |
| 180 | android_proxy_config_service->set_exclude_pac_url(true); |
| 181 | return service; |
| 182 | } |
| 183 | |
Lily Houghton | d567e86 | 2018-03-27 16:37:18 | [diff] [blame] | 184 | // Creates a proxy resolution service appropriate for this platform. |
Nicolas Arciniega | 8ec5bfa | 2020-03-20 05:07:26 | [diff] [blame] | 185 | std::unique_ptr<net::ProxyResolutionService> CreateProxyResolutionService( |
Misha Efimov | 1770a1b | 2018-01-11 15:27:27 | [diff] [blame] | 186 | std::unique_ptr<net::ProxyConfigService> proxy_config_service, |
| 187 | net::NetLog* net_log) { |
| 188 | // Android provides a local HTTP proxy server that handles proxying when a PAC |
| 189 | // URL is present. Create a proxy service without a resolver and rely on this |
| 190 | // local HTTP proxy. See: crbug.com/432539. |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 191 | return net::ConfiguredProxyResolutionService::CreateWithoutProxyResolver( |
Misha Efimov | 1770a1b | 2018-01-11 15:27:27 | [diff] [blame] | 192 | std::move(proxy_config_service), net_log); |
| 193 | } |
| 194 | |
Misha Efimov | d4ab3830 | 2018-01-30 23:56:42 | [diff] [blame] | 195 | // Creates default User-Agent request value, combining optional |
| 196 | // |partial_user_agent| with system-dependent values. |
| 197 | std::string CreateDefaultUserAgent(const std::string& partial_user_agent) { |
| 198 | // Cronet global state must be initialized to include application info |
| 199 | // into default user agent |
| 200 | cronet::EnsureInitialized(); |
| 201 | |
| 202 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 203 | std::string user_agent = base::android::ConvertJavaStringToUTF8( |
| 204 | cronet::Java_CronetLibraryLoader_getDefaultUserAgent(env)); |
| 205 | if (!partial_user_agent.empty()) |
| 206 | user_agent.insert(user_agent.size() - 1, "; " + partial_user_agent); |
| 207 | return user_agent; |
| 208 | } |
| 209 | |
Paul Jensen | 6a1ea3a | 2018-08-24 14:46:41 | [diff] [blame] | 210 | void SetNetworkThreadPriorityOnNetworkThread(double priority) { |
| 211 | int priority_int = priority; |
| 212 | DCHECK_LE(priority_int, 19); |
| 213 | DCHECK_GE(priority_int, -20); |
| 214 | if (priority_int >= -20 && priority_int <= 19) { |
| 215 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 216 | cronet::Java_CronetLibraryLoader_setNetworkThreadPriorityOnNetworkThread( |
| 217 | env, priority_int); |
| 218 | } |
| 219 | } |
| 220 | |
mef | 2028ab74 | 2014-09-12 15:16:43 | [diff] [blame] | 221 | } // namespace cronet |