1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
// Copyright (c) 2012 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 "webkit/browser/chromeos/fileapi/remote_file_system_operation.h"
#include "base/bind.h"
#include "base/platform_file.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "googleurl/src/gurl.h"
#include "net/url_request/url_request.h"
#include "webkit/browser/chromeos/fileapi/remote_file_stream_writer.h"
#include "webkit/browser/fileapi/file_system_url.h"
#include "webkit/browser/fileapi/file_writer_delegate.h"
using fileapi::FileSystemURL;
namespace chromeos {
RemoteFileSystemOperation::RemoteFileSystemOperation(
scoped_refptr<fileapi::RemoteFileSystemProxyInterface> remote_proxy)
: remote_proxy_(remote_proxy),
pending_operation_(kOperationNone) {
}
RemoteFileSystemOperation::~RemoteFileSystemOperation() {
}
void RemoteFileSystemOperation::GetMetadata(const FileSystemURL& url,
const GetMetadataCallback& callback) {
DCHECK(SetPendingOperationType(kOperationGetMetadata));
remote_proxy_->GetFileInfo(url, callback);
}
void RemoteFileSystemOperation::DirectoryExists(const FileSystemURL& url,
const StatusCallback& callback) {
DCHECK(SetPendingOperationType(kOperationDirectoryExists));
remote_proxy_->GetFileInfo(url,
base::Bind(&RemoteFileSystemOperation::DidDirectoryExists,
AsWeakPtr(), callback));
}
void RemoteFileSystemOperation::FileExists(const FileSystemURL& url,
const StatusCallback& callback) {
DCHECK(SetPendingOperationType(kOperationFileExists));
remote_proxy_->GetFileInfo(url,
base::Bind(base::Bind(&RemoteFileSystemOperation::DidFileExists,
AsWeakPtr(), callback)));
}
void RemoteFileSystemOperation::ReadDirectory(const FileSystemURL& url,
const ReadDirectoryCallback& callback) {
DCHECK(SetPendingOperationType(kOperationReadDirectory));
remote_proxy_->ReadDirectory(url, callback);
}
void RemoteFileSystemOperation::Remove(const FileSystemURL& url, bool recursive,
const StatusCallback& callback) {
DCHECK(SetPendingOperationType(kOperationRemove));
remote_proxy_->Remove(url, recursive,
base::Bind(&RemoteFileSystemOperation::DidFinishFileOperation,
AsWeakPtr(), callback));
}
void RemoteFileSystemOperation::CreateDirectory(
const FileSystemURL& url, bool exclusive, bool recursive,
const StatusCallback& callback) {
DCHECK(SetPendingOperationType(kOperationCreateDirectory));
remote_proxy_->CreateDirectory(url, exclusive, recursive,
base::Bind(&RemoteFileSystemOperation::DidFinishFileOperation,
AsWeakPtr(), callback));
}
void RemoteFileSystemOperation::CreateFile(const FileSystemURL& url,
bool exclusive,
const StatusCallback& callback) {
DCHECK(SetPendingOperationType(kOperationCreateFile));
remote_proxy_->CreateFile(url, exclusive,
base::Bind(&RemoteFileSystemOperation::DidFinishFileOperation,
AsWeakPtr(), callback));
}
void RemoteFileSystemOperation::Copy(const FileSystemURL& src_url,
const FileSystemURL& dest_url,
const StatusCallback& callback) {
DCHECK(SetPendingOperationType(kOperationCopy));
remote_proxy_->Copy(src_url, dest_url,
base::Bind(&RemoteFileSystemOperation::DidFinishFileOperation,
AsWeakPtr(), callback));
}
void RemoteFileSystemOperation::Move(const FileSystemURL& src_url,
const FileSystemURL& dest_url,
const StatusCallback& callback) {
DCHECK(SetPendingOperationType(kOperationMove));
remote_proxy_->Move(src_url, dest_url,
base::Bind(&RemoteFileSystemOperation::DidFinishFileOperation,
AsWeakPtr(), callback));
}
void RemoteFileSystemOperation::Write(
const FileSystemURL& url,
scoped_ptr<fileapi::FileWriterDelegate> writer_delegate,
scoped_ptr<net::URLRequest> blob_request,
const WriteCallback& callback) {
DCHECK(SetPendingOperationType(kOperationWrite));
file_writer_delegate_ = writer_delegate.Pass();
file_writer_delegate_->Start(
blob_request.Pass(),
base::Bind(&RemoteFileSystemOperation::DidWrite, AsWeakPtr(), callback));
}
void RemoteFileSystemOperation::Truncate(const FileSystemURL& url,
int64 length,
const StatusCallback& callback) {
DCHECK(SetPendingOperationType(kOperationTruncate));
remote_proxy_->Truncate(url, length,
base::Bind(&RemoteFileSystemOperation::DidFinishFileOperation,
AsWeakPtr(), callback));
}
void RemoteFileSystemOperation::Cancel(const StatusCallback& cancel_callback) {
DCHECK(cancel_callback_.is_null());
cancel_callback_ = cancel_callback;
if (file_writer_delegate_) {
DCHECK_EQ(kOperationWrite, pending_operation_);
// This will call DidWrite() with ABORT status code.
file_writer_delegate_->Cancel();
} else {
// For truncate we have no way to cancel the inflight operation (for now).
// Let it just run and dispatch cancel callback later.
DCHECK_EQ(kOperationTruncate, pending_operation_);
}
}
void RemoteFileSystemOperation::TouchFile(const FileSystemURL& url,
const base::Time& last_access_time,
const base::Time& last_modified_time,
const StatusCallback& callback) {
DCHECK(SetPendingOperationType(kOperationTouchFile));
remote_proxy_->TouchFile(
url,
last_access_time,
last_modified_time,
base::Bind(&RemoteFileSystemOperation::DidFinishFileOperation,
AsWeakPtr(), callback));
}
void RemoteFileSystemOperation::OpenFile(const FileSystemURL& url,
int file_flags,
base::ProcessHandle peer_handle,
const OpenFileCallback& callback) {
DCHECK(SetPendingOperationType(kOperationOpenFile));
remote_proxy_->OpenFile(
url,
file_flags,
peer_handle,
base::Bind(&RemoteFileSystemOperation::DidOpenFile,
AsWeakPtr(), url, callback));
}
fileapi::LocalFileSystemOperation*
RemoteFileSystemOperation::AsLocalFileSystemOperation() {
NOTIMPLEMENTED();
return NULL;
}
void RemoteFileSystemOperation::CreateSnapshotFile(
const FileSystemURL& url,
const SnapshotFileCallback& callback) {
DCHECK(SetPendingOperationType(kOperationCreateSnapshotFile));
remote_proxy_->CreateSnapshotFile(url, callback);
}
bool RemoteFileSystemOperation::SetPendingOperationType(OperationType type) {
if (pending_operation_ != kOperationNone)
return false;
pending_operation_ = type;
return true;
}
void RemoteFileSystemOperation::DidDirectoryExists(
const StatusCallback& callback,
base::PlatformFileError rv,
const base::PlatformFileInfo& file_info) {
if (rv == base::PLATFORM_FILE_OK && !file_info.is_directory)
rv = base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY;
callback.Run(rv);
}
void RemoteFileSystemOperation::DidFileExists(
const StatusCallback& callback,
base::PlatformFileError rv,
const base::PlatformFileInfo& file_info) {
if (rv == base::PLATFORM_FILE_OK && file_info.is_directory)
rv = base::PLATFORM_FILE_ERROR_NOT_A_FILE;
callback.Run(rv);
}
void RemoteFileSystemOperation::DidWrite(
const WriteCallback& write_callback,
base::PlatformFileError rv,
int64 bytes,
FileWriterDelegate::WriteProgressStatus write_status) {
bool complete = (write_status != FileWriterDelegate::SUCCESS_IO_PENDING);
StatusCallback cancel_callback = cancel_callback_;
write_callback.Run(rv, bytes, complete);
if (!cancel_callback.is_null())
cancel_callback.Run(base::PLATFORM_FILE_OK);
}
void RemoteFileSystemOperation::DidFinishFileOperation(
const StatusCallback& callback,
base::PlatformFileError rv) {
if (!cancel_callback_.is_null()) {
DCHECK_EQ(kOperationTruncate, pending_operation_);
StatusCallback cancel_callback = cancel_callback_;
callback.Run(base::PLATFORM_FILE_ERROR_ABORT);
cancel_callback.Run(base::PLATFORM_FILE_OK);
} else {
callback.Run(rv);
}
}
void RemoteFileSystemOperation::DidOpenFile(
const fileapi::FileSystemURL& url,
const OpenFileCallback& callback,
base::PlatformFileError result,
base::PlatformFile file,
base::ProcessHandle peer_handle) {
callback.Run(
result, file,
base::Bind(&fileapi::RemoteFileSystemProxyInterface::NotifyCloseFile,
remote_proxy_, url),
peer_handle);
}
} // namespace chromeos
|