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. |
71 int next_callback_id_; | 70 int next_callback_id_; |
72 typedef std::map<int, scoped_refptr<TrackedCallback> > PendingCallbackMap; | 71 typedef std::map<int, scoped_refptr<TrackedCallback> > PendingCallbackMap; |
(...skipping 18 matching lines...) Expand all Loading... |
91 } | 90 } |
92 | 91 |
93 PP_Resource FileRef::GetParent() { | 92 PP_Resource FileRef::GetParent() { |
94 PPB_FileRef_CreateInfo create_info; | 93 PPB_FileRef_CreateInfo create_info; |
95 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_GetParent( | 94 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_GetParent( |
96 API_ID_PPB_FILE_REF, host_resource(), &create_info)); | 95 API_ID_PPB_FILE_REF, host_resource(), &create_info)); |
97 return PPB_FileRef_Proxy::DeserializeFileRef(create_info); | 96 return PPB_FileRef_Proxy::DeserializeFileRef(create_info); |
98 } | 97 } |
99 | 98 |
100 int32_t FileRef::MakeDirectory(PP_Bool make_ancestors, | 99 int32_t FileRef::MakeDirectory(PP_Bool make_ancestors, |
101 PP_CompletionCallback callback) { | 100 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( | 101 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_MakeDirectory( |
107 API_ID_PPB_FILE_REF, host_resource(), make_ancestors, callback_id)); | 102 API_ID_PPB_FILE_REF, host_resource(), make_ancestors, |
| 103 SendCallback(callback))); |
108 return PP_OK_COMPLETIONPENDING; | 104 return PP_OK_COMPLETIONPENDING; |
109 } | 105 } |
110 | 106 |
111 int32_t FileRef::Touch(PP_Time last_access_time, | 107 int32_t FileRef::Touch(PP_Time last_access_time, |
112 PP_Time last_modified_time, | 108 PP_Time last_modified_time, |
113 PP_CompletionCallback callback) { | 109 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( | 110 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Touch( |
119 API_ID_PPB_FILE_REF, host_resource(), | 111 API_ID_PPB_FILE_REF, host_resource(), last_access_time, |
120 last_access_time, last_modified_time, callback_id)); | 112 last_modified_time, SendCallback(callback))); |
121 return PP_OK_COMPLETIONPENDING; | 113 return PP_OK_COMPLETIONPENDING; |
122 } | 114 } |
123 | 115 |
124 int32_t FileRef::Delete(PP_CompletionCallback callback) { | 116 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( | 117 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Delete( |
130 API_ID_PPB_FILE_REF, host_resource(), callback_id)); | 118 API_ID_PPB_FILE_REF, host_resource(), SendCallback(callback))); |
131 return PP_OK_COMPLETIONPENDING; | 119 return PP_OK_COMPLETIONPENDING; |
132 } | 120 } |
133 | 121 |
134 int32_t FileRef::Rename(PP_Resource new_file_ref, | 122 int32_t FileRef::Rename(PP_Resource new_file_ref, |
135 PP_CompletionCallback callback) { | 123 ApiCallbackType callback) { |
136 int callback_id = SendCallback(callback); | |
137 if (!callback_id) | |
138 return PP_ERROR_BADARGUMENT; | |
139 | |
140 Resource* new_file_ref_object = | 124 Resource* new_file_ref_object = |
141 PpapiGlobals::Get()->GetResourceTracker()->GetResource(new_file_ref); | 125 PpapiGlobals::Get()->GetResourceTracker()->GetResource(new_file_ref); |
142 if (!new_file_ref_object || | 126 if (!new_file_ref_object || |
143 new_file_ref_object->host_resource().instance() != pp_instance()) | 127 new_file_ref_object->host_resource().instance() != pp_instance()) |
144 return PP_ERROR_BADRESOURCE; | 128 return PP_ERROR_BADRESOURCE; |
145 | 129 |
146 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Rename( | 130 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_Rename( |
147 API_ID_PPB_FILE_REF, host_resource(), | 131 API_ID_PPB_FILE_REF, host_resource(), |
148 new_file_ref_object->host_resource(), callback_id)); | 132 new_file_ref_object->host_resource(), SendCallback(callback))); |
149 return PP_OK_COMPLETIONPENDING; | 133 return PP_OK_COMPLETIONPENDING; |
150 } | 134 } |
151 | 135 |
152 PP_Var FileRef::GetAbsolutePath() { | 136 PP_Var FileRef::GetAbsolutePath() { |
153 ReceiveSerializedVarReturnValue result; | 137 ReceiveSerializedVarReturnValue result; |
154 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_GetAbsolutePath( | 138 GetDispatcher()->Send(new PpapiHostMsg_PPBFileRef_GetAbsolutePath( |
155 API_ID_PPB_FILE_REF, host_resource(), &result)); | 139 API_ID_PPB_FILE_REF, host_resource(), &result)); |
156 return result.Return(GetDispatcher()); | 140 return result.Return(GetDispatcher()); |
157 } | 141 } |
158 | 142 |
159 void FileRef::ExecuteCallback(int callback_id, int32_t result) { | 143 void FileRef::ExecuteCallback(int callback_id, int32_t result) { |
160 PendingCallbackMap::iterator found = pending_callbacks_.find(callback_id); | 144 PendingCallbackMap::iterator found = pending_callbacks_.find(callback_id); |
161 if (found == pending_callbacks_.end()) { | 145 if (found == pending_callbacks_.end()) { |
162 // This will happen when the plugin deletes its resource with a pending | 146 // 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 | 147 // callback. The callback will be locally issued with an ABORTED call while |
164 // the operation may still be pending in the renderer. | 148 // the operation may still be pending in the renderer. |
165 return; | 149 return; |
166 } | 150 } |
167 | 151 |
168 // Executing the callback may mutate the callback list. | 152 // Executing the callback may mutate the callback list. |
169 scoped_refptr<TrackedCallback> callback = found->second; | 153 scoped_refptr<TrackedCallback> callback = found->second; |
170 pending_callbacks_.erase(found); | 154 pending_callbacks_.erase(found); |
171 callback->Run(result); | 155 callback->Run(result); |
172 } | 156 } |
173 | 157 |
174 int FileRef::SendCallback(PP_CompletionCallback callback) { | 158 int FileRef::SendCallback(ApiCallbackType callback) { |
175 if (!callback.func) | 159 // In extreme cases the IDs may wrap around, so avoid duplicates. |
176 return 0; | 160 while (pending_callbacks_.count(next_callback_id_)) { |
| 161 if (next_callback_id_ == std::numeric_limits<int>::max()) |
| 162 next_callback_id_ = 1; |
| 163 else |
| 164 next_callback_id_++; |
| 165 } |
177 | 166 |
178 // In extreme cases the IDs may wrap around, so avoid duplicates. | 167 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_++; | 168 return next_callback_id_++; |
184 } | 169 } |
185 | 170 |
186 namespace { | 171 namespace { |
187 | 172 |
188 InterfaceProxy* CreateFileRefProxy(Dispatcher* dispatcher) { | 173 InterfaceProxy* CreateFileRefProxy(Dispatcher* dispatcher) { |
189 return new PPB_FileRef_Proxy(dispatcher); | 174 return new PPB_FileRef_Proxy(dispatcher); |
190 } | 175 } |
191 | 176 |
192 } // namespace | 177 } // namespace |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 int32_t result, | 322 int32_t result, |
338 const HostResource& host_resource, | 323 const HostResource& host_resource, |
339 int callback_id) { | 324 int callback_id) { |
340 // Execute OnMsgCallbackComplete in the plugin process. | 325 // Execute OnMsgCallbackComplete in the plugin process. |
341 Send(new PpapiMsg_PPBFileRef_CallbackComplete( | 326 Send(new PpapiMsg_PPBFileRef_CallbackComplete( |
342 API_ID_PPB_FILE_REF, host_resource, callback_id, result)); | 327 API_ID_PPB_FILE_REF, host_resource, callback_id, result)); |
343 } | 328 } |
344 | 329 |
345 } // namespace proxy | 330 } // namespace proxy |
346 } // namespace ppapi | 331 } // namespace ppapi |
OLD | NEW |