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

Side by Side Diff: components/cronet/android/cronet_url_request_context.cc

Issue 586143002: Initial implementation of Cronet Async API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments, add mock errors to CronetUrlRequestTest. Created 6 years, 2 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
OLDNEW
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/chromium_url_request_context.h" 5 #include "components/cronet/android/cronet_url_request_context.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/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/metrics/statistics_recorder.h" 14 #include "base/metrics/statistics_recorder.h"
15 #include "base/values.h" 15 #include "base/values.h"
16 #include "components/cronet/android/chromium_url_request.h" 16 #include "components/cronet/android/cronet_url_request.h"
17 #include "components/cronet/android/url_request_adapter.h" 17 #include "components/cronet/android/cronet_url_request_adapter.h"
18 #include "components/cronet/android/url_request_context_adapter.h" 18 #include "components/cronet/android/cronet_url_request_context_adapter.h"
19 #include "components/cronet/url_request_context_config.h" 19 #include "components/cronet/url_request_context_config.h"
20 #include "jni/ChromiumUrlRequestContext_jni.h" 20 #include "jni/CronetUrlRequestContext_jni.h"
21 21
22 namespace { 22 namespace {
23 23
24 // Delegate of URLRequestContextAdapter that delivers callbacks to the Java 24 // Delegate of CronetURLRequestContextAdapter that delivers callbacks to the
25 // Java
25 // layer. 26 // layer.
26 class JniURLRequestContextAdapterDelegate 27 class JniCronetURLRequestContextAdapterDelegate
27 : public cronet::URLRequestContextAdapter:: 28 : public cronet::CronetURLRequestContextAdapter::
28 URLRequestContextAdapterDelegate { 29 CronetURLRequestContextAdapterDelegate {
29 public: 30 public:
30 JniURLRequestContextAdapterDelegate(JNIEnv* env, jobject owner) 31 JniCronetURLRequestContextAdapterDelegate(JNIEnv* env, jobject owner)
31 : owner_(env->NewGlobalRef(owner)) {} 32 : owner_(env->NewGlobalRef(owner)) {}
32 33
33 virtual void OnContextInitialized( 34 void OnContextInitialized(
34 cronet::URLRequestContextAdapter* context) override { 35 cronet::CronetURLRequestContextAdapter* context) override {
35 JNIEnv* env = base::android::AttachCurrentThread(); 36 JNIEnv* env = base::android::AttachCurrentThread();
36 cronet::Java_ChromiumUrlRequestContext_initNetworkThread(env, owner_); 37 cronet::Java_CronetUrlRequestContext_initNetworkThread(env, owner_);
37 // TODO(dplotnikov): figure out if we need to detach from the thread. 38 // TODO(dplotnikov): figure out if we need to detach from the thread.
38 // The documentation says we should detach just before the thread exits. 39 // The documentation says we should detach just before the thread exits.
39 } 40 }
40 41
41 protected: 42 protected:
42 virtual ~JniURLRequestContextAdapterDelegate() { 43 virtual ~JniCronetURLRequestContextAdapterDelegate() {
43 JNIEnv* env = base::android::AttachCurrentThread(); 44 JNIEnv* env = base::android::AttachCurrentThread();
44 env->DeleteGlobalRef(owner_); 45 env->DeleteGlobalRef(owner_);
45 } 46 }
46 47
47 private: 48 private:
48 jobject owner_; 49 jobject owner_;
49 }; 50 };
50 51
51 } // namespace 52 } // namespace
52 53
53 namespace cronet { 54 namespace cronet {
54 55
55 // Explicitly register static JNI functions. 56 // Explicitly register static JNI functions.
56 bool ChromiumUrlRequestContextRegisterJni(JNIEnv* env) { 57 bool CronetUrlRequestContextRegisterJni(JNIEnv* env) {
57 return RegisterNativesImpl(env); 58 return RegisterNativesImpl(env);
58 } 59 }
59 60
60 // Sets global user-agent to be used for all subsequent requests. 61 // Sets global user-agent to be used for all subsequent requests.
61 static jlong CreateRequestContextAdapter(JNIEnv* env, 62 static jlong CreateRequestContextAdapter(JNIEnv* env,
62 jobject object, 63 jobject object,
63 jobject context, 64 jobject context,
64 jstring user_agent,
65 jint log_level, 65 jint log_level,
66 jstring config) { 66 jstring config) {
67 std::string user_agent_string =
68 base::android::ConvertJavaStringToUTF8(env, user_agent);
69
70 std::string config_string = 67 std::string config_string =
71 base::android::ConvertJavaStringToUTF8(env, config); 68 base::android::ConvertJavaStringToUTF8(env, config);
72 69
73 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config_string)); 70 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config_string));
74 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) { 71 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) {
75 DLOG(ERROR) << "Bad JSON: " << config_string; 72 DLOG(ERROR) << "Bad JSON: " << config_string;
76 return 0; 73 return 0;
77 } 74 }
78 75
79 scoped_ptr<URLRequestContextConfig> context_config( 76 scoped_ptr<URLRequestContextConfig> context_config(
80 new URLRequestContextConfig()); 77 new URLRequestContextConfig());
81 base::JSONValueConverter<URLRequestContextConfig> converter; 78 base::JSONValueConverter<URLRequestContextConfig> converter;
82 if (!converter.Convert(*config_value, context_config.get())) { 79 if (!converter.Convert(*config_value, context_config.get())) {
83 DLOG(ERROR) << "Bad Config: " << config_value; 80 DLOG(ERROR) << "Bad Config: " << config_value;
84 return 0; 81 return 0;
85 } 82 }
86 83
87 // Set application context. 84 // Set application context.
88 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); 85 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
89 base::android::InitApplicationContext(env, scoped_context); 86 base::android::InitApplicationContext(env, scoped_context);
90 87
91 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts. 88 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts.
92 // Revisit this if each URLRequestContext would need an individual log level. 89 // Revisit this if each URLRequestContext would need an individual log level.
93 logging::SetMinLogLevel(static_cast<int>(log_level)); 90 logging::SetMinLogLevel(static_cast<int>(log_level));
94 91
95 // TODO(dplotnikov): set application context. 92 // TODO(dplotnikov): set application context.
96 URLRequestContextAdapter* adapter = new URLRequestContextAdapter( 93 CronetURLRequestContextAdapter* adapter = new CronetURLRequestContextAdapter(
xunjieli 2014/10/17 00:31:34 nit: There are two types of adapter, one is for th
mef 2014/10/17 20:19:42 Done.
97 new JniURLRequestContextAdapterDelegate(env, object), user_agent_string); 94 new JniCronetURLRequestContextAdapterDelegate(env, object));
98 adapter->AddRef(); // Hold onto this ref-counted object. 95 adapter->AddRef(); // Hold onto this ref-counted object.
99 adapter->Initialize(context_config.Pass()); 96 adapter->Initialize(context_config.Pass());
100 return reinterpret_cast<jlong>(adapter); 97 return reinterpret_cast<jlong>(adapter);
101 } 98 }
102 99
103 // Releases native objects. 100 // Releases native objects.
104 static void ReleaseRequestContextAdapter(JNIEnv* env, 101 static void ReleaseRequestContextAdapter(JNIEnv* env,
105 jobject object, 102 jobject object,
106 jlong urlRequestContextAdapter) { 103 jlong urlRequestContextAdapter) {
107 URLRequestContextAdapter* adapter = 104 CronetURLRequestContextAdapter* adapter =
108 reinterpret_cast<URLRequestContextAdapter*>(urlRequestContextAdapter); 105 reinterpret_cast<CronetURLRequestContextAdapter*>(
106 urlRequestContextAdapter);
109 // TODO(mef): Revisit this from thread safety point of view: Can we delete a 107 // TODO(mef): Revisit this from thread safety point of view: Can we delete a
110 // thread while running on that thread? 108 // thread while running on that thread?
111 // URLRequestContextAdapter is a ref-counted object, and may have pending 109 // CronetURLRequestContextAdapter is a ref-counted object, and may have
110 // pending
112 // tasks, 111 // tasks,
xunjieli 2014/10/17 00:31:34 nit: formatting.
mef 2014/10/17 20:19:42 Done.
113 // so we need to release it instead of deleting here. 112 // so we need to release it instead of deleting here.
114 adapter->Release(); 113 adapter->Release();
115 } 114 }
116 115
117 // Starts recording statistics. 116 // Starts recording statistics.
118 static void InitializeStatistics(JNIEnv* env, jobject jcaller) { 117 static void InitializeStatistics(JNIEnv* env, jobject jcaller) {
119 base::StatisticsRecorder::Initialize(); 118 base::StatisticsRecorder::Initialize();
120 } 119 }
121 120
122 // Gets current statistics with |filter| as a substring as JSON text (an empty 121 // Gets current statistics with |filter| as a substring as JSON text (an empty
123 // |filter| will include all registered histograms). 122 // |filter| will include all registered histograms).
124 static jstring GetStatisticsJSON(JNIEnv* env, jobject jcaller, jstring filter) { 123 static jstring GetStatisticsJSON(JNIEnv* env, jobject jcaller, jstring filter) {
125 std::string query = base::android::ConvertJavaStringToUTF8(env, filter); 124 std::string query = base::android::ConvertJavaStringToUTF8(env, filter);
126 std::string json = base::StatisticsRecorder::ToJSON(query); 125 std::string json = base::StatisticsRecorder::ToJSON(query);
127 return base::android::ConvertUTF8ToJavaString(env, json).Release(); 126 return base::android::ConvertUTF8ToJavaString(env, json).Release();
128 } 127 }
129 128
130 // Starts recording NetLog into file with |fileName|. 129 // Starts recording NetLog into file with |fileName|.
131 static void StartNetLogToFile(JNIEnv* env, 130 static void StartNetLogToFile(JNIEnv* env,
132 jobject jcaller, 131 jobject jcaller,
133 jlong urlRequestContextAdapter, 132 jlong urlRequestContextAdapter,
xunjieli 2014/10/17 00:31:35 nit: c_style_naming with a "j" prefix. eg. jurl_re
mef 2014/10/17 20:19:42 Done.
134 jstring fileName) { 133 jstring fileName) {
135 URLRequestContextAdapter* adapter = 134 CronetURLRequestContextAdapter* adapter =
136 reinterpret_cast<URLRequestContextAdapter*>(urlRequestContextAdapter); 135 reinterpret_cast<CronetURLRequestContextAdapter*>(
136 urlRequestContextAdapter);
137 std::string file_name = base::android::ConvertJavaStringToUTF8(env, fileName); 137 std::string file_name = base::android::ConvertJavaStringToUTF8(env, fileName);
138 adapter->StartNetLogToFile(file_name); 138 adapter->StartNetLogToFile(file_name);
139 } 139 }
140 140
141 // Stops recording NetLog. 141 // Stops recording NetLog.
142 static void StopNetLog(JNIEnv* env, 142 static void StopNetLog(JNIEnv* env,
143 jobject jcaller, 143 jobject jcaller,
144 jlong urlRequestContextAdapter) { 144 jlong urlRequestContextAdapter) {
145 URLRequestContextAdapter* adapter = 145 CronetURLRequestContextAdapter* adapter =
146 reinterpret_cast<URLRequestContextAdapter*>(urlRequestContextAdapter); 146 reinterpret_cast<CronetURLRequestContextAdapter*>(
147 urlRequestContextAdapter);
147 adapter->StopNetLog(); 148 adapter->StopNetLog();
148 } 149 }
149 150
150 } // namespace cronet 151 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698