Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / test / weburl_loader_mock.cc
blob056564a839cf96be509cab9fb7b6a2f7a71a7cdb
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)
15 : factory_(factory),
16 client_(NULL),
17 default_loader_(default_loader),
18 using_default_loader_(false),
19 is_deferred_(false),
20 this_deleted_(NULL) {
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.
26 if (this_deleted_)
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_);
35 if (!client_)
36 return;
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.
45 if (this_deleted)
46 return;
47 this_deleted_ = NULL;
49 if (error.reason) {
50 client_->didFail(this, error);
51 return;
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 GURL redirectURL(redirectResponse.httpHeaderField("Location"));
62 newRequest.setURL(redirectURL);
63 client_->willSendRequest(this, newRequest, redirectResponse);
64 return newRequest;
67 void WebURLLoaderMock::loadSynchronously(const blink::WebURLRequest& request,
68 blink::WebURLResponse& response,
69 blink::WebURLError& error,
70 blink::WebData& data) {
71 if (factory_->IsMockedURL(request.url())) {
72 factory_->LoadSynchronously(request, &response, &error, &data);
73 return;
75 DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
76 << "loadSynchronously shouldn't be falling back: "
77 << request.url().spec().data();
78 using_default_loader_ = true;
79 default_loader_->loadSynchronously(request, response, error, data);
82 void WebURLLoaderMock::loadAsynchronously(const blink::WebURLRequest& request,
83 blink::WebURLLoaderClient* client) {
84 if (factory_->IsMockedURL(request.url())) {
85 client_ = client;
86 factory_->LoadAsynchronouly(request, this);
87 return;
89 DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
90 << "loadAsynchronously shouldn't be falling back: "
91 << request.url().spec().data();
92 using_default_loader_ = true;
93 default_loader_->loadAsynchronously(request, client);
96 void WebURLLoaderMock::cancel() {
97 if (using_default_loader_) {
98 default_loader_->cancel();
99 return;
101 client_ = NULL;
102 factory_->CancelLoad(this);
105 void WebURLLoaderMock::setDefersLoading(bool deferred) {
106 is_deferred_ = deferred;
107 if (using_default_loader_) {
108 default_loader_->setDefersLoading(deferred);
109 return;
111 NOTIMPLEMENTED();