Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / cronet / android / org_chromium_net_UrlRequestContext.cc
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/org_chromium_net_UrlRequestContext.h"
6
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"
14
15 // Version of this build of Chromium NET.
16 #define CHROMIUM_NET_VERSION "1"
17
18 namespace {
19
20 const char kVersion[] = CHROMIUM_VERSION "/" CHROMIUM_NET_VERSION;
21
22 // Delegate of URLRequestContextPeer that delivers callbacks to the Java layer.
23 class JniURLRequestContextPeerDelegate
24     : public cronet::URLRequestContextPeer::URLRequestContextPeerDelegate {
25  public:
26   JniURLRequestContextPeerDelegate(JNIEnv* env, jobject owner)
27       : owner_(env->NewGlobalRef(owner)) {
28   }
29
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.
36   }
37
38  protected:
39   virtual ~JniURLRequestContextPeerDelegate() {
40     JNIEnv* env = base::android::AttachCurrentThread();
41     env->DeleteGlobalRef(owner_);
42   }
43
44  private:
45   jobject owner_;
46 };
47
48 }  // namespace
49
50 namespace cronet {
51
52 // Explicitly register static JNI functions.
53 bool UrlRequestContextRegisterJni(JNIEnv* env) {
54   return RegisterNativesImpl(env);
55 }
56
57 static jstring GetVersion(JNIEnv* env, jclass unused) {
58   return base::android::ConvertUTF8ToJavaString(env, kVersion).Release();
59 }
60
61 // Sets global user-agent to be used for all subsequent requests.
62 static jlong CreateRequestContextPeer(JNIEnv* env,
63                                       jobject object,
64                                       jobject context,
65                                       jstring user_agent,
66                                       jint log_level) {
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);
70
71   // Set application context.
72   base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
73   base::android::InitApplicationContext(env, scoped_context);
74
75   int logging_level = log_level;
76
77   // TODO(dplotnikov): set application context.
78   URLRequestContextPeer* peer = new URLRequestContextPeer(
79       new JniURLRequestContextPeerDelegate(env, object),
80       user_agent_string,
81       logging_level,
82       kVersion);
83   peer->AddRef();  // Hold onto this ref-counted object.
84   peer->Initialize();
85   return reinterpret_cast<jlong>(peer);
86 }
87
88 // Releases native objects.
89 static void ReleaseRequestContextPeer(JNIEnv* env,
90                                       jobject object,
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.
98   peer->Release();
99 }
100
101 // Starts recording statistics.
102 static void InitializeStatistics(JNIEnv* env, jobject jcaller) {
103   base::StatisticsRecorder::Initialize();
104 }
105
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();
112 }
113
114 // Starts recording NetLog into file with |fileName|.
115 static void StartNetLogToFile(JNIEnv* env,
116                               jobject jcaller,
117                               jlong urlRequestContextPeer,
118                               jstring fileName) {
119   URLRequestContextPeer* peer =
120       reinterpret_cast<URLRequestContextPeer*>(urlRequestContextPeer);
121   std::string file_name = base::android::ConvertJavaStringToUTF8(env, fileName);
122   peer->StartNetLogToFile(file_name);
123 }
124
125 // Stops recording NetLog.
126 static void StopNetLog(JNIEnv* env,
127                        jobject jcaller,
128                        jlong urlRequestContextPeer) {
129   URLRequestContextPeer* peer =
130       reinterpret_cast<URLRequestContextPeer*>(urlRequestContextPeer);
131   peer->StopNetLog();
132 }
133
134 }  // namespace cronet