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

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 Helen's comments, add CronetUrlRequestContextTest. Created 6 years, 1 month 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 void initJavaNetworkThread(jobject jowner) {
25 JNIEnv* jenv = base::android::AttachCurrentThread();
26 cronet::Java_CronetUrlRequestContext_initNetworkThread(jenv, jowner);
27 jenv->DeleteGlobalRef(jowner);
28 }
29
30 } // namespace
31
32 namespace cronet {
33
34 // Explicitly register static JNI functions.
35 bool CronetUrlRequestContextRegisterJni(JNIEnv* jenv) {
36 return RegisterNativesImpl(jenv);
37 }
38
39 // Sets global user-agent to be used for all subsequent requests.
40 static jlong CreateRequestContextAdapter(JNIEnv* jenv,
41 jobject jcaller,
42 jobject japp_context,
43 jint jlog_level,
44 jstring jconfig) {
45 std::string config_string =
46 base::android::ConvertJavaStringToUTF8(jenv, jconfig);
47
48 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config_string));
49 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) {
50 DLOG(ERROR) << "Bad JSON: " << config_string;
51 return 0;
52 }
53
54 scoped_ptr<URLRequestContextConfig> context_config(
55 new URLRequestContextConfig());
56 base::JSONValueConverter<URLRequestContextConfig> converter;
57 if (!converter.Convert(*config_value, context_config.get())) {
58 DLOG(ERROR) << "Bad Config: " << config_value;
59 return 0;
60 }
61
62 // Set application context.
63 base::android::ScopedJavaLocalRef<jobject> scoped_context(jenv, japp_context);
64 base::android::InitApplicationContext(jenv, scoped_context);
65
66 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts.
67 // Revisit this if each URLRequestContext would need an individual log level.
68 logging::SetMinLogLevel(static_cast<int>(jlog_level));
69
70 CronetURLRequestContextAdapter* context_adapter =
71 new CronetURLRequestContextAdapter();
72 base::Closure init_java_network_thread = base::Bind (
73 &initJavaNetworkThread,
74 jenv->NewGlobalRef(jcaller));
75 context_adapter->Initialize(context_config.Pass(), init_java_network_thread);
76
77 return reinterpret_cast<jlong>(context_adapter);
78 }
79
80 // Destroys native objects.
81 static void DestroyRequestContextAdapter(JNIEnv* jenv,
82 jobject jcaller,
83 jlong jurl_request_context_adapter) {
84 if (jurl_request_context_adapter == 0)
85 return;
86 CronetURLRequestContextAdapter* context_adapter =
87 reinterpret_cast<CronetURLRequestContextAdapter*>(
88 jurl_request_context_adapter);
89 context_adapter->Destroy();
90 }
91
92 // Starts recording statistics.
93 static void InitializeStatistics(JNIEnv* jenv, jobject jcaller) {
94 base::StatisticsRecorder::Initialize();
95 }
96
97 // Gets current statistics with |filter| as a substring as JSON text (an empty
98 // |filter| will include all registered histograms).
99 static jstring GetStatisticsJSON(JNIEnv* jenv,
100 jobject jcaller,
101 jstring jfilter) {
102 std::string query = base::android::ConvertJavaStringToUTF8(jenv, jfilter);
103 std::string json = base::StatisticsRecorder::ToJSON(query);
104 return base::android::ConvertUTF8ToJavaString(jenv, json).Release();
105 }
106
107 // Starts recording NetLog into file with |fileName|.
108 static void StartNetLogToFile(JNIEnv* jenv,
109 jobject jcaller,
110 jlong jurl_request_context_adapter,
111 jstring fileName) {
112 if (jurl_request_context_adapter == 0)
113 return;
114 CronetURLRequestContextAdapter* context_adapter =
115 reinterpret_cast<CronetURLRequestContextAdapter*>(
116 jurl_request_context_adapter);
117 std::string file_name =
118 base::android::ConvertJavaStringToUTF8(jenv, fileName);
119 context_adapter->StartNetLogToFile(file_name);
120 }
121
122 // Stops recording NetLog.
123 static void StopNetLog(JNIEnv* jenv,
124 jobject jcaller,
125 jlong jurl_request_context_adapter) {
126 if (jurl_request_context_adapter == 0)
127 return;
128 CronetURLRequestContextAdapter* context_adapter =
129 reinterpret_cast<CronetURLRequestContextAdapter*>(
130 jurl_request_context_adapter);
131 context_adapter->StopNetLog();
132 }
133
134 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698