Removed 'anonymous' from namespace, added whitespace in thread_restrictions.cc
[chromium-blink-merge.git] / content / test / weburl_loader_mock.cc
blob430262f051a09b911cf341b67fd12787dae57a86
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/test/weburl_loader_mock_factory.h"
10 #include "third_party/WebKit/public/platform/WebData.h"
11 #include "third_party/WebKit/public/platform/WebURLError.h"
12 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
13 #include "third_party/WebKit/public/platform/WebUnitTestSupport.h"
15 WebURLLoaderMock::WebURLLoaderMock(WebURLLoaderMockFactory* factory,
16 blink::WebURLLoader* default_loader)
17 : factory_(factory),
18 client_(NULL),
19 default_loader_(default_loader),
20 using_default_loader_(false),
21 is_deferred_(false),
22 weak_factory_(this) {
25 WebURLLoaderMock::~WebURLLoaderMock() {
28 void WebURLLoaderMock::ServeAsynchronousRequest(
29 blink::WebURLLoaderTestDelegate* delegate,
30 const blink::WebURLResponse& response,
31 const blink::WebData& data,
32 const blink::WebURLError& error) {
33 DCHECK(!using_default_loader_);
34 if (!client_)
35 return;
37 // If no delegate is provided then create an empty one. The default behavior
38 // will just proxy to the client.
39 scoped_ptr<blink::WebURLLoaderTestDelegate> defaultDelegate;
40 if (!delegate) {
41 defaultDelegate.reset(new blink::WebURLLoaderTestDelegate());
42 delegate = defaultDelegate.get();
45 // didReceiveResponse() and didReceiveData() might end up getting ::cancel()
46 // to be called which will make the ResourceLoader to delete |this|.
47 base::WeakPtr<WebURLLoaderMock> self(weak_factory_.GetWeakPtr());
49 delegate->didReceiveResponse(client_, this, response);
50 if (!self)
51 return;
53 if (error.reason) {
54 delegate->didFail(client_, this, error);
55 return;
57 delegate->didReceiveData(client_, this, data.data(), data.size(),
58 data.size());
59 if (!self)
60 return;
62 delegate->didFinishLoading(client_, this, 0, data.size());
65 blink::WebURLRequest WebURLLoaderMock::ServeRedirect(
66 const blink::WebURLResponse& redirectResponse) {
67 blink::WebURLRequest newRequest;
68 newRequest.initialize();
69 newRequest.setRequestContext(blink::WebURLRequest::RequestContextInternal);
70 GURL redirectURL(redirectResponse.httpHeaderField("Location"));
71 newRequest.setURL(redirectURL);
72 client_->willSendRequest(this, newRequest, redirectResponse);
73 return newRequest;
76 void WebURLLoaderMock::loadSynchronously(const blink::WebURLRequest& request,
77 blink::WebURLResponse& response,
78 blink::WebURLError& error,
79 blink::WebData& data) {
80 if (factory_->IsMockedURL(request.url())) {
81 factory_->LoadSynchronously(request, &response, &error, &data);
82 return;
84 DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
85 << "loadSynchronously shouldn't be falling back: "
86 << request.url().spec().data();
87 using_default_loader_ = true;
88 default_loader_->loadSynchronously(request, response, error, data);
91 void WebURLLoaderMock::loadAsynchronously(const blink::WebURLRequest& request,
92 blink::WebURLLoaderClient* client) {
93 if (factory_->IsMockedURL(request.url())) {
94 client_ = client;
95 factory_->LoadAsynchronouly(request, this);
96 return;
98 DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
99 << "loadAsynchronously shouldn't be falling back: "
100 << request.url().spec().data();
101 using_default_loader_ = true;
102 default_loader_->loadAsynchronously(request, client);
105 void WebURLLoaderMock::cancel() {
106 if (using_default_loader_) {
107 default_loader_->cancel();
108 return;
110 client_ = NULL;
111 factory_->CancelLoad(this);
114 void WebURLLoaderMock::setDefersLoading(bool deferred) {
115 is_deferred_ = deferred;
116 if (using_default_loader_) {
117 default_loader_->setDefersLoading(deferred);
118 return;
120 NOTIMPLEMENTED();