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/proxy/ppb_file_ref_proxy.h" | 5 #include "ppapi/proxy/ppb_file_ref_proxy.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
(...skipping 29 matching lines...) Expand all Loading... | |
40 virtual PP_Resource GetParent() OVERRIDE; | 40 virtual PP_Resource GetParent() OVERRIDE; |
41 virtual int32_t MakeDirectory( | 41 virtual int32_t MakeDirectory( |
42 PP_Bool make_ancestors, | 42 PP_Bool make_ancestors, |
43 scoped_refptr<TrackedCallback> callback) OVERRIDE; | 43 scoped_refptr<TrackedCallback> callback) OVERRIDE; |
44 virtual int32_t Touch(PP_Time last_access_time, | 44 virtual int32_t Touch(PP_Time last_access_time, |
45 PP_Time last_modified_time, | 45 PP_Time last_modified_time, |
46 scoped_refptr<TrackedCallback> callback) OVERRIDE; | 46 scoped_refptr<TrackedCallback> callback) OVERRIDE; |
47 virtual int32_t Delete(scoped_refptr<TrackedCallback> callback) OVERRIDE; | 47 virtual int32_t Delete(scoped_refptr<TrackedCallback> callback) OVERRIDE; |
48 virtual int32_t Rename(PP_Resource new_file_ref, | 48 virtual int32_t Rename(PP_Resource new_file_ref, |
49 scoped_refptr<TrackedCallback> callback) OVERRIDE; | 49 scoped_refptr<TrackedCallback> callback) OVERRIDE; |
50 virtual int32_t Query(PP_FileInfo *info, | |
yzshen1
2013/03/21 17:18:11
nit: '*' should be adjacent to PP_FileInfo
teravest
2013/03/21 19:48:57
Done.
| |
51 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
50 virtual PP_Var GetAbsolutePath() OVERRIDE; | 52 virtual PP_Var GetAbsolutePath() OVERRIDE; |
51 | 53 |
52 // Executes the pending callback with the given ID. See pending_callbacks_. | 54 // Executes the pending callback with the given ID. See pending_callbacks_. |
53 void ExecuteCallback(int callback_id, int32_t result); | 55 void ExecuteCallback(int callback_id, int32_t result); |
56 void SetFileInfo(int callback_id, const PP_FileInfo &info); | |
yzshen1
2013/03/21 17:18:11
nit: '&' should be adjacent to PP_FileInfo
teravest
2013/03/21 19:48:57
Done.
| |
54 | 57 |
55 private: | 58 private: |
56 PluginDispatcher* GetDispatcher() const { | 59 PluginDispatcher* GetDispatcher() const { |
57 return PluginDispatcher::GetForResource(this); | 60 return PluginDispatcher::GetForResource(this); |
58 } | 61 } |
59 | 62 |
60 // Adds a callback to the list and returns its ID. | 63 // Adds a callback to the list and returns its ID. |
61 int SendCallback(scoped_refptr<TrackedCallback> callback); | 64 int SendCallback(scoped_refptr<TrackedCallback> callback); |
62 | 65 |
63 // This class can have any number of out-standing requests with completion | 66 // This class can have any number of out-standing requests with completion |
64 // callbacks, in contrast to most resources which have one possible pending | 67 // callbacks, in contrast to most resources which have one possible pending |
65 // callback pending (like a Flush callback). | 68 // callback pending (like a Flush callback). |
66 // | 69 // |
67 // To keep track of them, assign integer IDs to the callbacks, which is how | 70 // To keep track of them, assign integer IDs to the callbacks, which is how |
68 // the callback will be identified when it's passed to the host and then | 71 // the callback will be identified when it's passed to the host and then |
69 // back here. Use unsigned so that overflow is well-defined. | 72 // back here. Use unsigned so that overflow is well-defined. |
70 unsigned int next_callback_id_; | 73 unsigned int next_callback_id_; |
71 typedef std::map<unsigned int, | 74 typedef std::map<unsigned int, |
72 scoped_refptr<TrackedCallback> > PendingCallbackMap; | 75 scoped_refptr<TrackedCallback> > PendingCallbackMap; |
73 PendingCallbackMap pending_callbacks_; | 76 PendingCallbackMap pending_callbacks_; |
74 | 77 |
78 // Used to keep pointers to PP_FileInfo instances that are written before | |
79 // callbacks are invoked. The id of a pending file info will match that of | |
80 // the corresponding callback. | |
81 typedef std::map<unsigned int, PP_FileInfo*> PendingFileInfoMap; | |
82 PendingFileInfoMap pending_file_infos_; | |
yzshen1
2013/03/21 17:18:11
You should do cleanup like |pending_callbacks_| wh
teravest
2013/03/21 19:48:57
Done.
| |
83 | |
75 DISALLOW_IMPLICIT_CONSTRUCTORS(FileRef); | 84 DISALLOW_IMPLICIT_CONSTRUCTORS(FileRef); |
76 }; | 85 }; |
77 | 86 |
78 FileRef::FileRef(const PPB_FileRef_CreateInfo& info) | 87 FileRef::FileRef(const PPB_FileRef_CreateInfo& info) |
79 : PPB_FileRef_Shared(OBJECT_IS_PROXY, info), | 88 : PPB_FileRef_Shared(OBJECT_IS_PROXY, info), |
80 next_callback_id_(0u) { | 89 next_callback_id_(0u) { |
81 } | 90 } |
82 | 91 |
83 FileRef::~FileRef() { | 92 FileRef::~FileRef() { |
84 // The callbacks map should have been cleared by LastPluginRefWasDeleted. | 93 // The callbacks map should have been cleared by LastPluginRefWasDeleted. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 if (!new_file_ref_object || | 136 if (!new_file_ref_object || |
128 new_file_ref_object->host_resource().instance() != pp_instance()) | 137 new_file_ref_object->host_resource().instance() != pp_instance()) |
129 return PP_ERROR_BADRESOURCE; | 138 return PP_ERROR_BADRESOURCE; |
130 | 139 |
131 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Rename( | 140 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Rename( |
132 API_ID_PPB_FILE_REF, host_resource(), | 141 API_ID_PPB_FILE_REF, host_resource(), |
133 new_file_ref_object->host_resource(), SendCallback(callback))); | 142 new_file_ref_object->host_resource(), SendCallback(callback))); |
134 return PP_OK_COMPLETIONPENDING; | 143 return PP_OK_COMPLETIONPENDING; |
135 } | 144 } |
136 | 145 |
146 int32_t FileRef::Query(PP_FileInfo* info, | |
147 scoped_refptr<TrackedCallback> callback) { | |
148 // Store the pending file info id. | |
149 int id = SendCallback(callback); | |
150 pending_file_infos_[id] = info; | |
151 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Query( | |
152 API_ID_PPB_FILE_REF, host_resource(), id)); | |
153 return PP_OK_COMPLETIONPENDING; | |
154 } | |
155 | |
137 PP_Var FileRef::GetAbsolutePath() { | 156 PP_Var FileRef::GetAbsolutePath() { |
138 ReceiveSerializedVarReturnValue result; | 157 ReceiveSerializedVarReturnValue result; |
139 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_GetAbsolutePath( | 158 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_GetAbsolutePath( |
140 API_ID_PPB_FILE_REF, host_resource(), &result)); | 159 API_ID_PPB_FILE_REF, host_resource(), &result)); |
141 return result.Return(GetDispatcher()); | 160 return result.Return(GetDispatcher()); |
142 } | 161 } |
143 | 162 |
144 void FileRef::ExecuteCallback(int callback_id, int32_t result) { | 163 void FileRef::ExecuteCallback(int callback_id, int32_t result) { |
145 PendingCallbackMap::iterator found = pending_callbacks_.find(callback_id); | 164 PendingCallbackMap::iterator found = pending_callbacks_.find(callback_id); |
146 if (found == pending_callbacks_.end()) { | 165 if (found == pending_callbacks_.end()) { |
147 // This will happen when the plugin deletes its resource with a pending | 166 // This will happen when the plugin deletes its resource with a pending |
148 // callback. The callback will be locally issued with an ABORTED call while | 167 // callback. The callback will be locally issued with an ABORTED call while |
149 // the operation may still be pending in the renderer. | 168 // the operation may still be pending in the renderer. |
150 return; | 169 return; |
151 } | 170 } |
152 | 171 |
153 // Executing the callback may mutate the callback list. | 172 // Executing the callback may mutate the callback list. |
154 scoped_refptr<TrackedCallback> callback = found->second; | 173 scoped_refptr<TrackedCallback> callback = found->second; |
155 pending_callbacks_.erase(found); | 174 pending_callbacks_.erase(found); |
156 callback->Run(result); | 175 callback->Run(result); |
157 } | 176 } |
158 | 177 |
178 void FileRef::SetFileInfo(int callback_id, const PP_FileInfo &info) { | |
yzshen1
2013/03/21 17:18:11
ditto.
teravest
2013/03/21 19:48:57
Done.
| |
179 PendingFileInfoMap::iterator found = pending_file_infos_.find(callback_id); | |
180 if (found == pending_file_infos_.end()) | |
181 return; | |
182 PP_FileInfo* target_info = found->second; | |
183 *target_info = info; | |
184 pending_file_infos_.erase(found); | |
185 } | |
186 | |
159 int FileRef::SendCallback(scoped_refptr<TrackedCallback> callback) { | 187 int FileRef::SendCallback(scoped_refptr<TrackedCallback> callback) { |
160 // In extreme cases the IDs may wrap around, so avoid duplicates. | 188 // In extreme cases the IDs may wrap around, so avoid duplicates. |
161 while (pending_callbacks_.count(next_callback_id_)) | 189 while (pending_callbacks_.count(next_callback_id_)) |
162 ++next_callback_id_; | 190 ++next_callback_id_; |
163 | 191 |
164 pending_callbacks_[next_callback_id_] = callback; | 192 pending_callbacks_[next_callback_id_] = callback; |
165 return next_callback_id_++; | 193 return next_callback_id_++; |
166 } | 194 } |
167 | 195 |
168 PPB_FileRef_Proxy::PPB_FileRef_Proxy(Dispatcher* dispatcher) | 196 PPB_FileRef_Proxy::PPB_FileRef_Proxy(Dispatcher* dispatcher) |
(...skipping 24 matching lines...) Expand all Loading... | |
193 bool handled = true; | 221 bool handled = true; |
194 IPC_BEGIN_MESSAGE_MAP(PPB_FileRef_Proxy, msg) | 222 IPC_BEGIN_MESSAGE_MAP(PPB_FileRef_Proxy, msg) |
195 #if !defined(OS_NACL) | 223 #if !defined(OS_NACL) |
196 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_Create, OnMsgCreate) | 224 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_Create, OnMsgCreate) |
197 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_GetParent, OnMsgGetParent) | 225 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_GetParent, OnMsgGetParent) |
198 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_MakeDirectory, | 226 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_MakeDirectory, |
199 OnMsgMakeDirectory) | 227 OnMsgMakeDirectory) |
200 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_Touch, OnMsgTouch) | 228 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_Touch, OnMsgTouch) |
201 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_Delete, OnMsgDelete) | 229 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_Delete, OnMsgDelete) |
202 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_Rename, OnMsgRename) | 230 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_Rename, OnMsgRename) |
231 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_Query, OnMsgQuery) | |
203 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_GetAbsolutePath, | 232 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileRef_GetAbsolutePath, |
204 OnMsgGetAbsolutePath) | 233 OnMsgGetAbsolutePath) |
205 #endif // !defined(OS_NACL) | 234 #endif // !defined(OS_NACL) |
206 | 235 |
207 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileRef_CallbackComplete, | 236 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileRef_CallbackComplete, |
208 OnMsgCallbackComplete) | 237 OnMsgCallbackComplete) |
238 IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileRef_QueryCallbackComplete, | |
239 OnMsgQueryCallbackComplete) | |
209 IPC_MESSAGE_UNHANDLED(handled = false) | 240 IPC_MESSAGE_UNHANDLED(handled = false) |
210 IPC_END_MESSAGE_MAP() | 241 IPC_END_MESSAGE_MAP() |
211 return handled; | 242 return handled; |
212 } | 243 } |
213 | 244 |
214 // static | 245 // static |
215 void PPB_FileRef_Proxy::SerializeFileRef(PP_Resource file_ref, | 246 void PPB_FileRef_Proxy::SerializeFileRef(PP_Resource file_ref, |
216 PPB_FileRef_CreateInfo* result) { | 247 PPB_FileRef_CreateInfo* result) { |
217 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, false); | 248 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, false); |
218 if (enter.succeeded()) | 249 if (enter.succeeded()) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
287 int callback_id) { | 318 int callback_id) { |
288 EnterHostFromHostResourceForceCallback<PPB_FileRef_API> enter( | 319 EnterHostFromHostResourceForceCallback<PPB_FileRef_API> enter( |
289 file_ref, callback_factory_, | 320 file_ref, callback_factory_, |
290 &PPB_FileRef_Proxy::OnCallbackCompleteInHost, file_ref, callback_id); | 321 &PPB_FileRef_Proxy::OnCallbackCompleteInHost, file_ref, callback_id); |
291 if (enter.succeeded()) { | 322 if (enter.succeeded()) { |
292 enter.SetResult(enter.object()->Rename(new_file_ref.host_resource(), | 323 enter.SetResult(enter.object()->Rename(new_file_ref.host_resource(), |
293 enter.callback())); | 324 enter.callback())); |
294 } | 325 } |
295 } | 326 } |
296 | 327 |
328 void PPB_FileRef_Proxy::OnMsgQuery(const HostResource& file_ref, | |
329 int callback_id) { | |
330 PP_FileInfo info; | |
yzshen1
2013/03/21 17:18:11
|info| is on the stack, it seems we rely on the fa
teravest
2013/03/21 19:48:57
info is on the heap now.
| |
331 EnterHostFromHostResourceForceCallback<PPB_FileRef_API> enter( | |
332 file_ref, callback_factory_, | |
333 &PPB_FileRef_Proxy::OnQueryCallbackCompleteInHost, file_ref, &info, | |
334 callback_id); | |
335 if (enter.succeeded()) | |
336 enter.SetResult(enter.object()->Query(&info, enter.callback())); | |
337 } | |
338 | |
297 void PPB_FileRef_Proxy::OnMsgGetAbsolutePath(const HostResource& host_resource, | 339 void PPB_FileRef_Proxy::OnMsgGetAbsolutePath(const HostResource& host_resource, |
298 SerializedVarReturnValue result) { | 340 SerializedVarReturnValue result) { |
299 EnterHostFromHostResource<PPB_FileRef_API> enter(host_resource); | 341 EnterHostFromHostResource<PPB_FileRef_API> enter(host_resource); |
300 if (enter.succeeded()) | 342 if (enter.succeeded()) |
301 result.Return(dispatcher(), enter.object()->GetAbsolutePath()); | 343 result.Return(dispatcher(), enter.object()->GetAbsolutePath()); |
302 } | 344 } |
303 #endif // !defined(OS_NACL) | 345 #endif // !defined(OS_NACL) |
304 | 346 |
305 void PPB_FileRef_Proxy::OnMsgCallbackComplete( | 347 void PPB_FileRef_Proxy::OnMsgCallbackComplete( |
306 const HostResource& host_resource, | 348 const HostResource& host_resource, |
307 int callback_id, | 349 int callback_id, |
308 int32_t result) { | 350 int32_t result) { |
309 // Forward the callback info to the plugin resource. | 351 // Forward the callback info to the plugin resource. |
310 EnterPluginFromHostResource<PPB_FileRef_API> enter(host_resource); | 352 EnterPluginFromHostResource<PPB_FileRef_API> enter(host_resource); |
311 if (enter.succeeded()) | 353 if (enter.succeeded()) |
312 static_cast<FileRef*>(enter.object())->ExecuteCallback(callback_id, result); | 354 static_cast<FileRef*>(enter.object())->ExecuteCallback(callback_id, result); |
313 } | 355 } |
314 | 356 |
357 void PPB_FileRef_Proxy::OnMsgQueryCallbackComplete( | |
358 const HostResource& host_resource, | |
359 PP_FileInfo info, | |
360 int callback_id, | |
361 int32_t result) { | |
362 EnterPluginFromHostResource<PPB_FileRef_API> enter(host_resource); | |
363 if (enter.succeeded()) { | |
364 // Set the FileInfo output parameter. | |
365 static_cast<FileRef*>(enter.object())->SetFileInfo(callback_id, info); | |
366 static_cast<FileRef*>(enter.object())->ExecuteCallback(callback_id, result); | |
367 } | |
368 } | |
369 | |
315 #if !defined(OS_NACL) | 370 #if !defined(OS_NACL) |
316 void PPB_FileRef_Proxy::OnCallbackCompleteInHost( | 371 void PPB_FileRef_Proxy::OnCallbackCompleteInHost( |
317 int32_t result, | 372 int32_t result, |
318 const HostResource& host_resource, | 373 const HostResource& host_resource, |
319 int callback_id) { | 374 int callback_id) { |
320 // Execute OnMsgCallbackComplete in the plugin process. | 375 // Execute OnMsgCallbackComplete in the plugin process. |
321 Send(new PpapiMsg_PPBFileRef_CallbackComplete( | 376 Send(new PpapiMsg_PPBFileRef_CallbackComplete( |
322 API_ID_PPB_FILE_REF, host_resource, callback_id, result)); | 377 API_ID_PPB_FILE_REF, host_resource, callback_id, result)); |
323 } | 378 } |
379 | |
380 void PPB_FileRef_Proxy::OnQueryCallbackCompleteInHost( | |
381 int32_t result, | |
382 const HostResource& host_resource, | |
383 PP_FileInfo* info, | |
384 int callback_id) { | |
385 if (result == PP_OK) { | |
386 Send(new PpapiMsg_PPBFileRef_QueryCallbackComplete( | |
387 API_ID_PPB_FILE_REF, host_resource, *info, callback_id, result)); | |
388 } else { | |
389 PP_FileInfo tmp_info; | |
390 memset(&tmp_info, 0, sizeof(tmp_info)); | |
391 Send(new PpapiMsg_PPBFileRef_QueryCallbackComplete( | |
392 API_ID_PPB_FILE_REF, host_resource, tmp_info, callback_id, result)); | |
393 } | |
394 } | |
324 #endif // !defined(OS_NACL) | 395 #endif // !defined(OS_NACL) |
325 | 396 |
326 } // namespace proxy | 397 } // namespace proxy |
327 } // namespace ppapi | 398 } // namespace ppapi |
OLD | NEW |