| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ppapi/shared_impl/ppb_file_io_shared.h" | 5 #include "ppapi/shared_impl/ppb_file_io_shared.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 PPB_FileIO_Shared::~PPB_FileIO_Shared() { | 52 PPB_FileIO_Shared::~PPB_FileIO_Shared() { |
| 53 } | 53 } |
| 54 | 54 |
| 55 thunk::PPB_FileIO_API* PPB_FileIO_Shared::AsPPB_FileIO_API() { | 55 thunk::PPB_FileIO_API* PPB_FileIO_Shared::AsPPB_FileIO_API() { |
| 56 return this; | 56 return this; |
| 57 } | 57 } |
| 58 | 58 |
| 59 int32_t PPB_FileIO_Shared::Open(PP_Resource file_ref, | 59 int32_t PPB_FileIO_Shared::Open(PP_Resource file_ref, |
| 60 int32_t open_flags, | 60 int32_t open_flags, |
| 61 PP_CompletionCallback callback) { | 61 scoped_refptr<TrackedCallback> callback) { |
| 62 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true); | 62 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true); |
| 63 if (enter.failed()) | 63 if (enter.failed()) |
| 64 return PP_ERROR_BADRESOURCE; | 64 return PP_ERROR_BADRESOURCE; |
| 65 | 65 |
| 66 int32_t rv = CommonCallValidation(false, OPERATION_EXCLUSIVE, callback); | 66 int32_t rv = CommonCallValidation(false, OPERATION_EXCLUSIVE); |
| 67 if (rv != PP_OK) | 67 if (rv != PP_OK) |
| 68 return rv; | 68 return rv; |
| 69 | 69 |
| 70 PP_FileSystemType type = enter.object()->GetFileSystemType(); | 70 PP_FileSystemType type = enter.object()->GetFileSystemType(); |
| 71 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && | 71 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && |
| 72 type != PP_FILESYSTEMTYPE_LOCALTEMPORARY && | 72 type != PP_FILESYSTEMTYPE_LOCALTEMPORARY && |
| 73 type != PP_FILESYSTEMTYPE_EXTERNAL) | 73 type != PP_FILESYSTEMTYPE_EXTERNAL) |
| 74 return PP_ERROR_FAILED; | 74 return PP_ERROR_FAILED; |
| 75 file_system_type_ = type; | 75 file_system_type_ = type; |
| 76 | 76 |
| 77 return OpenValidated(file_ref, enter.object(), open_flags, callback); | 77 return OpenValidated(file_ref, enter.object(), open_flags, callback); |
| 78 } | 78 } |
| 79 | 79 |
| 80 int32_t PPB_FileIO_Shared::Query(PP_FileInfo* info, | 80 int32_t PPB_FileIO_Shared::Query(PP_FileInfo* info, |
| 81 PP_CompletionCallback callback) { | 81 scoped_refptr<TrackedCallback> callback) { |
| 82 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 82 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); |
| 83 if (rv != PP_OK) | 83 if (rv != PP_OK) |
| 84 return rv; | 84 return rv; |
| 85 if (!info) | 85 if (!info) |
| 86 return PP_ERROR_BADARGUMENT; | 86 return PP_ERROR_BADARGUMENT; |
| 87 return QueryValidated(info, callback); | 87 return QueryValidated(info, callback); |
| 88 } | 88 } |
| 89 | 89 |
| 90 int32_t PPB_FileIO_Shared::Touch(PP_Time last_access_time, | 90 int32_t PPB_FileIO_Shared::Touch(PP_Time last_access_time, |
| 91 PP_Time last_modified_time, | 91 PP_Time last_modified_time, |
| 92 PP_CompletionCallback callback) { | 92 scoped_refptr<TrackedCallback> callback) { |
| 93 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 93 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); |
| 94 if (rv != PP_OK) | 94 if (rv != PP_OK) |
| 95 return rv; | 95 return rv; |
| 96 return TouchValidated(last_access_time, last_modified_time, callback); | 96 return TouchValidated(last_access_time, last_modified_time, callback); |
| 97 } | 97 } |
| 98 | 98 |
| 99 int32_t PPB_FileIO_Shared::Read(int64_t offset, | 99 int32_t PPB_FileIO_Shared::Read(int64_t offset, |
| 100 char* buffer, | 100 char* buffer, |
| 101 int32_t bytes_to_read, | 101 int32_t bytes_to_read, |
| 102 PP_CompletionCallback callback) { | 102 scoped_refptr<TrackedCallback> callback) { |
| 103 int32_t rv = CommonCallValidation(true, OPERATION_READ, callback); | 103 int32_t rv = CommonCallValidation(true, OPERATION_READ); |
| 104 if (rv != PP_OK) | 104 if (rv != PP_OK) |
| 105 return rv; | 105 return rv; |
| 106 return ReadValidated(offset, buffer, bytes_to_read, callback); | 106 return ReadValidated(offset, buffer, bytes_to_read, callback); |
| 107 } | 107 } |
| 108 | 108 |
| 109 int32_t PPB_FileIO_Shared::Write(int64_t offset, | 109 int32_t PPB_FileIO_Shared::Write(int64_t offset, |
| 110 const char* buffer, | 110 const char* buffer, |
| 111 int32_t bytes_to_write, | 111 int32_t bytes_to_write, |
| 112 PP_CompletionCallback callback) { | 112 scoped_refptr<TrackedCallback> callback) { |
| 113 int32_t rv = CommonCallValidation(true, OPERATION_WRITE, callback); | 113 int32_t rv = CommonCallValidation(true, OPERATION_WRITE); |
| 114 if (rv != PP_OK) | 114 if (rv != PP_OK) |
| 115 return rv; | 115 return rv; |
| 116 return WriteValidated(offset, buffer, bytes_to_write, callback); | 116 return WriteValidated(offset, buffer, bytes_to_write, callback); |
| 117 } | 117 } |
| 118 | 118 |
| 119 int32_t PPB_FileIO_Shared::SetLength(int64_t length, | 119 int32_t PPB_FileIO_Shared::SetLength(int64_t length, |
| 120 PP_CompletionCallback callback) { | 120 scoped_refptr<TrackedCallback> callback) { |
| 121 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 121 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); |
| 122 if (rv != PP_OK) | 122 if (rv != PP_OK) |
| 123 return rv; | 123 return rv; |
| 124 return SetLengthValidated(length, callback); | 124 return SetLengthValidated(length, callback); |
| 125 } | 125 } |
| 126 | 126 |
| 127 int32_t PPB_FileIO_Shared::Flush(PP_CompletionCallback callback) { | 127 int32_t PPB_FileIO_Shared::Flush(scoped_refptr<TrackedCallback> callback) { |
| 128 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 128 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE); |
| 129 if (rv != PP_OK) | 129 if (rv != PP_OK) |
| 130 return rv; | 130 return rv; |
| 131 return FlushValidated(callback); | 131 return FlushValidated(callback); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void PPB_FileIO_Shared::ExecuteGeneralCallback(int32_t pp_error) { | 134 void PPB_FileIO_Shared::ExecuteGeneralCallback(int32_t pp_error) { |
| 135 RunAndRemoveFirstPendingCallback(pp_error); | 135 RunAndRemoveFirstPendingCallback(pp_error); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void PPB_FileIO_Shared::ExecuteOpenFileCallback(int32_t pp_error) { | 138 void PPB_FileIO_Shared::ExecuteOpenFileCallback(int32_t pp_error) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 162 // The result code contains the number of bytes if it's positive. | 162 // The result code contains the number of bytes if it's positive. |
| 163 if (pp_error > 0) { | 163 if (pp_error > 0) { |
| 164 char* read_buffer = callbacks_.front().read_buffer; | 164 char* read_buffer = callbacks_.front().read_buffer; |
| 165 DCHECK(data); | 165 DCHECK(data); |
| 166 DCHECK(read_buffer); | 166 DCHECK(read_buffer); |
| 167 memcpy(read_buffer, data, pp_error); | 167 memcpy(read_buffer, data, pp_error); |
| 168 } | 168 } |
| 169 RunAndRemoveFirstPendingCallback(pp_error); | 169 RunAndRemoveFirstPendingCallback(pp_error); |
| 170 } | 170 } |
| 171 | 171 |
| 172 int32_t PPB_FileIO_Shared::CommonCallValidation( | 172 int32_t PPB_FileIO_Shared::CommonCallValidation(bool should_be_open, |
| 173 bool should_be_open, | 173 OperationType new_op) { |
| 174 OperationType new_op, | |
| 175 PP_CompletionCallback callback) { | |
| 176 // Only asynchronous operation is supported. | 174 // Only asynchronous operation is supported. |
| 177 if (!callback.func) | |
| 178 return PP_ERROR_BLOCKS_MAIN_THREAD; | |
| 179 | |
| 180 if (should_be_open) { | 175 if (should_be_open) { |
| 181 if (!file_open_) | 176 if (!file_open_) |
| 182 return PP_ERROR_FAILED; | 177 return PP_ERROR_FAILED; |
| 183 } else { | 178 } else { |
| 184 if (file_open_) | 179 if (file_open_) |
| 185 return PP_ERROR_FAILED; | 180 return PP_ERROR_FAILED; |
| 186 } | 181 } |
| 187 | 182 |
| 188 if (pending_op_ != OPERATION_NONE && | 183 if (pending_op_ != OPERATION_NONE && |
| 189 (pending_op_ != new_op || pending_op_ == OPERATION_EXCLUSIVE)) { | 184 (pending_op_ != new_op || pending_op_ == OPERATION_EXCLUSIVE)) { |
| 190 return PP_ERROR_INPROGRESS; | 185 return PP_ERROR_INPROGRESS; |
| 191 } | 186 } |
| 192 | 187 |
| 193 return PP_OK; | 188 return PP_OK; |
| 194 } | 189 } |
| 195 | 190 |
| 196 void PPB_FileIO_Shared::RegisterCallback(OperationType op, | 191 void PPB_FileIO_Shared::RegisterCallback( |
| 197 PP_CompletionCallback callback, | 192 OperationType op, |
| 198 char* read_buffer, | 193 scoped_refptr<TrackedCallback> callback, |
| 199 PP_FileInfo* info) { | 194 char* read_buffer, |
| 200 DCHECK(callback.func); | 195 PP_FileInfo* info) { |
| 201 DCHECK(pending_op_ == OPERATION_NONE || | 196 DCHECK(pending_op_ == OPERATION_NONE || |
| 202 (pending_op_ != OPERATION_EXCLUSIVE && pending_op_ == op)); | 197 (pending_op_ != OPERATION_EXCLUSIVE && pending_op_ == op)); |
| 203 | 198 |
| 204 CallbackEntry entry; | 199 CallbackEntry entry; |
| 205 entry.callback = new TrackedCallback(this, callback); | 200 entry.callback = callback; |
| 206 entry.read_buffer = read_buffer; | 201 entry.read_buffer = read_buffer; |
| 207 entry.info = info; | 202 entry.info = info; |
| 208 callbacks_.push_back(entry); | 203 callbacks_.push_back(entry); |
| 209 | 204 |
| 210 pending_op_ = op; | 205 pending_op_ = op; |
| 211 } | 206 } |
| 212 | 207 |
| 213 void PPB_FileIO_Shared::RunAndRemoveFirstPendingCallback(int32_t result) { | 208 void PPB_FileIO_Shared::RunAndRemoveFirstPendingCallback(int32_t result) { |
| 214 DCHECK(!callbacks_.empty()); | 209 DCHECK(!callbacks_.empty()); |
| 215 | 210 |
| 216 CallbackEntry front = callbacks_.front(); | 211 CallbackEntry front = callbacks_.front(); |
| 217 callbacks_.pop_front(); | 212 callbacks_.pop_front(); |
| 218 if (callbacks_.empty()) | 213 if (callbacks_.empty()) |
| 219 pending_op_ = OPERATION_NONE; | 214 pending_op_ = OPERATION_NONE; |
| 220 | 215 |
| 221 front.callback->Run(result); | 216 front.callback->Run(result); |
| 222 } | 217 } |
| 223 | 218 |
| 224 } // namespace ppapi | 219 } // namespace ppapi |
| OLD | NEW |