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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/file_util_proxy.h" | 10 #include "base/file_util_proxy.h" |
11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
12 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "ppapi/c/ppb_file_io.h" | 15 #include "ppapi/c/ppb_file_io.h" |
16 #include "ppapi/c/trusted/ppb_file_io_trusted.h" | 16 #include "ppapi/c/trusted/ppb_file_io_trusted.h" |
17 #include "ppapi/c/pp_completion_callback.h" | 17 #include "ppapi/c/pp_completion_callback.h" |
18 #include "ppapi/c/pp_errors.h" | 18 #include "ppapi/c/pp_errors.h" |
19 #include "ppapi/shared_impl/file_type_conversion.h" | |
20 #include "ppapi/shared_impl/time_conversion.h" | 19 #include "ppapi/shared_impl/time_conversion.h" |
21 #include "ppapi/thunk/enter.h" | 20 #include "ppapi/thunk/enter.h" |
22 #include "ppapi/thunk/ppb_file_ref_api.h" | 21 #include "ppapi/thunk/ppb_file_ref_api.h" |
23 #include "webkit/plugins/ppapi/common.h" | 22 #include "webkit/plugins/ppapi/common.h" |
| 23 #include "webkit/plugins/ppapi/file_type_conversions.h" |
24 #include "webkit/plugins/ppapi/plugin_module.h" | 24 #include "webkit/plugins/ppapi/plugin_module.h" |
25 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 25 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
26 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | 26 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
27 #include "webkit/plugins/ppapi/quota_file_io.h" | 27 #include "webkit/plugins/ppapi/quota_file_io.h" |
28 #include "webkit/plugins/ppapi/resource_helper.h" | 28 #include "webkit/plugins/ppapi/resource_helper.h" |
29 | 29 |
30 using ppapi::PPTimeToTime; | 30 using ppapi::PPTimeToTime; |
31 using ppapi::TimeToPPTime; | 31 using ppapi::TimeToPPTime; |
| 32 using ppapi::thunk::EnterResourceNoLock; |
| 33 using ppapi::thunk::PPB_FileIO_API; |
32 using ppapi::thunk::PPB_FileRef_API; | 34 using ppapi::thunk::PPB_FileRef_API; |
33 | 35 |
34 namespace webkit { | 36 namespace webkit { |
35 namespace ppapi { | 37 namespace ppapi { |
36 | 38 |
| 39 PPB_FileIO_Impl::CallbackEntry::CallbackEntry() |
| 40 : read_buffer(NULL) { |
| 41 } |
| 42 |
| 43 PPB_FileIO_Impl::CallbackEntry::CallbackEntry(const CallbackEntry& entry) |
| 44 : callback(entry.callback), |
| 45 read_buffer(entry.read_buffer) { |
| 46 } |
| 47 |
| 48 PPB_FileIO_Impl::CallbackEntry::~CallbackEntry() { |
| 49 } |
| 50 |
37 PPB_FileIO_Impl::PPB_FileIO_Impl(PP_Instance instance) | 51 PPB_FileIO_Impl::PPB_FileIO_Impl(PP_Instance instance) |
38 : ::ppapi::PPB_FileIO_Shared(instance), | 52 : Resource(instance), |
39 file_(base::kInvalidPlatformFileValue), | 53 file_(base::kInvalidPlatformFileValue), |
| 54 file_system_type_(PP_FILESYSTEMTYPE_INVALID), |
| 55 pending_op_(OPERATION_NONE), |
| 56 info_(NULL), |
40 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 57 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
41 } | 58 } |
42 | 59 |
43 PPB_FileIO_Impl::~PPB_FileIO_Impl() { | 60 PPB_FileIO_Impl::~PPB_FileIO_Impl() { |
44 Close(); | 61 Close(); |
45 } | 62 } |
46 | 63 |
47 int32_t PPB_FileIO_Impl::OpenValidated(PP_Resource file_ref_resource, | 64 PPB_FileIO_API* PPB_FileIO_Impl::AsPPB_FileIO_API() { |
48 PPB_FileRef_API* file_ref_api, | 65 return this; |
49 int32_t open_flags, | 66 } |
50 PP_CompletionCallback callback) { | 67 |
51 PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(file_ref_api); | 68 int32_t PPB_FileIO_Impl::Open(PP_Resource pp_file_ref, |
| 69 int32_t open_flags, |
| 70 PP_CompletionCallback callback) { |
| 71 EnterResourceNoLock<PPB_FileRef_API> enter(pp_file_ref, true); |
| 72 if (enter.failed()) |
| 73 return PP_ERROR_BADRESOURCE; |
| 74 PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(enter.object()); |
| 75 |
| 76 int32_t rv = CommonCallValidation(false, OPERATION_EXCLUSIVE, callback); |
| 77 if (rv != PP_OK) |
| 78 return rv; |
52 | 79 |
53 int flags = 0; | 80 int flags = 0; |
54 if (!::ppapi::PepperFileOpenFlagsToPlatformFileFlags(open_flags, &flags)) | 81 if (!PepperFileOpenFlagsToPlatformFileFlags(open_flags, &flags)) |
55 return PP_ERROR_BADARGUMENT; | 82 return PP_ERROR_BADARGUMENT; |
56 | 83 |
57 PluginDelegate* plugin_delegate = GetPluginDelegate(); | 84 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
58 if (!plugin_delegate) | 85 if (!plugin_delegate) |
59 return PP_ERROR_BADARGUMENT; | 86 return false; |
| 87 |
| 88 file_system_type_ = file_ref->GetFileSystemType(); |
| 89 if (file_system_type_ != PP_FILESYSTEMTYPE_LOCALPERSISTENT && |
| 90 file_system_type_ != PP_FILESYSTEMTYPE_LOCALTEMPORARY && |
| 91 file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) |
| 92 return PP_ERROR_FAILED; |
60 | 93 |
61 if (file_ref->HasValidFileSystem()) { | 94 if (file_ref->HasValidFileSystem()) { |
62 file_system_url_ = file_ref->GetFileSystemURL(); | 95 file_system_url_ = file_ref->GetFileSystemURL(); |
63 if (!plugin_delegate->AsyncOpenFileSystemURL( | 96 if (!plugin_delegate->AsyncOpenFileSystemURL( |
64 file_system_url_, flags, | 97 file_system_url_, flags, |
65 base::Bind(&PPB_FileIO_Impl::ExecutePlatformOpenFileCallback, | 98 base::Bind(&PPB_FileIO_Impl::AsyncOpenFileCallback, |
66 weak_factory_.GetWeakPtr()))) | 99 weak_factory_.GetWeakPtr()))) |
67 return PP_ERROR_FAILED; | 100 return PP_ERROR_FAILED; |
68 } else { | 101 } else { |
69 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) | 102 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) |
70 return PP_ERROR_FAILED; | 103 return PP_ERROR_FAILED; |
71 if (!plugin_delegate->AsyncOpenFile( | 104 if (!plugin_delegate->AsyncOpenFile( |
72 file_ref->GetSystemPath(), flags, | 105 file_ref->GetSystemPath(), flags, |
73 base::Bind(&PPB_FileIO_Impl::ExecutePlatformOpenFileCallback, | 106 base::Bind(&PPB_FileIO_Impl::AsyncOpenFileCallback, |
74 weak_factory_.GetWeakPtr()))) | 107 weak_factory_.GetWeakPtr()))) |
75 return PP_ERROR_FAILED; | 108 return PP_ERROR_FAILED; |
76 } | 109 } |
77 | 110 |
78 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); | 111 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
79 return PP_OK_COMPLETIONPENDING; | 112 return PP_OK_COMPLETIONPENDING; |
80 } | 113 } |
81 | 114 |
82 int32_t PPB_FileIO_Impl::QueryValidated(PP_FileInfo* info, | 115 int32_t PPB_FileIO_Impl::Query(PP_FileInfo* info, |
83 PP_CompletionCallback callback) { | 116 PP_CompletionCallback callback) { |
84 PluginDelegate* plugin_delegate = GetPluginDelegate(); | 117 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); |
| 118 if (rv != PP_OK) |
| 119 return rv; |
| 120 |
| 121 if (!info) |
| 122 return PP_ERROR_BADARGUMENT; |
| 123 |
| 124 DCHECK(!info_); // If |info_|, a callback should be pending (caught above). |
| 125 info_ = info; |
| 126 |
| 127 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
85 if (!plugin_delegate) | 128 if (!plugin_delegate) |
86 return PP_ERROR_FAILED; | 129 return PP_ERROR_FAILED; |
87 | 130 |
88 if (!base::FileUtilProxy::GetFileInfoFromPlatformFile( | 131 if (!base::FileUtilProxy::GetFileInfoFromPlatformFile( |
89 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, | 132 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, |
90 base::Bind(&PPB_FileIO_Impl::ExecutePlatformQueryCallback, | 133 base::Bind(&PPB_FileIO_Impl::QueryInfoCallback, |
91 weak_factory_.GetWeakPtr()))) | 134 weak_factory_.GetWeakPtr()))) |
92 return PP_ERROR_FAILED; | 135 return PP_ERROR_FAILED; |
93 | 136 |
94 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, info); | 137 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
95 return PP_OK_COMPLETIONPENDING; | 138 return PP_OK_COMPLETIONPENDING; |
96 } | 139 } |
97 | 140 |
98 int32_t PPB_FileIO_Impl::TouchValidated(PP_Time last_access_time, | 141 int32_t PPB_FileIO_Impl::Touch(PP_Time last_access_time, |
99 PP_Time last_modified_time, | 142 PP_Time last_modified_time, |
100 PP_CompletionCallback callback) { | 143 PP_CompletionCallback callback) { |
101 PluginDelegate* plugin_delegate = GetPluginDelegate(); | 144 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); |
| 145 if (rv != PP_OK) |
| 146 return rv; |
| 147 |
| 148 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
102 if (!plugin_delegate) | 149 if (!plugin_delegate) |
103 return PP_ERROR_FAILED; | 150 return PP_ERROR_FAILED; |
104 | 151 |
105 if (!base::FileUtilProxy::Touch( | 152 if (!base::FileUtilProxy::Touch( |
106 plugin_delegate->GetFileThreadMessageLoopProxy(), | 153 plugin_delegate->GetFileThreadMessageLoopProxy(), |
107 file_, PPTimeToTime(last_access_time), | 154 file_, PPTimeToTime(last_access_time), |
108 PPTimeToTime(last_modified_time), | 155 PPTimeToTime(last_modified_time), |
109 base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, | 156 base::Bind(&PPB_FileIO_Impl::StatusCallback, |
110 weak_factory_.GetWeakPtr()))) | 157 weak_factory_.GetWeakPtr()))) |
111 return PP_ERROR_FAILED; | 158 return PP_ERROR_FAILED; |
112 | 159 |
113 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); | 160 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
114 return PP_OK_COMPLETIONPENDING; | 161 return PP_OK_COMPLETIONPENDING; |
115 } | 162 } |
116 | 163 |
117 int32_t PPB_FileIO_Impl::ReadValidated(int64_t offset, | 164 int32_t PPB_FileIO_Impl::Read(int64_t offset, |
118 char* buffer, | 165 char* buffer, |
119 int32_t bytes_to_read, | 166 int32_t bytes_to_read, |
120 PP_CompletionCallback callback) { | 167 PP_CompletionCallback callback) { |
121 PluginDelegate* plugin_delegate = GetPluginDelegate(); | 168 int32_t rv = CommonCallValidation(true, OPERATION_READ, callback); |
| 169 if (rv != PP_OK) |
| 170 return rv; |
| 171 |
| 172 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
122 if (!plugin_delegate) | 173 if (!plugin_delegate) |
123 return PP_ERROR_FAILED; | 174 return PP_ERROR_FAILED; |
124 | 175 |
125 if (!base::FileUtilProxy::Read( | 176 if (!base::FileUtilProxy::Read( |
126 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, offset, | 177 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, offset, |
127 bytes_to_read, | 178 bytes_to_read, |
128 base::Bind(&PPB_FileIO_Impl::ExecutePlatformReadCallback, | 179 base::Bind(&PPB_FileIO_Impl::ReadCallback, |
129 weak_factory_.GetWeakPtr()))) | 180 weak_factory_.GetWeakPtr()))) |
130 return PP_ERROR_FAILED; | 181 return PP_ERROR_FAILED; |
131 | 182 |
132 RegisterCallback(OPERATION_READ, callback, buffer, NULL); | 183 RegisterCallback(OPERATION_READ, callback, buffer); |
133 return PP_OK_COMPLETIONPENDING; | 184 return PP_OK_COMPLETIONPENDING; |
134 } | 185 } |
135 | 186 |
136 int32_t PPB_FileIO_Impl::WriteValidated(int64_t offset, | 187 int32_t PPB_FileIO_Impl::Write(int64_t offset, |
137 const char* buffer, | 188 const char* buffer, |
138 int32_t bytes_to_write, | 189 int32_t bytes_to_write, |
139 PP_CompletionCallback callback) { | 190 PP_CompletionCallback callback) { |
140 PluginDelegate* plugin_delegate = GetPluginDelegate(); | 191 int32_t rv = CommonCallValidation(true, OPERATION_WRITE, callback); |
| 192 if (rv != PP_OK) |
| 193 return rv; |
| 194 |
| 195 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
141 if (!plugin_delegate) | 196 if (!plugin_delegate) |
142 return PP_ERROR_FAILED; | 197 return PP_ERROR_FAILED; |
143 | 198 |
144 if (quota_file_io_.get()) { | 199 if (quota_file_io_.get()) { |
145 if (!quota_file_io_->Write( | 200 if (!quota_file_io_->Write( |
146 offset, buffer, bytes_to_write, | 201 offset, buffer, bytes_to_write, |
147 base::Bind(&PPB_FileIO_Impl::ExecutePlatformWriteCallback, | 202 base::Bind(&PPB_FileIO_Impl::WriteCallback, |
148 weak_factory_.GetWeakPtr()))) | 203 weak_factory_.GetWeakPtr()))) |
149 return PP_ERROR_FAILED; | 204 return PP_ERROR_FAILED; |
150 } else { | 205 } else { |
151 if (!base::FileUtilProxy::Write( | 206 if (!base::FileUtilProxy::Write( |
152 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, offset, | 207 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, offset, |
153 buffer, bytes_to_write, | 208 buffer, bytes_to_write, |
154 base::Bind(&PPB_FileIO_Impl::ExecutePlatformWriteCallback, | 209 base::Bind(&PPB_FileIO_Impl::WriteCallback, |
155 weak_factory_.GetWeakPtr()))) | 210 weak_factory_.GetWeakPtr()))) |
156 return PP_ERROR_FAILED; | 211 return PP_ERROR_FAILED; |
157 } | 212 } |
158 | 213 |
159 RegisterCallback(OPERATION_WRITE, callback, NULL, NULL); | 214 RegisterCallback(OPERATION_WRITE, callback, NULL); |
160 return PP_OK_COMPLETIONPENDING; | 215 return PP_OK_COMPLETIONPENDING; |
161 } | 216 } |
162 | 217 |
163 int32_t PPB_FileIO_Impl::SetLengthValidated(int64_t length, | 218 int32_t PPB_FileIO_Impl::SetLength(int64_t length, |
164 PP_CompletionCallback callback) { | 219 PP_CompletionCallback callback) { |
165 PluginDelegate* plugin_delegate = GetPluginDelegate(); | 220 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); |
| 221 if (rv != PP_OK) |
| 222 return rv; |
| 223 |
| 224 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
166 if (!plugin_delegate) | 225 if (!plugin_delegate) |
167 return PP_ERROR_FAILED; | 226 return PP_ERROR_FAILED; |
168 | 227 |
169 if (quota_file_io_.get()) { | 228 if (quota_file_io_.get()) { |
170 if (!quota_file_io_->SetLength( | 229 if (!quota_file_io_->SetLength( |
171 length, | 230 length, |
172 base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, | 231 base::Bind(&PPB_FileIO_Impl::StatusCallback, |
173 weak_factory_.GetWeakPtr()))) | 232 weak_factory_.GetWeakPtr()))) |
174 return PP_ERROR_FAILED; | 233 return PP_ERROR_FAILED; |
175 } else { | 234 } else { |
176 if (!base::FileUtilProxy::Truncate( | 235 if (!base::FileUtilProxy::Truncate( |
177 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, length, | 236 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, length, |
178 base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, | 237 base::Bind(&PPB_FileIO_Impl::StatusCallback, |
179 weak_factory_.GetWeakPtr()))) | 238 weak_factory_.GetWeakPtr()))) |
180 return PP_ERROR_FAILED; | 239 return PP_ERROR_FAILED; |
181 } | 240 } |
182 | 241 |
183 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); | 242 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
184 return PP_OK_COMPLETIONPENDING; | 243 return PP_OK_COMPLETIONPENDING; |
185 } | 244 } |
186 | 245 |
187 int32_t PPB_FileIO_Impl::FlushValidated(PP_CompletionCallback callback) { | 246 int32_t PPB_FileIO_Impl::Flush(PP_CompletionCallback callback) { |
188 PluginDelegate* plugin_delegate = GetPluginDelegate(); | 247 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); |
| 248 if (rv != PP_OK) |
| 249 return rv; |
| 250 |
| 251 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
189 if (!plugin_delegate) | 252 if (!plugin_delegate) |
190 return PP_ERROR_FAILED; | 253 return PP_ERROR_FAILED; |
191 | 254 |
192 if (!base::FileUtilProxy::Flush( | 255 if (!base::FileUtilProxy::Flush( |
193 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, | 256 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, |
194 base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, | 257 base::Bind(&PPB_FileIO_Impl::StatusCallback, |
195 weak_factory_.GetWeakPtr()))) | 258 weak_factory_.GetWeakPtr()))) |
196 return PP_ERROR_FAILED; | 259 return PP_ERROR_FAILED; |
197 | 260 |
198 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); | 261 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
199 return PP_OK_COMPLETIONPENDING; | 262 return PP_OK_COMPLETIONPENDING; |
200 } | 263 } |
201 | 264 |
202 void PPB_FileIO_Impl::Close() { | 265 void PPB_FileIO_Impl::Close() { |
203 PluginDelegate* plugin_delegate = GetPluginDelegate(); | 266 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
204 if (file_ != base::kInvalidPlatformFileValue && plugin_delegate) { | 267 if (file_ != base::kInvalidPlatformFileValue && plugin_delegate) { |
205 base::FileUtilProxy::Close( | 268 base::FileUtilProxy::Close( |
206 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, | 269 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, |
207 base::FileUtilProxy::StatusCallback()); | 270 base::FileUtilProxy::StatusCallback()); |
208 file_ = base::kInvalidPlatformFileValue; | 271 file_ = base::kInvalidPlatformFileValue; |
209 quota_file_io_.reset(); | 272 quota_file_io_.reset(); |
210 } | 273 } |
211 } | 274 } |
212 | 275 |
213 int32_t PPB_FileIO_Impl::GetOSFileDescriptor() { | 276 int32_t PPB_FileIO_Impl::GetOSFileDescriptor() { |
(...skipping 11 matching lines...) Expand all Loading... |
225 PP_CompletionCallback callback) { | 288 PP_CompletionCallback callback) { |
226 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 289 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); |
227 if (rv != PP_OK) | 290 if (rv != PP_OK) |
228 return rv; | 291 return rv; |
229 | 292 |
230 if (!quota_file_io_.get()) | 293 if (!quota_file_io_.get()) |
231 return PP_OK; | 294 return PP_OK; |
232 | 295 |
233 if (!quota_file_io_->WillWrite( | 296 if (!quota_file_io_->WillWrite( |
234 offset, bytes_to_write, | 297 offset, bytes_to_write, |
235 base::Bind(&PPB_FileIO_Impl::ExecutePlatformWillWriteCallback, | 298 base::Bind(&PPB_FileIO_Impl::WillWriteCallback, |
236 weak_factory_.GetWeakPtr()))) | 299 weak_factory_.GetWeakPtr()))) |
237 return PP_ERROR_FAILED; | 300 return PP_ERROR_FAILED; |
238 | 301 |
239 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); | 302 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
240 return PP_OK_COMPLETIONPENDING; | 303 return PP_OK_COMPLETIONPENDING; |
241 } | 304 } |
242 | 305 |
243 int32_t PPB_FileIO_Impl::WillSetLength(int64_t length, | 306 int32_t PPB_FileIO_Impl::WillSetLength(int64_t length, |
244 PP_CompletionCallback callback) { | 307 PP_CompletionCallback callback) { |
245 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 308 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); |
246 if (rv != PP_OK) | 309 if (rv != PP_OK) |
247 return rv; | 310 return rv; |
248 | 311 |
249 if (!quota_file_io_.get()) | 312 if (!quota_file_io_.get()) |
250 return PP_OK; | 313 return PP_OK; |
251 | 314 |
252 if (!quota_file_io_->WillSetLength( | 315 if (!quota_file_io_->WillSetLength( |
253 length, | 316 length, |
254 base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, | 317 base::Bind(&PPB_FileIO_Impl::StatusCallback, |
255 weak_factory_.GetWeakPtr()))) | 318 weak_factory_.GetWeakPtr()))) |
256 return PP_ERROR_FAILED; | 319 return PP_ERROR_FAILED; |
257 | 320 |
258 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); | 321 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
259 return PP_OK_COMPLETIONPENDING; | 322 return PP_OK_COMPLETIONPENDING; |
260 } | 323 } |
261 | 324 |
262 PluginDelegate* PPB_FileIO_Impl::GetPluginDelegate() { | 325 int32_t PPB_FileIO_Impl::CommonCallValidation(bool should_be_open, |
263 return ResourceHelper::GetPluginDelegate(this); | 326 OperationType new_op, |
| 327 PP_CompletionCallback callback) { |
| 328 // Only asynchronous operation is supported. |
| 329 if (!callback.func) |
| 330 return PP_ERROR_BLOCKS_MAIN_THREAD; |
| 331 |
| 332 if (should_be_open) { |
| 333 if (file_ == base::kInvalidPlatformFileValue) |
| 334 return PP_ERROR_FAILED; |
| 335 } else { |
| 336 if (file_ != base::kInvalidPlatformFileValue) |
| 337 return PP_ERROR_FAILED; |
| 338 } |
| 339 |
| 340 if (pending_op_ != OPERATION_NONE && |
| 341 (pending_op_ != new_op || pending_op_ == OPERATION_EXCLUSIVE)) { |
| 342 return PP_ERROR_INPROGRESS; |
| 343 } |
| 344 |
| 345 return PP_OK; |
264 } | 346 } |
265 | 347 |
266 void PPB_FileIO_Impl::ExecutePlatformGeneralCallback( | 348 void PPB_FileIO_Impl::RegisterCallback(OperationType op, |
267 base::PlatformFileError error_code) { | 349 PP_CompletionCallback callback, |
268 ExecuteGeneralCallback(::ppapi::PlatformFileErrorToPepperError(error_code)); | 350 char* read_buffer) { |
| 351 DCHECK(callback.func); |
| 352 DCHECK(pending_op_ == OPERATION_NONE || |
| 353 (pending_op_ != OPERATION_EXCLUSIVE && pending_op_ == op)); |
| 354 |
| 355 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); |
| 356 if (!plugin_module) |
| 357 return; |
| 358 |
| 359 CallbackEntry entry; |
| 360 entry.callback = new TrackedCompletionCallback( |
| 361 plugin_module->GetCallbackTracker(), pp_resource(), callback); |
| 362 entry.read_buffer = read_buffer; |
| 363 |
| 364 callbacks_.push(entry); |
| 365 pending_op_ = op; |
269 } | 366 } |
270 | 367 |
271 void PPB_FileIO_Impl::ExecutePlatformOpenFileCallback( | 368 void PPB_FileIO_Impl::RunAndRemoveFirstPendingCallback(int32_t result) { |
| 369 DCHECK(!callbacks_.empty()); |
| 370 |
| 371 CallbackEntry front = callbacks_.front(); |
| 372 callbacks_.pop(); |
| 373 if (callbacks_.empty()) |
| 374 pending_op_ = OPERATION_NONE; |
| 375 |
| 376 front.callback->Run(result); // Will complete abortively if necessary. |
| 377 } |
| 378 |
| 379 void PPB_FileIO_Impl::StatusCallback(base::PlatformFileError error_code) { |
| 380 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { |
| 381 NOTREACHED(); |
| 382 return; |
| 383 } |
| 384 |
| 385 RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code)); |
| 386 } |
| 387 |
| 388 void PPB_FileIO_Impl::AsyncOpenFileCallback( |
272 base::PlatformFileError error_code, | 389 base::PlatformFileError error_code, |
273 base::PassPlatformFile file) { | 390 base::PassPlatformFile file) { |
| 391 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { |
| 392 NOTREACHED(); |
| 393 return; |
| 394 } |
| 395 |
274 DCHECK(file_ == base::kInvalidPlatformFileValue); | 396 DCHECK(file_ == base::kInvalidPlatformFileValue); |
275 file_ = file.ReleaseValue(); | 397 file_ = file.ReleaseValue(); |
276 | 398 |
277 DCHECK(!quota_file_io_.get()); | 399 DCHECK(!quota_file_io_.get()); |
278 if (file_ != base::kInvalidPlatformFileValue && | 400 if (file_ != base::kInvalidPlatformFileValue && |
279 (file_system_type_ == PP_FILESYSTEMTYPE_LOCALTEMPORARY || | 401 (file_system_type_ == PP_FILESYSTEMTYPE_LOCALTEMPORARY || |
280 file_system_type_ == PP_FILESYSTEMTYPE_LOCALPERSISTENT)) { | 402 file_system_type_ == PP_FILESYSTEMTYPE_LOCALPERSISTENT)) { |
281 quota_file_io_.reset(new QuotaFileIO( | 403 quota_file_io_.reset(new QuotaFileIO( |
282 pp_instance(), file_, file_system_url_, file_system_type_)); | 404 pp_instance(), file_, file_system_url_, file_system_type_)); |
283 } | 405 } |
284 | 406 |
285 ExecuteOpenFileCallback(::ppapi::PlatformFileErrorToPepperError(error_code)); | 407 RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code)); |
286 } | 408 } |
287 | 409 |
288 void PPB_FileIO_Impl::ExecutePlatformQueryCallback( | 410 void PPB_FileIO_Impl::QueryInfoCallback( |
289 base::PlatformFileError error_code, | 411 base::PlatformFileError error_code, |
290 const base::PlatformFileInfo& file_info) { | 412 const base::PlatformFileInfo& file_info) { |
291 PP_FileInfo pp_info; | |
292 pp_info.size = file_info.size; | |
293 pp_info.creation_time = TimeToPPTime(file_info.creation_time); | |
294 pp_info.last_access_time = TimeToPPTime(file_info.last_accessed); | |
295 pp_info.last_modified_time = TimeToPPTime(file_info.last_modified); | |
296 pp_info.system_type = file_system_type_; | |
297 if (file_info.is_directory) | |
298 pp_info.type = PP_FILETYPE_DIRECTORY; | |
299 else | |
300 pp_info.type = PP_FILETYPE_REGULAR; | |
301 | |
302 ExecuteQueryCallback(::ppapi::PlatformFileErrorToPepperError(error_code), | |
303 pp_info); | |
304 } | |
305 | |
306 void PPB_FileIO_Impl::ExecutePlatformReadCallback( | |
307 base::PlatformFileError error_code, | |
308 const char* data, int bytes_read) { | |
309 // Map the error code, OK getting mapped to the # of bytes read. | |
310 int32_t pp_result = ::ppapi::PlatformFileErrorToPepperError(error_code); | |
311 pp_result = pp_result == PP_OK ? bytes_read : pp_result; | |
312 ExecuteReadCallback(pp_result, data); | |
313 } | |
314 | |
315 void PPB_FileIO_Impl::ExecutePlatformWriteCallback( | |
316 base::PlatformFileError error_code, | |
317 int bytes_written) { | |
318 int32_t pp_result = ::ppapi::PlatformFileErrorToPepperError(error_code); | |
319 ExecuteGeneralCallback(pp_result == PP_OK ? bytes_written : pp_result); | |
320 } | |
321 | |
322 void PPB_FileIO_Impl::ExecutePlatformWillWriteCallback( | |
323 base::PlatformFileError error_code, | |
324 int bytes_written) { | |
325 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { | 413 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { |
326 NOTREACHED(); | 414 NOTREACHED(); |
327 return; | 415 return; |
| 416 } |
| 417 |
| 418 DCHECK(info_); |
| 419 if (error_code == base::PLATFORM_FILE_OK) { |
| 420 info_->size = file_info.size; |
| 421 info_->creation_time = TimeToPPTime(file_info.creation_time); |
| 422 info_->last_access_time = TimeToPPTime(file_info.last_accessed); |
| 423 info_->last_modified_time = TimeToPPTime(file_info.last_modified); |
| 424 info_->system_type = file_system_type_; |
| 425 if (file_info.is_directory) |
| 426 info_->type = PP_FILETYPE_DIRECTORY; |
| 427 else |
| 428 info_->type = PP_FILETYPE_REGULAR; |
| 429 } |
| 430 info_ = NULL; |
| 431 RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code)); |
| 432 } |
| 433 |
| 434 void PPB_FileIO_Impl::ReadCallback(base::PlatformFileError error_code, |
| 435 const char* data, int bytes_read) { |
| 436 if (pending_op_ != OPERATION_READ || callbacks_.empty()) { |
| 437 NOTREACHED(); |
| 438 return; |
| 439 } |
| 440 |
| 441 char* read_buffer = callbacks_.front().read_buffer; |
| 442 DCHECK(data); |
| 443 DCHECK(read_buffer); |
| 444 |
| 445 int rv; |
| 446 if (error_code == base::PLATFORM_FILE_OK) { |
| 447 rv = bytes_read; |
| 448 if (file_ != base::kInvalidPlatformFileValue) |
| 449 memcpy(read_buffer, data, bytes_read); |
| 450 } else { |
| 451 rv = PlatformFileErrorToPepperError(error_code); |
| 452 } |
| 453 |
| 454 RunAndRemoveFirstPendingCallback(rv); |
| 455 } |
| 456 |
| 457 void PPB_FileIO_Impl::WriteCallback(base::PlatformFileError error_code, |
| 458 int bytes_written) { |
| 459 if (pending_op_ != OPERATION_WRITE || callbacks_.empty()) { |
| 460 NOTREACHED(); |
| 461 return; |
| 462 } |
| 463 |
| 464 if (error_code != base::PLATFORM_FILE_OK) { |
| 465 RunAndRemoveFirstPendingCallback( |
| 466 PlatformFileErrorToPepperError(error_code)); |
| 467 } else { |
| 468 RunAndRemoveFirstPendingCallback(bytes_written); |
| 469 } |
| 470 } |
| 471 |
| 472 void PPB_FileIO_Impl::WillWriteCallback(base::PlatformFileError error_code, |
| 473 int bytes_written) { |
| 474 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { |
| 475 NOTREACHED(); |
| 476 return; |
328 } | 477 } |
329 | 478 |
330 if (error_code != base::PLATFORM_FILE_OK) { | 479 if (error_code != base::PLATFORM_FILE_OK) { |
331 RunAndRemoveFirstPendingCallback( | 480 RunAndRemoveFirstPendingCallback( |
332 ::ppapi::PlatformFileErrorToPepperError(error_code)); | 481 PlatformFileErrorToPepperError(error_code)); |
333 } else { | 482 } else { |
334 RunAndRemoveFirstPendingCallback(bytes_written); | 483 RunAndRemoveFirstPendingCallback(bytes_written); |
335 } | 484 } |
336 } | 485 } |
337 | 486 |
338 } // namespace ppapi | 487 } // namespace ppapi |
339 } // namespace webkit | 488 } // namespace webkit |
OLD | NEW |