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.
5 #include "components/cronet/android/org_chromium_net_UrlRequestContext.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/metrics/statistics_recorder.h"
10 #include "components/cronet/android/org_chromium_net_UrlRequest.h"
11 #include "components/cronet/android/url_request_context_peer.h"
12 #include "components/cronet/android/url_request_peer.h"
13 #include "jni/UrlRequestContext_jni.h"
15 // Version of this build of Chromium NET.
16 #define CHROMIUM_NET_VERSION "1"
20 const char kVersion
[] = CHROMIUM_VERSION
"/" CHROMIUM_NET_VERSION
;
22 // Delegate of URLRequestContextPeer that delivers callbacks to the Java layer.
23 class JniURLRequestContextPeerDelegate
24 : public cronet::URLRequestContextPeer::URLRequestContextPeerDelegate
{
26 JniURLRequestContextPeerDelegate(JNIEnv
* env
, jobject owner
)
27 : owner_(env
->NewGlobalRef(owner
)) {
30 virtual void OnContextInitialized(
31 cronet::URLRequestContextPeer
* context
) OVERRIDE
{
32 JNIEnv
* env
= base::android::AttachCurrentThread();
33 cronet::Java_UrlRequestContext_initNetworkThread(env
, owner_
);
34 // TODO(dplotnikov): figure out if we need to detach from the thread.
35 // The documentation says we should detach just before the thread exits.
39 virtual ~JniURLRequestContextPeerDelegate() {
40 JNIEnv
* env
= base::android::AttachCurrentThread();
41 env
->DeleteGlobalRef(owner_
);
52 // Explicitly register static JNI functions.
53 bool UrlRequestContextRegisterJni(JNIEnv
* env
) {
54 return RegisterNativesImpl(env
);
57 static jstring
GetVersion(JNIEnv
* env
, jclass unused
) {
58 return base::android::ConvertUTF8ToJavaString(env
, kVersion
).Release();
61 // Sets global user-agent to be used for all subsequent requests.
62 static jlong
CreateRequestContextPeer(JNIEnv
* env
,
67 const char* user_agent_utf8
= env
->GetStringUTFChars(user_agent
, NULL
);
68 std::string
user_agent_string(user_agent_utf8
);
69 env
->ReleaseStringUTFChars(user_agent
, user_agent_utf8
);
71 // Set application context.
72 base::android::ScopedJavaLocalRef
<jobject
> scoped_context(env
, context
);
73 base::android::InitApplicationContext(env
, scoped_context
);
75 int logging_level
= log_level
;
77 // TODO(dplotnikov): set application context.
78 URLRequestContextPeer
* peer
= new URLRequestContextPeer(
79 new JniURLRequestContextPeerDelegate(env
, object
),
83 peer
->AddRef(); // Hold onto this ref-counted object.
85 return reinterpret_cast<jlong
>(peer
);
88 // Releases native objects.
89 static void ReleaseRequestContextPeer(JNIEnv
* env
,
91 jlong urlRequestContextPeer
) {
92 URLRequestContextPeer
* peer
=
93 reinterpret_cast<URLRequestContextPeer
*>(urlRequestContextPeer
);
94 // TODO(mef): Revisit this from thread safety point of view: Can we delete a
95 // thread while running on that thread?
96 // URLRequestContextPeer is a ref-counted object, and may have pending tasks,
97 // so we need to release it instead of deleting here.
101 // Starts recording statistics.
102 static void InitializeStatistics(JNIEnv
* env
, jobject jcaller
) {
103 base::StatisticsRecorder::Initialize();
106 // Gets current statistics with |filter| as a substring as JSON text (an empty
107 // |filter| will include all registered histograms).
108 static jstring
GetStatisticsJSON(JNIEnv
* env
, jobject jcaller
, jstring filter
) {
109 std::string query
= base::android::ConvertJavaStringToUTF8(env
, filter
);
110 std::string json
= base::StatisticsRecorder::ToJSON(query
);
111 return base::android::ConvertUTF8ToJavaString(env
, json
).Release();
114 // Starts recording NetLog into file with |fileName|.
115 static void StartNetLogToFile(JNIEnv
* env
,
117 jlong urlRequestContextPeer
,
119 URLRequestContextPeer
* peer
=
120 reinterpret_cast<URLRequestContextPeer
*>(urlRequestContextPeer
);
121 std::string file_name
= base::android::ConvertJavaStringToUTF8(env
, fileName
);
122 peer
->StartNetLogToFile(file_name
);
125 // Stops recording NetLog.
126 static void StopNetLog(JNIEnv
* env
,
128 jlong urlRequestContextPeer
) {
129 URLRequestContextPeer
* peer
=
130 reinterpret_cast<URLRequestContextPeer
*>(urlRequestContextPeer
);
134 } // namespace cronet