summaryrefslogtreecommitdiffstats
path: root/content/test/weburl_loader_mock.cc
blob: 62922e2a0a0b0d46bf30a4ffe0b6c7ecade5d589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/test/weburl_loader_mock.h"

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "content/child/web_url_loader_impl.h"
#include "content/test/weburl_loader_mock_factory.h"
#include "third_party/WebKit/public/platform/URLConversion.h"
#include "third_party/WebKit/public/platform/WebData.h"
#include "third_party/WebKit/public/platform/WebURLError.h"
#include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
#include "third_party/WebKit/public/platform/WebUnitTestSupport.h"

WebURLLoaderMock::WebURLLoaderMock(WebURLLoaderMockFactory* factory,
                                   blink::WebURLLoader* default_loader)
    : factory_(factory),
      client_(NULL),
      default_loader_(default_loader),
      using_default_loader_(false),
      is_deferred_(false),
      weak_factory_(this) {
}

WebURLLoaderMock::~WebURLLoaderMock() {
}

void WebURLLoaderMock::ServeAsynchronousRequest(
    blink::WebURLLoaderTestDelegate* delegate,
    const blink::WebURLResponse& response,
    const blink::WebData& data,
    const blink::WebURLError& error) {
  DCHECK(!using_default_loader_);
  if (!client_)
    return;

  // If no delegate is provided then create an empty one. The default behavior
  // will just proxy to the client.
  scoped_ptr<blink::WebURLLoaderTestDelegate> defaultDelegate;
  if (!delegate) {
    defaultDelegate.reset(new blink::WebURLLoaderTestDelegate());
    delegate = defaultDelegate.get();
  }

  // didReceiveResponse() and didReceiveData() might end up getting ::cancel()
  // to be called which will make the ResourceLoader to delete |this|.
  base::WeakPtr<WebURLLoaderMock> self(weak_factory_.GetWeakPtr());

  delegate->didReceiveResponse(client_, this, response);
  if (!self)
    return;

  if (error.reason) {
    delegate->didFail(client_, this, error);
    return;
  }
  delegate->didReceiveData(client_, this, data.data(), data.size(),
                             data.size());
  if (!self)
    return;

  delegate->didFinishLoading(client_, this, 0, data.size());
}

blink::WebURLRequest WebURLLoaderMock::ServeRedirect(
    const blink::WebURLRequest& request,
    const blink::WebURLResponse& redirectResponse) {
  GURL redirectURL(
      blink::WebStringToGURL(redirectResponse.httpHeaderField("Location")));

  net::RedirectInfo redirectInfo;
  redirectInfo.new_method = request.httpMethod().utf8();
  redirectInfo.new_url = redirectURL;
  redirectInfo.new_first_party_for_cookies = redirectURL;

  blink::WebURLRequest newRequest;
  newRequest.initialize();
  content::WebURLLoaderImpl::PopulateURLRequestForRedirect(
      request,
      redirectInfo,
      request.referrerPolicy(),
      request.skipServiceWorker(),
      &newRequest);

  base::WeakPtr<WebURLLoaderMock> self(weak_factory_.GetWeakPtr());

  client_->willFollowRedirect(this, newRequest, redirectResponse);

  // |this| might be deleted in willFollowRedirect().
  if (!self)
    return newRequest;

  if (redirectURL != GURL(newRequest.url())) {
    // Only follow the redirect if WebKit left the URL unmodified.
    // We assume that WebKit only changes the URL to suppress a redirect, and we
    // assume that it does so by setting it to be invalid.
    DCHECK(!newRequest.url().isValid());
    cancel();
  }

  return newRequest;
}

void WebURLLoaderMock::loadSynchronously(const blink::WebURLRequest& request,
                                         blink::WebURLResponse& response,
                                         blink::WebURLError& error,
                                         blink::WebData& data) {
  if (factory_->IsMockedURL(request.url())) {
    factory_->LoadSynchronously(request, &response, &error, &data);
    return;
  }
  DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
      << "loadSynchronously shouldn't be falling back: "
      << request.url().string().utf8();
  using_default_loader_ = true;
  default_loader_->loadSynchronously(request, response, error, data);
}

void WebURLLoaderMock::loadAsynchronously(const blink::WebURLRequest& request,
                                          blink::WebURLLoaderClient* client) {
  if (factory_->IsMockedURL(request.url())) {
    client_ = client;
    factory_->LoadAsynchronouly(request, this);
    return;
  }
  DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
      << "loadAsynchronously shouldn't be falling back: "
      << request.url().string().utf8();
  using_default_loader_ = true;
  default_loader_->loadAsynchronously(request, client);
}

void WebURLLoaderMock::cancel() {
  if (using_default_loader_) {
    default_loader_->cancel();
    return;
  }
  client_ = NULL;
  factory_->CancelLoad(this);
}

void WebURLLoaderMock::setDefersLoading(bool deferred) {
  is_deferred_ = deferred;
  if (using_default_loader_) {
    default_loader_->setDefersLoading(deferred);
    return;
  }
  NOTIMPLEMENTED();
}

void WebURLLoaderMock::setLoadingTaskRunner(blink::WebTaskRunner*) {
  // In principle this is NOTIMPLEMENTED(), but if we put that here it floods
  // the console during webkit unit tests, so we leave the function empty.
}