Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <jni.h> | 5 #include <jni.h> |
| 6 | 6 |
| 7 #include "base/android/base_jni_registrar.h" | 7 #include "base/android/base_jni_registrar.h" |
| 8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 9 #include "base/android/jni_registrar.h" | 9 #include "base/android/jni_registrar.h" |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/path_service.h" | |
| 10 #include "components/cronet/android/cronet_loader.h" | 15 #include "components/cronet/android/cronet_loader.h" |
| 16 #include "net/base/net_errors.h" | |
| 17 #include "net/test/url_request/url_request_failed_job.h" | |
| 18 #include "net/test/url_request/url_request_mock_http_job.h" | |
| 19 #include "net/url_request/url_request_filter.h" | |
| 20 #include "net/url_request/url_request_interceptor.h" | |
| 11 | 21 |
| 12 namespace { | 22 namespace { |
| 13 | 23 |
| 24 // A mock URL that will produces a redirect. | |
| 25 const char kMockCronetTestRedirectUrl[] = "http://mock.cronet.redirect/"; | |
| 26 | |
| 27 // A mock URL that will return a 404 response. | |
| 28 const char kMockCronetTestSuccessUrl[] = "http://mock.cronet.success/"; | |
| 29 | |
| 30 // A mock URL that will result in a failed url request. | |
| 31 const char kMockCronetTestFailedUrl[] = "http://mock.cronet.failed/"; | |
| 32 | |
| 14 const base::android::RegistrationMethod kCronetTestsRegisteredMethods[] = { | 33 const base::android::RegistrationMethod kCronetTestsRegisteredMethods[] = { |
| 15 {"BaseAndroid", base::android::RegisterJni}, | 34 {"BaseAndroid", base::android::RegisterJni}, |
| 16 }; | 35 }; |
| 17 | 36 |
| 37 class CronetMockJobInterceptor : public net::URLRequestInterceptor { | |
|
mef
2014/09/19 14:12:00
Suggest: move CronetMockJobInterceptor into its ow
xunjieli
2014/09/19 15:00:04
Done.
| |
| 38 public: | |
| 39 CronetMockJobInterceptor() {} | |
| 40 virtual ~CronetMockJobInterceptor() {} | |
| 41 | |
| 42 // net::URLRequestInterceptor implementation | |
| 43 virtual net::URLRequestJob* MaybeInterceptRequest( | |
| 44 net::URLRequest* request, | |
| 45 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
| 46 base::FilePath test_files_root; | |
| 47 PathService::Get(base::DIR_ANDROID_APP_DATA, &test_files_root); | |
| 48 if (request->url() == GURL(kMockCronetTestRedirectUrl)) { | |
|
mef
2014/09/19 14:12:00
Would it make sense to make some kind of map betwe
xunjieli
2014/09/19 15:00:04
Acknowledged. I think this is a great idea. But ri
| |
| 49 return new net::URLRequestMockHTTPJob( | |
| 50 request, | |
| 51 network_delegate, | |
| 52 test_files_root.Append("redirect.html"), | |
| 53 base::MessageLoop::current()->message_loop_proxy()); | |
| 54 } | |
| 55 if (request->url() == GURL(kMockCronetTestSuccessUrl)) { | |
| 56 return new net::URLRequestMockHTTPJob( | |
| 57 request, | |
| 58 network_delegate, | |
| 59 test_files_root.Append("success.html"), | |
| 60 base::MessageLoop::current()->message_loop_proxy()); | |
| 61 } | |
| 62 return new net::URLRequestFailedJob( | |
| 63 request, network_delegate, net::ERR_FAILED); | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 DISALLOW_COPY_AND_ASSIGN(CronetMockJobInterceptor); | |
| 68 }; | |
| 69 | |
| 70 bool AddUrlInterceptors() { | |
| 71 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | |
| 72 return filter->AddUrlInterceptor(GURL(kMockCronetTestSuccessUrl), | |
| 73 scoped_ptr<net::URLRequestInterceptor>( | |
| 74 new CronetMockJobInterceptor())) && | |
| 75 filter->AddUrlInterceptor(GURL(kMockCronetTestRedirectUrl), | |
|
mef
2014/09/19 14:12:00
Again, having a map / list and a loop would avoid
xunjieli
2014/09/19 15:00:04
Acknowledged. Will tackle this in the next CL when
| |
| 76 scoped_ptr<net::URLRequestInterceptor>( | |
| 77 new CronetMockJobInterceptor())) && | |
| 78 filter->AddUrlInterceptor(GURL(kMockCronetTestFailedUrl), | |
| 79 scoped_ptr<net::URLRequestInterceptor>( | |
| 80 new CronetMockJobInterceptor())); | |
| 81 } | |
| 82 | |
| 18 } // namespace | 83 } // namespace |
| 19 | 84 |
| 20 // This is called by the VM when the shared library is first loaded. | 85 // This is called by the VM when the shared library is first loaded. |
| 21 // Checks the available version of JNI. Also, caches Java reflection artifacts. | 86 // Checks the available version of JNI. Also, caches Java reflection artifacts. |
| 22 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) { | 87 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
| 23 JNIEnv* env; | 88 JNIEnv* env; |
| 24 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { | 89 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 25 return -1; | 90 return -1; |
| 26 } | 91 } |
| 27 | 92 |
| 28 jint cronet_onload = cronet::CronetOnLoad(vm, reserved); | 93 jint cronet_onload = cronet::CronetOnLoad(vm, reserved); |
| 29 if (cronet_onload == -1) | 94 if (cronet_onload == -1) |
| 30 return cronet_onload; | 95 return cronet_onload; |
| 31 | 96 |
| 32 if (!base::android::RegisterNativeMethods( | 97 if (!base::android::RegisterNativeMethods( |
| 33 env, | 98 env, |
| 34 kCronetTestsRegisteredMethods, | 99 kCronetTestsRegisteredMethods, |
| 35 arraysize(kCronetTestsRegisteredMethods))) { | 100 arraysize(kCronetTestsRegisteredMethods))) { |
| 36 return -1; | 101 return -1; |
| 37 } | 102 } |
| 103 | |
| 104 if (!AddUrlInterceptors()) | |
| 105 return -1; | |
| 38 return cronet_onload; | 106 return cronet_onload; |
| 39 } | 107 } |
| 40 | 108 |
| 41 extern "C" void JNI_OnUnLoad(JavaVM* vm, void* reserved) { | 109 extern "C" void JNI_OnUnLoad(JavaVM* vm, void* reserved) { |
| 110 net::URLRequestFilter::GetInstance()->ClearHandlers(); | |
| 42 cronet::CronetOnUnLoad(vm, reserved); | 111 cronet::CronetOnUnLoad(vm, reserved); |
| 43 } | 112 } |
| 44 | 113 |
| OLD | NEW |