OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "webkit/fileapi/file_system_file_stream_reader.h" | |
6 | |
7 #include "base/files/file_util_proxy.h" | |
8 #include "base/platform_file.h" | |
9 #include "base/single_thread_task_runner.h" | |
10 #include "net/base/file_stream.h" | |
11 #include "net/base/io_buffer.h" | |
12 #include "net/base/net_errors.h" | |
13 #include "webkit/blob/local_file_stream_reader.h" | |
14 #include "webkit/browser/fileapi/file_system_task_runners.h" | |
15 #include "webkit/fileapi/file_system_context.h" | |
16 #include "webkit/fileapi/file_system_operation.h" | |
17 | |
18 using webkit_blob::LocalFileStreamReader; | |
19 | |
20 namespace fileapi { | |
21 | |
22 namespace { | |
23 | |
24 void ReadAdapter(base::WeakPtr<FileSystemFileStreamReader> reader, | |
25 net::IOBuffer* buf, int buf_len, | |
26 const net::CompletionCallback& callback) { | |
27 if (!reader) | |
28 return; | |
29 int rv = reader->Read(buf, buf_len, callback); | |
30 if (rv != net::ERR_IO_PENDING) | |
31 callback.Run(rv); | |
32 } | |
33 | |
34 void GetLengthAdapter(base::WeakPtr<FileSystemFileStreamReader> reader, | |
35 const net::Int64CompletionCallback& callback) { | |
36 if (!reader) | |
37 return; | |
38 int rv = reader->GetLength(callback); | |
39 if (rv != net::ERR_IO_PENDING) | |
40 callback.Run(rv); | |
41 } | |
42 | |
43 void Int64CallbackAdapter(const net::Int64CompletionCallback& callback, | |
44 int value) { | |
45 callback.Run(value); | |
46 } | |
47 | |
48 } // namespace | |
49 | |
50 FileSystemFileStreamReader::FileSystemFileStreamReader( | |
51 FileSystemContext* file_system_context, | |
52 const FileSystemURL& url, | |
53 int64 initial_offset, | |
54 const base::Time& expected_modification_time) | |
55 : file_system_context_(file_system_context), | |
56 url_(url), | |
57 initial_offset_(initial_offset), | |
58 expected_modification_time_(expected_modification_time), | |
59 has_pending_create_snapshot_(false), | |
60 weak_factory_(this) { | |
61 } | |
62 | |
63 FileSystemFileStreamReader::~FileSystemFileStreamReader() { | |
64 } | |
65 | |
66 int FileSystemFileStreamReader::Read( | |
67 net::IOBuffer* buf, int buf_len, | |
68 const net::CompletionCallback& callback) { | |
69 if (local_file_reader_) | |
70 return local_file_reader_->Read(buf, buf_len, callback); | |
71 return CreateSnapshot( | |
72 base::Bind(&ReadAdapter, weak_factory_.GetWeakPtr(), | |
73 make_scoped_refptr(buf), buf_len, callback), | |
74 callback); | |
75 } | |
76 | |
77 int64 FileSystemFileStreamReader::GetLength( | |
78 const net::Int64CompletionCallback& callback) { | |
79 if (local_file_reader_) | |
80 return local_file_reader_->GetLength(callback); | |
81 return CreateSnapshot( | |
82 base::Bind(&GetLengthAdapter, weak_factory_.GetWeakPtr(), callback), | |
83 base::Bind(&Int64CallbackAdapter, callback)); | |
84 } | |
85 | |
86 int FileSystemFileStreamReader::CreateSnapshot( | |
87 const base::Closure& callback, | |
88 const net::CompletionCallback& error_callback) { | |
89 DCHECK(!has_pending_create_snapshot_); | |
90 base::PlatformFileError error_code; | |
91 FileSystemOperation* operation = | |
92 file_system_context_->CreateFileSystemOperation(url_, &error_code); | |
93 if (error_code != base::PLATFORM_FILE_OK) | |
94 return net::PlatformFileErrorToNetError(error_code); | |
95 has_pending_create_snapshot_ = true; | |
96 operation->CreateSnapshotFile( | |
97 url_, | |
98 base::Bind(&FileSystemFileStreamReader::DidCreateSnapshot, | |
99 weak_factory_.GetWeakPtr(), | |
100 callback, | |
101 error_callback)); | |
102 return net::ERR_IO_PENDING; | |
103 } | |
104 | |
105 void FileSystemFileStreamReader::DidCreateSnapshot( | |
106 const base::Closure& callback, | |
107 const net::CompletionCallback& error_callback, | |
108 base::PlatformFileError file_error, | |
109 const base::PlatformFileInfo& file_info, | |
110 const base::FilePath& platform_path, | |
111 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) { | |
112 DCHECK(has_pending_create_snapshot_); | |
113 DCHECK(!local_file_reader_.get()); | |
114 has_pending_create_snapshot_ = false; | |
115 | |
116 if (file_error != base::PLATFORM_FILE_OK) { | |
117 error_callback.Run(net::PlatformFileErrorToNetError(file_error)); | |
118 return; | |
119 } | |
120 | |
121 // Keep the reference (if it's non-null) so that the file won't go away. | |
122 snapshot_ref_ = file_ref; | |
123 | |
124 local_file_reader_.reset( | |
125 new LocalFileStreamReader( | |
126 file_system_context_->task_runners()->file_task_runner(), | |
127 platform_path, initial_offset_, expected_modification_time_)); | |
128 | |
129 callback.Run(); | |
130 } | |
131 | |
132 } // namespace fileapi | |
OLD | NEW |