OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ppapi/shared_impl/ppb_file_io_shared.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/logging.h" | |
11 #include "base/message_loop.h" | |
12 #include "ppapi/c/pp_errors.h" | |
13 #include "ppapi/shared_impl/file_type_conversion.h" | |
14 #include "ppapi/shared_impl/time_conversion.h" | |
15 #include "ppapi/thunk/enter.h" | |
16 #include "ppapi/thunk/ppb_file_ref_api.h" | |
17 | |
18 namespace ppapi { | |
19 | |
20 using thunk::EnterResourceNoLock; | |
21 using thunk::PPB_FileIO_API; | |
22 using thunk::PPB_FileRef_API; | |
23 | |
24 PPB_FileIO_Shared::CallbackEntry::CallbackEntry() | |
25 : read_buffer(NULL), | |
26 info(NULL) { | |
27 } | |
28 | |
29 PPB_FileIO_Shared::CallbackEntry::CallbackEntry(const CallbackEntry& entry) | |
30 : callback(entry.callback), | |
31 read_buffer(entry.read_buffer), | |
32 info(entry.info) { | |
33 } | |
34 | |
35 PPB_FileIO_Shared::CallbackEntry::~CallbackEntry() { | |
36 } | |
37 | |
38 PPB_FileIO_Shared::PPB_FileIO_Shared(PP_Instance instance) | |
39 : Resource(instance), | |
40 file_system_type_(PP_FILESYSTEMTYPE_INVALID), | |
41 file_open_(false), | |
42 pending_op_(OPERATION_NONE) { | |
43 } | |
44 | |
45 PPB_FileIO_Shared::PPB_FileIO_Shared(const HostResource& host_resource) | |
46 : Resource(host_resource), | |
47 file_system_type_(PP_FILESYSTEMTYPE_INVALID), | |
48 file_open_(false), | |
49 pending_op_(OPERATION_NONE) { | |
50 } | |
51 | |
52 PPB_FileIO_Shared::~PPB_FileIO_Shared() { | |
53 // The callbacks list should have been cleared by LastPluginRefWasDeleted. | |
54 DCHECK(callbacks_.empty()); | |
55 } | |
56 | |
57 void PPB_FileIO_Shared::LastPluginRefWasDeleted() { | |
58 // Abort all pending callbacks. Do this by posting a task to avoid reentering | |
59 // the plugin's Release() call that probably deleted this object. | |
60 for (size_t i = 0; i < callbacks_.size(); i++) { | |
61 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
62 callbacks_[i].callback.func, callbacks_[i].callback.user_data, | |
63 static_cast<int32_t>(PP_ERROR_ABORTED))); | |
64 } | |
65 callbacks_.erase(callbacks_.begin(), callbacks_.end()); | |
66 } | |
67 | |
68 thunk::PPB_FileIO_API* PPB_FileIO_Shared::AsPPB_FileIO_API() { | |
69 return this; | |
70 } | |
71 | |
72 int32_t PPB_FileIO_Shared::Open(PP_Resource file_ref, | |
73 int32_t open_flags, | |
74 PP_CompletionCallback callback) { | |
75 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true); | |
76 if (enter.failed()) | |
77 return PP_ERROR_BADRESOURCE; | |
78 | |
79 int32_t rv = CommonCallValidation(false, OPERATION_EXCLUSIVE, callback); | |
80 if (rv != PP_OK) | |
81 return rv; | |
82 | |
83 PP_FileSystemType type = enter.object()->GetFileSystemType(); | |
84 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && | |
85 type != PP_FILESYSTEMTYPE_LOCALTEMPORARY && | |
86 type != PP_FILESYSTEMTYPE_EXTERNAL) | |
87 return PP_ERROR_FAILED; | |
88 file_system_type_ = type; | |
89 | |
90 return OpenValidated(file_ref, enter.object(), open_flags, callback); | |
91 } | |
92 | |
93 int32_t PPB_FileIO_Shared::Query(PP_FileInfo* info, | |
94 PP_CompletionCallback callback) { | |
95 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | |
96 if (rv != PP_OK) | |
97 return rv; | |
98 if (!info) | |
99 return PP_ERROR_BADARGUMENT; | |
100 return QueryValidated(info, callback); | |
101 } | |
102 | |
103 int32_t PPB_FileIO_Shared::Touch(PP_Time last_access_time, | |
104 PP_Time last_modified_time, | |
105 PP_CompletionCallback callback) { | |
106 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | |
107 if (rv != PP_OK) | |
108 return rv; | |
109 return TouchValidated(last_access_time, last_modified_time, callback); | |
110 } | |
111 | |
112 int32_t PPB_FileIO_Shared::Read(int64_t offset, | |
113 char* buffer, | |
114 int32_t bytes_to_read, | |
115 PP_CompletionCallback callback) { | |
116 int32_t rv = CommonCallValidation(true, OPERATION_READ, callback); | |
117 if (rv != PP_OK) | |
118 return rv; | |
119 return ReadValidated(offset, buffer, bytes_to_read, callback); | |
120 } | |
121 | |
122 int32_t PPB_FileIO_Shared::Write(int64_t offset, | |
123 const char* buffer, | |
124 int32_t bytes_to_write, | |
125 PP_CompletionCallback callback) { | |
126 int32_t rv = CommonCallValidation(true, OPERATION_WRITE, callback); | |
127 if (rv != PP_OK) | |
128 return rv; | |
129 return WriteValidated(offset, buffer, bytes_to_write, callback); | |
130 } | |
131 | |
132 int32_t PPB_FileIO_Shared::SetLength(int64_t length, | |
133 PP_CompletionCallback callback) { | |
134 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | |
135 if (rv != PP_OK) | |
136 return rv; | |
137 return SetLengthValidated(length, callback); | |
138 } | |
139 | |
140 int32_t PPB_FileIO_Shared::Flush(PP_CompletionCallback callback) { | |
141 int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); | |
142 if (rv != PP_OK) | |
143 return rv; | |
144 return FlushValidated(callback); | |
145 } | |
146 | |
147 void PPB_FileIO_Shared::ExecuteGeneralCallback(int32_t pp_error) { | |
148 RunAndRemoveFirstPendingCallback(pp_error); | |
149 } | |
150 | |
151 void PPB_FileIO_Shared::ExecuteOpenFileCallback(int32_t pp_error) { | |
152 if (pp_error == PP_OK) | |
153 file_open_ = true; | |
154 ExecuteGeneralCallback(pp_error); | |
155 } | |
156 | |
157 void PPB_FileIO_Shared::ExecuteQueryCallback(int32_t pp_error, | |
158 const PP_FileInfo& info) { | |
159 if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty() || | |
160 !callbacks_.front().info) { | |
161 NOTREACHED(); | |
162 return; | |
163 } | |
164 *callbacks_.front().info = info; | |
165 RunAndRemoveFirstPendingCallback(pp_error); | |
166 } | |
167 | |
168 void PPB_FileIO_Shared::ExecuteReadCallback(int32_t pp_error, | |
169 const char* data) { | |
170 if (pending_op_ != OPERATION_READ || callbacks_.empty()) { | |
171 NOTREACHED(); | |
172 return; | |
173 } | |
174 | |
175 char* read_buffer = callbacks_.front().read_buffer; | |
176 DCHECK(data); | |
177 DCHECK(read_buffer); | |
178 | |
179 // The result code contains the number of bytes if it's positive. | |
180 if (pp_error > 0) | |
181 memcpy(read_buffer, data, pp_error); | |
182 RunAndRemoveFirstPendingCallback(pp_error); | |
183 } | |
184 | |
185 int32_t PPB_FileIO_Shared::CommonCallValidation( | |
186 bool should_be_open, | |
187 OperationType new_op, | |
188 PP_CompletionCallback callback) { | |
189 // Only asynchronous operation is supported. | |
190 if (!callback.func) | |
191 return PP_ERROR_BLOCKS_MAIN_THREAD; | |
192 | |
193 if (should_be_open) { | |
194 if (!file_open_) | |
195 return PP_ERROR_FAILED; | |
196 } else { | |
197 if (file_open_) | |
198 return PP_ERROR_FAILED; | |
199 } | |
200 | |
201 if (pending_op_ != OPERATION_NONE && | |
202 (pending_op_ != new_op || pending_op_ == OPERATION_EXCLUSIVE)) { | |
203 return PP_ERROR_INPROGRESS; | |
204 } | |
205 | |
206 return PP_OK; | |
207 } | |
208 | |
209 void PPB_FileIO_Shared::RegisterCallback(OperationType op, | |
210 PP_CompletionCallback callback, | |
211 char* read_buffer, | |
212 PP_FileInfo* info) { | |
213 DCHECK(callback.func); | |
214 DCHECK(pending_op_ == OPERATION_NONE || | |
215 (pending_op_ != OPERATION_EXCLUSIVE && pending_op_ == op)); | |
216 | |
217 CallbackEntry entry; | |
218 entry.callback = callback; | |
219 entry.read_buffer = read_buffer; | |
220 entry.info = info; | |
221 callbacks_.push_back(entry); | |
222 | |
223 pending_op_ = op; | |
224 } | |
225 | |
226 void PPB_FileIO_Shared::RunAndRemoveFirstPendingCallback(int32_t result) { | |
227 DCHECK(!callbacks_.empty()); | |
228 | |
229 CallbackEntry front = callbacks_.front(); | |
230 callbacks_.pop_front(); | |
231 if (callbacks_.empty()) | |
232 pending_op_ = OPERATION_NONE; | |
233 | |
234 PP_RunCompletionCallback(&front.callback, result); | |
235 } | |
236 | |
237 } // namespace ppapi | |
OLD | NEW |