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

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: More tests, more comments, more cancel. 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
(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 "components/cronet/android/cronet_url_request_context.h"
6
7 #include <string>
8
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/json/json_reader.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/metrics/statistics_recorder.h"
15 #include "base/values.h"
16 #include "components/cronet/android/cronet_url_request.h"
17 #include "components/cronet/android/cronet_url_request_adapter.h"
18 #include "components/cronet/android/cronet_url_request_context_adapter.h"
19 #include "components/cronet/url_request_context_config.h"
20 #include "jni/CronetUrlRequestContext_jni.h"
21
22 namespace {
23
24 // Delegate of CronetURLRequestContextAdapter that delivers callbacks to the
25 // Java layer.
26 class JniCronetURLRequestContextAdapterDelegate
mmenke 2014/10/23 16:26:01 Do we really need a delegate for this? This adds
mef 2014/10/24 03:31:44 Done.
27 : public cronet::CronetURLRequestContextAdapter::
28 CronetURLRequestContextAdapterDelegate {
29 public:
30 JniCronetURLRequestContextAdapterDelegate(JNIEnv* jenv, jobject jowner)
31 : owner_(jenv->NewGlobalRef(jowner)) {}
32
33 void OnContextInitialized(
34 cronet::CronetURLRequestContextAdapter* context_adapter) override {
35 JNIEnv* jenv = base::android::AttachCurrentThread();
36 cronet::Java_CronetUrlRequestContext_initNetworkThread(jenv, owner_);
37 }
38
39 protected:
40 virtual ~JniCronetURLRequestContextAdapterDelegate() {
41 JNIEnv* jenv = base::android::AttachCurrentThread();
42 jenv->DeleteGlobalRef(owner_);
43 }
44
45 private:
46 jobject owner_;
47 };
48
49 } // namespace
50
51 namespace cronet {
52
53 // Explicitly register static JNI functions.
54 bool CronetUrlRequestContextRegisterJni(JNIEnv* jenv) {
55 return RegisterNativesImpl(jenv);
56 }
57
58 // Sets global user-agent to be used for all subsequent requests.
59 static jlong CreateRequestContextAdapter(JNIEnv* jenv,
60 jobject object,
xunjieli 2014/10/22 21:02:16 nit: "jobject jcaller" as it's the case in other m
mef 2014/10/24 03:31:44 Done.
61 jobject japp_context,
62 jint jlog_level,
63 jstring jconfig) {
64 std::string config_string =
65 base::android::ConvertJavaStringToUTF8(jenv, jconfig);
66
67 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config_string));
68 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) {
69 DLOG(ERROR) << "Bad JSON: " << config_string;
70 return 0;
71 }
72
73 scoped_ptr<URLRequestContextConfig> context_config(
74 new URLRequestContextConfig());
75 base::JSONValueConverter<URLRequestContextConfig> converter;
76 if (!converter.Convert(*config_value, context_config.get())) {
77 DLOG(ERROR) << "Bad Config: " << config_value;
78 return 0;
79 }
80
81 // Set application context.
82 base::android::ScopedJavaLocalRef<jobject> scoped_context(jenv, japp_context);
83 base::android::InitApplicationContext(jenv, scoped_context);
84
85 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts.
86 // Revisit this if each URLRequestContext would need an individual log level.
87 logging::SetMinLogLevel(static_cast<int>(jlog_level));
88
89 // TODO(dplotnikov): set application context.
mmenke 2014/10/23 16:26:01 ?
mef 2014/10/24 03:31:44 Copy-paste. Removed.
90 CronetURLRequestContextAdapter* context_adapter =
91 new CronetURLRequestContextAdapter(
92 new JniCronetURLRequestContextAdapterDelegate(jenv, object));
93 context_adapter->AddRef(); // Hold onto this ref-counted object.
94 context_adapter->Initialize(context_config.Pass());
95 return reinterpret_cast<jlong>(context_adapter);
96 }
97
98 // Releases native objects.
99 static void ReleaseRequestContextAdapter(JNIEnv* jenv,
100 jobject jcaller,
101 jlong jurl_request_context_adapter) {
102 CronetURLRequestContextAdapter* context_adapter =
103 reinterpret_cast<CronetURLRequestContextAdapter*>(
104 jurl_request_context_adapter);
105 // TODO(mef): Revisit this from thread safety point of view: Can we delete a
106 // thread while running on that thread?
mmenke 2014/10/23 16:26:01 No, we can't - we use pthread_join to terminate a
mef 2014/10/24 03:31:44 Done.
107 // CronetURLRequestContextAdapter is a ref-counted object, and may have
108 // pending tasks, so we need to release it instead of deleting here.
109 context_adapter->Release();
110 }
111
112 // Starts recording statistics.
113 static void InitializeStatistics(JNIEnv* jenv, jobject jcaller) {
114 base::StatisticsRecorder::Initialize();
115 }
116
117 // Gets current statistics with |filter| as a substring as JSON text (an empty
118 // |filter| will include all registered histograms).
119 static jstring GetStatisticsJSON(JNIEnv* jenv,
120 jobject jcaller,
121 jstring jfilter) {
122 std::string query = base::android::ConvertJavaStringToUTF8(jenv, jfilter);
123 std::string json = base::StatisticsRecorder::ToJSON(query);
124 return base::android::ConvertUTF8ToJavaString(jenv, json).Release();
125 }
126
127 // Starts recording NetLog into file with |fileName|.
128 static void StartNetLogToFile(JNIEnv* jenv,
129 jobject jcaller,
130 jlong jurl_request_context_adapter,
131 jstring fileName) {
132 CronetURLRequestContextAdapter* context_adapter =
133 reinterpret_cast<CronetURLRequestContextAdapter*>(
134 jurl_request_context_adapter);
135 std::string file_name =
136 base::android::ConvertJavaStringToUTF8(jenv, fileName);
137 context_adapter->StartNetLogToFile(file_name);
138 }
139
140 // Stops recording NetLog.
141 static void StopNetLog(JNIEnv* jenv,
142 jobject jcaller,
143 jlong jurl_request_context_adapter) {
144 CronetURLRequestContextAdapter* context_adapter =
145 reinterpret_cast<CronetURLRequestContextAdapter*>(
146 jurl_request_context_adapter);
147 context_adapter->StopNetLog();
148 }
149
150 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698