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

Unified 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 side-by-side diff with in-line comments
Download patch
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..df0a3eff84cd77e506bf786af1a66b8cfc45fd00
--- /dev/null
+++ b/components/cronet/android/cronet_url_request_context.cc
@@ -0,0 +1,150 @@
+// 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 {
+
+// Delegate of CronetURLRequestContextAdapter that delivers callbacks to the
+// Java layer.
+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.
+ : public cronet::CronetURLRequestContextAdapter::
+ CronetURLRequestContextAdapterDelegate {
+ public:
+ JniCronetURLRequestContextAdapterDelegate(JNIEnv* jenv, jobject jowner)
+ : owner_(jenv->NewGlobalRef(jowner)) {}
+
+ void OnContextInitialized(
+ cronet::CronetURLRequestContextAdapter* context_adapter) override {
+ JNIEnv* jenv = base::android::AttachCurrentThread();
+ cronet::Java_CronetUrlRequestContext_initNetworkThread(jenv, owner_);
+ }
+
+ protected:
+ virtual ~JniCronetURLRequestContextAdapterDelegate() {
+ JNIEnv* jenv = base::android::AttachCurrentThread();
+ jenv->DeleteGlobalRef(owner_);
+ }
+
+ private:
+ jobject owner_;
+};
+
+} // 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 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.
+ 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));
+
+ // TODO(dplotnikov): set application context.
mmenke 2014/10/23 16:26:01 ?
mef 2014/10/24 03:31:44 Copy-paste. Removed.
+ CronetURLRequestContextAdapter* context_adapter =
+ new CronetURLRequestContextAdapter(
+ new JniCronetURLRequestContextAdapterDelegate(jenv, object));
+ context_adapter->AddRef(); // Hold onto this ref-counted object.
+ context_adapter->Initialize(context_config.Pass());
+ return reinterpret_cast<jlong>(context_adapter);
+}
+
+// Releases native objects.
+static void ReleaseRequestContextAdapter(JNIEnv* jenv,
+ jobject jcaller,
+ jlong jurl_request_context_adapter) {
+ CronetURLRequestContextAdapter* context_adapter =
+ reinterpret_cast<CronetURLRequestContextAdapter*>(
+ jurl_request_context_adapter);
+ // TODO(mef): Revisit this from thread safety point of view: Can we delete a
+ // 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.
+ // CronetURLRequestContextAdapter is a ref-counted object, and may have
+ // pending tasks, so we need to release it instead of deleting here.
+ context_adapter->Release();
+}
+
+// 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) {
+ 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) {
+ CronetURLRequestContextAdapter* context_adapter =
+ reinterpret_cast<CronetURLRequestContextAdapter*>(
+ jurl_request_context_adapter);
+ context_adapter->StopNetLog();
+}
+
+} // namespace cronet

Powered by Google App Engine
This is Rietveld 408576698