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" |
19 #include "ppapi/shared_impl/time_conversion.h" | 20 #include "ppapi/shared_impl/time_conversion.h" |
20 #include "ppapi/thunk/enter.h" | 21 #include "ppapi/thunk/enter.h" |
21 #include "ppapi/thunk/ppb_file_ref_api.h" | 22 #include "ppapi/thunk/ppb_file_ref_api.h" |
22 #include "webkit/plugins/ppapi/common.h" | 23 #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; | |
34 using ppapi::thunk::PPB_FileRef_API; | 32 using ppapi::thunk::PPB_FileRef_API; |
35 | 33 |
36 namespace webkit { | 34 namespace webkit { |
37 namespace ppapi { | 35 namespace ppapi { |
38 | 36 |
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 | |
51 PPB_FileIO_Impl::PPB_FileIO_Impl(PP_Instance instance) | 37 PPB_FileIO_Impl::PPB_FileIO_Impl(PP_Instance instance) |
52 : Resource(instance), | 38 : ::ppapi::PPB_FileIO_Shared(instance), |
53 file_(base::kInvalidPlatformFileValue), | 39 file_(base::kInvalidPlatformFileValue), |
54 file_system_type_(PP_FILESYSTEMTYPE_INVALID), | |
55 pending_op_(OPERATION_NONE), | |
56 info_(NULL), | |
57 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 40 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
58 } | 41 } |
59 | 42 |
60 PPB_FileIO_Impl::~PPB_FileIO_Impl() { | 43 PPB_FileIO_Impl::~PPB_FileIO_Impl() { |
61 Close(); | 44 Close(); |
62 } | 45 } |
63 | 46 |
64 PPB_FileIO_API* PPB_FileIO_Impl::AsPPB_FileIO_API() { | 47 int32_t PPB_FileIO_Impl::OpenValidated(PP_Resource file_ref_resource, |
65 return this; | 48 PPB_FileRef_API* file_ref_api, |
66 } | 49 int32_t open_flags, |
67 | 50 PP_CompletionCallback callback) { |
68 int32_t PPB_FileIO_Impl::Open(PP_Resource pp_file_ref, | 51 PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(file_ref_api); |
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; | |
79 | 52 |
80 int flags = 0; | 53 int flags = 0; |
81 if (!PepperFileOpenFlagsToPlatformFileFlags(open_flags, &flags)) | 54 if (!::ppapi::PepperFileOpenFlagsToPlatformFileFlags(open_flags, &flags)) |
82 return PP_ERROR_BADARGUMENT; | 55 return PP_ERROR_BADARGUMENT; |
83 | 56 |
84 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | 57 PluginDelegate* plugin_delegate = GetPluginDelegate(); |
85 if (!plugin_delegate) | 58 if (!plugin_delegate) |
86 return false; | 59 return PP_ERROR_BADARGUMENT; |
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; | |
93 | 60 |
94 if (file_ref->HasValidFileSystem()) { | 61 if (file_ref->HasValidFileSystem()) { |
95 file_system_url_ = file_ref->GetFileSystemURL(); | 62 file_system_url_ = file_ref->GetFileSystemURL(); |
96 if (!plugin_delegate->AsyncOpenFileSystemURL( | 63 if (!plugin_delegate->AsyncOpenFileSystemURL( |
97 file_system_url_, flags, | 64 file_system_url_, flags, |
98 base::Bind(&PPB_FileIO_Impl::AsyncOpenFileCallback, | 65 base::Bind(&PPB_FileIO_Impl::ExecutePlatformOpenFileCallback, |
99 weak_factory_.GetWeakPtr()))) | 66 weak_factory_.GetWeakPtr()))) |
100 return PP_ERROR_FAILED; | 67 return PP_ERROR_FAILED; |
101 } else { | 68 } else { |
102 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) | 69 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) |
103 return PP_ERROR_FAILED; | 70 return PP_ERROR_FAILED; |
104 if (!plugin_delegate->AsyncOpenFile( | 71 if (!plugin_delegate->AsyncOpenFile( |
105 file_ref->GetSystemPath(), flags, | 72 file_ref->GetSystemPath(), flags, |
106 base::Bind(&PPB_FileIO_Impl::AsyncOpenFileCallback, | 73 base::Bind(&PPB_FileIO_Impl::ExecutePlatformOpenFileCallback, |
107 weak_factory_.GetWeakPtr()))) | 74 weak_factory_.GetWeakPtr()))) |
108 return PP_ERROR_FAILED; | 75 return PP_ERROR_FAILED; |
109 } | 76 } |
110 | 77 |
111 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); | 78 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); |
112 return PP_OK_COMPLETIONPENDING; | 79 return PP_OK_COMPLETIONPENDING; |
113 } | 80 } |
114 | 81 |
115 int32_t PPB_FileIO_Impl::Query(PP_FileInfo* info, | 82 int32_t PPB_FileIO_Impl::QueryValidated(PP_FileInfo* info, |
116 PP_CompletionCallback callback) { | 83 PP_CompletionCallback callback) { |
117 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 84 PluginDelegate* plugin_delegate = GetPluginDelegate(); |
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); | |
128 if (!plugin_delegate) | 85 if (!plugin_delegate) |
129 return PP_ERROR_FAILED; | 86 return PP_ERROR_FAILED; |
130 | 87 |
131 if (!base::FileUtilProxy::GetFileInfoFromPlatformFile( | 88 if (!base::FileUtilProxy::GetFileInfoFromPlatformFile( |
132 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, | 89 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, |
133 base::Bind(&PPB_FileIO_Impl::QueryInfoCallback, | 90 base::Bind(&PPB_FileIO_Impl::ExecutePlatformQueryCallback, |
134 weak_factory_.GetWeakPtr()))) | 91 weak_factory_.GetWeakPtr()))) |
135 return PP_ERROR_FAILED; | 92 return PP_ERROR_FAILED; |
136 | 93 |
137 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); | 94 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, info); |
138 return PP_OK_COMPLETIONPENDING; | 95 return PP_OK_COMPLETIONPENDING; |
139 } | 96 } |
140 | 97 |
141 int32_t PPB_FileIO_Impl::Touch(PP_Time last_access_time, | 98 int32_t PPB_FileIO_Impl::TouchValidated(PP_Time last_access_time, |
142 PP_Time last_modified_time, | 99 PP_Time last_modified_time, |
143 PP_CompletionCallback callback) { | 100 PP_CompletionCallback callback) { |
144 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 101 PluginDelegate* plugin_delegate = GetPluginDelegate(); |
145 if (rv != PP_OK) | |
146 return rv; | |
147 | |
148 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
149 if (!plugin_delegate) | 102 if (!plugin_delegate) |
150 return PP_ERROR_FAILED; | 103 return PP_ERROR_FAILED; |
151 | 104 |
152 if (!base::FileUtilProxy::Touch( | 105 if (!base::FileUtilProxy::Touch( |
153 plugin_delegate->GetFileThreadMessageLoopProxy(), | 106 plugin_delegate->GetFileThreadMessageLoopProxy(), |
154 file_, PPTimeToTime(last_access_time), | 107 file_, PPTimeToTime(last_access_time), |
155 PPTimeToTime(last_modified_time), | 108 PPTimeToTime(last_modified_time), |
156 base::Bind(&PPB_FileIO_Impl::StatusCallback, | 109 base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, |
157 weak_factory_.GetWeakPtr()))) | 110 weak_factory_.GetWeakPtr()))) |
158 return PP_ERROR_FAILED; | 111 return PP_ERROR_FAILED; |
159 | 112 |
160 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); | 113 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); |
161 return PP_OK_COMPLETIONPENDING; | 114 return PP_OK_COMPLETIONPENDING; |
162 } | 115 } |
163 | 116 |
164 int32_t PPB_FileIO_Impl::Read(int64_t offset, | 117 int32_t PPB_FileIO_Impl::ReadValidated(int64_t offset, |
165 char* buffer, | 118 char* buffer, |
166 int32_t bytes_to_read, | 119 int32_t bytes_to_read, |
167 PP_CompletionCallback callback) { | 120 PP_CompletionCallback callback) { |
168 int32_t rv = CommonCallValidation(true, OPERATION_READ, callback); | 121 PluginDelegate* plugin_delegate = GetPluginDelegate(); |
169 if (rv != PP_OK) | |
170 return rv; | |
171 | |
172 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
173 if (!plugin_delegate) | 122 if (!plugin_delegate) |
174 return PP_ERROR_FAILED; | 123 return PP_ERROR_FAILED; |
175 | 124 |
176 if (!base::FileUtilProxy::Read( | 125 if (!base::FileUtilProxy::Read( |
177 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, offset, | 126 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, offset, |
178 bytes_to_read, | 127 bytes_to_read, |
179 base::Bind(&PPB_FileIO_Impl::ReadCallback, | 128 base::Bind(&PPB_FileIO_Impl::ExecutePlatformReadCallback, |
180 weak_factory_.GetWeakPtr()))) | 129 weak_factory_.GetWeakPtr()))) |
181 return PP_ERROR_FAILED; | 130 return PP_ERROR_FAILED; |
182 | 131 |
183 RegisterCallback(OPERATION_READ, callback, buffer); | 132 RegisterCallback(OPERATION_READ, callback, buffer, NULL); |
184 return PP_OK_COMPLETIONPENDING; | 133 return PP_OK_COMPLETIONPENDING; |
185 } | 134 } |
186 | 135 |
187 int32_t PPB_FileIO_Impl::Write(int64_t offset, | 136 int32_t PPB_FileIO_Impl::WriteValidated(int64_t offset, |
188 const char* buffer, | 137 const char* buffer, |
189 int32_t bytes_to_write, | 138 int32_t bytes_to_write, |
190 PP_CompletionCallback callback) { | 139 PP_CompletionCallback callback) { |
191 int32_t rv = CommonCallValidation(true, OPERATION_WRITE, callback); | 140 PluginDelegate* plugin_delegate = GetPluginDelegate(); |
192 if (rv != PP_OK) | |
193 return rv; | |
194 | |
195 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
196 if (!plugin_delegate) | 141 if (!plugin_delegate) |
197 return PP_ERROR_FAILED; | 142 return PP_ERROR_FAILED; |
198 | 143 |
199 if (quota_file_io_.get()) { | 144 if (quota_file_io_.get()) { |
200 if (!quota_file_io_->Write( | 145 if (!quota_file_io_->Write( |
201 offset, buffer, bytes_to_write, | 146 offset, buffer, bytes_to_write, |
202 base::Bind(&PPB_FileIO_Impl::WriteCallback, | 147 base::Bind(&PPB_FileIO_Impl::ExecutePlatformWriteCallback, |
203 weak_factory_.GetWeakPtr()))) | 148 weak_factory_.GetWeakPtr()))) |
204 return PP_ERROR_FAILED; | 149 return PP_ERROR_FAILED; |
205 } else { | 150 } else { |
206 if (!base::FileUtilProxy::Write( | 151 if (!base::FileUtilProxy::Write( |
207 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, offset, | 152 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, offset, |
208 buffer, bytes_to_write, | 153 buffer, bytes_to_write, |
209 base::Bind(&PPB_FileIO_Impl::WriteCallback, | 154 base::Bind(&PPB_FileIO_Impl::ExecutePlatformWriteCallback, |
210 weak_factory_.GetWeakPtr()))) | 155 weak_factory_.GetWeakPtr()))) |
211 return PP_ERROR_FAILED; | 156 return PP_ERROR_FAILED; |
212 } | 157 } |
213 | 158 |
214 RegisterCallback(OPERATION_WRITE, callback, NULL); | 159 RegisterCallback(OPERATION_WRITE, callback, NULL, NULL); |
215 return PP_OK_COMPLETIONPENDING; | 160 return PP_OK_COMPLETIONPENDING; |
216 } | 161 } |
217 | 162 |
218 int32_t PPB_FileIO_Impl::SetLength(int64_t length, | 163 int32_t PPB_FileIO_Impl::SetLengthValidated(int64_t length, |
219 PP_CompletionCallback callback) { | 164 PP_CompletionCallback callback) { |
220 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 165 PluginDelegate* plugin_delegate = GetPluginDelegate(); |
221 if (rv != PP_OK) | |
222 return rv; | |
223 | |
224 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
225 if (!plugin_delegate) | 166 if (!plugin_delegate) |
226 return PP_ERROR_FAILED; | 167 return PP_ERROR_FAILED; |
227 | 168 |
228 if (quota_file_io_.get()) { | 169 if (quota_file_io_.get()) { |
229 if (!quota_file_io_->SetLength( | 170 if (!quota_file_io_->SetLength( |
230 length, | 171 length, |
231 base::Bind(&PPB_FileIO_Impl::StatusCallback, | 172 base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, |
232 weak_factory_.GetWeakPtr()))) | 173 weak_factory_.GetWeakPtr()))) |
233 return PP_ERROR_FAILED; | 174 return PP_ERROR_FAILED; |
234 } else { | 175 } else { |
235 if (!base::FileUtilProxy::Truncate( | 176 if (!base::FileUtilProxy::Truncate( |
236 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, length, | 177 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, length, |
237 base::Bind(&PPB_FileIO_Impl::StatusCallback, | 178 base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, |
238 weak_factory_.GetWeakPtr()))) | 179 weak_factory_.GetWeakPtr()))) |
239 return PP_ERROR_FAILED; | 180 return PP_ERROR_FAILED; |
240 } | 181 } |
241 | 182 |
242 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); | 183 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); |
243 return PP_OK_COMPLETIONPENDING; | 184 return PP_OK_COMPLETIONPENDING; |
244 } | 185 } |
245 | 186 |
246 int32_t PPB_FileIO_Impl::Flush(PP_CompletionCallback callback) { | 187 int32_t PPB_FileIO_Impl::FlushValidated(PP_CompletionCallback callback) { |
247 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 188 PluginDelegate* plugin_delegate = GetPluginDelegate(); |
248 if (rv != PP_OK) | |
249 return rv; | |
250 | |
251 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
252 if (!plugin_delegate) | 189 if (!plugin_delegate) |
253 return PP_ERROR_FAILED; | 190 return PP_ERROR_FAILED; |
254 | 191 |
255 if (!base::FileUtilProxy::Flush( | 192 if (!base::FileUtilProxy::Flush( |
256 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, | 193 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, |
257 base::Bind(&PPB_FileIO_Impl::StatusCallback, | 194 base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, |
258 weak_factory_.GetWeakPtr()))) | 195 weak_factory_.GetWeakPtr()))) |
259 return PP_ERROR_FAILED; | 196 return PP_ERROR_FAILED; |
260 | 197 |
261 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); | 198 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); |
262 return PP_OK_COMPLETIONPENDING; | 199 return PP_OK_COMPLETIONPENDING; |
263 } | 200 } |
264 | 201 |
265 void PPB_FileIO_Impl::Close() { | 202 void PPB_FileIO_Impl::Close() { |
266 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | 203 PluginDelegate* plugin_delegate = GetPluginDelegate(); |
267 if (file_ != base::kInvalidPlatformFileValue && plugin_delegate) { | 204 if (file_ != base::kInvalidPlatformFileValue && plugin_delegate) { |
268 base::FileUtilProxy::Close( | 205 base::FileUtilProxy::Close( |
269 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, | 206 plugin_delegate->GetFileThreadMessageLoopProxy(), file_, |
270 base::FileUtilProxy::StatusCallback()); | 207 base::FileUtilProxy::StatusCallback()); |
271 file_ = base::kInvalidPlatformFileValue; | 208 file_ = base::kInvalidPlatformFileValue; |
272 quota_file_io_.reset(); | 209 quota_file_io_.reset(); |
273 } | 210 } |
274 } | 211 } |
275 | 212 |
276 int32_t PPB_FileIO_Impl::GetOSFileDescriptor() { | 213 int32_t PPB_FileIO_Impl::GetOSFileDescriptor() { |
(...skipping 11 matching lines...) Expand all Loading... |
288 PP_CompletionCallback callback) { | 225 PP_CompletionCallback callback) { |
289 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 226 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); |
290 if (rv != PP_OK) | 227 if (rv != PP_OK) |
291 return rv; | 228 return rv; |
292 | 229 |
293 if (!quota_file_io_.get()) | 230 if (!quota_file_io_.get()) |
294 return PP_OK; | 231 return PP_OK; |
295 | 232 |
296 if (!quota_file_io_->WillWrite( | 233 if (!quota_file_io_->WillWrite( |
297 offset, bytes_to_write, | 234 offset, bytes_to_write, |
298 base::Bind(&PPB_FileIO_Impl::WillWriteCallback, | 235 base::Bind(&PPB_FileIO_Impl::ExecutePlatformWillWriteCallback, |
299 weak_factory_.GetWeakPtr()))) | 236 weak_factory_.GetWeakPtr()))) |
300 return PP_ERROR_FAILED; | 237 return PP_ERROR_FAILED; |
301 | 238 |
302 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); | 239 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); |
303 return PP_OK_COMPLETIONPENDING; | 240 return PP_OK_COMPLETIONPENDING; |
304 } | 241 } |
305 | 242 |
306 int32_t PPB_FileIO_Impl::WillSetLength(int64_t length, | 243 int32_t PPB_FileIO_Impl::WillSetLength(int64_t length, |
307 PP_CompletionCallback callback) { | 244 PP_CompletionCallback callback) { |
308 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | 245 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); |
309 if (rv != PP_OK) | 246 if (rv != PP_OK) |
310 return rv; | 247 return rv; |
311 | 248 |
312 if (!quota_file_io_.get()) | 249 if (!quota_file_io_.get()) |
313 return PP_OK; | 250 return PP_OK; |
314 | 251 |
315 if (!quota_file_io_->WillSetLength( | 252 if (!quota_file_io_->WillSetLength( |
316 length, | 253 length, |
317 base::Bind(&PPB_FileIO_Impl::StatusCallback, | 254 base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, |
318 weak_factory_.GetWeakPtr()))) | 255 weak_factory_.GetWeakPtr()))) |
319 return PP_ERROR_FAILED; | 256 return PP_ERROR_FAILED; |
320 | 257 |
321 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); | 258 RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); |
322 return PP_OK_COMPLETIONPENDING; | 259 return PP_OK_COMPLETIONPENDING; |
323 } | 260 } |
324 | 261 |
325 int32_t PPB_FileIO_Impl::CommonCallValidation(bool should_be_open, | 262 PluginDelegate* PPB_FileIO_Impl::GetPluginDelegate() { |
326 OperationType new_op, | 263 return ResourceHelper::GetPluginDelegate(this); |
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; | |
346 } | 264 } |
347 | 265 |
348 void PPB_FileIO_Impl::RegisterCallback(OperationType op, | 266 void PPB_FileIO_Impl::ExecutePlatformGeneralCallback( |
349 PP_CompletionCallback callback, | 267 base::PlatformFileError error_code) { |
350 char* read_buffer) { | 268 ExecuteGeneralCallback(::ppapi::PlatformFileErrorToPepperError(error_code)); |
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; | |
366 } | 269 } |
367 | 270 |
368 void PPB_FileIO_Impl::RunAndRemoveFirstPendingCallback(int32_t result) { | 271 void PPB_FileIO_Impl::ExecutePlatformOpenFileCallback( |
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( | |
389 base::PlatformFileError error_code, | 272 base::PlatformFileError error_code, |
390 base::PassPlatformFile file) { | 273 base::PassPlatformFile file) { |
391 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { | |
392 NOTREACHED(); | |
393 return; | |
394 } | |
395 | |
396 DCHECK(file_ == base::kInvalidPlatformFileValue); | 274 DCHECK(file_ == base::kInvalidPlatformFileValue); |
397 file_ = file.ReleaseValue(); | 275 file_ = file.ReleaseValue(); |
398 | 276 |
399 DCHECK(!quota_file_io_.get()); | 277 DCHECK(!quota_file_io_.get()); |
400 if (file_ != base::kInvalidPlatformFileValue && | 278 if (file_ != base::kInvalidPlatformFileValue && |
401 (file_system_type_ == PP_FILESYSTEMTYPE_LOCALTEMPORARY || | 279 (file_system_type_ == PP_FILESYSTEMTYPE_LOCALTEMPORARY || |
402 file_system_type_ == PP_FILESYSTEMTYPE_LOCALPERSISTENT)) { | 280 file_system_type_ == PP_FILESYSTEMTYPE_LOCALPERSISTENT)) { |
403 quota_file_io_.reset(new QuotaFileIO( | 281 quota_file_io_.reset(new QuotaFileIO( |
404 pp_instance(), file_, file_system_url_, file_system_type_)); | 282 pp_instance(), file_, file_system_url_, file_system_type_)); |
405 } | 283 } |
406 | 284 |
407 RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code)); | 285 ExecuteOpenFileCallback(::ppapi::PlatformFileErrorToPepperError(error_code)); |
408 } | 286 } |
409 | 287 |
410 void PPB_FileIO_Impl::QueryInfoCallback( | 288 void PPB_FileIO_Impl::ExecutePlatformQueryCallback( |
411 base::PlatformFileError error_code, | 289 base::PlatformFileError error_code, |
412 const base::PlatformFileInfo& file_info) { | 290 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) { |
413 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { | 325 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { |
414 NOTREACHED(); | 326 NOTREACHED(); |
415 return; | 327 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; | |
477 } | 328 } |
478 | 329 |
479 if (error_code != base::PLATFORM_FILE_OK) { | 330 if (error_code != base::PLATFORM_FILE_OK) { |
480 RunAndRemoveFirstPendingCallback( | 331 RunAndRemoveFirstPendingCallback( |
481 PlatformFileErrorToPepperError(error_code)); | 332 ::ppapi::PlatformFileErrorToPepperError(error_code)); |
482 } else { | 333 } else { |
483 RunAndRemoveFirstPendingCallback(bytes_written); | 334 RunAndRemoveFirstPendingCallback(bytes_written); |
484 } | 335 } |
485 } | 336 } |
486 | 337 |
487 } // namespace ppapi | 338 } // namespace ppapi |
488 } // namespace webkit | 339 } // namespace webkit |
OLD | NEW |