Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: net/cronet/android/org_chromium_net_UrlRequestContext.cc

Issue 145213003: Initial upload of cronet for Android. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address code review comments. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
5 #include "net/cronet/android/org_chromium_net_UrlRequestContext.h"
6
7 #include <stdio.h>
8
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/at_exit.h"
13 #include "base/i18n/icu_util.h"
14 #include "net/android/net_jni_registrar.h"
15 #include "net/cronet/android/org_chromium_net_UrlRequest.h"
16 #include "net/cronet/android/url_request_context_peer.h"
17 #include "net/cronet/android/url_request_peer.h"
18
19 // Version of this build of Chromium NET.
20 #define CHROMIUM_NET_VERSION "1"
21
22 namespace {
mmenke 2014/03/04 19:36:20 nit: blank line after namespace {
mef 2014/03/05 20:54:13 Done.
23 const char kVersion[] = CHROMIUM_VERSION "/" CHROMIUM_NET_VERSION;
24 const char kClassName[] = "org/chromium/net/UrlRequestContext";
25
26 jclass g_class;
27 jmethodID g_method_initNetworkThread;
28 jfieldID g_field_mRequestContext;
29
30 base::AtExitManager* g_at_exit_manager = NULL;
31
32 // Stores a reference to the peer in a java field.
33 void SetNativeObject(JNIEnv* env, jobject object, URLRequestContextPeer* peer) {
34 env->SetLongField(
35 object, g_field_mRequestContext, reinterpret_cast<jlong>(peer));
36 }
37
38 // Returns a reference to the peer, which is stored in a field of the java
39 // object.
40 URLRequestContextPeer* GetNativeObject(JNIEnv* env, jobject object) {
41 return reinterpret_cast<URLRequestContextPeer*>(
42 env->GetLongField(object, g_field_mRequestContext));
43 }
44
45 // Delegate of URLRequestContextPeer that delivers callbacks to the Java layer.
46 class JniURLRequestContextPeerDelegate
47 : public URLRequestContextPeer::URLRequestContextPeerDelegate {
48 public:
49 JniURLRequestContextPeerDelegate(JNIEnv* env, jobject owner)
50 : owner_(env->NewGlobalRef(owner)) {
51 env->GetJavaVM(&vm_);
52 }
53
54 virtual void OnContextInitialized(URLRequestContextPeer* context) OVERRIDE {
55 JNIEnv* env = GetEnv(vm_);
56 env->CallVoidMethod(owner_, g_method_initNetworkThread);
57 if (env->ExceptionOccurred()) {
58 env->ExceptionDescribe();
59 env->ExceptionClear();
60 }
61
62 // TODO(dplotnikov): figure out if we need to detach from the thread.
63 // The documentation says we should detach just before the thread exits.
64 }
65
66 protected:
67 virtual ~JniURLRequestContextPeerDelegate() {
68 GetEnv(vm_)->DeleteGlobalRef(owner_);
69 }
70
71 private:
72 jobject owner_;
73 JavaVM* vm_;
74 };
75
76 } // namespace
77
78 // Checks the available version of JNI. Also, caches Java reflection artifacts.
79 jint JNI_OnLoad(JavaVM* vm, void* reserved) {
80 JNIEnv* env;
81 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
82 return -1;
83 }
84
85 g_class = (jclass)env->NewGlobalRef(env->FindClass(kClassName));
86 g_method_initNetworkThread =
87 env->GetMethodID(g_class, "initNetworkThread", "()V");
88 g_field_mRequestContext = env->GetFieldID(g_class, "mRequestContext", "J");
89 if (!g_class || !g_method_initNetworkThread || !g_field_mRequestContext) {
90 return -1;
91 }
92
93 base::android::InitVM(vm);
94
95 if (!base::android::RegisterJni(env)) {
96 return -1;
97 }
98
99 if (!UrlRequestRegisterJni(env)) {
100 return -1;
101 }
102
103 if (!net::android::RegisterJni(env)) {
104 return -1;
105 }
106
107 g_at_exit_manager = new base::AtExitManager();
108
109 base::i18n::InitializeICU();
110
111 return JNI_VERSION_1_6;
112 }
113
114 JNIEnv* GetEnv(JavaVM* vm) {
115 // We need to make sure this native thread is attached to the JVM before
116 // we can call any Java methods.
117 JNIEnv* env;
118 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) ==
119 JNI_EDETACHED) {
120 vm->AttachCurrentThread(&env, NULL);
121 }
122 return env;
123 }
124
125 URLRequestContextPeer* GetURLRequestContextPeer(JNIEnv* env,
126 jobject request_context) {
127 return GetNativeObject(env, request_context);
128 }
129
130 JNIEXPORT jstring JNICALL
131 Java_org_chromium_net_UrlRequestContext_getVersion(JNIEnv* env,
132 jobject object) {
133 return env->NewStringUTF(kVersion);
134 }
135
136 // Sets global user-agent to be used for all subsequent requests.
137 JNIEXPORT void JNICALL
138 Java_org_chromium_net_UrlRequestContext_nativeInitialize(JNIEnv* env,
139 jobject object,
140 jobject context,
141 jstring user_agent,
142 jint log_level) {
143 const char* user_agent_utf8 = env->GetStringUTFChars(user_agent, NULL);
144 std::string user_agent_string(user_agent_utf8);
145 env->ReleaseStringUTFChars(user_agent, user_agent_utf8);
146
147 // Set application context.
148 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
149 base::android::InitApplicationContext(env, scoped_context);
150
151 int logging_level = log_level;
152
153 // TODO(dplotnikov): set application context.
154 URLRequestContextPeer* peer = new URLRequestContextPeer(
155 new JniURLRequestContextPeerDelegate(env, object),
156 user_agent_string,
157 logging_level,
158 kVersion);
159 peer->AddRef(); // Hold onto this ref-counted object.
160
161 SetNativeObject(env, object, peer);
162
163 peer->Initialize();
164 }
165
166 // Releases native objects.
167 JNIEXPORT void JNICALL
168 Java_org_chromium_net_UrlRequestContext_nativeFinalize(JNIEnv* env,
169 jobject object) {
170 // URLRequestContextPeer is a ref-counted object, so we need to release it
171 // instead of deleting outright.
172 GetNativeObject(env, object)->Release();
173 SetNativeObject(env, object, NULL);
174 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698