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/files/file_util.h" | |
| 12 #include "base/files/scoped_file.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/url_request/url_request_filter.h" | |
| 18 #include "net/test/url_request/url_request_failed_job.h" | |
| 19 #include "net/test/url_request/url_request_mock_http_job.h" | |
| 11 | 20 |
| 12 namespace { | 21 namespace { |
| 13 | 22 |
| 23 // A mock URL that will return a 200 response. | |
| 24 const char* const kMockCronetTestUrl = "http://mock.cronet.success/"; | |
| 25 | |
| 26 // A mock URL that will result in a failed url request. | |
| 27 const char* const kMockCronetTestFailedUrl = "http://mock.cronet.failed/"; | |
| 28 | |
| 14 const base::android::RegistrationMethod kCronetTestsRegisteredMethods[] = { | 29 const base::android::RegistrationMethod kCronetTestsRegisteredMethods[] = { |
| 15 {"BaseAndroid", base::android::RegisterJni}, | 30 {"BaseAndroid", base::android::RegisterJni}, |
| 16 }; | 31 }; |
| 17 | 32 |
| 33 // static | |
| 34 net::URLRequestJob* CronetURLRequestJobFactory( | |
| 35 net::URLRequest* request, | |
| 36 net::NetworkDelegate* network_delegate, | |
| 37 const std::string& scheme) { | |
| 38 base::FilePath test_files_root; | |
| 39 DCHECK(PathService::Get(base::DIR_SOURCE_ROOT, &test_files_root)); | |
| 40 | |
| 41 if (request->url() == GURL(kMockCronetTestUrl)) { | |
| 42 return new net::URLRequestMockHTTPJob( | |
| 43 request, | |
| 44 network_delegate, | |
| 45 test_files_root.Append("success.html"), | |
| 46 base::MessageLoop::current()->message_loop_proxy()); | |
| 47 } | |
| 48 return new net::URLRequestFailedJob( | |
| 49 request, network_delegate, net::ERR_FAILED); | |
| 50 } | |
| 51 | |
| 52 void PrepareTestFiles() { | |
|
xunjieli
2014/09/16 15:23:47
Do you think we will need many test files?
xunjieli
2014/09/16 15:25:36
If so, we probably need this function to be in a n
mef
2014/09/16 22:02:34
sgtm, we may need it to be more configurable / abs
xunjieli
2014/09/16 22:14:46
The problem is that URLRequestMockHTTPJob (net/tes
| |
| 53 base::FilePath test_files_root; | |
| 54 DCHECK(PathService::Get(base::DIR_SOURCE_ROOT, &test_files_root)); | |
|
mef
2014/09/16 22:02:34
I think DIR_SOURCE_ROOT is actual src/ on desktop
xunjieli
2014/09/16 22:14:46
So on Android, this is the directory of external s
| |
| 55 | |
| 56 base::FilePath success_file_path = | |
| 57 test_files_root.AppendASCII("success.html"); | |
| 58 base::ScopedFILE success_file(base::OpenFile(success_file_path, "w")); | |
| 59 DCHECK(success_file); | |
| 60 fprintf(success_file.get(), "<html></html>"); | |
| 61 base::FilePath success_headers_file_path = | |
| 62 test_files_root.AppendASCII("success.html.mock-http-headers"); | |
| 63 base::ScopedFILE success_headers_file( | |
| 64 base::OpenFile(success_headers_file_path, "w")); | |
| 65 DCHECK(success_headers_file); | |
| 66 fprintf(success_headers_file.get(), "HTTP/1.1 200 OK"); | |
|
xunjieli
2014/09/16 15:23:47
This success.html.mock-http-headers is not actuall
| |
| 67 | |
| 68 base::CloseFile(success_file.get()); | |
| 69 base::CloseFile(success_headers_file.get()); | |
| 70 } | |
| 71 | |
| 72 void AddUrlHandlers() { | |
| 73 PrepareTestFiles(); | |
| 74 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | |
| 75 DCHECK(filter->AddUrlHandler(GURL(kMockCronetTestUrl), | |
| 76 &CronetURLRequestJobFactory)); | |
| 77 DCHECK(filter->AddUrlHandler(GURL(kMockCronetTestFailedUrl), | |
| 78 &CronetURLRequestJobFactory)); | |
| 79 } | |
| 80 | |
| 18 } // namespace | 81 } // namespace |
| 19 | 82 |
| 20 // This is called by the VM when the shared library is first loaded. | 83 // 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. | 84 // Checks the available version of JNI. Also, caches Java reflection artifacts. |
| 22 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) { | 85 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
| 23 JNIEnv* env; | 86 JNIEnv* env; |
| 24 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { | 87 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 25 return -1; | 88 return -1; |
| 26 } | 89 } |
| 27 | 90 |
| 28 jint cronet_onload = cronet::CronetOnLoad(vm, reserved); | 91 jint cronet_onload = cronet::CronetOnLoad(vm, reserved); |
| 29 if (cronet_onload == -1) | 92 if (cronet_onload == -1) |
| 30 return cronet_onload; | 93 return cronet_onload; |
| 31 | 94 |
| 32 if (!base::android::RegisterNativeMethods( | 95 if (!base::android::RegisterNativeMethods( |
| 33 env, | 96 env, |
| 34 kCronetTestsRegisteredMethods, | 97 kCronetTestsRegisteredMethods, |
| 35 arraysize(kCronetTestsRegisteredMethods))) { | 98 arraysize(kCronetTestsRegisteredMethods))) { |
| 36 return -1; | 99 return -1; |
| 37 } | 100 } |
| 101 | |
| 102 AddUrlHandlers(); | |
| 38 return cronet_onload; | 103 return cronet_onload; |
| 39 } | 104 } |
| 40 | 105 |
| 41 extern "C" void JNI_OnUnLoad(JavaVM* vm, void* reserved) { | 106 extern "C" void JNI_OnUnLoad(JavaVM* vm, void* reserved) { |
| 42 cronet::CronetOnUnLoad(vm, reserved); | 107 cronet::CronetOnUnLoad(vm, reserved); |
| 43 } | 108 } |
| 44 | 109 |
| OLD | NEW |