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/logging.h"
16 #include "base/message_loop/message_loop.h"
17 #include "components/cronet/android/chromium_url_request.h"
18 #include "components/cronet/android/chromium_url_request_context.h"
19 #include "components/cronet/android/cronet_histogram_manager.h"
20 #include "components/cronet/android/cronet_upload_data_stream_delegate.h"
21 #include "components/cronet/android/cronet_url_request_adapter.h"
22 #include "components/cronet/android/cronet_url_request_context_adapter.h"
23 #include "jni/CronetLibraryLoader_jni.h"
24 #include "net/android/net_jni_registrar.h"
25 #include "net/android/network_change_notifier_factory_android.h"
26 #include "net/base/network_change_notifier.h"
27 #include "url/android/url_jni_registrar.h"
28 #include "url/url_util.h"
30 #if !defined(USE_ICU_ALTERNATIVES_ON_ANDROID)
31 #include "base/i18n/icu_util.h"
37 const base::android::RegistrationMethod kCronetRegisteredMethods
[] = {
38 {"BaseAndroid", base::android::RegisterJni
},
39 {"ChromiumUrlRequest", ChromiumUrlRequestRegisterJni
},
40 {"ChromiumUrlRequestContext", ChromiumUrlRequestContextRegisterJni
},
41 {"CronetHistogramManager", CronetHistogramManagerRegisterJni
},
42 {"CronetLibraryLoader", RegisterNativesImpl
},
43 {"CronetUploadDataStreamDelegate",
44 CronetUploadDataStreamDelegateRegisterJni
},
45 {"CronetUrlRequestAdapter", CronetUrlRequestAdapterRegisterJni
},
46 {"CronetUrlRequestContextAdapter",
47 CronetUrlRequestContextAdapterRegisterJni
},
48 {"NetAndroid", net::android::RegisterJni
},
49 {"UrlAndroid", url::android::RegisterJni
},
52 // MessageLoop on the main thread, which is where objects that receive Java
53 // notifications generally live.
54 base::MessageLoop
* g_main_message_loop
= nullptr;
56 net::NetworkChangeNotifier
* g_network_change_notifier
= nullptr;
58 bool RegisterJNI(JNIEnv
* env
) {
59 return base::android::RegisterNativeMethods(
60 env
, kCronetRegisteredMethods
, arraysize(kCronetRegisteredMethods
));
70 // Checks the available version of JNI. Also, caches Java reflection artifacts.
71 jint
CronetOnLoad(JavaVM
* vm
, void* reserved
) {
72 std::vector
<base::android::RegisterCallback
> register_callbacks
;
73 register_callbacks
.push_back(base::Bind(&RegisterJNI
));
74 std::vector
<base::android::InitCallback
> init_callbacks
;
75 init_callbacks
.push_back(base::Bind(&Init
));
76 if (!base::android::OnJNIOnLoadRegisterJNI(vm
, register_callbacks
) ||
77 !base::android::OnJNIOnLoadInit(init_callbacks
)) {
80 return JNI_VERSION_1_6
;
83 void CronetOnUnLoad(JavaVM
* jvm
, void* reserved
) {
84 base::android::LibraryLoaderExitHook();
87 void CronetInitApplicationContext(JNIEnv
* env
,
89 jobject japp_context
) {
90 // Set application context.
91 base::android::ScopedJavaLocalRef
<jobject
> scoped_app_context(env
,
93 base::android::InitApplicationContext(env
, scoped_app_context
);
96 void CronetInitOnMainThread(JNIEnv
* env
, jclass jcaller
) {
97 #if !defined(USE_ICU_ALTERNATIVES_ON_ANDROID)
98 base::i18n::InitializeICU();
101 DCHECK(!base::MessageLoop::current());
102 DCHECK(!g_main_message_loop
);
103 g_main_message_loop
= new base::MessageLoopForUI();
104 base::MessageLoopForUI::current()->Start();
105 DCHECK(!g_network_change_notifier
);
106 net::NetworkChangeNotifier::SetFactory(
107 new net::NetworkChangeNotifierFactoryAndroid());
108 g_network_change_notifier
= net::NetworkChangeNotifier::Create();
111 } // namespace cronet