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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/chromeos/file_system_provider/fileapi/buffering_file_st ream_reader.h"
6
7 #include <algorithm>
8
9 #include "base/message_loop/message_loop.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12
13 namespace chromeos {
14 namespace file_system_provider {
15
16 BufferingFileStreamReader::BufferingFileStreamReader(
17 scoped_ptr<webkit_blob::FileStreamReader> file_stream_reader,
18 int buffer_size)
19 : file_stream_reader_(file_stream_reader.Pass()),
20 buffer_size_(buffer_size),
21 preloading_buffer_(new net::IOBuffer(buffer_size_)),
22 preloading_buffer_offset_(0),
23 buffered_bytes_(0),
24 weak_ptr_factory_(this) {
25 }
26
27 BufferingFileStreamReader::~BufferingFileStreamReader() {
28 }
29
30 int BufferingFileStreamReader::Read(net::IOBuffer* buffer,
31 int buffer_length,
32 const net::CompletionCallback& callback) {
33 // Return as much as available in the internal buffer. It may be less than
34 // |buffer_length|, what is valid.
35 const int bytes_read =
36 CopyFromBuffer(make_scoped_refptr(buffer), buffer_length);
37 if (bytes_read)
38 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:
39
40 // Nothing copied, so contents have to be preloaded.
41 Preload(base::Bind(&BufferingFileStreamReader::OnPreloadCompleted,
42 weak_ptr_factory_.GetWeakPtr(),
43 make_scoped_refptr(buffer),
44 buffer_length,
45 callback));
46
47 return net::ERR_IO_PENDING;
48 }
49
50 int64 BufferingFileStreamReader::GetLength(
51 const net::Int64CompletionCallback& callback) {
52 return file_stream_reader_->GetLength(callback);
53 }
54
55 int BufferingFileStreamReader::CopyFromBuffer(
56 scoped_refptr<net::IOBuffer> buffer,
57 int buffer_length) {
58 const int read_bytes = std::min(buffer_length, buffered_bytes_);
59
60 memcpy(buffer->data(),
61 preloading_buffer_->data() + preloading_buffer_offset_,
62 read_bytes);
63 preloading_buffer_offset_ += read_bytes;
64 buffered_bytes_ -= read_bytes;
65
66 return read_bytes;
67 }
68
69 void BufferingFileStreamReader::Preload(
70 const net::CompletionCallback& callback) {
71 // TODO(mtomasz): Dynamically calculate the chunk size. Start from a small
72 // one, then increase for consecutive requests. That would improve performance
73 // when reading just small chunks, instead of the entire file.
74 const int preload_bytes = buffer_size_;
75
76 const int result =
77 file_stream_reader_->Read(preloading_buffer_, preload_bytes, callback);
78
79 if (result != net::ERR_IO_PENDING) {
80 base::MessageLoopProxy::current()->PostTask(FROM_HERE,
81 base::Bind(callback, result));
82 }
83 }
84
85 void BufferingFileStreamReader::OnPreloadCompleted(
86 scoped_refptr<net::IOBuffer> buffer,
87 int buffer_length,
88 const net::CompletionCallback& callback,
89 int result) {
90 if (result < 0) {
91 callback.Run(result);
92 return;
93 }
94
95 preloading_buffer_offset_ = 0;
96 buffered_bytes_ = result;
97
98 callback.Run(CopyFromBuffer(buffer, buffer_length));
99 }
100
101 } // namespace file_system_provider
102 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698