OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/cronet/android/org_chromium_net_UrlRequestContext.h" | 5 #include "components/cronet/android/org_chromium_net_UrlRequestContext.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/metrics/statistics_recorder.h" | 13 #include "base/metrics/statistics_recorder.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "components/cronet/android/org_chromium_net_UrlRequest.h" | 15 #include "components/cronet/android/org_chromium_net_UrlRequest.h" |
16 #include "components/cronet/android/url_request_context_peer.h" | 16 #include "components/cronet/android/url_request_context_peer.h" |
17 #include "components/cronet/android/url_request_peer.h" | 17 #include "components/cronet/android/url_request_peer.h" |
18 #include "components/cronet/url_request_context_config.h" | 18 #include "components/cronet/url_request_context_config.h" |
19 #include "jni/UrlRequestContext_jni.h" | 19 #include "jni/UrlRequestContext_jni.h" |
20 | 20 |
21 // Version of this build of Chromium NET. | |
22 #define CHROMIUM_NET_VERSION "1" | |
23 | |
24 namespace { | 21 namespace { |
25 | 22 |
26 const char kVersion[] = CHROMIUM_VERSION "/" CHROMIUM_NET_VERSION; | |
27 | |
28 // Delegate of URLRequestContextPeer that delivers callbacks to the Java layer. | 23 // Delegate of URLRequestContextPeer that delivers callbacks to the Java layer. |
29 class JniURLRequestContextPeerDelegate | 24 class JniURLRequestContextPeerDelegate |
30 : public cronet::URLRequestContextPeer::URLRequestContextPeerDelegate { | 25 : public cronet::URLRequestContextPeer::URLRequestContextPeerDelegate { |
31 public: | 26 public: |
32 JniURLRequestContextPeerDelegate(JNIEnv* env, jobject owner) | 27 JniURLRequestContextPeerDelegate(JNIEnv* env, jobject owner) |
33 : owner_(env->NewGlobalRef(owner)) { | 28 : owner_(env->NewGlobalRef(owner)) { |
34 } | 29 } |
35 | 30 |
36 virtual void OnContextInitialized( | 31 virtual void OnContextInitialized( |
37 cronet::URLRequestContextPeer* context) OVERRIDE { | 32 cronet::URLRequestContextPeer* context) OVERRIDE { |
(...skipping 15 matching lines...) Expand all Loading... |
53 | 48 |
54 } // namespace | 49 } // namespace |
55 | 50 |
56 namespace cronet { | 51 namespace cronet { |
57 | 52 |
58 // Explicitly register static JNI functions. | 53 // Explicitly register static JNI functions. |
59 bool UrlRequestContextRegisterJni(JNIEnv* env) { | 54 bool UrlRequestContextRegisterJni(JNIEnv* env) { |
60 return RegisterNativesImpl(env); | 55 return RegisterNativesImpl(env); |
61 } | 56 } |
62 | 57 |
63 static jstring GetVersion(JNIEnv* env, jclass unused) { | |
64 return base::android::ConvertUTF8ToJavaString(env, kVersion).Release(); | |
65 } | |
66 | |
67 // Sets global user-agent to be used for all subsequent requests. | 58 // Sets global user-agent to be used for all subsequent requests. |
68 static jlong CreateRequestContextPeer(JNIEnv* env, | 59 static jlong CreateRequestContextPeer(JNIEnv* env, |
69 jobject object, | 60 jobject object, |
70 jobject context, | 61 jobject context, |
71 jstring user_agent, | 62 jstring user_agent, |
72 jint log_level, | 63 jint log_level, |
73 jstring config) { | 64 jstring config) { |
74 std::string user_agent_string = | 65 std::string user_agent_string = |
75 base::android::ConvertJavaStringToUTF8(env, user_agent); | 66 base::android::ConvertJavaStringToUTF8(env, user_agent); |
76 | 67 |
(...skipping 17 matching lines...) Expand all Loading... |
94 // Set application context. | 85 // Set application context. |
95 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); | 86 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); |
96 base::android::InitApplicationContext(env, scoped_context); | 87 base::android::InitApplicationContext(env, scoped_context); |
97 | 88 |
98 int logging_level = log_level; | 89 int logging_level = log_level; |
99 | 90 |
100 // TODO(dplotnikov): set application context. | 91 // TODO(dplotnikov): set application context. |
101 URLRequestContextPeer* peer = new URLRequestContextPeer( | 92 URLRequestContextPeer* peer = new URLRequestContextPeer( |
102 new JniURLRequestContextPeerDelegate(env, object), | 93 new JniURLRequestContextPeerDelegate(env, object), |
103 user_agent_string, | 94 user_agent_string, |
104 logging_level, | 95 logging_level); |
105 kVersion); | |
106 peer->AddRef(); // Hold onto this ref-counted object. | 96 peer->AddRef(); // Hold onto this ref-counted object. |
107 peer->Initialize(context_config.Pass()); | 97 peer->Initialize(context_config.Pass()); |
108 return reinterpret_cast<jlong>(peer); | 98 return reinterpret_cast<jlong>(peer); |
109 } | 99 } |
110 | 100 |
111 // Releases native objects. | 101 // Releases native objects. |
112 static void ReleaseRequestContextPeer(JNIEnv* env, | 102 static void ReleaseRequestContextPeer(JNIEnv* env, |
113 jobject object, | 103 jobject object, |
114 jlong urlRequestContextPeer) { | 104 jlong urlRequestContextPeer) { |
115 URLRequestContextPeer* peer = | 105 URLRequestContextPeer* peer = |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 // Stops recording NetLog. | 138 // Stops recording NetLog. |
149 static void StopNetLog(JNIEnv* env, | 139 static void StopNetLog(JNIEnv* env, |
150 jobject jcaller, | 140 jobject jcaller, |
151 jlong urlRequestContextPeer) { | 141 jlong urlRequestContextPeer) { |
152 URLRequestContextPeer* peer = | 142 URLRequestContextPeer* peer = |
153 reinterpret_cast<URLRequestContextPeer*>(urlRequestContextPeer); | 143 reinterpret_cast<URLRequestContextPeer*>(urlRequestContextPeer); |
154 peer->StopNetLog(); | 144 peer->StopNetLog(); |
155 } | 145 } |
156 | 146 |
157 } // namespace cronet | 147 } // namespace cronet |
OLD | NEW |