Delete unused downloads page asset.
[chromium-blink-merge.git] / storage / browser / fileapi / file_system_file_stream_reader.cc
blobdc1dcce5e3e7d939dde9a4941d5dda2bba55c7c1
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/file_system_file_stream_reader.h"
7 #include "base/files/file_util_proxy.h"
8 #include "base/single_thread_task_runner.h"
9 #include "net/base/file_stream.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "storage/browser/fileapi/file_system_context.h"
13 #include "storage/browser/fileapi/file_system_operation_runner.h"
15 using storage::FileStreamReader;
17 // TODO(kinuko): Remove this temporary namespace hack after we move both
18 // blob and fileapi into content namespace.
19 namespace storage {
21 FileStreamReader* FileStreamReader::CreateForFileSystemFile(
22 storage::FileSystemContext* file_system_context,
23 const storage::FileSystemURL& url,
24 int64 initial_offset,
25 const base::Time& expected_modification_time) {
26 return new storage::FileSystemFileStreamReader(
27 file_system_context, url, initial_offset, expected_modification_time);
30 } // namespace storage
32 namespace storage {
34 namespace {
36 void ReadAdapter(base::WeakPtr<FileSystemFileStreamReader> reader,
37 net::IOBuffer* buf, int buf_len,
38 const net::CompletionCallback& callback) {
39 if (!reader.get())
40 return;
41 int rv = reader->Read(buf, buf_len, callback);
42 if (rv != net::ERR_IO_PENDING)
43 callback.Run(rv);
46 void GetLengthAdapter(base::WeakPtr<FileSystemFileStreamReader> reader,
47 const net::Int64CompletionCallback& callback) {
48 if (!reader.get())
49 return;
50 int rv = reader->GetLength(callback);
51 if (rv != net::ERR_IO_PENDING)
52 callback.Run(rv);
55 void Int64CallbackAdapter(const net::Int64CompletionCallback& callback,
56 int value) {
57 callback.Run(value);
60 } // namespace
62 FileSystemFileStreamReader::~FileSystemFileStreamReader() {
65 int FileSystemFileStreamReader::Read(
66 net::IOBuffer* buf, int buf_len,
67 const net::CompletionCallback& callback) {
68 if (local_file_reader_)
69 return local_file_reader_->Read(buf, buf_len, callback);
70 return CreateSnapshot(
71 base::Bind(&ReadAdapter, weak_factory_.GetWeakPtr(),
72 make_scoped_refptr(buf), buf_len, callback),
73 callback);
76 int64 FileSystemFileStreamReader::GetLength(
77 const net::Int64CompletionCallback& callback) {
78 if (local_file_reader_)
79 return local_file_reader_->GetLength(callback);
80 return CreateSnapshot(
81 base::Bind(&GetLengthAdapter, weak_factory_.GetWeakPtr(), callback),
82 base::Bind(&Int64CallbackAdapter, callback));
85 FileSystemFileStreamReader::FileSystemFileStreamReader(
86 FileSystemContext* file_system_context,
87 const FileSystemURL& url,
88 int64 initial_offset,
89 const base::Time& expected_modification_time)
90 : file_system_context_(file_system_context),
91 url_(url),
92 initial_offset_(initial_offset),
93 expected_modification_time_(expected_modification_time),
94 has_pending_create_snapshot_(false),
95 weak_factory_(this) {
98 int FileSystemFileStreamReader::CreateSnapshot(
99 const base::Closure& callback,
100 const net::CompletionCallback& error_callback) {
101 DCHECK(!has_pending_create_snapshot_);
102 has_pending_create_snapshot_ = true;
103 file_system_context_->operation_runner()->CreateSnapshotFile(
104 url_,
105 base::Bind(&FileSystemFileStreamReader::DidCreateSnapshot,
106 weak_factory_.GetWeakPtr(),
107 callback,
108 error_callback));
109 return net::ERR_IO_PENDING;
112 void FileSystemFileStreamReader::DidCreateSnapshot(
113 const base::Closure& callback,
114 const net::CompletionCallback& error_callback,
115 base::File::Error file_error,
116 const base::File::Info& file_info,
117 const base::FilePath& platform_path,
118 const scoped_refptr<storage::ShareableFileReference>& file_ref) {
119 DCHECK(has_pending_create_snapshot_);
120 DCHECK(!local_file_reader_.get());
121 has_pending_create_snapshot_ = false;
123 if (file_error != base::File::FILE_OK) {
124 error_callback.Run(net::FileErrorToNetError(file_error));
125 return;
128 // Keep the reference (if it's non-null) so that the file won't go away.
129 snapshot_ref_ = file_ref;
131 local_file_reader_.reset(
132 FileStreamReader::CreateForLocalFile(
133 file_system_context_->default_file_task_runner(),
134 platform_path, initial_offset_, expected_modification_time_));
136 callback.Run();
139 } // namespace storage