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

Unified 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: Created 4 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: net/filter/mock_stream_source.cc
diff --git a/net/filter/mock_stream_source.cc b/net/filter/mock_stream_source.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b2feca666dee6aa4a5ff975ecad7ac92deb59657
--- /dev/null
+++ b/net/filter/mock_stream_source.cc
@@ -0,0 +1,75 @@
+// Copyright 2015 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 "net/filter/mock_stream_source.h"
+
+namespace net {
+
+MockStreamSource::MockStreamSource()
+ : awaiting_completion_(false),
+ dest_buffer_(nullptr),
+ dest_buffer_size_(0),
+ total_bytes_output_(0) {}
+
+MockStreamSource::~MockStreamSource() {}
+
+MockStreamSource::QueuedResult::QueuedResult(const char* data,
+ size_t len,
+ Error error,
+ bool sync)
+ : data(data), len(len), error(error), sync(sync) {}
+
+void MockStreamSource::AddReadResult(const char* data,
+ size_t len,
+ Error error,
+ bool sync) {
+ QueuedResult result(data, len, error, sync);
+ results_.push(result);
+}
+
+void MockStreamSource::CompleteNextRead() {
+ DCHECK(awaiting_completion_);
+ awaiting_completion_ = false;
+ QueuedResult r = results_.front();
+ DCHECK(!r.sync);
+ results_.pop();
+ DCHECK_GE(dest_buffer_size_, r.len);
+ memcpy(dest_buffer_->data(), r.data, r.len);
+ if (r.error == OK)
+ total_bytes_output_ += r.len;
+ callback_.Run(r.error, r.len);
+}
+
+net::Error MockStreamSource::Read(IOBuffer* dest_buffer,
+ size_t buffer_size,
+ size_t* bytes_read,
+ const OnReadCompleteCallback& callback) {
+ DCHECK(!awaiting_completion_);
+ if (results_.empty()) {
+ *bytes_read = 0;
+ return OK;
+ }
+
+ QueuedResult r = results_.front();
+ if (!r.sync) {
+ awaiting_completion_ = true;
+ dest_buffer_ = dest_buffer;
+ dest_buffer_size_ = buffer_size;
+ callback_ = callback;
+ return ERR_IO_PENDING;
+ }
+
+ results_.pop();
+ DCHECK_GE(buffer_size, r.len);
+ memcpy(dest_buffer->data(), r.data, r.len);
+ *bytes_read = r.len;
+ total_bytes_output_ += r.len;
+ return r.error;
+}
+
+size_t MockStreamSource::GetBytesOutput() const {
+ return total_bytes_output_;
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698