File: mock_url_request_job_factory.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (147 lines) | stat: -rw-r--r-- 5,729 bytes parent folder | download
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
// 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 "mock_url_request_job_factory.h"

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/memory/ptr_util.h"
#include "components/cronet/android/test/cronet_test_util.h"
#include "jni/MockUrlRequestJobFactory_jni.h"
#include "net/test/url_request/ssl_certificate_error_job.h"
#include "net/test/url_request/url_request_failed_job.h"
#include "net/test/url_request/url_request_hanging_read_job.h"
#include "net/test/url_request/url_request_mock_data_job.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_filter.h"
#include "net/url_request/url_request_intercepting_job_factory.h"
#include "url/gurl.h"

using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;

namespace cronet {

// Intercept URLRequestJob creation using URLRequestFilter from
// libcronet_tests.so
class UrlInterceptorJobFactoryHandle {
 public:
  // |jcontext_adapter| points to a URLRequestContextAdapater.
  UrlInterceptorJobFactoryHandle(jlong jcontext_adapter)
      : jcontext_adapter_(jcontext_adapter) {
    TestUtil::RunAfterContextInit(
        jcontext_adapter,
        base::Bind(&UrlInterceptorJobFactoryHandle::InitOnNetworkThread,
                   base::Unretained(this)));
  }
  // Should only be called on network thread; other threads should use
  // ShutDown().
  ~UrlInterceptorJobFactoryHandle() {
    DCHECK(
        TestUtil::GetTaskRunner(jcontext_adapter_)->BelongsToCurrentThread());
    TestUtil::GetURLRequestContext(jcontext_adapter_)
        ->set_job_factory(old_job_factory_);
  }

  void ShutDown() {
    TestUtil::RunAfterContextInit(
        jcontext_adapter_,
        base::Bind(&UrlInterceptorJobFactoryHandle::ShutdownOnNetworkThread,
                   base::Unretained(this)));
  }

 private:
  void InitOnNetworkThread() {
    net::URLRequestContext* request_context =
        TestUtil::GetURLRequestContext(jcontext_adapter_);
    old_job_factory_ = request_context->job_factory();
    new_job_factory_.reset(new net::URLRequestInterceptingJobFactory(
        const_cast<net::URLRequestJobFactory*>(old_job_factory_),
        net::URLRequestFilter::GetInstance()));
    request_context->set_job_factory(new_job_factory_.get());
  }

  void ShutdownOnNetworkThread() { delete this; }

  // The URLRequestContextAdapater this object intercepts from.
  const jlong jcontext_adapter_;
  // URLRequestJobFactory previously used in URLRequestContext.
  const net::URLRequestJobFactory* old_job_factory_;
  // URLRequestJobFactory inserted during tests to intercept URLRequests with
  // libcronet's URLRequestFilter.
  std::unique_ptr<net::URLRequestInterceptingJobFactory> new_job_factory_;
};

// URL interceptors are registered with the URLRequestFilter in
// libcronet_tests.so.  However tests are run on libcronet.so.  Use the
// URLRequestFilter in libcronet_tests.so with the URLRequestContext in
// libcronet.so by installing a URLRequestInterceptingJobFactory
// that calls into libcronet_tests.so's URLRequestFilter.
jlong AddUrlInterceptors(JNIEnv* env,
                         const JavaParamRef<jclass>& jcaller,
                         jlong jcontext_adapter) {
  net::URLRequestMockDataJob::AddUrlHandler();
  net::URLRequestFailedJob::AddUrlHandler();
  net::URLRequestHangingReadJob::AddUrlHandler();
  net::SSLCertificateErrorJob::AddUrlHandler();
  return reinterpret_cast<jlong>(
      new UrlInterceptorJobFactoryHandle(jcontext_adapter));
}

// Put back the old URLRequestJobFactory into the URLRequestContext.
void RemoveUrlInterceptorJobFactory(JNIEnv* env,
                                    const JavaParamRef<jclass>& jcaller,
                                    jlong jinterceptor_handle) {
  reinterpret_cast<UrlInterceptorJobFactoryHandle*>(jinterceptor_handle)
      ->ShutDown();
}

ScopedJavaLocalRef<jstring> GetMockUrlWithFailure(
    JNIEnv* jenv,
    const JavaParamRef<jclass>& jcaller,
    jint jphase,
    jint jnet_error) {
  GURL url(net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
      static_cast<net::URLRequestFailedJob::FailurePhase>(jphase),
      static_cast<int>(jnet_error)));
  return base::android::ConvertUTF8ToJavaString(jenv, url.spec());
}

ScopedJavaLocalRef<jstring> GetMockUrlForData(
    JNIEnv* jenv,
    const JavaParamRef<jclass>& jcaller,
    const JavaParamRef<jstring>& jdata,
    jint jdata_repeat_count) {
  std::string data(base::android::ConvertJavaStringToUTF8(jenv, jdata));
  GURL url(net::URLRequestMockDataJob::GetMockHttpUrl(data,
                                                      jdata_repeat_count));
  return base::android::ConvertUTF8ToJavaString(jenv, url.spec());
}

ScopedJavaLocalRef<jstring> GetMockUrlForSSLCertificateError(
    JNIEnv* jenv,
    const JavaParamRef<jclass>& jcaller) {
  GURL url(net::SSLCertificateErrorJob::GetMockUrl());
  return base::android::ConvertUTF8ToJavaString(jenv, url.spec());
}

ScopedJavaLocalRef<jstring> GetMockUrlForClientCertificateRequest(
    JNIEnv* jenv,
    const JavaParamRef<jclass>& jcaller) {
  GURL url(net::URLRequestMockDataJob::GetMockUrlForClientCertificateRequest());
  return base::android::ConvertUTF8ToJavaString(jenv, url.spec());
}

ScopedJavaLocalRef<jstring> GetMockUrlForHangingRead(
    JNIEnv* jenv,
    const JavaParamRef<jclass>& jcaller) {
  GURL url(net::URLRequestHangingReadJob::GetMockHttpUrl());
  return base::android::ConvertUTF8ToJavaString(jenv, url.spec());
}

bool RegisterMockUrlRequestJobFactory(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

}  // namespace cronet