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

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: NET_EXPORT RequestPriorityToString to avoid buildbot errors. 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 static const char kVersion[] = CHROMIUM_VERSION "/" CHROMIUM_NET_VERSION;
23
24 static jclass g_class;
25 static jmethodID g_method_initNetworkThread;
26 static jfieldID g_field_mRequestContext;
mmenke 2014/02/27 23:06:02 fix names / anonymous namespace
mef 2014/03/03 19:15:13 Done.
27
28 static base::AtExitManager* s_at_exit_manager = NULL;
mmenke 2014/02/27 23:06:02 Suggest using a g_ prefix for consistency with abo
mef 2014/03/03 19:15:13 Done.
29
30 /*
31 * Checks the available version of JNI. Also, caches Java reflection artifacts.
32 */
mmenke 2014/02/27 23:06:02 C++ style comments
mef 2014/03/03 19:15:13 Done.
33 jint JNI_OnLoad(JavaVM* vm, void* reserved) {
34 JNIEnv* env;
35 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
36 return -1;
37 }
38
39 const char* class_name = "org/chromium/net/UrlRequestContext";
mmenke 2014/02/27 23:06:02 Seems like this should be "const char kClassName[]
mef 2014/03/03 19:15:13 Done.
40 g_class = (jclass) env->NewGlobalRef(env->FindClass(class_name));
41 g_method_initNetworkThread =
42 env->GetMethodID(g_class, "initNetworkThread", "()V");
43 g_field_mRequestContext =
44 env->GetFieldID(g_class, "mRequestContext", "J");
45 if (!g_class || !g_method_initNetworkThread || !g_field_mRequestContext) {
46 return -1;
47 }
48
49 base::android::InitVM(vm);
50
51 if (!base::android::RegisterJni(env)) {
52 return -1;
53 }
54
55 if (!UrlRequestRegisterJni(env)) {
56 return -1;
57 }
58
59 if (!net::android::RegisterJni(env)) {
60 return -1;
61 }
62
63 s_at_exit_manager = new base::AtExitManager();
64
65 base::i18n::InitializeICU();
66
67 return JNI_VERSION_1_6;
68 }
69
70 JNIEnv* GetEnv(JavaVM* vm) {
71 // We need to make sure this native thread is attached to the JVM before
72 // we can call any Java methods.
73 JNIEnv* env;
74 if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6)
75 == JNI_EDETACHED) {
mmenke 2014/02/27 23:06:02 nit: "==" on previous line.
mef 2014/03/03 19:15:13 Done.
76 vm->AttachCurrentThread(&env, NULL);
77 }
78 return env;
79 }
80
81 /*
82 * Delegate of URLRequestContextPeer that delivers callbacks to the Java layer.
83 */
mmenke 2014/02/27 23:06:02 C++-style comment?
mef 2014/03/03 19:15:13 Done.
84 class JniURLRequestContextPeerDelegate :
mmenke 2014/02/27 23:06:02 Move into an anonymous namespace?
mef 2014/03/03 19:15:13 Done.
85 public URLRequestContextPeer::URLRequestContextPeerDelegate {
86 jobject owner_;
87 JavaVM* vm_;
mmenke 2014/02/27 23:06:02 These should go at bottom of class, in aa private
mef 2014/03/03 19:15:13 Done.
88
89 public:
90 JniURLRequestContextPeerDelegate(JNIEnv* env, jobject owner) {
91 owner_ = env->NewGlobalRef(owner);
mmenke 2014/02/27 23:06:02 This can be set in an initializer list.
mef 2014/03/03 19:15:13 Done.
92
93 env->GetJavaVM(&vm_);
94 }
95
96 virtual void OnContextInitialized(URLRequestContextPeer* context) OVERRIDE {
97 JNIEnv* env = GetEnv(vm_);
98 env->CallVoidMethod(owner_, g_method_initNetworkThread);
99 if (env->ExceptionOccurred()) {
100 env->ExceptionDescribe();
101 env->ExceptionClear();
102 }
103
104 // TODO(dplotnikov): figure out if we need to detach from the thread.
105 // The documentation says we should detach just before the thread exits.
106 }
107
108 protected:
109 virtual ~JniURLRequestContextPeerDelegate() {
110 GetEnv(vm_)->DeleteGlobalRef(owner_);
111 }
112 };
113
114 /*
115 * Stores a reference to the peer in a java field.
116 */
117 static void SetNativeObject(JNIEnv *env, jobject object,
118 URLRequestContextPeer* peer) {
119 env->SetLongField(object, g_field_mRequestContext,
120 reinterpret_cast<jlong>(peer));
121 }
122
123 /*
124 * Returns a reference to the peer, which is stored in a field of
125 * the java object.
126 */
127 static URLRequestContextPeer* GetNativeObject(JNIEnv* env, jobject object) {
mmenke 2014/02/27 23:06:02 Move these into an anonymous namespace?
mef 2014/03/03 19:15:13 Done.
128 return reinterpret_cast<URLRequestContextPeer *>(
129 env->GetLongField(object, g_field_mRequestContext));
130 }
131
132 URLRequestContextPeer* GetURLRequestContextPeer(JNIEnv* env,
133 jobject request_context) {
134 return GetNativeObject(env, request_context);
135 }
136
137 JNIEXPORT jstring JNICALL
138 Java_org_chromium_net_UrlRequestContext_getVersion(
139 JNIEnv* env, jobject object) {
140 return env->NewStringUTF(kVersion);
141 }
142
143 /*
144 * Sets global user-agent to be used for all subsequent requests.
145 */
146 JNIEXPORT void JNICALL
147 Java_org_chromium_net_UrlRequestContext_nativeInitialize(
148 JNIEnv* env,
149 jobject object,
150 jobject context,
151 jstring user_agent,
152 jint log_level) {
153 const char *user_agent_utf8 = env->GetStringUTFChars(user_agent, NULL);
154 std::string user_agent_string(user_agent_utf8);
155 env->ReleaseStringUTFChars(user_agent, user_agent_utf8);
156
157 // Set application context
mmenke 2014/02/27 23:06:02 Comments should end in periods, here and below.
mef 2014/03/03 19:15:13 Done.
158 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
159 base::android::InitApplicationContext(env, scoped_context);
160
161 int logging_level = log_level;
162
163 // TODO(dplotnikov): set application context
164 URLRequestContextPeer* peer = new URLRequestContextPeer(
165 new JniURLRequestContextPeerDelegate(env, object), user_agent_string,
166 logging_level, kVersion);
167 peer->AddRef(); // Hold onto this ref-counted object.
168
169 SetNativeObject(env, object, peer);
170
171 peer->Initialize();
172 }
173
174 /*
175 * Releases native objects.
176 */
177 JNIEXPORT void JNICALL
178 Java_org_chromium_net_UrlRequestContext_nativeFinalize(
179 JNIEnv* env, jobject object) {
180 // URLRequestContextPeer is a ref-counted object, so we need to release it
181 // instead of deleting outright.
182 GetNativeObject(env, object)->Release();
183 SetNativeObject(env, object, NULL);
184 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698