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 blink::WebURLLoader
* default_loader
)
17 default_loader_(default_loader
),
18 using_default_loader_(false),
23 WebURLLoaderMock::~WebURLLoaderMock() {
24 // When |this_deleted_| is not null, there is someone interested to know if
25 // |this| got deleted. We notify them by setting the pointed value to true.
27 *this_deleted_
= true;
30 void WebURLLoaderMock::ServeAsynchronousRequest(
31 const blink::WebURLResponse
& response
,
32 const blink::WebData
& data
,
33 const blink::WebURLError
& error
) {
34 DCHECK(!using_default_loader_
);
38 bool this_deleted
= false;
39 this_deleted_
= &this_deleted
;
40 client_
->didReceiveResponse(this, response
);
42 // didReceiveResponse might end up getting ::cancel() to be called which will
43 // make the ResourceLoader to delete |this|. If that happens, |this_deleted|,
44 // created on the stack, will be set to true.
50 client_
->didFail(this, error
);
53 client_
->didReceiveData(this, data
.data(), data
.size(), data
.size());
54 client_
->didFinishLoading(this, 0, data
.size());
57 blink::WebURLRequest
WebURLLoaderMock::ServeRedirect(
58 const blink::WebURLResponse
& redirectResponse
) {
59 blink::WebURLRequest newRequest
;
60 newRequest
.initialize();
61 newRequest
.setRequestContext(blink::WebURLRequest::RequestContextInternal
);
62 GURL
redirectURL(redirectResponse
.httpHeaderField("Location"));
63 newRequest
.setURL(redirectURL
);
64 client_
->willSendRequest(this, newRequest
, redirectResponse
);
68 void WebURLLoaderMock::loadSynchronously(const blink::WebURLRequest
& request
,
69 blink::WebURLResponse
& response
,
70 blink::WebURLError
& error
,
71 blink::WebData
& data
) {
72 if (factory_
->IsMockedURL(request
.url())) {
73 factory_
->LoadSynchronously(request
, &response
, &error
, &data
);
76 DCHECK(static_cast<const GURL
&>(request
.url()).SchemeIs("data"))
77 << "loadSynchronously shouldn't be falling back: "
78 << request
.url().spec().data();
79 using_default_loader_
= true;
80 default_loader_
->loadSynchronously(request
, response
, error
, data
);
83 void WebURLLoaderMock::loadAsynchronously(const blink::WebURLRequest
& request
,
84 blink::WebURLLoaderClient
* client
) {
85 if (factory_
->IsMockedURL(request
.url())) {
87 factory_
->LoadAsynchronouly(request
, this);
90 DCHECK(static_cast<const GURL
&>(request
.url()).SchemeIs("data"))
91 << "loadAsynchronously shouldn't be falling back: "
92 << request
.url().spec().data();
93 using_default_loader_
= true;
94 default_loader_
->loadAsynchronously(request
, client
);
97 void WebURLLoaderMock::cancel() {
98 if (using_default_loader_
) {
99 default_loader_
->cancel();
103 factory_
->CancelLoad(this);
106 void WebURLLoaderMock::setDefersLoading(bool deferred
) {
107 is_deferred_
= deferred
;
108 if (using_default_loader_
) {
109 default_loader_
->setDefersLoading(deferred
);