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

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: Refactor common logic Created 4 years, 9 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::SOURCE_NONE, nullptr),
11 awaiting_completion_(false),
12 dest_buffer_(nullptr),
13 dest_buffer_size_(0) {}
14
15 MockStreamSource::~MockStreamSource() {}
16
17 MockStreamSource::QueuedResult::QueuedResult(const char* data,
18 size_t len,
19 Error error,
20 bool sync)
21 : data(data), len(len), error(error), sync(sync) {}
22
23 void MockStreamSource::AddReadResult(const char* data,
24 size_t len,
25 Error error,
26 bool sync) {
27 QueuedResult result(data, len, error, sync);
28 results_.push(result);
29 }
30
31 void MockStreamSource::CompleteNextRead() {
32 DCHECK(awaiting_completion_);
33 awaiting_completion_ = false;
34 QueuedResult r = results_.front();
35 DCHECK(!r.sync);
36 results_.pop();
37 DCHECK_GE(dest_buffer_size_, r.len);
38 memcpy(dest_buffer_->data(), r.data, r.len);
39 callback_.Run(r.error, r.len);
40 }
41
42 Error MockStreamSource::ReadInternal(IOBuffer* dest_buffer,
43 size_t buffer_size,
44 size_t* bytes_read) {
45 DCHECK(!awaiting_completion_);
46 if (results_.empty()) {
47 *bytes_read = 0;
48 return OK;
49 }
50
51 QueuedResult r = results_.front();
52 if (!r.sync) {
53 awaiting_completion_ = true;
54 dest_buffer_ = dest_buffer;
55 dest_buffer_size_ = buffer_size;
56 return ERR_IO_PENDING;
57 }
58
59 results_.pop();
60 DCHECK_GE(buffer_size, r.len);
61 memcpy(dest_buffer->data(), r.data, r.len);
62 *bytes_read = r.len;
63 return r.error;
64 }
65
66 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698