blob: 4eb37be53111c6eba35ce5cb6fc5c95b8727816b [file] [log] [blame]
[email protected]3469ea22013-09-14 01:24:471// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]0fbd5222010-06-21 20:54:092// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]3469ea22013-09-14 01:24:475#include "content/test/weburl_loader_mock.h"
[email protected]0fbd5222010-06-21 20:54:096
[email protected]1407b6e2010-08-27 21:39:487#include "base/logging.h"
[email protected]3469ea22013-09-14 01:24:478#include "content/test/weburl_loader_mock_factory.h"
[email protected]c10884462013-05-30 00:22:099#include "third_party/WebKit/public/platform/WebData.h"
10#include "third_party/WebKit/public/platform/WebURLError.h"
11#include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
[email protected]0fbd5222010-06-21 20:54:0912
13WebURLLoaderMock::WebURLLoaderMock(WebURLLoaderMockFactory* factory,
14 WebKit::WebURLLoader* default_loader)
15 : factory_(factory),
16 client_(NULL),
17 default_loader_(default_loader),
[email protected]6a712e7c2011-12-17 05:26:1118 using_default_loader_(false),
19 is_deferred_(false) {
[email protected]0fbd5222010-06-21 20:54:0920}
21
22WebURLLoaderMock::~WebURLLoaderMock() {
23}
24
25void WebURLLoaderMock::ServeAsynchronousRequest(
26 const WebKit::WebURLResponse& response,
27 const WebKit::WebData& data,
28 const WebKit::WebURLError& error) {
29 DCHECK(!using_default_loader_);
30 if (!client_)
31 return;
32
33 client_->didReceiveResponse(this, response);
[email protected]55e82fc8e2012-08-03 21:45:2934
35 if (error.reason) {
36 client_->didFail(this, error);
37 return;
38 }
[email protected]780e83b72011-04-07 13:28:0439 client_->didReceiveData(this, data.data(), data.size(), data.size());
[email protected]82d1aa72010-10-01 18:37:1640 client_->didFinishLoading(this, 0);
[email protected]0fbd5222010-06-21 20:54:0941}
42
[email protected]6a712e7c2011-12-17 05:26:1143WebKit::WebURLRequest WebURLLoaderMock::ServeRedirect(
44 const WebKit::WebURLResponse& redirectResponse) {
45 WebKit::WebURLRequest newRequest;
46 newRequest.initialize();
47 GURL redirectURL(redirectResponse.httpHeaderField("Location"));
48 newRequest.setURL(redirectURL);
49 client_->willSendRequest(this, newRequest, redirectResponse);
50 return newRequest;
51}
52
[email protected]0fbd5222010-06-21 20:54:0953void WebURLLoaderMock::loadSynchronously(const WebKit::WebURLRequest& request,
54 WebKit::WebURLResponse& response,
55 WebKit::WebURLError& error,
56 WebKit::WebData& data) {
57 if (factory_->IsMockedURL(request.url())) {
58 factory_->LoadSynchronously(request, &response, &error, &data);
59 return;
60 }
[email protected]2cf5cd72013-06-24 06:05:1761 DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
62 << "loadSynchronously shouldn't be falling back: "
63 << request.url().spec().data();
[email protected]0fbd5222010-06-21 20:54:0964 using_default_loader_ = true;
65 default_loader_->loadSynchronously(request, response, error, data);
66}
67
68void WebURLLoaderMock::loadAsynchronously(const WebKit::WebURLRequest& request,
69 WebKit::WebURLLoaderClient* client) {
70 if (factory_->IsMockedURL(request.url())) {
71 client_ = client;
72 factory_->LoadAsynchronouly(request, this);
73 return;
74 }
[email protected]2cf5cd72013-06-24 06:05:1775 DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
76 << "loadAsynchronously shouldn't be falling back: "
77 << request.url().spec().data();
[email protected]0fbd5222010-06-21 20:54:0978 using_default_loader_ = true;
79 default_loader_->loadAsynchronously(request, client);
80}
81
82void WebURLLoaderMock::cancel() {
83 if (using_default_loader_) {
84 default_loader_->cancel();
85 return;
86 }
87 client_ = NULL;
88 factory_->CancelLoad(this);
89}
90
91void WebURLLoaderMock::setDefersLoading(bool deferred) {
[email protected]6a712e7c2011-12-17 05:26:1192 is_deferred_ = deferred;
[email protected]0fbd5222010-06-21 20:54:0993 if (using_default_loader_) {
94 default_loader_->setDefersLoading(deferred);
95 return;
96 }
97 NOTIMPLEMENTED();
98}