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/browser/net/aw_request_interceptor.h" | 5 #include "android_webview/browser/net/aw_request_interceptor.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "android_webview/browser/aw_contents_io_thread_client.h" | 9 #include "android_webview/browser/aw_contents_io_thread_client.h" |
8 #include "android_webview/browser/input_stream.h" | 10 #include "android_webview/browser/input_stream.h" |
9 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" | 11 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" |
10 #include "android_webview/browser/net/aw_web_resource_response.h" | 12 #include "android_webview/browser/net/aw_web_resource_response.h" |
11 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
12 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
13 #include "base/supports_user_data.h" | 15 #include "base/supports_user_data.h" |
14 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/resource_request_info.h" | 17 #include "content/public/browser/resource_request_info.h" |
16 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
17 #include "net/url_request/url_request_job.h" | 19 #include "net/url_request/url_request_job.h" |
18 | 20 |
19 namespace android_webview { | 21 namespace android_webview { |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 const void* const kRequestAlreadyHasJobDataKey = &kRequestAlreadyHasJobDataKey; | 25 const void* const kRequestAlreadyHasJobDataKey = &kRequestAlreadyHasJobDataKey; |
24 | 26 |
25 class StreamReaderJobDelegateImpl | 27 class StreamReaderJobDelegateImpl |
26 : public AndroidStreamReaderURLRequestJob::Delegate { | 28 : public AndroidStreamReaderURLRequestJob::Delegate { |
27 public: | 29 public: |
28 StreamReaderJobDelegateImpl( | 30 StreamReaderJobDelegateImpl( |
29 scoped_ptr<AwWebResourceResponse> aw_web_resource_response) | 31 scoped_ptr<AwWebResourceResponse> aw_web_resource_response) |
30 : aw_web_resource_response_(aw_web_resource_response.Pass()) { | 32 : aw_web_resource_response_(std::move(aw_web_resource_response)) { |
31 DCHECK(aw_web_resource_response_); | 33 DCHECK(aw_web_resource_response_); |
32 } | 34 } |
33 | 35 |
34 scoped_ptr<InputStream> OpenInputStream(JNIEnv* env, | 36 scoped_ptr<InputStream> OpenInputStream(JNIEnv* env, |
35 const GURL& url) override { | 37 const GURL& url) override { |
36 return aw_web_resource_response_->GetInputStream(env).Pass(); | 38 return aw_web_resource_response_->GetInputStream(env); |
37 } | 39 } |
38 | 40 |
39 void OnInputStreamOpenFailed(net::URLRequest* request, | 41 void OnInputStreamOpenFailed(net::URLRequest* request, |
40 bool* restart) override { | 42 bool* restart) override { |
41 *restart = false; | 43 *restart = false; |
42 } | 44 } |
43 | 45 |
44 bool GetMimeType(JNIEnv* env, | 46 bool GetMimeType(JNIEnv* env, |
45 net::URLRequest* request, | 47 net::URLRequest* request, |
46 android_webview::InputStream* stream, | 48 android_webview::InputStream* stream, |
(...skipping 25 matching lines...) Expand all Loading... |
72 | 74 |
73 private: | 75 private: |
74 scoped_ptr<AwWebResourceResponse> aw_web_resource_response_; | 76 scoped_ptr<AwWebResourceResponse> aw_web_resource_response_; |
75 }; | 77 }; |
76 | 78 |
77 class ShouldInterceptRequestAdaptor | 79 class ShouldInterceptRequestAdaptor |
78 : public AndroidStreamReaderURLRequestJob::DelegateObtainer { | 80 : public AndroidStreamReaderURLRequestJob::DelegateObtainer { |
79 public: | 81 public: |
80 explicit ShouldInterceptRequestAdaptor( | 82 explicit ShouldInterceptRequestAdaptor( |
81 scoped_ptr<AwContentsIoThreadClient> io_thread_client) | 83 scoped_ptr<AwContentsIoThreadClient> io_thread_client) |
82 : io_thread_client_(io_thread_client.Pass()), weak_factory_(this) {} | 84 : io_thread_client_(std::move(io_thread_client)), weak_factory_(this) {} |
83 ~ShouldInterceptRequestAdaptor() override {} | 85 ~ShouldInterceptRequestAdaptor() override {} |
84 | 86 |
85 void ObtainDelegate(net::URLRequest* request, | 87 void ObtainDelegate(net::URLRequest* request, |
86 const Callback& callback) override { | 88 const Callback& callback) override { |
87 callback_ = callback; | 89 callback_ = callback; |
88 io_thread_client_->ShouldInterceptRequestAsync( | 90 io_thread_client_->ShouldInterceptRequestAsync( |
89 // The request is only used while preparing the call, not retained. | 91 // The request is only used while preparing the call, not retained. |
90 request, | 92 request, |
91 base::Bind(&ShouldInterceptRequestAdaptor::WebResourceResponseObtained, | 93 base::Bind(&ShouldInterceptRequestAdaptor::WebResourceResponseObtained, |
92 // The lifetime of the DelegateObtainer is managed by | 94 // The lifetime of the DelegateObtainer is managed by |
93 // AndroidStreamReaderURLRequestJob, it might get deleted. | 95 // AndroidStreamReaderURLRequestJob, it might get deleted. |
94 weak_factory_.GetWeakPtr())); | 96 weak_factory_.GetWeakPtr())); |
95 } | 97 } |
96 | 98 |
97 private: | 99 private: |
98 void WebResourceResponseObtained( | 100 void WebResourceResponseObtained( |
99 scoped_ptr<AwWebResourceResponse> response) { | 101 scoped_ptr<AwWebResourceResponse> response) { |
100 if (response) { | 102 if (response) { |
101 callback_.Run( | 103 callback_.Run(make_scoped_ptr( |
102 make_scoped_ptr(new StreamReaderJobDelegateImpl(response.Pass()))); | 104 new StreamReaderJobDelegateImpl(std::move(response)))); |
103 } else { | 105 } else { |
104 callback_.Run(nullptr); | 106 callback_.Run(nullptr); |
105 } | 107 } |
106 } | 108 } |
107 | 109 |
108 scoped_ptr<AwContentsIoThreadClient> io_thread_client_; | 110 scoped_ptr<AwContentsIoThreadClient> io_thread_client_; |
109 Callback callback_; | 111 Callback callback_; |
110 base::WeakPtrFactory<ShouldInterceptRequestAdaptor> weak_factory_; | 112 base::WeakPtrFactory<ShouldInterceptRequestAdaptor> weak_factory_; |
111 | 113 |
112 DISALLOW_COPY_AND_ASSIGN(ShouldInterceptRequestAdaptor); | 114 DISALLOW_COPY_AND_ASSIGN(ShouldInterceptRequestAdaptor); |
(...skipping 30 matching lines...) Expand all Loading... |
143 if (referrer.is_valid() && | 145 if (referrer.is_valid() && |
144 (!request->is_pending() || request->is_redirecting())) { | 146 (!request->is_pending() || request->is_redirecting())) { |
145 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer, | 147 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer, |
146 referrer.spec(), true); | 148 referrer.spec(), true); |
147 } | 149 } |
148 request->SetUserData(kRequestAlreadyHasJobDataKey, | 150 request->SetUserData(kRequestAlreadyHasJobDataKey, |
149 new base::SupportsUserData::Data()); | 151 new base::SupportsUserData::Data()); |
150 return new AndroidStreamReaderURLRequestJob( | 152 return new AndroidStreamReaderURLRequestJob( |
151 request, network_delegate, | 153 request, network_delegate, |
152 make_scoped_ptr( | 154 make_scoped_ptr( |
153 new ShouldInterceptRequestAdaptor(io_thread_client.Pass())), | 155 new ShouldInterceptRequestAdaptor(std::move(io_thread_client))), |
154 true); | 156 true); |
155 } | 157 } |
156 | 158 |
157 } // namespace android_webview | 159 } // namespace android_webview |
OLD | NEW |