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

Unified Diff: chrome/browser/chromeos/file_system_provider/fileapi/buffering_file_stream_reader.cc

Issue 318563002: [fsp] Introduce BufferingFileStreamReader to read files in bigger chunks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed. Created 6 years, 6 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: chrome/browser/chromeos/file_system_provider/fileapi/buffering_file_stream_reader.cc
diff --git a/chrome/browser/chromeos/file_system_provider/fileapi/buffering_file_stream_reader.cc b/chrome/browser/chromeos/file_system_provider/fileapi/buffering_file_stream_reader.cc
new file mode 100644
index 0000000000000000000000000000000000000000..df8c3af846bc1be07758c016d11459160d60974b
--- /dev/null
+++ b/chrome/browser/chromeos/file_system_provider/fileapi/buffering_file_stream_reader.cc
@@ -0,0 +1,102 @@
+// Copyright 2014 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 "chrome/browser/chromeos/file_system_provider/fileapi/buffering_file_stream_reader.h"
+
+#include <algorithm>
+
+#include "base/message_loop/message_loop.h"
+#include "net/base/io_buffer.h"
+#include "net/base/net_errors.h"
+
+namespace chromeos {
+namespace file_system_provider {
+
+BufferingFileStreamReader::BufferingFileStreamReader(
+ scoped_ptr<webkit_blob::FileStreamReader> file_stream_reader,
+ int buffer_size)
+ : file_stream_reader_(file_stream_reader.Pass()),
+ buffer_size_(buffer_size),
+ preloading_buffer_(new net::IOBuffer(buffer_size_)),
+ preloading_buffer_offset_(0),
+ buffered_bytes_(0),
+ weak_ptr_factory_(this) {
+}
+
+BufferingFileStreamReader::~BufferingFileStreamReader() {
+}
+
+int BufferingFileStreamReader::Read(net::IOBuffer* buffer,
+ int buffer_length,
+ const net::CompletionCallback& callback) {
+ // Return as much as available in the internal buffer. It may be less than
+ // |buffer_length|, what is valid.
+ const int bytes_read =
+ CopyFromBuffer(make_scoped_refptr(buffer), buffer_length);
+ if (bytes_read)
+ return bytes_read;
hashimoto 2014/06/06 03:36:52 How about adding here something like: if (buffer_
mtomasz 2014/06/06 04:22:56 The buffer length is not user controlled, but it i
hashimoto 2014/06/09 11:12:04 Isn't "static const size_t kMaximumMessageSize = 1
mtomasz 2014/06/09 13:51:16 You're right, of course 128MB. In such case, the e
mtomasz 2014/06/10 08:38:42 I thought about it again, and IMHO we should have
hashimoto 2014/06/10 10:43:24 If it was really a purpose of this class, having a
mtomasz 2014/06/10 12:23:55 Do you want to crash with a static assert in such
hashimoto 2014/06/11 06:01:03 Static assert failure doesn't cause a crash. It ca
mtomasz 2014/06/16 04:12:18 We can't use them then. We need to compile Chrome,
hashimoto 2014/06/19 07:27:45 What I meant is that if you really care about the
mtomasz 2014/06/20 04:20:23 I already answered this question. > > > > - Requi
hashimoto 2014/06/27 02:17:58 Adding a test case is better than being the bottle
mtomasz 2014/06/27 05:00:25 TOn 2014/06/27 02:17:58, hashimoto wrote:
+
+ // Nothing copied, so contents have to be preloaded.
+ Preload(base::Bind(&BufferingFileStreamReader::OnPreloadCompleted,
+ weak_ptr_factory_.GetWeakPtr(),
+ make_scoped_refptr(buffer),
+ buffer_length,
+ callback));
+
+ return net::ERR_IO_PENDING;
+}
+
+int64 BufferingFileStreamReader::GetLength(
+ const net::Int64CompletionCallback& callback) {
+ return file_stream_reader_->GetLength(callback);
+}
+
+int BufferingFileStreamReader::CopyFromBuffer(
+ scoped_refptr<net::IOBuffer> buffer,
+ int buffer_length) {
+ const int read_bytes = std::min(buffer_length, buffered_bytes_);
+
+ memcpy(buffer->data(),
+ preloading_buffer_->data() + preloading_buffer_offset_,
+ read_bytes);
+ preloading_buffer_offset_ += read_bytes;
+ buffered_bytes_ -= read_bytes;
+
+ return read_bytes;
+}
+
+void BufferingFileStreamReader::Preload(
+ const net::CompletionCallback& callback) {
+ // TODO(mtomasz): Dynamically calculate the chunk size. Start from a small
+ // one, then increase for consecutive requests. That would improve performance
+ // when reading just small chunks, instead of the entire file.
+ const int preload_bytes = buffer_size_;
+
+ const int result =
+ file_stream_reader_->Read(preloading_buffer_, preload_bytes, callback);
+
+ if (result != net::ERR_IO_PENDING) {
+ base::MessageLoopProxy::current()->PostTask(FROM_HERE,
+ base::Bind(callback, result));
+ }
+}
+
+void BufferingFileStreamReader::OnPreloadCompleted(
+ scoped_refptr<net::IOBuffer> buffer,
+ int buffer_length,
+ const net::CompletionCallback& callback,
+ int result) {
+ if (result < 0) {
+ callback.Run(result);
+ return;
+ }
+
+ preloading_buffer_offset_ = 0;
+ buffered_bytes_ = result;
+
+ callback.Run(CopyFromBuffer(buffer, buffer_length));
+}
+
+} // namespace file_system_provider
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698