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 #ifndef PPAPI_PROXY_ENTER_PROXY_H_ | 5 #ifndef PPAPI_PROXY_ENTER_PROXY_H_ |
6 #define PPAPI_PROXY_ENTER_PROXY_H_ | 6 #define PPAPI_PROXY_ENTER_PROXY_H_ |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "ppapi/cpp/completion_callback.h" | 9 #include "ppapi/cpp/completion_callback.h" |
10 #include "ppapi/proxy/host_dispatcher.h" | 10 #include "ppapi/proxy/host_dispatcher.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 DCHECK(this->failed() || | 38 DCHECK(this->failed() || |
39 PluginDispatcher::GetForInstance(host_resource.instance())); | 39 PluginDispatcher::GetForInstance(host_resource.instance())); |
40 } | 40 } |
41 }; | 41 }; |
42 | 42 |
43 template<typename ResourceT> | 43 template<typename ResourceT> |
44 class EnterHostFromHostResource | 44 class EnterHostFromHostResource |
45 : public thunk::EnterResourceNoLock<ResourceT> { | 45 : public thunk::EnterResourceNoLock<ResourceT> { |
46 public: | 46 public: |
47 explicit EnterHostFromHostResource(const HostResource& host_resource) | 47 explicit EnterHostFromHostResource(const HostResource& host_resource) |
48 : thunk::EnterResourceNoLock<ResourceT>( | 48 : thunk::EnterResourceNoLock<ResourceT>(host_resource.host_resource(), |
49 host_resource.host_resource(), false) { | 49 false) { |
50 // Validate that we're in the host rather than the plugin. Otherwise this | 50 // Validate that we're in the host rather than the plugin. Otherwise this |
51 // object will do the wrong thing. In the host, the instance should have | 51 // object will do the wrong thing. In the host, the instance should have |
52 // a corresponding host disptacher (assuming the resource is valid). | 52 // a corresponding host disptacher (assuming the resource is valid). |
| 53 DCHECK(this->failed() || |
| 54 HostDispatcher::GetForInstance(host_resource.instance())); |
| 55 } |
| 56 EnterHostFromHostResource(const HostResource& host_resource, |
| 57 const pp::CompletionCallback& callback) |
| 58 : thunk::EnterResourceNoLock<ResourceT>(host_resource.host_resource(), |
| 59 callback.pp_completion_callback(), |
| 60 false) { |
| 61 // Validate that we're in the host rather than the plugin. Otherwise this |
| 62 // object will do the wrong thing. In the host, the instance should have |
| 63 // a corresponding host disptacher (assuming the resource is valid). |
53 DCHECK(this->failed() || | 64 DCHECK(this->failed() || |
54 HostDispatcher::GetForInstance(host_resource.instance())); | 65 HostDispatcher::GetForInstance(host_resource.instance())); |
55 } | 66 } |
56 }; | 67 }; |
57 | 68 |
58 // Enters a resource and forces a completion callback to be issued. | 69 // Enters a resource and forces a completion callback to be issued. |
59 // | 70 // |
60 // This is used when implementing the host (renderer) side of a resource | 71 // This is used when implementing the host (renderer) side of a resource |
61 // function that issues a completion callback. In all cases, we need to issue | 72 // function that issues a completion callback. In all cases, we need to issue |
62 // the callback to avoid hanging the plugin. | 73 // the callback to avoid hanging the plugin. |
(...skipping 22 matching lines...) Expand all Loading... |
85 // void MyClass::SendResult(int32_t result, const HostResource& res) { | 96 // void MyClass::SendResult(int32_t result, const HostResource& res) { |
86 // Send(new FooMsg_FooComplete(..., result, res)); | 97 // Send(new FooMsg_FooComplete(..., result, res)); |
87 // } | 98 // } |
88 template<typename ResourceT> | 99 template<typename ResourceT> |
89 class EnterHostFromHostResourceForceCallback | 100 class EnterHostFromHostResourceForceCallback |
90 : public EnterHostFromHostResource<ResourceT> { | 101 : public EnterHostFromHostResource<ResourceT> { |
91 public: | 102 public: |
92 EnterHostFromHostResourceForceCallback( | 103 EnterHostFromHostResourceForceCallback( |
93 const HostResource& host_resource, | 104 const HostResource& host_resource, |
94 const pp::CompletionCallback& callback) | 105 const pp::CompletionCallback& callback) |
95 : EnterHostFromHostResource<ResourceT>(host_resource), | 106 : EnterHostFromHostResource<ResourceT>(host_resource, callback), |
96 needs_running_(true), | 107 needs_running_(true) { |
97 callback_(callback) { | |
98 } | 108 } |
99 | 109 |
100 // For callbacks that take no parameters except the "int32_t result". Most | 110 // For callbacks that take no parameters except the "int32_t result". Most |
101 // implementations will use the 1-extra-argument constructor below. | 111 // implementations will use the 1-extra-argument constructor below. |
102 template<class CallbackFactory, typename Method> | 112 template<class CallbackFactory, typename Method> |
103 EnterHostFromHostResourceForceCallback( | 113 EnterHostFromHostResourceForceCallback( |
104 const HostResource& host_resource, | 114 const HostResource& host_resource, |
105 CallbackFactory& factory, | 115 CallbackFactory& factory, |
106 Method method) | 116 Method method) |
107 : EnterHostFromHostResource<ResourceT>(host_resource), | 117 : EnterHostFromHostResource<ResourceT>(host_resource, |
108 needs_running_(true), | 118 factory.NewOptionalCallback(method)), |
109 callback_(factory.NewOptionalCallback(method)) { | 119 needs_running_(true) { |
110 if (this->failed()) | 120 if (this->failed()) |
111 RunCallback(PP_ERROR_BADRESOURCE); | 121 RunCallback(PP_ERROR_BADRESOURCE); |
112 } | 122 } |
113 | 123 |
114 // For callbacks that take an extra parameter as a closure. | 124 // For callbacks that take an extra parameter as a closure. |
115 template<class CallbackFactory, typename Method, typename A> | 125 template<class CallbackFactory, typename Method, typename A> |
116 EnterHostFromHostResourceForceCallback( | 126 EnterHostFromHostResourceForceCallback( |
117 const HostResource& host_resource, | 127 const HostResource& host_resource, |
118 CallbackFactory& factory, | 128 CallbackFactory& factory, |
119 Method method, | 129 Method method, |
120 const A& a) | 130 const A& a) |
121 : EnterHostFromHostResource<ResourceT>(host_resource), | 131 : EnterHostFromHostResource<ResourceT>(host_resource, |
122 needs_running_(true), | 132 factory.NewOptionalCallback(method, a)), |
123 callback_(factory.NewOptionalCallback(method, a)) { | 133 needs_running_(true) { |
124 if (this->failed()) | 134 if (this->failed()) |
125 RunCallback(PP_ERROR_BADRESOURCE); | 135 RunCallback(PP_ERROR_BADRESOURCE); |
126 } | 136 } |
127 | 137 |
128 // For callbacks that take two extra parameters as a closure. | 138 // For callbacks that take two extra parameters as a closure. |
129 template<class CallbackFactory, typename Method, typename A, typename B> | 139 template<class CallbackFactory, typename Method, typename A, typename B> |
130 EnterHostFromHostResourceForceCallback( | 140 EnterHostFromHostResourceForceCallback( |
131 const HostResource& host_resource, | 141 const HostResource& host_resource, |
132 CallbackFactory& factory, | 142 CallbackFactory& factory, |
133 Method method, | 143 Method method, |
134 const A& a, | 144 const A& a, |
135 const B& b) | 145 const B& b) |
136 : EnterHostFromHostResource<ResourceT>(host_resource), | 146 : EnterHostFromHostResource<ResourceT>(host_resource, |
137 needs_running_(true), | 147 factory.NewOptionalCallback(method, a, b)), |
138 callback_(factory.NewOptionalCallback(method, a, b)) { | 148 needs_running_(true) { |
139 if (this->failed()) | 149 if (this->failed()) |
140 RunCallback(PP_ERROR_BADRESOURCE); | 150 RunCallback(PP_ERROR_BADRESOURCE); |
141 } | 151 } |
142 | 152 |
143 ~EnterHostFromHostResourceForceCallback() { | 153 ~EnterHostFromHostResourceForceCallback() { |
144 if (needs_running_) { | 154 if (needs_running_) { |
145 NOTREACHED() << "Should always call SetResult except in the " | 155 NOTREACHED() << "Should always call SetResult except in the " |
146 "initialization failed case."; | 156 "initialization failed case."; |
147 RunCallback(PP_ERROR_FAILED); | 157 RunCallback(PP_ERROR_FAILED); |
148 } | 158 } |
149 } | 159 } |
150 | 160 |
151 void SetResult(int32_t result) { | 161 void SetResult(int32_t result) { |
152 DCHECK(needs_running_) << "Don't call SetResult when there already is one."; | 162 DCHECK(needs_running_) << "Don't call SetResult when there already is one."; |
| 163 if (result != PP_OK_COMPLETIONPENDING) |
| 164 RunCallback(result); |
153 needs_running_ = false; | 165 needs_running_ = false; |
154 if (result != PP_OK_COMPLETIONPENDING) | 166 this->ClearCallback(); |
155 callback_.Run(result); | |
156 } | |
157 | |
158 PP_CompletionCallback callback() { | |
159 return callback_.pp_completion_callback(); | |
160 } | 167 } |
161 | 168 |
162 private: | 169 private: |
163 void RunCallback(int32_t result) { | 170 void RunCallback(int32_t result) { |
164 DCHECK(needs_running_); | 171 DCHECK(needs_running_); |
165 needs_running_ = false; | 172 needs_running_ = false; |
166 callback_.Run(result); | 173 this->callback()->Run(result); |
| 174 this->ClearCallback(); |
167 } | 175 } |
168 | 176 |
169 bool needs_running_; | 177 bool needs_running_; |
170 pp::CompletionCallback callback_; | |
171 }; | 178 }; |
172 | 179 |
173 // Like EnterHostFromHostResourceForceCallback but for Function APIs. It takes | 180 // Like EnterHostFromHostResourceForceCallback but for Function APIs. It takes |
174 // an instance instead of a resource ID. | 181 // an instance instead of a resource ID. |
175 template<typename FunctionT> | 182 template<typename FunctionT> |
176 class EnterHostFunctionForceCallback | 183 class EnterHostFunctionForceCallback |
177 : public thunk::EnterFunctionNoLock<FunctionT> { | 184 : public thunk::EnterFunctionNoLock<FunctionT> { |
178 public: | 185 public: |
179 EnterHostFunctionForceCallback( | 186 EnterHostFunctionForceCallback( |
180 PP_Instance instance, | 187 PP_Instance instance, |
181 const pp::CompletionCallback& callback) | 188 const pp::CompletionCallback& callback) |
182 : thunk::EnterFunctionNoLock<FunctionT>(instance, false), | 189 : thunk::EnterFunctionNoLock<FunctionT>(instance, false), |
183 needs_running_(true), | 190 needs_running_(true) { |
184 callback_(callback) { | |
185 if (this->failed()) | 191 if (this->failed()) |
186 RunCallback(PP_ERROR_BADARGUMENT); | 192 RunCallback(PP_ERROR_BADARGUMENT); |
187 } | 193 } |
188 | 194 |
189 ~EnterHostFunctionForceCallback() { | 195 ~EnterHostFunctionForceCallback() { |
190 if (needs_running_) { | 196 if (needs_running_) { |
191 NOTREACHED() << "Should always call SetResult except in the " | 197 NOTREACHED() << "Should always call SetResult except in the " |
192 "initialization failed case."; | 198 "initialization failed case."; |
193 RunCallback(PP_ERROR_FAILED); | 199 RunCallback(PP_ERROR_FAILED); |
194 } | 200 } |
195 } | 201 } |
196 | 202 |
197 void SetResult(int32_t result) { | 203 void SetResult(int32_t result) { |
198 DCHECK(needs_running_) << "Don't call SetResult when there already is one."; | 204 DCHECK(needs_running_) << "Don't call SetResult when there already is one."; |
| 205 if (result != PP_OK_COMPLETIONPENDING) |
| 206 RunCallback(result); |
199 needs_running_ = false; | 207 needs_running_ = false; |
200 if (result != PP_OK_COMPLETIONPENDING) | 208 this->ClearCallback(); |
201 callback_.Run(result); | |
202 } | |
203 | |
204 PP_CompletionCallback callback() { | |
205 return callback_.pp_completion_callback(); | |
206 } | 209 } |
207 | 210 |
208 private: | 211 private: |
209 void RunCallback(int32_t result) { | 212 void RunCallback(int32_t result) { |
210 DCHECK(needs_running_); | 213 DCHECK(needs_running_); |
211 needs_running_ = false; | 214 needs_running_ = false; |
212 callback_.Run(result); | 215 this->callback()->Run(result); |
| 216 this->ClearCallback(); |
213 } | 217 } |
214 | 218 |
215 bool needs_running_; | 219 bool needs_running_; |
216 pp::CompletionCallback callback_; | |
217 }; | 220 }; |
218 | 221 |
219 } // namespace proxy | 222 } // namespace proxy |
220 } // namespace ppapi | 223 } // namespace ppapi |
221 | 224 |
222 #endif // PPAPI_PROXY_ENTER_PROXY_H_ | 225 #endif // PPAPI_PROXY_ENTER_PROXY_H_ |
OLD | NEW |