summaryrefslogtreecommitdiff
path: root/chromium/content/browser/loader/mock_resource_loader.cc
blob: 7444b0ad7926b6e8f5931ceae0df3a625a8a33bb (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2016 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/browser/loader/mock_resource_loader.h"

#include <memory>

#include "base/memory/ref_counted.h"
#include "content/browser/loader/resource_controller.h"
#include "content/browser/loader/resource_handler.h"
#include "net/base/io_buffer.h"
#include "net/url_request/url_request_status.h"
#include "services/network/public/cpp/resource_response.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

class MockResourceLoader::TestResourceController : public ResourceController {
 public:
  explicit TestResourceController(base::WeakPtr<MockResourceLoader> mock_loader)
      : mock_loader_(mock_loader) {}
  ~TestResourceController() override {}

  void Resume() override { mock_loader_->OnResume(); }

  void Cancel() override { CancelWithError(net::ERR_ABORTED); }

  void CancelWithError(int error_code) override {
    mock_loader_->OnCancel(error_code);
  }

  base::WeakPtr<MockResourceLoader> mock_loader_;
};

MockResourceLoader::MockResourceLoader(ResourceHandler* resource_handler)
    : resource_handler_(resource_handler), weak_factory_(this) {
  resource_handler_->SetDelegate(this);
}

MockResourceLoader::~MockResourceLoader() {}

MockResourceLoader::Status MockResourceLoader::OnWillStart(const GURL& url) {
  EXPECT_FALSE(weak_factory_.HasWeakPtrs());
  EXPECT_EQ(Status::IDLE, status_);

  status_ = Status::CALLING_HANDLER;
  resource_handler_->OnWillStart(url, std::make_unique<TestResourceController>(
                                          weak_factory_.GetWeakPtr()));
  if (status_ == Status::CALLING_HANDLER)
    status_ = Status::CALLBACK_PENDING;
  return status_;
}

MockResourceLoader::Status MockResourceLoader::OnRequestRedirected(
    const net::RedirectInfo& redirect_info,
    scoped_refptr<network::ResourceResponse> response) {
  EXPECT_FALSE(weak_factory_.HasWeakPtrs());
  EXPECT_EQ(Status::IDLE, status_);

  status_ = Status::CALLING_HANDLER;
  // Note that |this| does not hold onto |response|, to match ResourceLoader's
  // behavior. If |resource_handler_| wants to use |response| asynchronously, it
  // needs to hold onto its own pointer to it.
  resource_handler_->OnRequestRedirected(
      redirect_info, response.get(),
      std::make_unique<TestResourceController>(weak_factory_.GetWeakPtr()));
  if (status_ == Status::CALLING_HANDLER)
    status_ = Status::CALLBACK_PENDING;
  return status_;
}

MockResourceLoader::Status MockResourceLoader::OnResponseStarted(
    scoped_refptr<network::ResourceResponse> response) {
  EXPECT_FALSE(weak_factory_.HasWeakPtrs());
  EXPECT_EQ(Status::IDLE, status_);

  status_ = Status::CALLING_HANDLER;
  // Note that |this| does not hold onto |response|, to match ResourceLoader's
  // behavior. If |resource_handler_| wants to use |response| asynchronously, it
  // needs to hold onto its own pointer to it.
  resource_handler_->OnResponseStarted(
      response.get(),
      std::make_unique<TestResourceController>(weak_factory_.GetWeakPtr()));
  if (status_ == Status::CALLING_HANDLER)
    status_ = Status::CALLBACK_PENDING;
  return status_;
}

MockResourceLoader::Status MockResourceLoader::OnWillRead() {
  EXPECT_FALSE(weak_factory_.HasWeakPtrs());
  EXPECT_EQ(Status::IDLE, status_);

  status_ = Status::CALLING_HANDLER;
  waiting_on_buffer_ = true;
  resource_handler_->OnWillRead(
      &io_buffer_, &io_buffer_size_,
      std::make_unique<TestResourceController>(weak_factory_.GetWeakPtr()));
  if (status_ == Status::CALLING_HANDLER) {
    // Shouldn't update  |io_buffer_| or |io_buffer_size_| yet if Resume()
    // hasn't yet been called.
    EXPECT_FALSE(io_buffer_);
    EXPECT_EQ(0, io_buffer_size_);

    status_ = Status::CALLBACK_PENDING;
  }

  return status_;
};

MockResourceLoader::Status MockResourceLoader::OnReadCompleted(
    base::StringPiece bytes) {
  EXPECT_FALSE(weak_factory_.HasWeakPtrs());
  EXPECT_EQ(Status::IDLE, status_);
  EXPECT_LE(bytes.size(), static_cast<size_t>(io_buffer_size_));

  status_ = Status::CALLING_HANDLER;
  std::copy(bytes.begin(), bytes.end(), io_buffer_->data());
  io_buffer_ = nullptr;
  io_buffer_size_ = 0;
  resource_handler_->OnReadCompleted(
      bytes.size(),
      std::make_unique<TestResourceController>(weak_factory_.GetWeakPtr()));
  if (status_ == Status::CALLING_HANDLER)
    status_ = Status::CALLBACK_PENDING;
  return status_;
}

MockResourceLoader::Status MockResourceLoader::OnResponseCompleted(
    const net::URLRequestStatus& status) {
  EXPECT_FALSE(weak_factory_.HasWeakPtrs());
  // This should only happen while the ResourceLoader is idle or the request was
  // canceled.
  EXPECT_TRUE(status_ == Status::IDLE ||
              (!status.is_success() && status_ == Status::CANCELED &&
               error_code_ == status.error()));

  io_buffer_ = nullptr;
  io_buffer_size_ = 0;
  status_ = Status::CALLING_HANDLER;
  resource_handler_->OnResponseCompleted(
      status,
      std::make_unique<TestResourceController>(weak_factory_.GetWeakPtr()));
  if (status_ == Status::CALLING_HANDLER)
    status_ = Status::CALLBACK_PENDING;
  EXPECT_NE(Status::CANCELED, status_);
  return status_;
}

MockResourceLoader::Status
MockResourceLoader::OnResponseCompletedFromExternalOutOfBandCancel(
    const net::URLRequestStatus& url_request_status) {
  // This can happen at any point, except from a recursive call from
  // ResourceHandler.
  EXPECT_NE(Status::CALLING_HANDLER, status_);

  waiting_on_buffer_ = false;
  io_buffer_ = nullptr;
  io_buffer_size_ = 0;
  status_ = Status::CALLING_HANDLER;

  resource_handler_->OnResponseCompleted(
      url_request_status,
      std::make_unique<TestResourceController>(weak_factory_.GetWeakPtr()));
  if (status_ == Status::CALLING_HANDLER)
    status_ = Status::CALLBACK_PENDING;
  EXPECT_NE(Status::CANCELED, status_);
  return status_;
}

void MockResourceLoader::WaitUntilIdleOrCanceled() {
  if (status_ == Status::IDLE || status_ == Status::CANCELED)
    return;
  EXPECT_FALSE(canceled_or_idle_run_loop_);
  canceled_or_idle_run_loop_.reset(new base::RunLoop());
  canceled_or_idle_run_loop_->Run();
  canceled_or_idle_run_loop_.reset();
  EXPECT_TRUE(status_ == Status::IDLE || status_ == Status::CANCELED);
}

void MockResourceLoader::OutOfBandCancel(int error_code, bool tell_renderer) {
  // Shouldn't be called in-band.
  EXPECT_NE(Status::CALLING_HANDLER, status_);

  status_ = Status::CANCELED;
  canceled_out_of_band_ = true;

  // If OnWillRead was deferred, no longer waiting on a buffer.
  waiting_on_buffer_ = false;

  // To mimic real behavior, keep old error, in the case of double-cancel.
  if (error_code_ == net::OK)
    error_code_ = error_code;
}

void MockResourceLoader::OnCancel(int error_code) {
  // It's currently allowed to be canceled in-band after being cancelled
  // out-of-band, so do nothing, unless the status is no longer CANCELED, which
  // which case, OnResponseCompleted has already been called, and cancels aren't
  // expected then.
  // TODO(mmenke):  Make CancelOutOfBand synchronously destroy the
  // ResourceLoader.
  if (canceled_out_of_band_ && status_ == Status::CANCELED)
    return;

  // Shouldn't update |io_buffer_| or |io_buffer_size_| on cancel.
  if (waiting_on_buffer_) {
    EXPECT_FALSE(io_buffer_);
    EXPECT_EQ(0, io_buffer_size_);
    waiting_on_buffer_ = false;
  }

  EXPECT_LT(error_code, 0);
  EXPECT_TRUE(status_ == Status::CALLBACK_PENDING ||
              status_ == Status::CALLING_HANDLER);

  status_ = Status::CANCELED;
  error_code_ = error_code;
  if (canceled_or_idle_run_loop_)
    canceled_or_idle_run_loop_->Quit();
}

void MockResourceLoader::OnResume() {
  if (waiting_on_buffer_) {
    EXPECT_TRUE(io_buffer_);
    EXPECT_LT(0, io_buffer_size_);

    waiting_on_buffer_ = false;
  }

  // Shouldn't update |io_buffer_| or |io_buffer_size_| on cancel.
  EXPECT_TRUE(status_ == Status::CALLBACK_PENDING ||
              status_ == Status::CALLING_HANDLER);

  status_ = Status::IDLE;
  if (canceled_or_idle_run_loop_)
    canceled_or_idle_run_loop_->Quit();
}

}  // namespace content