Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / fileapi / mock_url_request_delegate.cc
blob40a975abd90652b30367f3cd4ccd91f2415fdc5f
1 // Copyright 2014 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/browser/fileapi/mock_url_request_delegate.h"
7 #include "base/profiler/scoped_tracker.h"
8 #include "base/run_loop.h"
9 #include "net/base/io_buffer.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace {
13 const int kBufferSize = 1024;
16 namespace content {
18 MockURLRequestDelegate::MockURLRequestDelegate()
19 : io_buffer_(new net::IOBuffer(kBufferSize)) {
22 MockURLRequestDelegate::~MockURLRequestDelegate() {
25 void MockURLRequestDelegate::OnResponseStarted(net::URLRequest* request) {
26 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
27 tracked_objects::ScopedTracker tracking_profile(
28 FROM_HERE_WITH_EXPLICIT_FUNCTION(
29 "422516 MockURLRequestDelegate::OnResponseStarted"));
31 if (request->status().is_success()) {
32 EXPECT_TRUE(request->response_headers());
33 ReadSome(request);
34 } else {
35 RequestComplete();
39 void MockURLRequestDelegate::OnReadCompleted(net::URLRequest* request,
40 int bytes_read) {
41 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
42 tracked_objects::ScopedTracker tracking_profile(
43 FROM_HERE_WITH_EXPLICIT_FUNCTION(
44 "422516 MockURLRequestDelegate::OnReadCompleted"));
46 if (bytes_read > 0)
47 ReceiveData(request, bytes_read);
48 else
49 RequestComplete();
52 void MockURLRequestDelegate::ReadSome(net::URLRequest* request) {
53 if (!request->is_pending()) {
54 RequestComplete();
55 return;
58 int bytes_read = 0;
59 if (!request->Read(io_buffer_.get(), kBufferSize, &bytes_read)) {
60 if (!request->status().is_io_pending())
61 RequestComplete();
62 return;
65 ReceiveData(request, bytes_read);
68 void MockURLRequestDelegate::ReceiveData(net::URLRequest* request,
69 int bytes_read) {
70 if (bytes_read) {
71 response_data_.append(io_buffer_->data(),
72 static_cast<size_t>(bytes_read));
73 ReadSome(request);
74 } else {
75 RequestComplete();
79 void MockURLRequestDelegate::RequestComplete() {
80 base::MessageLoop::current()->Quit();
83 } // namespace