Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/plugins/ppapi/ppb_file_io_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_file_io_impl.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/file_util_proxy.h" | 9 #include "base/file_util_proxy.h" |
| 10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "ppapi/c/ppb_file_io.h" | 14 #include "ppapi/c/ppb_file_io.h" |
| 15 #include "ppapi/c/trusted/ppb_file_io_trusted.h" | 15 #include "ppapi/c/trusted/ppb_file_io_trusted.h" |
| 16 #include "ppapi/c/pp_completion_callback.h" | 16 #include "ppapi/c/pp_completion_callback.h" |
| 17 #include "ppapi/c/pp_errors.h" | 17 #include "ppapi/c/pp_errors.h" |
| 18 #include "ppapi/shared_impl/time_conversion.h" | 18 #include "ppapi/shared_impl/time_conversion.h" |
| 19 #include "ppapi/thunk/enter.h" | 19 #include "ppapi/thunk/enter.h" |
| 20 #include "ppapi/thunk/ppb_file_ref_api.h" | 20 #include "ppapi/thunk/ppb_file_ref_api.h" |
| 21 #include "webkit/plugins/ppapi/common.h" | 21 #include "webkit/plugins/ppapi/common.h" |
| 22 #include "webkit/plugins/ppapi/file_type_conversions.h" | 22 #include "webkit/plugins/ppapi/file_type_conversions.h" |
| 23 #include "webkit/plugins/ppapi/plugin_module.h" | 23 #include "webkit/plugins/ppapi/plugin_module.h" |
| 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 25 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | 25 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
| 26 #include "webkit/plugins/ppapi/quota_file_io.h" | |
| 26 #include "webkit/plugins/ppapi/resource_tracker.h" | 27 #include "webkit/plugins/ppapi/resource_tracker.h" |
| 27 | 28 |
| 28 using ppapi::PPTimeToTime; | 29 using ppapi::PPTimeToTime; |
| 29 using ppapi::TimeToPPTime; | 30 using ppapi::TimeToPPTime; |
| 30 using ppapi::thunk::EnterResourceNoLock; | 31 using ppapi::thunk::EnterResourceNoLock; |
| 31 using ppapi::thunk::PPB_FileIO_API; | 32 using ppapi::thunk::PPB_FileIO_API; |
| 32 using ppapi::thunk::PPB_FileRef_API; | 33 using ppapi::thunk::PPB_FileRef_API; |
| 33 | 34 |
| 34 namespace webkit { | 35 namespace webkit { |
| 35 namespace ppapi { | 36 namespace ppapi { |
| 36 | 37 |
| 37 PPB_FileIO_Impl::CallbackEntry::CallbackEntry() | 38 PPB_FileIO_Impl::CallbackEntry::CallbackEntry() |
| 38 : read_buffer(NULL) { | 39 : read_buffer(NULL) { |
| 39 } | 40 } |
| 40 | 41 |
| 41 PPB_FileIO_Impl::CallbackEntry::CallbackEntry(const CallbackEntry& entry) | 42 PPB_FileIO_Impl::CallbackEntry::CallbackEntry(const CallbackEntry& entry) |
| 42 : callback(entry.callback), | 43 : callback(entry.callback), |
| 43 read_buffer(entry.read_buffer) { | 44 read_buffer(entry.read_buffer) { |
| 44 } | 45 } |
| 45 | 46 |
| 46 PPB_FileIO_Impl::CallbackEntry::~CallbackEntry() { | 47 PPB_FileIO_Impl::CallbackEntry::~CallbackEntry() { |
| 47 } | 48 } |
| 48 | 49 |
| 49 PPB_FileIO_Impl::PPB_FileIO_Impl(PluginInstance* instance) | 50 PPB_FileIO_Impl::PPB_FileIO_Impl(PluginInstance* instance) |
| 50 : Resource(instance), | 51 : Resource(instance), |
| 51 ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)), | 52 ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)), |
| 52 file_(base::kInvalidPlatformFileValue), | 53 file_(base::kInvalidPlatformFileValue), |
| 54 file_system_type_(PP_FILESYSTEMTYPE_INVALID), | |
| 53 pending_op_(OPERATION_NONE), | 55 pending_op_(OPERATION_NONE), |
| 54 info_(NULL) { | 56 info_(NULL) { |
| 55 } | 57 } |
| 56 | 58 |
| 57 PPB_FileIO_Impl::~PPB_FileIO_Impl() { | 59 PPB_FileIO_Impl::~PPB_FileIO_Impl() { |
| 58 Close(); | 60 Close(); |
| 59 } | 61 } |
| 60 | 62 |
| 61 PPB_FileIO_API* PPB_FileIO_Impl::AsPPB_FileIO_API() { | 63 PPB_FileIO_API* PPB_FileIO_Impl::AsPPB_FileIO_API() { |
| 62 return this; | 64 return this; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 82 switch (file_system_type_) { | 84 switch (file_system_type_) { |
| 83 case PP_FILESYSTEMTYPE_EXTERNAL: | 85 case PP_FILESYSTEMTYPE_EXTERNAL: |
| 84 if (!instance()->delegate()->AsyncOpenFile( | 86 if (!instance()->delegate()->AsyncOpenFile( |
| 85 file_ref->GetSystemPath(), flags, | 87 file_ref->GetSystemPath(), flags, |
| 86 callback_factory_.NewCallback( | 88 callback_factory_.NewCallback( |
| 87 &PPB_FileIO_Impl::AsyncOpenFileCallback))) | 89 &PPB_FileIO_Impl::AsyncOpenFileCallback))) |
| 88 return PP_ERROR_FAILED; | 90 return PP_ERROR_FAILED; |
| 89 break; | 91 break; |
| 90 case PP_FILESYSTEMTYPE_LOCALPERSISTENT: | 92 case PP_FILESYSTEMTYPE_LOCALPERSISTENT: |
| 91 case PP_FILESYSTEMTYPE_LOCALTEMPORARY: | 93 case PP_FILESYSTEMTYPE_LOCALTEMPORARY: |
| 94 file_system_url_ = file_ref->GetFileSystemURL(); | |
| 92 if (!instance()->delegate()->AsyncOpenFileSystemURL( | 95 if (!instance()->delegate()->AsyncOpenFileSystemURL( |
| 93 file_ref->GetFileSystemURL(), flags, | 96 file_system_url_, flags, |
| 94 callback_factory_.NewCallback( | 97 callback_factory_.NewCallback( |
| 95 &PPB_FileIO_Impl::AsyncOpenFileCallback))) | 98 &PPB_FileIO_Impl::AsyncOpenFileCallback))) |
| 96 return PP_ERROR_FAILED; | 99 return PP_ERROR_FAILED; |
| 97 break; | 100 break; |
| 98 default: | 101 default: |
| 99 return PP_ERROR_FAILED; | 102 return PP_ERROR_FAILED; |
| 100 } | 103 } |
| 101 | 104 |
| 102 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); | 105 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
| 103 return PP_OK_COMPLETIONPENDING; | 106 return PP_OK_COMPLETIONPENDING; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 } | 164 } |
| 162 | 165 |
| 163 int32_t PPB_FileIO_Impl::Write(int64_t offset, | 166 int32_t PPB_FileIO_Impl::Write(int64_t offset, |
| 164 const char* buffer, | 167 const char* buffer, |
| 165 int32_t bytes_to_write, | 168 int32_t bytes_to_write, |
| 166 PP_CompletionCallback callback) { | 169 PP_CompletionCallback callback) { |
| 167 int32_t rv = CommonCallValidation(true, OPERATION_WRITE, callback); | 170 int32_t rv = CommonCallValidation(true, OPERATION_WRITE, callback); |
| 168 if (rv != PP_OK) | 171 if (rv != PP_OK) |
| 169 return rv; | 172 return rv; |
| 170 | 173 |
| 171 if (!base::FileUtilProxy::Write( | 174 if (quota_file_io_.get()) { |
| 172 instance()->delegate()->GetFileThreadMessageLoopProxy(), | 175 if (!quota_file_io_->Write( |
| 173 file_, offset, buffer, bytes_to_write, | 176 offset, buffer, bytes_to_write, |
| 174 callback_factory_.NewCallback(&PPB_FileIO_Impl::WriteCallback))) | 177 callback_factory_.NewCallback(&PPB_FileIO_Impl::WriteCallback))) |
| 175 return PP_ERROR_FAILED; | 178 return PP_ERROR_FAILED; |
| 179 } else { | |
| 180 if (!base::FileUtilProxy::Write( | |
| 181 instance()->delegate()->GetFileThreadMessageLoopProxy(), | |
| 182 file_, offset, buffer, bytes_to_write, | |
| 183 callback_factory_.NewCallback(&PPB_FileIO_Impl::WriteCallback))) | |
| 184 return PP_ERROR_FAILED; | |
| 185 } | |
| 176 | 186 |
| 177 RegisterCallback(OPERATION_WRITE, callback, NULL); | 187 RegisterCallback(OPERATION_WRITE, callback, NULL); |
| 178 return PP_OK_COMPLETIONPENDING; | 188 return PP_OK_COMPLETIONPENDING; |
| 179 } | 189 } |
| 180 | 190 |
| 181 int32_t PPB_FileIO_Impl::SetLength(int64_t length, | 191 int32_t PPB_FileIO_Impl::SetLength(int64_t length, |
| 182 PP_CompletionCallback callback) { | 192 PP_CompletionCallback callback) { |
| 183 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 193 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); |
| 184 if (rv != PP_OK) | 194 if (rv != PP_OK) |
| 185 return rv; | 195 return rv; |
| 186 | 196 |
| 187 if (!base::FileUtilProxy::Truncate( | 197 if (quota_file_io_.get()) { |
| 188 instance()->delegate()->GetFileThreadMessageLoopProxy(), | 198 if (!quota_file_io_->SetLength( |
| 189 file_, length, | 199 length, |
| 190 callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback))) | 200 callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback))) |
| 191 return PP_ERROR_FAILED; | 201 return PP_ERROR_FAILED; |
| 202 } else { | |
| 203 if (!base::FileUtilProxy::Truncate( | |
| 204 instance()->delegate()->GetFileThreadMessageLoopProxy(), | |
| 205 file_, length, | |
| 206 callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback))) | |
| 207 return PP_ERROR_FAILED; | |
| 208 } | |
| 192 | 209 |
| 193 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); | 210 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
| 194 return PP_OK_COMPLETIONPENDING; | 211 return PP_OK_COMPLETIONPENDING; |
| 195 } | 212 } |
| 196 | 213 |
| 197 int32_t PPB_FileIO_Impl::Flush(PP_CompletionCallback callback) { | 214 int32_t PPB_FileIO_Impl::Flush(PP_CompletionCallback callback) { |
| 198 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 215 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); |
| 199 if (rv != PP_OK) | 216 if (rv != PP_OK) |
| 200 return rv; | 217 return rv; |
| 201 | 218 |
| 202 if (!base::FileUtilProxy::Flush( | 219 if (!base::FileUtilProxy::Flush( |
| 203 instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, | 220 instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, |
| 204 callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback))) | 221 callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback))) |
| 205 return PP_ERROR_FAILED; | 222 return PP_ERROR_FAILED; |
| 206 | 223 |
| 207 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); | 224 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
| 208 return PP_OK_COMPLETIONPENDING; | 225 return PP_OK_COMPLETIONPENDING; |
| 209 } | 226 } |
| 210 | 227 |
| 211 void PPB_FileIO_Impl::Close() { | 228 void PPB_FileIO_Impl::Close() { |
| 212 if (file_ != base::kInvalidPlatformFileValue) { | 229 if (file_ != base::kInvalidPlatformFileValue) { |
| 213 base::FileUtilProxy::Close( | 230 base::FileUtilProxy::Close( |
| 214 instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, NULL); | 231 instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, NULL); |
| 215 file_ = base::kInvalidPlatformFileValue; | 232 file_ = base::kInvalidPlatformFileValue; |
| 233 quota_file_io_.reset(); | |
| 216 } | 234 } |
| 217 } | 235 } |
| 218 | 236 |
| 219 int32_t PPB_FileIO_Impl::GetOSFileDescriptor() { | 237 int32_t PPB_FileIO_Impl::GetOSFileDescriptor() { |
| 220 #if defined(OS_POSIX) | 238 #if defined(OS_POSIX) |
| 221 return file_; | 239 return file_; |
| 222 #elif defined(OS_WIN) | 240 #elif defined(OS_WIN) |
| 223 return reinterpret_cast<uintptr_t>(file_); | 241 return reinterpret_cast<uintptr_t>(file_); |
| 224 #else | 242 #else |
| 225 #error "Platform not supported." | 243 #error "Platform not supported." |
| 226 #endif | 244 #endif |
| 227 } | 245 } |
| 228 | 246 |
| 229 int32_t PPB_FileIO_Impl::WillWrite(int64_t offset, | 247 int32_t PPB_FileIO_Impl::WillWrite(int64_t offset, |
| 230 int32_t bytes_to_write, | 248 int32_t bytes_to_write, |
| 231 PP_CompletionCallback callback) { | 249 PP_CompletionCallback callback) { |
| 232 // TODO(dumi): implement me | 250 int32_t rv = CommonCallValidation(true, OPERATION_WRITE, callback); |
|
yzshen1
2011/07/26 19:26:44
Using OPERATION_WRITE means it can be queued up in
kinuko
2011/07/27 09:33:14
Guessing how the API is supposed to be used, makin
| |
| 233 return PP_OK; | 251 if (rv != PP_OK) |
| 252 return rv; | |
| 253 | |
| 254 if (!quota_file_io_.get()) | |
| 255 return PP_OK; | |
| 256 | |
| 257 if (!quota_file_io_->WillWrite( | |
| 258 offset, bytes_to_write, | |
| 259 callback_factory_.NewCallback(&PPB_FileIO_Impl::WriteCallback))) | |
| 260 return PP_ERROR_FAILED; | |
| 261 | |
| 262 RegisterCallback(OPERATION_WRITE, callback, NULL); | |
|
yzshen1
2011/07/26 19:26:44
Please see comments on line 250.
kinuko
2011/07/27 09:33:14
Done.
| |
| 263 return PP_OK_COMPLETIONPENDING; | |
| 234 } | 264 } |
| 235 | 265 |
| 236 int32_t PPB_FileIO_Impl::WillSetLength(int64_t length, | 266 int32_t PPB_FileIO_Impl::WillSetLength(int64_t length, |
| 237 PP_CompletionCallback callback) { | 267 PP_CompletionCallback callback) { |
| 238 // TODO(dumi): implement me | 268 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); |
| 239 return PP_OK; | 269 if (rv != PP_OK) |
| 270 return rv; | |
| 271 | |
| 272 if (!quota_file_io_.get()) | |
| 273 return PP_OK; | |
| 274 | |
| 275 if (!quota_file_io_->WillSetLength( | |
| 276 length, | |
| 277 callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback))) | |
| 278 return PP_ERROR_FAILED; | |
| 279 | |
| 280 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); | |
| 281 return PP_OK_COMPLETIONPENDING; | |
| 240 } | 282 } |
| 241 | 283 |
| 242 int32_t PPB_FileIO_Impl::CommonCallValidation(bool should_be_open, | 284 int32_t PPB_FileIO_Impl::CommonCallValidation(bool should_be_open, |
| 243 OperationType new_op, | 285 OperationType new_op, |
| 244 PP_CompletionCallback callback) { | 286 PP_CompletionCallback callback) { |
| 245 // Only asynchronous operation is supported. | 287 // Only asynchronous operation is supported. |
| 246 if (!callback.func) { | 288 if (!callback.func) { |
| 247 NOTIMPLEMENTED(); | 289 NOTIMPLEMENTED(); |
| 248 return PP_ERROR_BADARGUMENT; | 290 return PP_ERROR_BADARGUMENT; |
| 249 } | 291 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 void PPB_FileIO_Impl::AsyncOpenFileCallback( | 347 void PPB_FileIO_Impl::AsyncOpenFileCallback( |
| 306 base::PlatformFileError error_code, | 348 base::PlatformFileError error_code, |
| 307 base::PassPlatformFile file) { | 349 base::PassPlatformFile file) { |
| 308 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { | 350 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { |
| 309 NOTREACHED(); | 351 NOTREACHED(); |
| 310 return; | 352 return; |
| 311 } | 353 } |
| 312 | 354 |
| 313 DCHECK(file_ == base::kInvalidPlatformFileValue); | 355 DCHECK(file_ == base::kInvalidPlatformFileValue); |
| 314 file_ = file.ReleaseValue(); | 356 file_ = file.ReleaseValue(); |
| 357 | |
| 358 DCHECK(!quota_file_io_.get()); | |
| 359 if (file_ != base::kInvalidPlatformFileValue && | |
| 360 (file_system_type_ == PP_FILESYSTEMTYPE_LOCALTEMPORARY || | |
| 361 file_system_type_ == PP_FILESYSTEMTYPE_LOCALPERSISTENT)) { | |
| 362 quota_file_io_.reset(new QuotaFileIO( | |
| 363 instance(), file_, file_system_url_, file_system_type_)); | |
| 364 } | |
| 365 | |
| 315 RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code)); | 366 RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code)); |
| 316 } | 367 } |
| 317 | 368 |
| 318 void PPB_FileIO_Impl::QueryInfoCallback( | 369 void PPB_FileIO_Impl::QueryInfoCallback( |
| 319 base::PlatformFileError error_code, | 370 base::PlatformFileError error_code, |
| 320 const base::PlatformFileInfo& file_info) { | 371 const base::PlatformFileInfo& file_info) { |
| 321 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { | 372 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { |
| 322 NOTREACHED(); | 373 NOTREACHED(); |
| 323 return; | 374 return; |
| 324 } | 375 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 372 if (error_code != base::PLATFORM_FILE_OK) { | 423 if (error_code != base::PLATFORM_FILE_OK) { |
| 373 RunAndRemoveFirstPendingCallback( | 424 RunAndRemoveFirstPendingCallback( |
| 374 PlatformFileErrorToPepperError(error_code)); | 425 PlatformFileErrorToPepperError(error_code)); |
| 375 } else { | 426 } else { |
| 376 RunAndRemoveFirstPendingCallback(bytes_written); | 427 RunAndRemoveFirstPendingCallback(bytes_written); |
| 377 } | 428 } |
| 378 } | 429 } |
| 379 | 430 |
| 380 } // namespace ppapi | 431 } // namespace ppapi |
| 381 } // namespace webkit | 432 } // namespace webkit |
| OLD | NEW |