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> | 7 #include <utility> |
8 | 8 |
9 #include "android_webview/browser/aw_contents_io_thread_client.h" | 9 #include "android_webview/browser/aw_contents_io_thread_client.h" |
10 #include "android_webview/browser/input_stream.h" | 10 #include "android_webview/browser/input_stream.h" |
11 #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" |
12 #include "android_webview/browser/net/aw_web_resource_response.h" | 12 #include "android_webview/browser/net/aw_web_resource_response.h" |
| 13 #include "base/memory/ptr_util.h" |
13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/supports_user_data.h" | 16 #include "base/supports_user_data.h" |
16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/resource_request_info.h" | 18 #include "content/public/browser/resource_request_info.h" |
18 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
19 #include "net/url_request/url_request_job.h" | 20 #include "net/url_request/url_request_job.h" |
20 | 21 |
21 namespace android_webview { | 22 namespace android_webview { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 const void* const kRequestAlreadyHasJobDataKey = &kRequestAlreadyHasJobDataKey; | 26 const void* const kRequestAlreadyHasJobDataKey = &kRequestAlreadyHasJobDataKey; |
26 | 27 |
27 class StreamReaderJobDelegateImpl | 28 class StreamReaderJobDelegateImpl |
28 : public AndroidStreamReaderURLRequestJob::Delegate { | 29 : public AndroidStreamReaderURLRequestJob::Delegate { |
29 public: | 30 public: |
30 StreamReaderJobDelegateImpl( | 31 StreamReaderJobDelegateImpl( |
31 scoped_ptr<AwWebResourceResponse> aw_web_resource_response) | 32 std::unique_ptr<AwWebResourceResponse> aw_web_resource_response) |
32 : aw_web_resource_response_(std::move(aw_web_resource_response)) { | 33 : aw_web_resource_response_(std::move(aw_web_resource_response)) { |
33 DCHECK(aw_web_resource_response_); | 34 DCHECK(aw_web_resource_response_); |
34 } | 35 } |
35 | 36 |
36 scoped_ptr<InputStream> OpenInputStream(JNIEnv* env, | 37 std::unique_ptr<InputStream> OpenInputStream(JNIEnv* env, |
37 const GURL& url) override { | 38 const GURL& url) override { |
38 return aw_web_resource_response_->GetInputStream(env); | 39 return aw_web_resource_response_->GetInputStream(env); |
39 } | 40 } |
40 | 41 |
41 void OnInputStreamOpenFailed(net::URLRequest* request, | 42 void OnInputStreamOpenFailed(net::URLRequest* request, |
42 bool* restart) override { | 43 bool* restart) override { |
43 *restart = false; | 44 *restart = false; |
44 } | 45 } |
45 | 46 |
46 bool GetMimeType(JNIEnv* env, | 47 bool GetMimeType(JNIEnv* env, |
47 net::URLRequest* request, | 48 net::URLRequest* request, |
(...skipping 18 matching lines...) Expand all Loading... |
66 std::string status_line("HTTP/1.1 "); | 67 std::string status_line("HTTP/1.1 "); |
67 status_line.append(base::IntToString(status_code)); | 68 status_line.append(base::IntToString(status_code)); |
68 status_line.append(" "); | 69 status_line.append(" "); |
69 status_line.append(reason_phrase); | 70 status_line.append(reason_phrase); |
70 headers->ReplaceStatusLine(status_line); | 71 headers->ReplaceStatusLine(status_line); |
71 } | 72 } |
72 aw_web_resource_response_->GetResponseHeaders(env, headers); | 73 aw_web_resource_response_->GetResponseHeaders(env, headers); |
73 } | 74 } |
74 | 75 |
75 private: | 76 private: |
76 scoped_ptr<AwWebResourceResponse> aw_web_resource_response_; | 77 std::unique_ptr<AwWebResourceResponse> aw_web_resource_response_; |
77 }; | 78 }; |
78 | 79 |
79 class ShouldInterceptRequestAdaptor | 80 class ShouldInterceptRequestAdaptor |
80 : public AndroidStreamReaderURLRequestJob::DelegateObtainer { | 81 : public AndroidStreamReaderURLRequestJob::DelegateObtainer { |
81 public: | 82 public: |
82 explicit ShouldInterceptRequestAdaptor( | 83 explicit ShouldInterceptRequestAdaptor( |
83 scoped_ptr<AwContentsIoThreadClient> io_thread_client) | 84 std::unique_ptr<AwContentsIoThreadClient> io_thread_client) |
84 : io_thread_client_(std::move(io_thread_client)), weak_factory_(this) {} | 85 : io_thread_client_(std::move(io_thread_client)), weak_factory_(this) {} |
85 ~ShouldInterceptRequestAdaptor() override {} | 86 ~ShouldInterceptRequestAdaptor() override {} |
86 | 87 |
87 void ObtainDelegate(net::URLRequest* request, | 88 void ObtainDelegate(net::URLRequest* request, |
88 const Callback& callback) override { | 89 const Callback& callback) override { |
89 callback_ = callback; | 90 callback_ = callback; |
90 io_thread_client_->ShouldInterceptRequestAsync( | 91 io_thread_client_->ShouldInterceptRequestAsync( |
91 // The request is only used while preparing the call, not retained. | 92 // The request is only used while preparing the call, not retained. |
92 request, | 93 request, |
93 base::Bind(&ShouldInterceptRequestAdaptor::WebResourceResponseObtained, | 94 base::Bind(&ShouldInterceptRequestAdaptor::WebResourceResponseObtained, |
94 // The lifetime of the DelegateObtainer is managed by | 95 // The lifetime of the DelegateObtainer is managed by |
95 // AndroidStreamReaderURLRequestJob, it might get deleted. | 96 // AndroidStreamReaderURLRequestJob, it might get deleted. |
96 weak_factory_.GetWeakPtr())); | 97 weak_factory_.GetWeakPtr())); |
97 } | 98 } |
98 | 99 |
99 private: | 100 private: |
100 void WebResourceResponseObtained( | 101 void WebResourceResponseObtained( |
101 scoped_ptr<AwWebResourceResponse> response) { | 102 std::unique_ptr<AwWebResourceResponse> response) { |
102 if (response) { | 103 if (response) { |
103 callback_.Run(make_scoped_ptr( | 104 callback_.Run(base::WrapUnique( |
104 new StreamReaderJobDelegateImpl(std::move(response)))); | 105 new StreamReaderJobDelegateImpl(std::move(response)))); |
105 } else { | 106 } else { |
106 callback_.Run(nullptr); | 107 callback_.Run(nullptr); |
107 } | 108 } |
108 } | 109 } |
109 | 110 |
110 scoped_ptr<AwContentsIoThreadClient> io_thread_client_; | 111 std::unique_ptr<AwContentsIoThreadClient> io_thread_client_; |
111 Callback callback_; | 112 Callback callback_; |
112 base::WeakPtrFactory<ShouldInterceptRequestAdaptor> weak_factory_; | 113 base::WeakPtrFactory<ShouldInterceptRequestAdaptor> weak_factory_; |
113 | 114 |
114 DISALLOW_COPY_AND_ASSIGN(ShouldInterceptRequestAdaptor); | 115 DISALLOW_COPY_AND_ASSIGN(ShouldInterceptRequestAdaptor); |
115 }; | 116 }; |
116 | 117 |
117 scoped_ptr<AwContentsIoThreadClient> GetCorrespondingIoThreadClient( | 118 std::unique_ptr<AwContentsIoThreadClient> GetCorrespondingIoThreadClient( |
118 net::URLRequest* request) { | 119 net::URLRequest* request) { |
119 if (content::ResourceRequestInfo::OriginatedFromServiceWorker(request)) | 120 if (content::ResourceRequestInfo::OriginatedFromServiceWorker(request)) |
120 return AwContentsIoThreadClient::GetServiceWorkerIoThreadClient(); | 121 return AwContentsIoThreadClient::GetServiceWorkerIoThreadClient(); |
121 | 122 |
122 int render_process_id, render_frame_id; | 123 int render_process_id, render_frame_id; |
123 if (!content::ResourceRequestInfo::GetRenderFrameForRequest( | 124 if (!content::ResourceRequestInfo::GetRenderFrameForRequest( |
124 request, &render_process_id, &render_frame_id)) { | 125 request, &render_process_id, &render_frame_id)) { |
125 return nullptr; | 126 return nullptr; |
126 } | 127 } |
127 | 128 |
128 return AwContentsIoThreadClient::FromID(render_process_id, render_frame_id); | 129 return AwContentsIoThreadClient::FromID(render_process_id, render_frame_id); |
129 } | 130 } |
130 | 131 |
131 } // namespace | 132 } // namespace |
132 | 133 |
133 AwRequestInterceptor::AwRequestInterceptor() {} | 134 AwRequestInterceptor::AwRequestInterceptor() {} |
134 | 135 |
135 AwRequestInterceptor::~AwRequestInterceptor() {} | 136 AwRequestInterceptor::~AwRequestInterceptor() {} |
136 | 137 |
137 net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest( | 138 net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest( |
138 net::URLRequest* request, | 139 net::URLRequest* request, |
139 net::NetworkDelegate* network_delegate) const { | 140 net::NetworkDelegate* network_delegate) const { |
140 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 141 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
141 | 142 |
142 // MaybeInterceptRequest can be called multiple times for the same request. | 143 // MaybeInterceptRequest can be called multiple times for the same request. |
143 if (request->GetUserData(kRequestAlreadyHasJobDataKey)) | 144 if (request->GetUserData(kRequestAlreadyHasJobDataKey)) |
144 return nullptr; | 145 return nullptr; |
145 | 146 |
146 scoped_ptr<AwContentsIoThreadClient> io_thread_client = | 147 std::unique_ptr<AwContentsIoThreadClient> io_thread_client = |
147 GetCorrespondingIoThreadClient(request); | 148 GetCorrespondingIoThreadClient(request); |
148 | 149 |
149 if (!io_thread_client) | 150 if (!io_thread_client) |
150 return nullptr; | 151 return nullptr; |
151 | 152 |
152 GURL referrer(request->referrer()); | 153 GURL referrer(request->referrer()); |
153 if (referrer.is_valid() && | 154 if (referrer.is_valid() && |
154 (!request->is_pending() || request->is_redirecting())) { | 155 (!request->is_pending() || request->is_redirecting())) { |
155 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer, | 156 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer, |
156 referrer.spec(), true); | 157 referrer.spec(), true); |
157 } | 158 } |
158 request->SetUserData(kRequestAlreadyHasJobDataKey, | 159 request->SetUserData(kRequestAlreadyHasJobDataKey, |
159 new base::SupportsUserData::Data()); | 160 new base::SupportsUserData::Data()); |
160 return new AndroidStreamReaderURLRequestJob( | 161 return new AndroidStreamReaderURLRequestJob( |
161 request, network_delegate, | 162 request, network_delegate, |
162 make_scoped_ptr( | 163 base::WrapUnique( |
163 new ShouldInterceptRequestAdaptor(std::move(io_thread_client))), | 164 new ShouldInterceptRequestAdaptor(std::move(io_thread_client))), |
164 true); | 165 true); |
165 } | 166 } |
166 | 167 |
167 } // namespace android_webview | 168 } // namespace android_webview |
OLD | NEW |