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