Don't preload rarely seen large images
[chromium-blink-merge.git] / storage / browser / fileapi / local_file_stream_reader.cc
blobe8473eb7568e798438e839e4f30b12456f7229c4
1 // Copyright (c) 2012 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.
5 #include "storage/browser/fileapi/local_file_stream_reader.h"
7 #include "base/files/file_util.h"
8 #include "base/files/file_util_proxy.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/task_runner.h"
12 #include "net/base/file_stream.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
16 namespace storage {
18 namespace {
20 const int kOpenFlagsForRead = base::File::FLAG_OPEN |
21 base::File::FLAG_READ |
22 base::File::FLAG_ASYNC;
24 } // namespace
26 FileStreamReader* FileStreamReader::CreateForLocalFile(
27 base::TaskRunner* task_runner,
28 const base::FilePath& file_path,
29 int64 initial_offset,
30 const base::Time& expected_modification_time) {
31 return new LocalFileStreamReader(task_runner, file_path, initial_offset,
32 expected_modification_time);
35 LocalFileStreamReader::~LocalFileStreamReader() {
38 int LocalFileStreamReader::Read(net::IOBuffer* buf, int buf_len,
39 const net::CompletionCallback& callback) {
40 DCHECK(!has_pending_open_);
41 if (stream_impl_)
42 return stream_impl_->Read(buf, buf_len, callback);
43 return Open(base::Bind(&LocalFileStreamReader::DidOpenForRead,
44 weak_factory_.GetWeakPtr(),
45 make_scoped_refptr(buf), buf_len, callback));
48 int64 LocalFileStreamReader::GetLength(
49 const net::Int64CompletionCallback& callback) {
50 const bool posted = base::FileUtilProxy::GetFileInfo(
51 task_runner_.get(),
52 file_path_,
53 base::Bind(&LocalFileStreamReader::DidGetFileInfoForGetLength,
54 weak_factory_.GetWeakPtr(),
55 callback));
56 DCHECK(posted);
57 return net::ERR_IO_PENDING;
60 LocalFileStreamReader::LocalFileStreamReader(
61 base::TaskRunner* task_runner,
62 const base::FilePath& file_path,
63 int64 initial_offset,
64 const base::Time& expected_modification_time)
65 : task_runner_(task_runner),
66 file_path_(file_path),
67 initial_offset_(initial_offset),
68 expected_modification_time_(expected_modification_time),
69 has_pending_open_(false),
70 weak_factory_(this) {}
72 int LocalFileStreamReader::Open(const net::CompletionCallback& callback) {
73 DCHECK(!has_pending_open_);
74 DCHECK(!stream_impl_.get());
75 has_pending_open_ = true;
77 // Call GetLength first to make it perform last-modified-time verification,
78 // and then call DidVerifyForOpen for do the rest.
79 return GetLength(base::Bind(&LocalFileStreamReader::DidVerifyForOpen,
80 weak_factory_.GetWeakPtr(), callback));
83 void LocalFileStreamReader::DidVerifyForOpen(
84 const net::CompletionCallback& callback,
85 int64 get_length_result) {
86 if (get_length_result < 0) {
87 callback.Run(static_cast<int>(get_length_result));
88 return;
91 stream_impl_.reset(new net::FileStream(task_runner_));
92 const int result = stream_impl_->Open(
93 file_path_, kOpenFlagsForRead,
94 base::Bind(&LocalFileStreamReader::DidOpenFileStream,
95 weak_factory_.GetWeakPtr(),
96 callback));
97 if (result != net::ERR_IO_PENDING)
98 callback.Run(result);
101 void LocalFileStreamReader::DidOpenFileStream(
102 const net::CompletionCallback& callback,
103 int result) {
104 if (result != net::OK) {
105 callback.Run(result);
106 return;
108 result = stream_impl_->Seek(
109 initial_offset_, base::Bind(&LocalFileStreamReader::DidSeekFileStream,
110 weak_factory_.GetWeakPtr(), callback));
111 if (result != net::ERR_IO_PENDING) {
112 callback.Run(result);
116 void LocalFileStreamReader::DidSeekFileStream(
117 const net::CompletionCallback& callback,
118 int64 seek_result) {
119 if (seek_result < 0) {
120 callback.Run(static_cast<int>(seek_result));
121 return;
123 if (seek_result != initial_offset_) {
124 callback.Run(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE);
125 return;
127 callback.Run(net::OK);
130 void LocalFileStreamReader::DidOpenForRead(
131 net::IOBuffer* buf,
132 int buf_len,
133 const net::CompletionCallback& callback,
134 int open_result) {
135 DCHECK(has_pending_open_);
136 has_pending_open_ = false;
137 if (open_result != net::OK) {
138 stream_impl_.reset();
139 callback.Run(open_result);
140 return;
142 DCHECK(stream_impl_.get());
143 const int read_result = stream_impl_->Read(buf, buf_len, callback);
144 if (read_result != net::ERR_IO_PENDING)
145 callback.Run(read_result);
148 void LocalFileStreamReader::DidGetFileInfoForGetLength(
149 const net::Int64CompletionCallback& callback,
150 base::File::Error error,
151 const base::File::Info& file_info) {
152 if (file_info.is_directory) {
153 callback.Run(net::ERR_FILE_NOT_FOUND);
154 return;
156 if (error != base::File::FILE_OK) {
157 callback.Run(net::FileErrorToNetError(error));
158 return;
160 if (!VerifySnapshotTime(expected_modification_time_, file_info)) {
161 callback.Run(net::ERR_UPLOAD_FILE_CHANGED);
162 return;
164 callback.Run(file_info.size);
167 } // namespace storage