Update broken references to image assets
[chromium-blink-merge.git] / content / test / weburl_loader_mock.cc
blob23250491988ae7f8ed88b11eca125e337d2a90ff
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"
12 #include "third_party/WebKit/public/platform/WebUnitTestSupport.h"
14 WebURLLoaderMock::WebURLLoaderMock(WebURLLoaderMockFactory* factory,
15 blink::WebURLLoader* default_loader)
16 : factory_(factory),
17 client_(NULL),
18 default_loader_(default_loader),
19 using_default_loader_(false),
20 is_deferred_(false),
21 weak_factory_(this) {
24 WebURLLoaderMock::~WebURLLoaderMock() {
27 void WebURLLoaderMock::ServeAsynchronousRequest(
28 blink::WebURLLoaderTestDelegate* delegate,
29 const blink::WebURLResponse& response,
30 const blink::WebData& data,
31 const blink::WebURLError& error) {
32 DCHECK(!using_default_loader_);
33 if (!client_)
34 return;
36 // didReceiveResponse() and didReceiveData() might end up getting ::cancel()
37 // to be called which will make the ResourceLoader to delete |this|.
38 base::WeakPtr<WebURLLoaderMock> self(weak_factory_.GetWeakPtr());
40 client_->didReceiveResponse(this, response);
41 if (!self)
42 return;
44 if (error.reason) {
45 client_->didFail(this, error);
46 return;
48 if (delegate) {
49 delegate->didReceiveData(client_, this, data.data(), data.size(),
50 data.size());
51 } else {
52 client_->didReceiveData(this, data.data(), data.size(), data.size());
55 if (!self)
56 return;
58 client_->didFinishLoading(this, 0, data.size());
61 blink::WebURLRequest WebURLLoaderMock::ServeRedirect(
62 const blink::WebURLResponse& redirectResponse) {
63 blink::WebURLRequest newRequest;
64 newRequest.initialize();
65 newRequest.setRequestContext(blink::WebURLRequest::RequestContextInternal);
66 GURL redirectURL(redirectResponse.httpHeaderField("Location"));
67 newRequest.setURL(redirectURL);
68 client_->willSendRequest(this, newRequest, redirectResponse);
69 return newRequest;
72 void WebURLLoaderMock::loadSynchronously(const blink::WebURLRequest& request,
73 blink::WebURLResponse& response,
74 blink::WebURLError& error,
75 blink::WebData& data) {
76 if (factory_->IsMockedURL(request.url())) {
77 factory_->LoadSynchronously(request, &response, &error, &data);
78 return;
80 DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
81 << "loadSynchronously shouldn't be falling back: "
82 << request.url().spec().data();
83 using_default_loader_ = true;
84 default_loader_->loadSynchronously(request, response, error, data);
87 void WebURLLoaderMock::loadAsynchronously(const blink::WebURLRequest& request,
88 blink::WebURLLoaderClient* client) {
89 if (factory_->IsMockedURL(request.url())) {
90 client_ = client;
91 factory_->LoadAsynchronouly(request, this);
92 return;
94 DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
95 << "loadAsynchronously shouldn't be falling back: "
96 << request.url().spec().data();
97 using_default_loader_ = true;
98 default_loader_->loadAsynchronously(request, client);
101 void WebURLLoaderMock::cancel() {
102 if (using_default_loader_) {
103 default_loader_->cancel();
104 return;
106 client_ = NULL;
107 factory_->CancelLoad(this);
110 void WebURLLoaderMock::setDefersLoading(bool deferred) {
111 is_deferred_ = deferred;
112 if (using_default_loader_) {
113 default_loader_->setDefersLoading(deferred);
114 return;
116 NOTIMPLEMENTED();