Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(362)

Side by Side Diff: net/filter/mock_stream_source.cc

Issue 1662763002: [ON HOLD] Implement pull-based design for content decoding (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 #include "net/filter/mock_stream_source.h"
6
7 namespace net {
8
9 MockStreamSource::MockStreamSource()
10 : StreamSource(StreamSource::TYPE_NONE, nullptr),
11 awaiting_completion_(false),
12 dest_buffer_(nullptr),
13 dest_buffer_size_(0) {}
14
15 MockStreamSource::~MockStreamSource() {}
16
17 Error MockStreamSource::Read(IOBuffer* dest_buffer,
18 size_t buffer_size,
19 size_t* bytes_read,
20 const OnReadCompleteCallback& callback) {
21 DCHECK(!awaiting_completion_);
22 if (results_.empty()) {
23 *bytes_read = 0;
24 return OK;
25 }
26
27 QueuedResult r = results_.front();
28 if (!r.sync) {
29 awaiting_completion_ = true;
30 dest_buffer_ = dest_buffer;
31 dest_buffer_size_ = buffer_size;
32 callback_ = callback;
33 return ERR_IO_PENDING;
34 }
35
36 results_.pop();
37 DCHECK_GE(buffer_size, r.len);
38 memcpy(dest_buffer->data(), r.data, r.len);
39 *bytes_read = r.len;
40 return r.error;
41 }
42
43 MockStreamSource::QueuedResult::QueuedResult(const char* data,
44 size_t len,
45 Error error,
46 bool sync)
47 : data(data), len(len), error(error), sync(sync) {}
48
49 void MockStreamSource::AddReadResult(const char* data,
50 size_t len,
51 Error error,
52 bool sync) {
53 QueuedResult result(data, len, error, sync);
54 results_.push(result);
55 }
56
57 void MockStreamSource::CompleteNextRead() {
58 DCHECK(awaiting_completion_);
59 awaiting_completion_ = false;
60 QueuedResult r = results_.front();
61 DCHECK(!r.sync);
62 results_.pop();
63 DCHECK_GE(dest_buffer_size_, r.len);
64 memcpy(dest_buffer_->data(), r.data, r.len);
65 callback_.Run(r.error, r.len);
66 }
67
68 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698