1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/test/weburl_loader_mock.h"
7 #include "base/logging.h"
8 #include "content/test/weburl_loader_mock_factory.h"
9 #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"
13 WebURLLoaderMock::WebURLLoaderMock(WebURLLoaderMockFactory
* factory
,
14 WebKit::WebURLLoader
* default_loader
)
17 default_loader_(default_loader
),
18 using_default_loader_(false),
22 WebURLLoaderMock::~WebURLLoaderMock() {
25 void WebURLLoaderMock::ServeAsynchronousRequest(
26 const WebKit::WebURLResponse
& response
,
27 const WebKit::WebData
& data
,
28 const WebKit::WebURLError
& error
) {
29 DCHECK(!using_default_loader_
);
33 client_
->didReceiveResponse(this, response
);
36 client_
->didFail(this, error
);
39 client_
->didReceiveData(this, data
.data(), data
.size(), data
.size());
40 client_
->didFinishLoading(this, 0);
43 WebKit::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
);
53 void 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
);
61 DCHECK(static_cast<const GURL
&>(request
.url()).SchemeIs("data"))
62 << "loadSynchronously shouldn't be falling back: "
63 << request
.url().spec().data();
64 using_default_loader_
= true;
65 default_loader_
->loadSynchronously(request
, response
, error
, data
);
68 void WebURLLoaderMock::loadAsynchronously(const WebKit::WebURLRequest
& request
,
69 WebKit::WebURLLoaderClient
* client
) {
70 if (factory_
->IsMockedURL(request
.url())) {
72 factory_
->LoadAsynchronouly(request
, this);
75 DCHECK(static_cast<const GURL
&>(request
.url()).SchemeIs("data"))
76 << "loadAsynchronously shouldn't be falling back: "
77 << request
.url().spec().data();
78 using_default_loader_
= true;
79 default_loader_
->loadAsynchronously(request
, client
);
82 void WebURLLoaderMock::cancel() {
83 if (using_default_loader_
) {
84 default_loader_
->cancel();
88 factory_
->CancelLoad(this);
91 void WebURLLoaderMock::setDefersLoading(bool deferred
) {
92 is_deferred_
= deferred
;
93 if (using_default_loader_
) {
94 default_loader_
->setDefersLoading(deferred
);