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

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 more comments. 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 context_adapter->AddRef(); // Hold onto this ref-counted object.
73 base::Closure init_java_network_thread = base::Bind (
74 &initJavaNetworkThread,
75 jenv->NewGlobalRef(jcaller));
76 context_adapter->Initialize(context_config.Pass(), init_java_network_thread);
77
78 return reinterpret_cast<jlong>(context_adapter);
79 }
80
81 // Releases native objects.
82 static void ReleaseRequestContextAdapter(JNIEnv* jenv,
83 jobject jcaller,
84 jlong jurl_request_context_adapter) {
85 CronetURLRequestContextAdapter* context_adapter =
86 reinterpret_cast<CronetURLRequestContextAdapter*>(
87 jurl_request_context_adapter);
88 // CronetURLRequestContextAdapter is a ref-counted object, and may have
89 // pending tasks, so we need to release it instead of deleting here.
90 context_adapter->Release();
91 }
92
93 // Starts recording statistics.
94 static void InitializeStatistics(JNIEnv* jenv, jobject jcaller) {
95 base::StatisticsRecorder::Initialize();
96 }
97
98 // Gets current statistics with |filter| as a substring as JSON text (an empty
99 // |filter| will include all registered histograms).
100 static jstring GetStatisticsJSON(JNIEnv* jenv,
101 jobject jcaller,
102 jstring jfilter) {
103 std::string query = base::android::ConvertJavaStringToUTF8(jenv, jfilter);
104 std::string json = base::StatisticsRecorder::ToJSON(query);
105 return base::android::ConvertUTF8ToJavaString(jenv, json).Release();
106 }
107
108 // Starts recording NetLog into file with |fileName|.
109 static void StartNetLogToFile(JNIEnv* jenv,
110 jobject jcaller,
111 jlong jurl_request_context_adapter,
112 jstring fileName) {
113 CronetURLRequestContextAdapter* context_adapter =
114 reinterpret_cast<CronetURLRequestContextAdapter*>(
115 jurl_request_context_adapter);
116 std::string file_name =
117 base::android::ConvertJavaStringToUTF8(jenv, fileName);
118 context_adapter->StartNetLogToFile(file_name);
119 }
120
121 // Stops recording NetLog.
122 static void StopNetLog(JNIEnv* jenv,
123 jobject jcaller,
124 jlong jurl_request_context_adapter) {
125 CronetURLRequestContextAdapter* context_adapter =
126 reinterpret_cast<CronetURLRequestContextAdapter*>(
127 jurl_request_context_adapter);
128 context_adapter->StopNetLog();
129 }
130
131 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698