Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(494)

Side by Side Diff: webkit/glue/cpp_bound_class.cc

Issue 8550010: base::Bind() conversion for webkit. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 // This file contains definitions for CppBoundClass 5 // This file contains definitions for CppBoundClass
6 6
7 // Here's the control flow of a JS method getting forwarded to a class. 7 // Here's the control flow of a JS method getting forwarded to a class.
8 // - Something calls our NPObject with a function like "Invoke". 8 // - Something calls our NPObject with a function like "Invoke".
9 // - CppNPObject's static invoke() function forwards it to its attached 9 // - CppNPObject's static invoke() function forwards it to its attached
10 // CppBoundClass's Invoke() method. 10 // CppBoundClass's Invoke() method.
11 // - CppBoundClass has then overridden Invoke() to look up the function 11 // - CppBoundClass has then overridden Invoke() to look up the function
12 // name in its internal map of methods, and then calls the appropriate 12 // name in its internal map of methods, and then calls the appropriate
13 // method. 13 // method.
14 14
15 #include "base/compiler_specific.h" 15 #include "base/compiler_specific.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/stl_util.h"
17 #include "base/utf_string_conversions.h" 18 #include "base/utf_string_conversions.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
21 #include "webkit/glue/cpp_bound_class.h" 22 #include "webkit/glue/cpp_bound_class.h"
22 23
23 using WebKit::WebBindings; 24 using WebKit::WebBindings;
24 using WebKit::WebFrame; 25 using WebKit::WebFrame;
25 26
26 namespace { 27 namespace {
(...skipping 10 matching lines...) Expand all
37 value_->Set(value); 38 value_->Set(value);
38 return true; 39 return true;
39 } 40 }
40 41
41 private: 42 private:
42 CppVariant* value_; 43 CppVariant* value_;
43 }; 44 };
44 45
45 class GetterPropertyCallback : public CppBoundClass::PropertyCallback { 46 class GetterPropertyCallback : public CppBoundClass::PropertyCallback {
46 public: 47 public:
47 GetterPropertyCallback(CppBoundClass::GetterCallback* callback) 48 GetterPropertyCallback(const CppBoundClass::GetterCallback& callback)
48 : callback_(callback) { } 49 : callback_(callback) { }
49 50
50 virtual bool GetValue(CppVariant* value) { 51 virtual bool GetValue(CppVariant* value) {
51 callback_->Run(value); 52 callback_.Run(value);
52 return true; 53 return true;
53 } 54 }
54 55
55 virtual bool SetValue(const CppVariant& value) { 56 virtual bool SetValue(const CppVariant& value) {
56 return false; 57 return false;
57 } 58 }
58 59
59 private: 60 private:
60 scoped_ptr<CppBoundClass::GetterCallback> callback_; 61 CppBoundClass::GetterCallback callback_;
61 }; 62 };
62 63
63 } 64 }
64 65
65 // Our special NPObject type. We extend an NPObject with a pointer to a 66 // Our special NPObject type. We extend an NPObject with a pointer to a
66 // CppBoundClass, which is just a C++ interface that we forward all NPObject 67 // CppBoundClass, which is just a C++ interface that we forward all NPObject
67 // callbacks to. 68 // callbacks to.
68 struct CppNPObject { 69 struct CppNPObject {
69 NPObject parent; // This must be the first field in the struct. 70 NPObject parent; // This must be the first field in the struct.
70 CppBoundClass* bound_class; 71 CppBoundClass* bound_class;
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 const NPVariant* value) { 172 const NPVariant* value) {
172 CppNPObject* obj = reinterpret_cast<CppNPObject*>(np_obj); 173 CppNPObject* obj = reinterpret_cast<CppNPObject*>(np_obj);
173 return obj->bound_class->SetProperty(ident, value); 174 return obj->bound_class->SetProperty(ident, value);
174 } 175 }
175 176
176 CppBoundClass::CppBoundClass() 177 CppBoundClass::CppBoundClass()
177 : bound_to_frame_(false) { 178 : bound_to_frame_(false) {
178 } 179 }
179 180
180 CppBoundClass::~CppBoundClass() { 181 CppBoundClass::~CppBoundClass() {
181 for (MethodList::iterator i = methods_.begin(); i != methods_.end(); ++i) 182 STLDeleteValues(&properties_);
182 delete i->second;
183
184 for (PropertyList::iterator i = properties_.begin(); i != properties_.end();
185 ++i) {
186 delete i->second;
187 }
188 183
189 // Unregister ourselves if we were bound to a frame. 184 // Unregister ourselves if we were bound to a frame.
190 if (bound_to_frame_) 185 if (bound_to_frame_)
191 WebBindings::unregisterObject(NPVARIANT_TO_OBJECT(self_variant_)); 186 WebBindings::unregisterObject(NPVARIANT_TO_OBJECT(self_variant_));
192 } 187 }
193 188
194 bool CppBoundClass::HasMethod(NPIdentifier ident) const { 189 bool CppBoundClass::HasMethod(NPIdentifier ident) const {
195 return (methods_.find(ident) != methods_.end()); 190 return (methods_.find(ident) != methods_.end());
196 } 191 }
197 192
198 bool CppBoundClass::HasProperty(NPIdentifier ident) const { 193 bool CppBoundClass::HasProperty(NPIdentifier ident) const {
199 return (properties_.find(ident) != properties_.end()); 194 return (properties_.find(ident) != properties_.end());
200 } 195 }
201 196
202 bool CppBoundClass::Invoke(NPIdentifier ident, 197 bool CppBoundClass::Invoke(NPIdentifier ident,
203 const NPVariant* args, 198 const NPVariant* args,
awong 2011/11/21 21:46:56 Fix indenting?
dcheng 2011/11/21 22:04:16 Done.
204 size_t arg_count, 199 size_t arg_count,
205 NPVariant* result) { 200 NPVariant* result) {
206 MethodList::const_iterator method = methods_.find(ident); 201 MethodList::const_iterator method = methods_.find(ident);
207 Callback* callback; 202 Callback callback;
208 if (method == methods_.end()) { 203 if (method == methods_.end()) {
209 if (fallback_callback_.get()) { 204 if (!fallback_callback_.is_null()) {
210 callback = fallback_callback_.get(); 205 callback = fallback_callback_;
211 } else { 206 } else {
212 VOID_TO_NPVARIANT(*result); 207 VOID_TO_NPVARIANT(*result);
213 return false; 208 return false;
214 } 209 }
215 } else { 210 } else {
216 callback = (*method).second; 211 callback = method->second;
217 } 212 }
218 213
219 // Build a CppArgumentList argument vector from the NPVariants coming in. 214 // Build a CppArgumentList argument vector from the NPVariants coming in.
220 CppArgumentList cpp_args(arg_count); 215 CppArgumentList cpp_args(arg_count);
221 for (size_t i = 0; i < arg_count; i++) 216 for (size_t i = 0; i < arg_count; i++)
222 cpp_args[i].Set(args[i]); 217 cpp_args[i].Set(args[i]);
223 218
224 CppVariant cpp_result; 219 CppVariant cpp_result;
225 callback->Run(cpp_args, &cpp_result); 220 callback.Run(cpp_args, &cpp_result);
226 221
227 cpp_result.CopyToNPVariant(result); 222 cpp_result.CopyToNPVariant(result);
228 return true; 223 return true;
229 } 224 }
230 225
231 bool CppBoundClass::GetProperty(NPIdentifier ident, NPVariant* result) const { 226 bool CppBoundClass::GetProperty(NPIdentifier ident, NPVariant* result) const {
232 PropertyList::const_iterator callback = properties_.find(ident); 227 PropertyList::const_iterator callback = properties_.find(ident);
233 if (callback == properties_.end()) { 228 if (callback == properties_.end()) {
234 VOID_TO_NPVARIANT(*result); 229 VOID_TO_NPVARIANT(*result);
235 return false; 230 return false;
(...skipping 10 matching lines...) Expand all
246 const NPVariant* value) { 241 const NPVariant* value) {
247 PropertyList::iterator callback = properties_.find(ident); 242 PropertyList::iterator callback = properties_.find(ident);
248 if (callback == properties_.end()) 243 if (callback == properties_.end())
249 return false; 244 return false;
250 245
251 CppVariant cpp_value; 246 CppVariant cpp_value;
252 cpp_value.Set(*value); 247 cpp_value.Set(*value);
253 return (*callback).second->SetValue(cpp_value); 248 return (*callback).second->SetValue(cpp_value);
254 } 249 }
255 250
256 void CppBoundClass::BindCallback(const std::string& name, Callback* callback) { 251 void CppBoundClass::BindCallback(const std::string& name,
252 const Callback& callback) {
257 NPIdentifier ident = WebBindings::getStringIdentifier(name.c_str()); 253 NPIdentifier ident = WebBindings::getStringIdentifier(name.c_str());
258 MethodList::iterator old_callback = methods_.find(ident); 254 MethodList::iterator old_callback = methods_.find(ident);
259 if (old_callback != methods_.end()) { 255 if (old_callback != methods_.end() && callback.is_null()) {
csilv 2011/11/21 21:26:31 If the callback is null, there's no reason to call
dcheng 2011/11/21 22:04:16 Done.
260 delete old_callback->second; 256 methods_.erase(old_callback);
261 if (callback == NULL) { 257 return;
262 methods_.erase(old_callback);
263 return;
264 }
265 } 258 }
266 259
267 methods_[ident] = callback; 260 methods_[ident] = callback;
268 } 261 }
269 262
270 void CppBoundClass::BindGetterCallback(const std::string& name, 263 void CppBoundClass::BindGetterCallback(const std::string& name,
271 GetterCallback* callback) { 264 const GetterCallback& callback) {
272 PropertyCallback* property_callback = callback == NULL ? 265 PropertyCallback* property_callback = callback.is_null() ?
273 NULL : new GetterPropertyCallback(callback); 266 NULL : new GetterPropertyCallback(callback);
274 267
275 BindProperty(name, property_callback); 268 BindProperty(name, property_callback);
276 } 269 }
277 270
278 void CppBoundClass::BindProperty(const std::string& name, CppVariant* prop) { 271 void CppBoundClass::BindProperty(const std::string& name, CppVariant* prop) {
279 PropertyCallback* property_callback = prop == NULL ? 272 PropertyCallback* property_callback = prop == NULL ?
280 NULL : new CppVariantPropertyCallback(prop); 273 NULL : new CppVariantPropertyCallback(prop);
281 274
282 BindProperty(name, property_callback); 275 BindProperty(name, property_callback);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 JSC::JSLock lock(false); 318 JSC::JSLock lock(false);
326 #endif 319 #endif
327 320
328 // BindToWindowObject will take its own reference to the NPObject, and clean 321 // BindToWindowObject will take its own reference to the NPObject, and clean
329 // up after itself. It will also (indirectly) register the object with V8, 322 // up after itself. It will also (indirectly) register the object with V8,
330 // so we must remember this so we can unregister it when we're destroyed. 323 // so we must remember this so we can unregister it when we're destroyed.
331 frame->bindToWindowObject(ASCIIToUTF16(classname), 324 frame->bindToWindowObject(ASCIIToUTF16(classname),
332 NPVARIANT_TO_OBJECT(*GetAsCppVariant())); 325 NPVARIANT_TO_OBJECT(*GetAsCppVariant()));
333 bound_to_frame_ = true; 326 bound_to_frame_ = true;
334 } 327 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698