| 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 22 matching lines...) Expand all Loading... |
| 33 public: | 33 public: |
| 34 explicit FileRef(const PPB_FileRef_CreateInfo& info); | 34 explicit FileRef(const PPB_FileRef_CreateInfo& info); |
| 35 virtual ~FileRef(); | 35 virtual ~FileRef(); |
| 36 | 36 |
| 37 // Resource overrides. | 37 // Resource overrides. |
| 38 virtual void LastPluginRefWasDeleted() OVERRIDE; | 38 virtual void LastPluginRefWasDeleted() OVERRIDE; |
| 39 | 39 |
| 40 // PPB_FileRef_API implementation (not provided by PPB_FileRef_Shared). | 40 // PPB_FileRef_API implementation (not provided by PPB_FileRef_Shared). |
| 41 virtual PP_Resource GetParent() OVERRIDE; | 41 virtual PP_Resource GetParent() OVERRIDE; |
| 42 virtual int32_t MakeDirectory(PP_Bool make_ancestors, | 42 virtual int32_t MakeDirectory(PP_Bool make_ancestors, |
| 43 PP_CompletionCallback callback) OVERRIDE; | 43 ApiCallbackType 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 PP_CompletionCallback callback) OVERRIDE; | 46 ApiCallbackType callback) OVERRIDE; |
| 47 virtual int32_t Delete(PP_CompletionCallback callback) OVERRIDE; | 47 virtual int32_t Delete(ApiCallbackType callback) OVERRIDE; |
| 48 virtual int32_t Rename(PP_Resource new_file_ref, | 48 virtual int32_t Rename(PP_Resource new_file_ref, |
| 49 PP_CompletionCallback callback) OVERRIDE; | 49 ApiCallbackType callback) OVERRIDE; |
| 50 virtual PP_Var GetAbsolutePath() OVERRIDE; | 50 virtual PP_Var GetAbsolutePath() OVERRIDE; |
| 51 | 51 |
| 52 // Executes the pending callback with the given ID. See pending_callbacks_. | 52 // Executes the pending callback with the given ID. See pending_callbacks_. |
| 53 void ExecuteCallback(int callback_id, int32_t result); | 53 void ExecuteCallback(int callback_id, int32_t result); |
| 54 | 54 |
| 55 private: | 55 private: |
| 56 PluginDispatcher* GetDispatcher() const { | 56 PluginDispatcher* GetDispatcher() const { |
| 57 return PluginDispatcher::GetForResource(this); | 57 return PluginDispatcher::GetForResource(this); |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Adds a callback to the list and returns its ID. Returns 0 if the callback | 60 // Adds a callback to the list and returns its ID. |
| 61 // is invalid. | 61 int SendCallback(ApiCallbackType callback); |
| 62 int SendCallback(PP_CompletionCallback callback); | |
| 63 | 62 |
| 64 // This class can have any number of out-standing requests with completion | 63 // This class can have any number of out-standing requests with completion |
| 65 // callbacks, in contrast to most resources which have one possible pending | 64 // callbacks, in contrast to most resources which have one possible pending |
| 66 // callback pending (like a Flush callback). | 65 // callback pending (like a Flush callback). |
| 67 // | 66 // |
| 68 // To keep track of them, assign integer IDs to the callbacks, which is how | 67 // To keep track of them, assign integer IDs to the callbacks, which is how |
| 69 // the callback will be identified when it's passed to the host and then | 68 // the callback will be identified when it's passed to the host and then |
| 70 // back here. | 69 // back here. Use unsigned so that overflow is well-defined. |
| 71 int next_callback_id_; | 70 unsigned int next_callback_id_; |
| 72 typedef std::map<int, scoped_refptr<TrackedCallback> > PendingCallbackMap; | 71 typedef std::map<unsigned int, |
| 72 scoped_refptr<TrackedCallback> > PendingCallbackMap; |
| 73 PendingCallbackMap pending_callbacks_; | 73 PendingCallbackMap pending_callbacks_; |
| 74 | 74 |
| 75 DISALLOW_IMPLICIT_CONSTRUCTORS(FileRef); | 75 DISALLOW_IMPLICIT_CONSTRUCTORS(FileRef); |
| 76 }; | 76 }; |
| 77 | 77 |
| 78 FileRef::FileRef(const PPB_FileRef_CreateInfo& info) | 78 FileRef::FileRef(const PPB_FileRef_CreateInfo& info) |
| 79 : PPB_FileRef_Shared(OBJECT_IS_PROXY, info), | 79 : PPB_FileRef_Shared(OBJECT_IS_PROXY, info), |
| 80 next_callback_id_(1) { | 80 next_callback_id_(0u) { |
| 81 } | 81 } |
| 82 | 82 |
| 83 FileRef::~FileRef() { | 83 FileRef::~FileRef() { |
| 84 // The callbacks map should have been cleared by LastPluginRefWasDeleted. | 84 // The callbacks map should have been cleared by LastPluginRefWasDeleted. |
| 85 DCHECK(pending_callbacks_.empty()); | 85 DCHECK(pending_callbacks_.empty()); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void FileRef::LastPluginRefWasDeleted() { | 88 void FileRef::LastPluginRefWasDeleted() { |
| 89 // The callback tracker will abort our callbacks for us. | 89 // The callback tracker will abort our callbacks for us. |
| 90 pending_callbacks_.clear(); | 90 pending_callbacks_.clear(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 PP_Resource FileRef::GetParent() { | 93 PP_Resource FileRef::GetParent() { |
| 94 PPB_FileRef_CreateInfo create_info; | 94 PPB_FileRef_CreateInfo create_info; |
| 95 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_GetParent( | 95 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_GetParent( |
| 96 API_ID_PPB_FILE_REF, host_resource(), &create_info)); | 96 API_ID_PPB_FILE_REF, host_resource(), &create_info)); |
| 97 return PPB_FileRef_Proxy::DeserializeFileRef(create_info); | 97 return PPB_FileRef_Proxy::DeserializeFileRef(create_info); |
| 98 } | 98 } |
| 99 | 99 |
| 100 int32_t FileRef::MakeDirectory(PP_Bool make_ancestors, | 100 int32_t FileRef::MakeDirectory(PP_Bool make_ancestors, |
| 101 PP_CompletionCallback callback) { | 101 ApiCallbackType callback) { |
| 102 int callback_id = SendCallback(callback); | |
| 103 if (!callback_id) | |
| 104 return PP_ERROR_BADARGUMENT; | |
| 105 | |
| 106 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_MakeDirectory( | 102 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_MakeDirectory( |
| 107 API_ID_PPB_FILE_REF, host_resource(), make_ancestors, callback_id)); | 103 API_ID_PPB_FILE_REF, host_resource(), make_ancestors, |
| 104 SendCallback(callback))); |
| 108 return PP_OK_COMPLETIONPENDING; | 105 return PP_OK_COMPLETIONPENDING; |
| 109 } | 106 } |
| 110 | 107 |
| 111 int32_t FileRef::Touch(PP_Time last_access_time, | 108 int32_t FileRef::Touch(PP_Time last_access_time, |
| 112 PP_Time last_modified_time, | 109 PP_Time last_modified_time, |
| 113 PP_CompletionCallback callback) { | 110 ApiCallbackType callback) { |
| 114 int callback_id = SendCallback(callback); | |
| 115 if (!callback_id) | |
| 116 return PP_ERROR_BADARGUMENT; | |
| 117 | |
| 118 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Touch( | 111 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Touch( |
| 119 API_ID_PPB_FILE_REF, host_resource(), | 112 API_ID_PPB_FILE_REF, host_resource(), last_access_time, |
| 120 last_access_time, last_modified_time, callback_id)); | 113 last_modified_time, SendCallback(callback))); |
| 121 return PP_OK_COMPLETIONPENDING; | 114 return PP_OK_COMPLETIONPENDING; |
| 122 } | 115 } |
| 123 | 116 |
| 124 int32_t FileRef::Delete(PP_CompletionCallback callback) { | 117 int32_t FileRef::Delete(ApiCallbackType callback) { |
| 125 int callback_id = SendCallback(callback); | |
| 126 if (!callback_id) | |
| 127 return PP_ERROR_BADARGUMENT; | |
| 128 | |
| 129 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Delete( | 118 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Delete( |
| 130 API_ID_PPB_FILE_REF, host_resource(), callback_id)); | 119 API_ID_PPB_FILE_REF, host_resource(), SendCallback(callback))); |
| 131 return PP_OK_COMPLETIONPENDING; | 120 return PP_OK_COMPLETIONPENDING; |
| 132 } | 121 } |
| 133 | 122 |
| 134 int32_t FileRef::Rename(PP_Resource new_file_ref, | 123 int32_t FileRef::Rename(PP_Resource new_file_ref, |
| 135 PP_CompletionCallback callback) { | 124 ApiCallbackType callback) { |
| 136 int callback_id = SendCallback(callback); | |
| 137 if (!callback_id) | |
| 138 return PP_ERROR_BADARGUMENT; | |
| 139 | |
| 140 Resource* new_file_ref_object = | 125 Resource* new_file_ref_object = |
| 141 PpapiGlobals::Get()->GetResourceTracker()->GetResource(new_file_ref); | 126 PpapiGlobals::Get()->GetResourceTracker()->GetResource(new_file_ref); |
| 142 if (!new_file_ref_object || | 127 if (!new_file_ref_object || |
| 143 new_file_ref_object->host_resource().instance() != pp_instance()) | 128 new_file_ref_object->host_resource().instance() != pp_instance()) |
| 144 return PP_ERROR_BADRESOURCE; | 129 return PP_ERROR_BADRESOURCE; |
| 145 | 130 |
| 146 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Rename( | 131 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Rename( |
| 147 API_ID_PPB_FILE_REF, host_resource(), | 132 API_ID_PPB_FILE_REF, host_resource(), |
| 148 new_file_ref_object->host_resource(), callback_id)); | 133 new_file_ref_object->host_resource(), SendCallback(callback))); |
| 149 return PP_OK_COMPLETIONPENDING; | 134 return PP_OK_COMPLETIONPENDING; |
| 150 } | 135 } |
| 151 | 136 |
| 152 PP_Var FileRef::GetAbsolutePath() { | 137 PP_Var FileRef::GetAbsolutePath() { |
| 153 ReceiveSerializedVarReturnValue result; | 138 ReceiveSerializedVarReturnValue result; |
| 154 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_GetAbsolutePath( | 139 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_GetAbsolutePath( |
| 155 API_ID_PPB_FILE_REF, host_resource(), &result)); | 140 API_ID_PPB_FILE_REF, host_resource(), &result)); |
| 156 return result.Return(GetDispatcher()); | 141 return result.Return(GetDispatcher()); |
| 157 } | 142 } |
| 158 | 143 |
| 159 void FileRef::ExecuteCallback(int callback_id, int32_t result) { | 144 void FileRef::ExecuteCallback(int callback_id, int32_t result) { |
| 160 PendingCallbackMap::iterator found = pending_callbacks_.find(callback_id); | 145 PendingCallbackMap::iterator found = pending_callbacks_.find(callback_id); |
| 161 if (found == pending_callbacks_.end()) { | 146 if (found == pending_callbacks_.end()) { |
| 162 // This will happen when the plugin deletes its resource with a pending | 147 // This will happen when the plugin deletes its resource with a pending |
| 163 // callback. The callback will be locally issued with an ABORTED call while | 148 // callback. The callback will be locally issued with an ABORTED call while |
| 164 // the operation may still be pending in the renderer. | 149 // the operation may still be pending in the renderer. |
| 165 return; | 150 return; |
| 166 } | 151 } |
| 167 | 152 |
| 168 // Executing the callback may mutate the callback list. | 153 // Executing the callback may mutate the callback list. |
| 169 scoped_refptr<TrackedCallback> callback = found->second; | 154 scoped_refptr<TrackedCallback> callback = found->second; |
| 170 pending_callbacks_.erase(found); | 155 pending_callbacks_.erase(found); |
| 171 callback->Run(result); | 156 callback->Run(result); |
| 172 } | 157 } |
| 173 | 158 |
| 174 int FileRef::SendCallback(PP_CompletionCallback callback) { | 159 int FileRef::SendCallback(ApiCallbackType callback) { |
| 175 if (!callback.func) | 160 // In extreme cases the IDs may wrap around, so avoid duplicates. |
| 176 return 0; | 161 while (pending_callbacks_.count(next_callback_id_)) |
| 162 ++next_callback_id_; |
| 177 | 163 |
| 178 // In extreme cases the IDs may wrap around, so avoid duplicates. | 164 pending_callbacks_[next_callback_id_] = callback; |
| 179 while (pending_callbacks_.find(next_callback_id_) != pending_callbacks_.end()) | |
| 180 next_callback_id_++; | |
| 181 | |
| 182 pending_callbacks_[next_callback_id_] = new TrackedCallback(this, callback); | |
| 183 return next_callback_id_++; | 165 return next_callback_id_++; |
| 184 } | 166 } |
| 185 | 167 |
| 186 namespace { | 168 namespace { |
| 187 | 169 |
| 188 InterfaceProxy* CreateFileRefProxy(Dispatcher* dispatcher) { | 170 InterfaceProxy* CreateFileRefProxy(Dispatcher* dispatcher) { |
| 189 return new PPB_FileRef_Proxy(dispatcher); | 171 return new PPB_FileRef_Proxy(dispatcher); |
| 190 } | 172 } |
| 191 | 173 |
| 192 } // namespace | 174 } // namespace |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 int32_t result, | 319 int32_t result, |
| 338 const HostResource& host_resource, | 320 const HostResource& host_resource, |
| 339 int callback_id) { | 321 int callback_id) { |
| 340 // Execute OnMsgCallbackComplete in the plugin process. | 322 // Execute OnMsgCallbackComplete in the plugin process. |
| 341 Send(new PpapiMsg_PPBFileRef_CallbackComplete( | 323 Send(new PpapiMsg_PPBFileRef_CallbackComplete( |
| 342 API_ID_PPB_FILE_REF, host_resource, callback_id, result)); | 324 API_ID_PPB_FILE_REF, host_resource, callback_id, result)); |
| 343 } | 325 } |
| 344 | 326 |
| 345 } // namespace proxy | 327 } // namespace proxy |
| 346 } // namespace ppapi | 328 } // namespace ppapi |
| OLD | NEW |