Chromium Code Reviews| Index: components/cronet/android/cronet_url_request_context.cc |
| diff --git a/components/cronet/android/cronet_url_request_context.cc b/components/cronet/android/cronet_url_request_context.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..52091f8f94847431f87af8d5d64baf836ccdd5c1 |
| --- /dev/null |
| +++ b/components/cronet/android/cronet_url_request_context.cc |
| @@ -0,0 +1,134 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/cronet/android/cronet_url_request_context.h" |
| + |
| +#include <string> |
| + |
| +#include "base/android/jni_android.h" |
| +#include "base/android/jni_string.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/metrics/statistics_recorder.h" |
| +#include "base/values.h" |
| +#include "components/cronet/android/cronet_url_request.h" |
| +#include "components/cronet/android/cronet_url_request_adapter.h" |
| +#include "components/cronet/android/cronet_url_request_context_adapter.h" |
| +#include "components/cronet/url_request_context_config.h" |
| +#include "jni/CronetUrlRequestContext_jni.h" |
| + |
| +namespace { |
| + |
| +void initJavaNetworkThread(jobject jowner) { |
| + JNIEnv* jenv = base::android::AttachCurrentThread(); |
| + cronet::Java_CronetUrlRequestContext_initNetworkThread(jenv, jowner); |
| + jenv->DeleteGlobalRef(jowner); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace cronet { |
| + |
| +// Explicitly register static JNI functions. |
| +bool CronetUrlRequestContextRegisterJni(JNIEnv* jenv) { |
| + return RegisterNativesImpl(jenv); |
| +} |
| + |
| +// Sets global user-agent to be used for all subsequent requests. |
| +static jlong CreateRequestContextAdapter(JNIEnv* jenv, |
| + jobject jcaller, |
| + jobject japp_context, |
| + jint jlog_level, |
| + jstring jconfig) { |
| + std::string config_string = |
| + base::android::ConvertJavaStringToUTF8(jenv, jconfig); |
| + |
| + scoped_ptr<base::Value> config_value(base::JSONReader::Read(config_string)); |
| + if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) { |
| + DLOG(ERROR) << "Bad JSON: " << config_string; |
| + return 0; |
| + } |
| + |
| + scoped_ptr<URLRequestContextConfig> context_config( |
| + new URLRequestContextConfig()); |
| + base::JSONValueConverter<URLRequestContextConfig> converter; |
| + if (!converter.Convert(*config_value, context_config.get())) { |
| + DLOG(ERROR) << "Bad Config: " << config_value; |
| + return 0; |
| + } |
| + |
| + // Set application context. |
| + base::android::ScopedJavaLocalRef<jobject> scoped_context(jenv, japp_context); |
| + base::android::InitApplicationContext(jenv, scoped_context); |
| + |
| + // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts. |
| + // Revisit this if each URLRequestContext would need an individual log level. |
| + logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
|
mmenke
2014/10/31 15:49:22
Suggestion: Don't take this on context creation,
mef
2014/10/31 20:39:15
Done.
|
| + |
| + CronetURLRequestContextAdapter* context_adapter = |
| + new CronetURLRequestContextAdapter(); |
| + base::Closure init_java_network_thread = base::Bind ( |
|
mmenke
2014/10/31 15:49:22
nit: Remove space before open paren.
mef
2014/10/31 20:39:15
Done.
|
| + &initJavaNetworkThread, |
| + jenv->NewGlobalRef(jcaller)); |
|
mmenke
2014/10/31 15:49:22
Can we used a scoped java ref here? Does, admitte
mef
2014/10/31 20:39:15
Acknowledged. We probably can, but I'm not sure ho
mmenke
2014/10/31 21:03:24
Think you just pass it as a ScopedJavaGlobalRef (O
mef
2014/10/31 21:43:15
Done.
|
| + context_adapter->Initialize(context_config.Pass(), init_java_network_thread); |
| + |
| + return reinterpret_cast<jlong>(context_adapter); |
| +} |
| + |
| +// Destroys native objects. |
| +static void DestroyRequestContextAdapter(JNIEnv* jenv, |
| + jobject jcaller, |
| + jlong jurl_request_context_adapter) { |
| + if (jurl_request_context_adapter == 0) |
| + return; |
| + CronetURLRequestContextAdapter* context_adapter = |
| + reinterpret_cast<CronetURLRequestContextAdapter*>( |
| + jurl_request_context_adapter); |
| + context_adapter->Destroy(); |
| +} |
| + |
| +// Starts recording statistics. |
| +static void InitializeStatistics(JNIEnv* jenv, jobject jcaller) { |
| + base::StatisticsRecorder::Initialize(); |
| +} |
| + |
| +// Gets current statistics with |filter| as a substring as JSON text (an empty |
| +// |filter| will include all registered histograms). |
| +static jstring GetStatisticsJSON(JNIEnv* jenv, |
| + jobject jcaller, |
| + jstring jfilter) { |
| + std::string query = base::android::ConvertJavaStringToUTF8(jenv, jfilter); |
| + std::string json = base::StatisticsRecorder::ToJSON(query); |
| + return base::android::ConvertUTF8ToJavaString(jenv, json).Release(); |
| +} |
| + |
| +// Starts recording NetLog into file with |fileName|. |
| +static void StartNetLogToFile(JNIEnv* jenv, |
| + jobject jcaller, |
| + jlong jurl_request_context_adapter, |
| + jstring fileName) { |
| + if (jurl_request_context_adapter == 0) |
| + return; |
| + CronetURLRequestContextAdapter* context_adapter = |
| + reinterpret_cast<CronetURLRequestContextAdapter*>( |
| + jurl_request_context_adapter); |
| + std::string file_name = |
| + base::android::ConvertJavaStringToUTF8(jenv, fileName); |
| + context_adapter->StartNetLogToFile(file_name); |
| +} |
| + |
| +// Stops recording NetLog. |
| +static void StopNetLog(JNIEnv* jenv, |
| + jobject jcaller, |
| + jlong jurl_request_context_adapter) { |
| + if (jurl_request_context_adapter == 0) |
| + return; |
| + CronetURLRequestContextAdapter* context_adapter = |
| + reinterpret_cast<CronetURLRequestContextAdapter*>( |
| + jurl_request_context_adapter); |
| + context_adapter->StopNetLog(); |
| +} |
| + |
| +} // namespace cronet |