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 "content/public/renderer/resource_fetcher.h" | 5 #include "content/public/renderer/resource_fetcher.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 } | 149 } |
150 | 150 |
151 void ResourceFetcherDownloadOnRenderer(const GURL& url) { | 151 void ResourceFetcherDownloadOnRenderer(const GURL& url) { |
152 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); | 152 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); |
153 | 153 |
154 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); | 154 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); |
155 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); | 155 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); |
156 fetcher->Start(frame, | 156 fetcher->Start(frame, |
157 WebURLRequest::RequestContextInternal, | 157 WebURLRequest::RequestContextInternal, |
158 WebURLRequest::FrameTypeNone, | 158 WebURLRequest::FrameTypeNone, |
159 ResourceFetcher::PLATFORM_LOADER, | |
160 delegate->NewCallback()); | 159 delegate->NewCallback()); |
161 | 160 |
162 delegate->WaitForResponse(); | 161 delegate->WaitForResponse(); |
163 | 162 |
164 ASSERT_TRUE(delegate->completed()); | 163 ASSERT_TRUE(delegate->completed()); |
165 EXPECT_EQ(delegate->response().httpStatusCode(), 200); | 164 EXPECT_EQ(delegate->response().httpStatusCode(), 200); |
166 std::string text = delegate->data(); | 165 std::string text = delegate->data(); |
167 EXPECT_TRUE(text.find("Basic html test.") != std::string::npos); | 166 EXPECT_TRUE(text.find("Basic html test.") != std::string::npos); |
168 } | 167 } |
169 | 168 |
170 void ResourceFetcher404OnRenderer(const GURL& url) { | 169 void ResourceFetcher404OnRenderer(const GURL& url) { |
171 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); | 170 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); |
172 | 171 |
173 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); | 172 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); |
174 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); | 173 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); |
175 fetcher->Start(frame, | 174 fetcher->Start(frame, |
176 WebURLRequest::RequestContextInternal, | 175 WebURLRequest::RequestContextInternal, |
177 WebURLRequest::FrameTypeNone, | 176 WebURLRequest::FrameTypeNone, |
178 ResourceFetcher::PLATFORM_LOADER, | |
179 delegate->NewCallback()); | 177 delegate->NewCallback()); |
180 | 178 |
181 delegate->WaitForResponse(); | 179 delegate->WaitForResponse(); |
182 | 180 |
183 ASSERT_TRUE(delegate->completed()); | 181 ASSERT_TRUE(delegate->completed()); |
184 EXPECT_EQ(delegate->response().httpStatusCode(), 404); | 182 EXPECT_EQ(delegate->response().httpStatusCode(), 404); |
185 } | 183 } |
186 | 184 |
187 void ResourceFetcherDidFailOnRenderer() { | 185 void ResourceFetcherDidFailOnRenderer() { |
188 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); | 186 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); |
189 | 187 |
190 // Try to fetch a page on a site that doesn't exist. | 188 // Try to fetch a page on a site that doesn't exist. |
191 GURL url("http://localhost:1339/doesnotexist"); | 189 GURL url("http://localhost:1339/doesnotexist"); |
192 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); | 190 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); |
193 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); | 191 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); |
194 fetcher->Start(frame, | 192 fetcher->Start(frame, |
195 WebURLRequest::RequestContextInternal, | 193 WebURLRequest::RequestContextInternal, |
196 WebURLRequest::FrameTypeNone, | 194 WebURLRequest::FrameTypeNone, |
197 ResourceFetcher::PLATFORM_LOADER, | |
198 delegate->NewCallback()); | 195 delegate->NewCallback()); |
199 | 196 |
200 delegate->WaitForResponse(); | 197 delegate->WaitForResponse(); |
201 | 198 |
202 // When we fail, we still call the Delegate callback but we pass in empty | 199 // When we fail, we still call the Delegate callback but we pass in empty |
203 // values. | 200 // values. |
204 EXPECT_TRUE(delegate->completed()); | 201 EXPECT_TRUE(delegate->completed()); |
205 EXPECT_TRUE(delegate->response().isNull()); | 202 EXPECT_TRUE(delegate->response().isNull()); |
206 EXPECT_EQ(delegate->data(), std::string()); | 203 EXPECT_EQ(delegate->data(), std::string()); |
207 EXPECT_FALSE(delegate->timed_out()); | 204 EXPECT_FALSE(delegate->timed_out()); |
208 } | 205 } |
209 | 206 |
210 void ResourceFetcherTimeoutOnRenderer(const GURL& url) { | 207 void ResourceFetcherTimeoutOnRenderer(const GURL& url) { |
211 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); | 208 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); |
212 | 209 |
213 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); | 210 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); |
214 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); | 211 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); |
215 fetcher->Start(frame, | 212 fetcher->Start(frame, |
216 WebURLRequest::RequestContextInternal, | 213 WebURLRequest::RequestContextInternal, |
217 WebURLRequest::FrameTypeNone, | 214 WebURLRequest::FrameTypeNone, |
218 ResourceFetcher::PLATFORM_LOADER, | |
219 delegate->NewCallback()); | 215 delegate->NewCallback()); |
220 fetcher->SetTimeout(base::TimeDelta()); | 216 fetcher->SetTimeout(base::TimeDelta()); |
221 | 217 |
222 delegate->WaitForResponse(); | 218 delegate->WaitForResponse(); |
223 | 219 |
224 // When we timeout, we still call the Delegate callback but we pass in empty | 220 // When we timeout, we still call the Delegate callback but we pass in empty |
225 // values. | 221 // values. |
226 EXPECT_TRUE(delegate->completed()); | 222 EXPECT_TRUE(delegate->completed()); |
227 EXPECT_TRUE(delegate->response().isNull()); | 223 EXPECT_TRUE(delegate->response().isNull()); |
228 EXPECT_EQ(delegate->data(), std::string()); | 224 EXPECT_EQ(delegate->data(), std::string()); |
229 EXPECT_FALSE(delegate->timed_out()); | 225 EXPECT_FALSE(delegate->timed_out()); |
230 } | 226 } |
231 | 227 |
232 void ResourceFetcherDeletedInCallbackOnRenderer(const GURL& url) { | 228 void ResourceFetcherDeletedInCallbackOnRenderer(const GURL& url) { |
233 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); | 229 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); |
234 | 230 |
235 std::unique_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate); | 231 std::unique_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate); |
236 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); | 232 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); |
237 fetcher->Start(frame, | 233 fetcher->Start(frame, |
238 WebURLRequest::RequestContextInternal, | 234 WebURLRequest::RequestContextInternal, |
239 WebURLRequest::FrameTypeNone, | 235 WebURLRequest::FrameTypeNone, |
240 ResourceFetcher::PLATFORM_LOADER, | |
241 delegate->NewCallback()); | 236 delegate->NewCallback()); |
242 fetcher->SetTimeout(base::TimeDelta()); | 237 fetcher->SetTimeout(base::TimeDelta()); |
243 delegate->SetFetcher(fetcher.release()); | 238 delegate->SetFetcher(fetcher.release()); |
244 | 239 |
245 delegate->WaitForResponse(); | 240 delegate->WaitForResponse(); |
246 EXPECT_FALSE(delegate->timed_out()); | 241 EXPECT_FALSE(delegate->timed_out()); |
247 } | 242 } |
248 | 243 |
249 void ResourceFetcherPost(const GURL& url) { | 244 void ResourceFetcherPost(const GURL& url) { |
250 const char* kBody = "Really nifty POST body!"; | 245 const char* kBody = "Really nifty POST body!"; |
251 | 246 |
252 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); | 247 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); |
253 | 248 |
254 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); | 249 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); |
255 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); | 250 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); |
256 fetcher->SetMethod("POST"); | 251 fetcher->SetMethod("POST"); |
257 fetcher->SetBody(kBody); | 252 fetcher->SetBody(kBody); |
258 fetcher->Start(frame, | 253 fetcher->Start(frame, |
259 WebURLRequest::RequestContextInternal, | 254 WebURLRequest::RequestContextInternal, |
260 WebURLRequest::FrameTypeNone, | 255 WebURLRequest::FrameTypeNone, |
261 ResourceFetcher::PLATFORM_LOADER, | |
262 delegate->NewCallback()); | 256 delegate->NewCallback()); |
263 | 257 |
264 delegate->WaitForResponse(); | 258 delegate->WaitForResponse(); |
265 ASSERT_TRUE(delegate->completed()); | 259 ASSERT_TRUE(delegate->completed()); |
266 EXPECT_EQ(delegate->response().httpStatusCode(), 200); | 260 EXPECT_EQ(delegate->response().httpStatusCode(), 200); |
267 EXPECT_EQ(kBody, delegate->data()); | 261 EXPECT_EQ(kBody, delegate->data()); |
268 } | 262 } |
269 | 263 |
270 void ResourceFetcherSetHeader(const GURL& url) { | 264 void ResourceFetcherSetHeader(const GURL& url) { |
271 const char* kHeader = "Rather boring header."; | 265 const char* kHeader = "Rather boring header."; |
272 | 266 |
273 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); | 267 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame(); |
274 | 268 |
275 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); | 269 std::unique_ptr<FetcherDelegate> delegate(new FetcherDelegate); |
276 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); | 270 std::unique_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url)); |
277 fetcher->SetHeader("header", kHeader); | 271 fetcher->SetHeader("header", kHeader); |
278 fetcher->Start(frame, | 272 fetcher->Start(frame, |
279 WebURLRequest::RequestContextInternal, | 273 WebURLRequest::RequestContextInternal, |
280 WebURLRequest::FrameTypeNone, | 274 WebURLRequest::FrameTypeNone, |
281 ResourceFetcher::PLATFORM_LOADER, | |
282 delegate->NewCallback()); | 275 delegate->NewCallback()); |
283 | 276 |
284 delegate->WaitForResponse(); | 277 delegate->WaitForResponse(); |
285 ASSERT_TRUE(delegate->completed()); | 278 ASSERT_TRUE(delegate->completed()); |
286 EXPECT_EQ(delegate->response().httpStatusCode(), 200); | 279 EXPECT_EQ(delegate->response().httpStatusCode(), 200); |
287 EXPECT_EQ(kHeader, delegate->data()); | 280 EXPECT_EQ(kHeader, delegate->data()); |
288 } | 281 } |
289 | 282 |
290 int32_t render_view_routing_id_; | 283 int32_t render_view_routing_id_; |
291 }; | 284 }; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 ASSERT_TRUE(embedded_test_server()->Start()); | 375 ASSERT_TRUE(embedded_test_server()->Start()); |
383 GURL url(embedded_test_server()->GetURL("/echoheader?header")); | 376 GURL url(embedded_test_server()->GetURL("/echoheader?header")); |
384 | 377 |
385 PostTaskToInProcessRendererAndWait( | 378 PostTaskToInProcessRendererAndWait( |
386 base::Bind( | 379 base::Bind( |
387 &ResourceFetcherTests::ResourceFetcherSetHeader, | 380 &ResourceFetcherTests::ResourceFetcherSetHeader, |
388 base::Unretained(this), url)); | 381 base::Unretained(this), url)); |
389 } | 382 } |
390 | 383 |
391 } // namespace content | 384 } // namespace content |
OLD | NEW |