summaryrefslogtreecommitdiffstats
path: root/components/cronet/android/cronet_url_request.cc
blob: ed63c5b851b02de7fd5643098e4055af2aabced1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// 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.h"

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/logging.h"
#include "base/macros.h"
#include "components/cronet/android/cronet_url_request_adapter.h"
#include "components/cronet/android/cronet_url_request_context_adapter.h"
#include "jni/CronetUrlRequest_jni.h"
#include "net/base/net_errors.h"
#include "net/base/request_priority.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_util.h"

using base::android::ConvertUTF8ToJavaString;

// This file contains all the plumbing to handle the bidirectional communication
// between the Java CronetURLRequest and C++ CronetURLRequestAdapter.

namespace cronet {
namespace {

// A delegate of CronetURLRequestAdapter that delivers callbacks to the Java
// layer. Created on a Java thread, but always called and destroyed on the
// Network thread.
class JniCronetURLRequestAdapterDelegate
    : public CronetURLRequestAdapter::CronetURLRequestAdapterDelegate {
 public:
  JniCronetURLRequestAdapterDelegate(JNIEnv* env, jobject owner) {
    owner_.Reset(env, owner);
  }

  // CronetURLRequestAdapter::CronetURLRequestAdapterDelegate implementation.

  void OnRedirect(const GURL& new_location, int http_status_code) override {
    JNIEnv* env = base::android::AttachCurrentThread();
    cronet::Java_CronetUrlRequest_onRedirect(
        env,
        owner_.obj(),
        ConvertUTF8ToJavaString(env, new_location.spec()).obj(),
        http_status_code);
  }

  void OnResponseStarted(int http_status_code) override {
    JNIEnv* env = base::android::AttachCurrentThread();
    cronet::Java_CronetUrlRequest_onResponseStarted(env,
                                                    owner_.obj(),
                                                    http_status_code);
  }

  void OnBytesRead(unsigned char* bytes,
                   int bytes_read) override {
    JNIEnv* env = base::android::AttachCurrentThread();
    base::android::ScopedJavaLocalRef<jobject> java_buffer(
        env, env->NewDirectByteBuffer(bytes, bytes_read));
    cronet::Java_CronetUrlRequest_onDataReceived(
        env, owner_.obj(), java_buffer.obj());
  }

  void OnRequestFinished() override {
    JNIEnv* env = base::android::AttachCurrentThread();
    cronet::Java_CronetUrlRequest_onSucceeded(env, owner_.obj());
  }

  void OnError(int net_error) override {
    JNIEnv* env = base::android::AttachCurrentThread();
    cronet::Java_CronetUrlRequest_onError(
        env,
        owner_.obj(),
        net_error,
        ConvertUTF8ToJavaString(env, net::ErrorToString(net_error)).obj());
  }

 private:
  ~JniCronetURLRequestAdapterDelegate() override {
  }

  // Java object that owns the CronetURLRequestContextAdapter, which owns this
  // delegate.
  base::android::ScopedJavaGlobalRef<jobject> owner_;

  DISALLOW_COPY_AND_ASSIGN(JniCronetURLRequestAdapterDelegate);
};

}  // namespace

// Explicitly register static JNI functions.
bool CronetUrlRequestRegisterJni(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

static jlong CreateRequestAdapter(JNIEnv* env,
                                  jobject jurl_request,
                                  jlong jurl_request_context_adapter,
                                  jstring jurl_string,
                                  jint jpriority) {
  CronetURLRequestContextAdapter* context_adapter =
      reinterpret_cast<CronetURLRequestContextAdapter*>(
          jurl_request_context_adapter);
  DCHECK(context_adapter);

  GURL url(base::android::ConvertJavaStringToUTF8(env, jurl_string));

  VLOG(1) << "New chromium network request_adapter: "
          << url.possibly_invalid_spec();

  scoped_ptr<CronetURLRequestAdapter::CronetURLRequestAdapterDelegate> delegate(
      new JniCronetURLRequestAdapterDelegate(env, jurl_request));

  CronetURLRequestAdapter* adapter = new CronetURLRequestAdapter(
      context_adapter,
      delegate.Pass(),
      url,
      static_cast<net::RequestPriority>(jpriority));

  return reinterpret_cast<jlong>(adapter);
}

static jboolean SetHttpMethod(JNIEnv* env,
                              jobject jurl_request,
                              jlong jurl_request_adapter,
                              jstring jmethod) {
  DCHECK(jurl_request_adapter);
  CronetURLRequestAdapter* request_adapter =
      reinterpret_cast<CronetURLRequestAdapter*>(jurl_request_adapter);
  DCHECK(!request_adapter->IsOnNetworkThread());
  std::string method(base::android::ConvertJavaStringToUTF8(env, jmethod));
  // Http method is a token, just as header name.
  if (!net::HttpUtil::IsValidHeaderName(method))
    return JNI_FALSE;
  request_adapter->set_method(method);
  return JNI_TRUE;
}

static jboolean AddHeader(JNIEnv* env,
                          jobject jurl_request,
                          jlong jurl_request_adapter,
                          jstring jname,
                          jstring jvalue) {
  DCHECK(jurl_request_adapter);
  CronetURLRequestAdapter* request_adapter =
      reinterpret_cast<CronetURLRequestAdapter*>(jurl_request_adapter);
  DCHECK(!request_adapter->IsOnNetworkThread());
  std::string name(base::android::ConvertJavaStringToUTF8(env, jname));
  std::string value(base::android::ConvertJavaStringToUTF8(env, jvalue));
  if (!net::HttpUtil::IsValidHeaderName(name) ||
      !net::HttpUtil::IsValidHeaderValue(value)) {
    return JNI_FALSE;
  }
  request_adapter->AddRequestHeader(name, value);
  return JNI_TRUE;
}

static void Start(JNIEnv* env,
                  jobject jurl_request,
                  jlong jurl_request_adapter) {
  DCHECK(jurl_request_adapter);
  CronetURLRequestAdapter* request_adapter =
      reinterpret_cast<CronetURLRequestAdapter*>(jurl_request_adapter);
  DCHECK(!request_adapter->IsOnNetworkThread());
  request_adapter->PostTaskToNetworkThread(
      FROM_HERE,
      base::Bind(&CronetURLRequestAdapter::Start,
                 base::Unretained(request_adapter)));
}

static void DestroyRequestAdapter(JNIEnv* env,
                                  jobject jurl_request,
                                  jlong jurl_request_adapter) {
  DCHECK(jurl_request_adapter);
  CronetURLRequestAdapter* request_adapter =
      reinterpret_cast<CronetURLRequestAdapter*>(jurl_request_adapter);
  DCHECK(!request_adapter->IsOnNetworkThread());
  request_adapter->PostTaskToNetworkThread(
      FROM_HERE,
      base::Bind(&CronetURLRequestAdapter::Destroy,
                 base::Unretained(request_adapter)));
}

static void ReceiveData(JNIEnv* env,
                        jobject jcaller,
                        jlong jurl_request_adapter) {
  DCHECK(jurl_request_adapter);
  CronetURLRequestAdapter* request_adapter =
      reinterpret_cast<CronetURLRequestAdapter*>(jurl_request_adapter);
  DCHECK(!request_adapter->IsOnNetworkThread());
  request_adapter->PostTaskToNetworkThread(
      FROM_HERE,
      base::Bind(&CronetURLRequestAdapter::ReadData,
                 base::Unretained(request_adapter)));
}

static void FollowDeferredRedirect(JNIEnv* env,
                                   jobject jcaller,
                                   jlong jurl_request_adapter) {
  DCHECK(jurl_request_adapter);
  CronetURLRequestAdapter* request_adapter =
      reinterpret_cast<CronetURLRequestAdapter*>(jurl_request_adapter);
  DCHECK(!request_adapter->IsOnNetworkThread());
  request_adapter->PostTaskToNetworkThread(
      FROM_HERE,
      base::Bind(&CronetURLRequestAdapter::FollowDeferredRedirect,
                 base::Unretained(request_adapter)));
}

static void PopulateResponseHeaders(JNIEnv* env,
                                    jobject jurl_request,
                                    jlong jurl_request_adapter,
                                    jobject jheaders_map) {
  DCHECK(jurl_request_adapter);
  CronetURLRequestAdapter* request_adapter =
      reinterpret_cast<CronetURLRequestAdapter*>(jurl_request_adapter);
  DCHECK(request_adapter->IsOnNetworkThread());

  const net::HttpResponseHeaders* headers =
      request_adapter->GetResponseHeaders();
  if (headers == nullptr)
    return;

  void* iter = nullptr;
  std::string header_name;
  std::string header_value;
  while (headers->EnumerateHeaderLines(&iter, &header_name, &header_value)) {
    ScopedJavaLocalRef<jstring> name =
        ConvertUTF8ToJavaString(env, header_name);
    ScopedJavaLocalRef<jstring> value =
        ConvertUTF8ToJavaString(env, header_value);
    Java_CronetUrlRequest_onAppendResponseHeader(
        env, jurl_request, jheaders_map, name.obj(), value.obj());
  }
}

static jstring GetNegotiatedProtocol(JNIEnv* env,
                                     jobject jurl_request,
                                     jlong jurl_request_adapter) {
  DCHECK(jurl_request_adapter);
  CronetURLRequestAdapter* request_adapter =
      reinterpret_cast<CronetURLRequestAdapter*>(jurl_request_adapter);
  DCHECK(request_adapter->IsOnNetworkThread());
  return ConvertUTF8ToJavaString(
      env, request_adapter->GetNegotiatedProtocol()).Release();
}

static jboolean GetWasCached(JNIEnv* env,
                             jobject jurl_request,
                             jlong jurl_request_adapter) {
  DCHECK(jurl_request_adapter);
  CronetURLRequestAdapter* request_adapter =
      reinterpret_cast<CronetURLRequestAdapter*>(jurl_request_adapter);
  DCHECK(request_adapter->IsOnNetworkThread());
  return request_adapter->GetWasCached() ? JNI_TRUE : JNI_FALSE;
}

static jlong GetTotalReceivedBytes(JNIEnv* env,
                                   jobject jurl_request,
                                   jlong jurl_request_adapter) {
  DCHECK(jurl_request_adapter);
  CronetURLRequestAdapter* request_adapter =
      reinterpret_cast<CronetURLRequestAdapter*>(jurl_request_adapter);
  DCHECK(request_adapter->IsOnNetworkThread());
  return request_adapter->GetTotalReceivedBytes();
}

}  // namespace cronet