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_registrar.h"
10 #include "base/android/jni_android.h"
11 #include "base/android/jni_registrar.h"
12 #include "base/android/jni_utils.h"
13 #include "base/at_exit.h"
14 #include "base/logging.h"
15 #include "base/message_loop/message_loop.h"
16 #include "components/cronet/android/chromium_url_request.h"
17 #include "components/cronet/android/chromium_url_request_context.h"
18 #include "components/cronet/android/cronet_histogram_manager.h"
19 #include "components/cronet/android/cronet_upload_data_stream_delegate.h"
20 #include "components/cronet/android/cronet_url_request.h"
21 #include "components/cronet/android/cronet_url_request_context_adapter.h"
22 #include "jni/CronetLibraryLoader_jni.h"
23 #include "net/android/net_jni_registrar.h"
24 #include "net/android/network_change_notifier_factory_android.h"
25 #include "net/base/network_change_notifier.h"
26 #include "url/android/url_jni_registrar.h"
27 #include "url/url_util.h"
29 #if !defined(USE_ICU_ALTERNATIVES_ON_ANDROID)
30 #include "base/i18n/icu_util.h"
36 const base::android::RegistrationMethod kCronetRegisteredMethods
[] = {
37 {"BaseAndroid", base::android::RegisterJni
},
38 {"ChromiumUrlRequest", ChromiumUrlRequestRegisterJni
},
39 {"ChromiumUrlRequestContext", ChromiumUrlRequestContextRegisterJni
},
40 {"CronetHistogramManager", CronetHistogramManagerRegisterJni
},
41 {"CronetLibraryLoader", RegisterNativesImpl
},
42 {"CronetUploadDataStreamDelegate",
43 CronetUploadDataStreamDelegateRegisterJni
},
44 {"CronetUrlRequest", CronetUrlRequestRegisterJni
},
45 {"CronetUrlRequestContextAdapter",
46 CronetUrlRequestContextAdapterRegisterJni
},
47 {"NetAndroid", net::android::RegisterJni
},
48 {"UrlAndroid", url::android::RegisterJni
},
51 base::AtExitManager
* g_at_exit_manager
= NULL
;
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;
60 // Checks the available version of JNI. Also, caches Java reflection artifacts.
61 jint
CronetOnLoad(JavaVM
* vm
, void* reserved
) {
63 if (vm
->GetEnv(reinterpret_cast<void**>(&env
), JNI_VERSION_1_6
) != JNI_OK
) {
67 base::android::InitVM(vm
);
69 if (!base::android::RegisterNativeMethods(
70 env
, kCronetRegisteredMethods
, arraysize(kCronetRegisteredMethods
))) {
74 g_at_exit_manager
= new base::AtExitManager();
76 base::android::InitReplacementClassLoader(env
,
77 base::android::GetClassLoader(env
));
80 return JNI_VERSION_1_6
;
83 void CronetOnUnLoad(JavaVM
* jvm
, void* reserved
) {
84 if (g_at_exit_manager
) {
85 delete g_at_exit_manager
;
86 g_at_exit_manager
= NULL
;
90 void CronetInitOnMainThread(JNIEnv
* env
, jclass jcaller
, jobject jcontext
) {
91 // Set application context.
92 base::android::ScopedJavaLocalRef
<jobject
> scoped_context(env
, jcontext
);
93 base::android::InitApplicationContext(env
, scoped_context
);
95 #if !defined(USE_ICU_ALTERNATIVES_ON_ANDROID)
96 base::i18n::InitializeICU();
99 DCHECK(!base::MessageLoop::current());
100 DCHECK(!g_main_message_loop
);
101 g_main_message_loop
= new base::MessageLoopForUI();
102 base::MessageLoopForUI::current()->Start();
103 DCHECK(!g_network_change_notifier
);
104 net::NetworkChangeNotifier::SetFactory(
105 new net::NetworkChangeNotifierFactoryAndroid());
106 g_network_change_notifier
= net::NetworkChangeNotifier::Create();
109 } // namespace cronet