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.
5 #include "components/cronet/android/cronet_library_loader.h"
9 #include "base/android/base_jni_onload.h"
10 #include "base/android/base_jni_registrar.h"
11 #include "base/android/jni_android.h"
12 #include "base/android/jni_registrar.h"
13 #include "base/android/jni_utils.h"
14 #include "base/android/library_loader/library_loader_hooks.h"
15 #include "base/command_line.h"
16 #include "base/logging.h"
17 #include "base/message_loop/message_loop.h"
18 #include "components/cronet/android/chromium_url_request.h"
19 #include "components/cronet/android/chromium_url_request_context.h"
20 #include "components/cronet/android/cronet_histogram_manager.h"
21 #include "components/cronet/android/cronet_upload_data_stream_adapter.h"
22 #include "components/cronet/android/cronet_url_request_adapter.h"
23 #include "components/cronet/android/cronet_url_request_context_adapter.h"
24 #include "components/cronet/version.h"
25 #include "jni/CronetLibraryLoader_jni.h"
26 #include "net/android/net_jni_registrar.h"
27 #include "net/android/network_change_notifier_factory_android.h"
28 #include "net/base/network_change_notifier.h"
29 #include "url/android/url_jni_registrar.h"
30 #include "url/url_util.h"
32 #if !defined(USE_ICU_ALTERNATIVES_ON_ANDROID)
33 #include "base/i18n/icu_util.h"
39 const base::android::RegistrationMethod kCronetRegisteredMethods
[] = {
40 {"BaseAndroid", base::android::RegisterJni
},
41 {"ChromiumUrlRequest", ChromiumUrlRequestRegisterJni
},
42 {"ChromiumUrlRequestContext", ChromiumUrlRequestContextRegisterJni
},
43 {"CronetHistogramManager", CronetHistogramManagerRegisterJni
},
44 {"CronetLibraryLoader", RegisterNativesImpl
},
45 {"CronetUploadDataStreamAdapter",
46 CronetUploadDataStreamAdapterRegisterJni
},
47 {"CronetUrlRequestAdapter", CronetUrlRequestAdapterRegisterJni
},
48 {"CronetUrlRequestContextAdapter",
49 CronetUrlRequestContextAdapterRegisterJni
},
50 {"NetAndroid", net::android::RegisterJni
},
51 {"UrlAndroid", url::android::RegisterJni
},
54 // MessageLoop on the main thread, which is where objects that receive Java
55 // notifications generally live.
56 base::MessageLoop
* g_main_message_loop
= nullptr;
58 net::NetworkChangeNotifier
* g_network_change_notifier
= nullptr;
60 bool RegisterJNI(JNIEnv
* env
) {
61 return base::android::RegisterNativeMethods(
62 env
, kCronetRegisteredMethods
, arraysize(kCronetRegisteredMethods
));
72 // Checks the available version of JNI. Also, caches Java reflection artifacts.
73 jint
CronetOnLoad(JavaVM
* vm
, void* reserved
) {
74 std::vector
<base::android::RegisterCallback
> register_callbacks
;
75 register_callbacks
.push_back(base::Bind(&RegisterJNI
));
76 std::vector
<base::android::InitCallback
> init_callbacks
;
77 init_callbacks
.push_back(base::Bind(&Init
));
78 if (!base::android::OnJNIOnLoadRegisterJNI(vm
, register_callbacks
) ||
79 !base::android::OnJNIOnLoadInit(init_callbacks
)) {
82 return JNI_VERSION_1_6
;
85 void CronetOnUnLoad(JavaVM
* jvm
, void* reserved
) {
86 base::android::LibraryLoaderExitHook();
89 void CronetInitApplicationContext(JNIEnv
* env
,
91 jobject japp_context
) {
92 // Set application context.
93 base::android::ScopedJavaLocalRef
<jobject
> scoped_app_context(env
,
95 base::android::InitApplicationContext(env
, scoped_app_context
);
98 void CronetInitOnMainThread(JNIEnv
* env
, jclass jcaller
) {
99 #if !defined(USE_ICU_ALTERNATIVES_ON_ANDROID)
100 base::i18n::InitializeICU();
103 // TODO(bengr): Remove once Data Reduction Proxy no longer needs this for
104 // configuration information.
105 base::CommandLine::Init(0, nullptr);
106 DCHECK(!base::MessageLoop::current());
107 DCHECK(!g_main_message_loop
);
108 g_main_message_loop
= new base::MessageLoopForUI();
109 base::MessageLoopForUI::current()->Start();
110 DCHECK(!g_network_change_notifier
);
111 net::NetworkChangeNotifier::SetFactory(
112 new net::NetworkChangeNotifierFactoryAndroid());
113 g_network_change_notifier
= net::NetworkChangeNotifier::Create();
116 jstring
GetCronetVersion(JNIEnv
* env
, jclass jcaller
) {
117 return base::android::ConvertUTF8ToJavaString(env
, CRONET_VERSION
).Release();
120 } // namespace cronet