Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / content / test / weburl_loader_mock.cc
blob30826fbb18c2aae1d2a840c39295fad520278abe
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 "base/memory/scoped_ptr.h"
9 #include "content/child/web_url_loader_impl.h"
10 #include "content/test/weburl_loader_mock_factory.h"
11 #include "third_party/WebKit/public/platform/WebData.h"
12 #include "third_party/WebKit/public/platform/WebURLError.h"
13 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
14 #include "third_party/WebKit/public/platform/WebUnitTestSupport.h"
16 WebURLLoaderMock::WebURLLoaderMock(WebURLLoaderMockFactory* factory,
17 blink::WebURLLoader* default_loader)
18 : factory_(factory),
19 client_(NULL),
20 default_loader_(default_loader),
21 using_default_loader_(false),
22 is_deferred_(false),
23 weak_factory_(this) {
26 WebURLLoaderMock::~WebURLLoaderMock() {
29 void WebURLLoaderMock::ServeAsynchronousRequest(
30 blink::WebURLLoaderTestDelegate* delegate,
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 // If no delegate is provided then create an empty one. The default behavior
39 // will just proxy to the client.
40 scoped_ptr<blink::WebURLLoaderTestDelegate> defaultDelegate;
41 if (!delegate) {
42 defaultDelegate.reset(new blink::WebURLLoaderTestDelegate());
43 delegate = defaultDelegate.get();
46 // didReceiveResponse() and didReceiveData() might end up getting ::cancel()
47 // to be called which will make the ResourceLoader to delete |this|.
48 base::WeakPtr<WebURLLoaderMock> self(weak_factory_.GetWeakPtr());
50 delegate->didReceiveResponse(client_, this, response);
51 if (!self)
52 return;
54 if (error.reason) {
55 delegate->didFail(client_, this, error);
56 return;
58 delegate->didReceiveData(client_, this, data.data(), data.size(),
59 data.size());
60 if (!self)
61 return;
63 delegate->didFinishLoading(client_, this, 0, data.size());
66 blink::WebURLRequest WebURLLoaderMock::ServeRedirect(
67 const blink::WebURLRequest& request,
68 const blink::WebURLResponse& redirectResponse) {
69 GURL redirectURL(redirectResponse.httpHeaderField("Location"));
71 net::RedirectInfo redirectInfo;
72 redirectInfo.new_method = request.httpMethod().utf8();
73 redirectInfo.new_url = redirectURL;
74 redirectInfo.new_first_party_for_cookies = redirectURL;
76 blink::WebURLRequest newRequest;
77 newRequest.initialize();
78 content::WebURLLoaderImpl::PopulateURLRequestForRedirect(
79 request,
80 redirectInfo,
81 request.referrerPolicy(),
82 request.skipServiceWorker(),
83 &newRequest);
85 base::WeakPtr<WebURLLoaderMock> self(weak_factory_.GetWeakPtr());
87 client_->willSendRequest(this, newRequest, redirectResponse);
89 // |this| might be deleted in willSendRequest().
90 if (!self)
91 return newRequest;
93 if (redirectURL != GURL(newRequest.url())) {
94 // Only follow the redirect if WebKit left the URL unmodified.
95 // We assume that WebKit only changes the URL to suppress a redirect, and we
96 // assume that it does so by setting it to be invalid.
97 DCHECK(!newRequest.url().isValid());
98 cancel();
101 return newRequest;
104 void WebURLLoaderMock::loadSynchronously(const blink::WebURLRequest& request,
105 blink::WebURLResponse& response,
106 blink::WebURLError& error,
107 blink::WebData& data) {
108 if (factory_->IsMockedURL(request.url())) {
109 factory_->LoadSynchronously(request, &response, &error, &data);
110 return;
112 DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
113 << "loadSynchronously shouldn't be falling back: "
114 << request.url().spec().data();
115 using_default_loader_ = true;
116 default_loader_->loadSynchronously(request, response, error, data);
119 void WebURLLoaderMock::loadAsynchronously(const blink::WebURLRequest& request,
120 blink::WebURLLoaderClient* client) {
121 if (factory_->IsMockedURL(request.url())) {
122 client_ = client;
123 factory_->LoadAsynchronouly(request, this);
124 return;
126 DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
127 << "loadAsynchronously shouldn't be falling back: "
128 << request.url().spec().data();
129 using_default_loader_ = true;
130 default_loader_->loadAsynchronously(request, client);
133 void WebURLLoaderMock::cancel() {
134 if (using_default_loader_) {
135 default_loader_->cancel();
136 return;
138 client_ = NULL;
139 factory_->CancelLoad(this);
142 void WebURLLoaderMock::setDefersLoading(bool deferred) {
143 is_deferred_ = deferred;
144 if (using_default_loader_) {
145 default_loader_->setDefersLoading(deferred);
146 return;
148 NOTIMPLEMENTED();