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

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

Powered by Google App Engine
This is Rietveld 408576698