OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "android_webview/native/intercepted_request_data_impl.h" | 5 #include "android_webview/native/intercepted_request_data_impl.h" |
6 | 6 |
7 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" | 7 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" |
8 #include "android_webview/native/input_stream_impl.h" | 8 #include "android_webview/native/input_stream_impl.h" |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
11 #include "jni/InterceptedRequestData_jni.h" | 11 #include "jni/InterceptedRequestData_jni.h" |
12 #include "net/url_request/url_request.h" | 12 #include "net/url_request/url_request.h" |
13 #include "net/url_request/url_request_job.h" | 13 #include "net/url_request/url_request_job.h" |
14 | 14 |
15 using base::android::ScopedJavaLocalRef; | 15 using base::android::ScopedJavaLocalRef; |
16 | 16 |
17 namespace android_webview { | 17 namespace android_webview { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 class StreamReaderJobDelegateImpl : | 21 class StreamReaderJobDelegateImpl |
22 public AndroidStreamReaderURLRequestJob::Delegate { | 22 : public AndroidStreamReaderURLRequestJob::Delegate { |
23 public: | 23 public: |
24 StreamReaderJobDelegateImpl( | 24 StreamReaderJobDelegateImpl( |
25 const InterceptedRequestDataImpl* intercepted_request_data) | 25 scoped_refptr<InterceptedRequestData::Holder> holder) |
26 : intercepted_request_data_impl_(intercepted_request_data) { | 26 : holder_(holder) { |
27 DCHECK(intercepted_request_data_impl_); | 27 DCHECK(holder.get()); |
28 } | 28 } |
29 | 29 |
30 virtual scoped_ptr<InputStream> OpenInputStream( | 30 virtual void OnStart() OVERRIDE { |
31 JNIEnv* env, | 31 if (!holder_.get()) return; |
32 const GURL& url) OVERRIDE { | |
33 return intercepted_request_data_impl_->GetInputStream(env).Pass(); | |
34 } | |
35 | 32 |
36 virtual void OnInputStreamOpenFailed(net::URLRequest* request, | 33 // This is called on the IO thread so there is no risk of a race when |
37 bool* restart) OVERRIDE { | 34 // transferring ownership of the InterceptedRequestData |data| member. |
38 *restart = false; | 35 intercepted_request_data_impl_.reset( |
39 } | 36 static_cast<InterceptedRequestDataImpl*>(holder_->data.release())); |
| 37 holder_ = NULL; |
| 38 } |
40 | 39 |
41 virtual bool GetMimeType(JNIEnv* env, | 40 virtual scoped_ptr<InputStream> OpenInputStream(JNIEnv* env, |
42 net::URLRequest* request, | 41 const GURL& url) OVERRIDE { |
43 android_webview::InputStream* stream, | 42 if (!intercepted_request_data_impl_) return scoped_ptr<InputStream>(); |
44 std::string* mime_type) OVERRIDE { | 43 return intercepted_request_data_impl_->GetInputStream(env).Pass(); |
45 return intercepted_request_data_impl_->GetMimeType(env, mime_type); | 44 } |
46 } | |
47 | 45 |
48 virtual bool GetCharset(JNIEnv* env, | 46 virtual void OnInputStreamOpenFailed(net::URLRequest* request, |
49 net::URLRequest* request, | 47 bool* restart) OVERRIDE { |
50 android_webview::InputStream* stream, | 48 *restart = false; |
51 std::string* charset) OVERRIDE { | 49 } |
52 return intercepted_request_data_impl_->GetCharset(env, charset); | 50 |
53 } | 51 virtual bool GetMimeType(JNIEnv* env, |
| 52 net::URLRequest* request, |
| 53 android_webview::InputStream* stream, |
| 54 std::string* mime_type) OVERRIDE { |
| 55 if (!intercepted_request_data_impl_) return false; |
| 56 return intercepted_request_data_impl_->GetMimeType(env, mime_type); |
| 57 } |
| 58 |
| 59 virtual bool GetCharset(JNIEnv* env, |
| 60 net::URLRequest* request, |
| 61 android_webview::InputStream* stream, |
| 62 std::string* charset) OVERRIDE { |
| 63 if (!intercepted_request_data_impl_) return false; |
| 64 return intercepted_request_data_impl_->GetCharset(env, charset); |
| 65 } |
54 | 66 |
55 private: | 67 private: |
56 const InterceptedRequestDataImpl* intercepted_request_data_impl_; | 68 scoped_refptr<InterceptedRequestData::Holder> holder_; |
| 69 scoped_ptr<InterceptedRequestDataImpl> intercepted_request_data_impl_; |
57 }; | 70 }; |
58 | 71 |
59 } // namespace | 72 } // namespace |
60 | 73 |
| 74 // static |
| 75 net::URLRequestJob* InterceptedRequestData::CreateJobFor( |
| 76 scoped_refptr<InterceptedRequestData::Holder> holder, |
| 77 net::URLRequest* request, |
| 78 net::NetworkDelegate* network_delegate) { |
| 79 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate> |
| 80 stream_reader_job_delegate_impl(new StreamReaderJobDelegateImpl(holder)); |
| 81 return new AndroidStreamReaderURLRequestJob( |
| 82 request, network_delegate, stream_reader_job_delegate_impl.Pass()); |
| 83 } |
| 84 |
61 InterceptedRequestDataImpl::InterceptedRequestDataImpl( | 85 InterceptedRequestDataImpl::InterceptedRequestDataImpl( |
62 const base::android::JavaRef<jobject>& obj) | 86 const base::android::JavaRef<jobject>& obj) |
63 : java_object_(obj) { | 87 : java_object_(obj) { |
64 } | 88 } |
65 | 89 |
66 InterceptedRequestDataImpl::~InterceptedRequestDataImpl() { | 90 InterceptedRequestDataImpl::~InterceptedRequestDataImpl() { |
67 } | 91 } |
68 | 92 |
69 scoped_ptr<InputStream> | 93 scoped_ptr<InputStream> |
70 InterceptedRequestDataImpl::GetInputStream(JNIEnv* env) const { | 94 InterceptedRequestDataImpl::GetInputStream(JNIEnv* env) const { |
(...skipping 21 matching lines...) Expand all Loading... |
92 if (jstring_charset.is_null()) | 116 if (jstring_charset.is_null()) |
93 return false; | 117 return false; |
94 *charset = ConvertJavaStringToUTF8(jstring_charset); | 118 *charset = ConvertJavaStringToUTF8(jstring_charset); |
95 return true; | 119 return true; |
96 } | 120 } |
97 | 121 |
98 bool RegisterInterceptedRequestData(JNIEnv* env) { | 122 bool RegisterInterceptedRequestData(JNIEnv* env) { |
99 return RegisterNativesImpl(env); | 123 return RegisterNativesImpl(env); |
100 } | 124 } |
101 | 125 |
102 net::URLRequestJob* InterceptedRequestDataImpl::CreateJobFor( | |
103 net::URLRequest* request, | |
104 net::NetworkDelegate* network_delegate) const { | |
105 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate> | |
106 stream_reader_job_delegate_impl(new StreamReaderJobDelegateImpl(this)); | |
107 return new AndroidStreamReaderURLRequestJob( | |
108 request, network_delegate, stream_reader_job_delegate_impl.Pass()); | |
109 } | |
110 | |
111 } // namespace android_webview | 126 } // namespace android_webview |
OLD | NEW |