| OLD | NEW |
| 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 /* | 5 /* |
| 6 CppBoundClass class: | 6 CppBoundClass class: |
| 7 This base class serves as a parent for C++ classes designed to be bound to | 7 This base class serves as a parent for C++ classes designed to be bound to |
| 8 JavaScript objects. | 8 JavaScript objects. |
| 9 | 9 |
| 10 Subclasses should define the constructor to build the property and method | 10 Subclasses should define the constructor to build the property and method |
| 11 lists needed to bind this class to a JS object. They should also declare | 11 lists needed to bind this class to a JS object. They should also declare |
| 12 and define member variables and methods to be exposed to JS through | 12 and define member variables and methods to be exposed to JS through |
| 13 that object. | 13 that object. |
| 14 | 14 |
| 15 See cpp_binding_example.{h|cc} for an example. | 15 See cpp_binding_example.{h|cc} for an example. |
| 16 */ | 16 */ |
| (...skipping 29 matching lines...) Expand all Loading... |
| 46 // as window.<classname>. The owner of the CppBoundObject is responsible for | 46 // as window.<classname>. The owner of the CppBoundObject is responsible for |
| 47 // keeping the object around while the frame is alive, and for destroying it | 47 // keeping the object around while the frame is alive, and for destroying it |
| 48 // afterwards. | 48 // afterwards. |
| 49 void BindToJavascript(WebFrame* frame, const std::wstring& classname); | 49 void BindToJavascript(WebFrame* frame, const std::wstring& classname); |
| 50 | 50 |
| 51 // The type of callbacks. | 51 // The type of callbacks. |
| 52 typedef Callback2<const CppArgumentList&, CppVariant*>::Type Callback; | 52 typedef Callback2<const CppArgumentList&, CppVariant*>::Type Callback; |
| 53 | 53 |
| 54 // Used by a test. Returns true if a method with name |name| exists, | 54 // Used by a test. Returns true if a method with name |name| exists, |
| 55 // regardless of whether a fallback is registered. | 55 // regardless of whether a fallback is registered. |
| 56 bool IsMethodRegistered(std::string name); | 56 bool IsMethodRegistered(std::string name) const; |
| 57 | 57 |
| 58 protected: | 58 protected: |
| 59 // Bind the Javascript method called |name| to the C++ callback |callback|. | 59 // Bind the Javascript method called |name| to the C++ callback |callback|. |
| 60 void BindCallback(std::string name, Callback* callback); | 60 void BindCallback(std::string name, Callback* callback); |
| 61 | 61 |
| 62 // A wrapper for BindCallback, to simplify the common case of binding a | 62 // A wrapper for BindCallback, to simplify the common case of binding a |
| 63 // method on the current object. Though not verified here, |method| | 63 // method on the current object. Though not verified here, |method| |
| 64 // must be a method of this CppBoundClass subclass. | 64 // must be a method of this CppBoundClass subclass. |
| 65 template<typename T> | 65 template<typename T> |
| 66 void BindMethod(std::string name, | 66 void BindMethod(std::string name, |
| 67 void (T::*method)(const CppArgumentList&, CppVariant*)) { | 67 void (T::*method)(const CppArgumentList&, CppVariant*)) { |
| 68 Callback* callback = | 68 Callback* callback = |
| 69 NewCallback<T, const CppArgumentList&, CppVariant*>( | 69 NewCallback<T, const CppArgumentList&, CppVariant*>( |
| 70 static_cast<T*>(this), method); | 70 static_cast<T*>(this), method); |
| 71 BindCallback(name, callback); | 71 BindCallback(name, callback); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 100 NewCallback<T, const CppArgumentList&, CppVariant*>( | 100 NewCallback<T, const CppArgumentList&, CppVariant*>( |
| 101 static_cast<T*>(this), method); | 101 static_cast<T*>(this), method); |
| 102 BindFallbackCallback(callback); | 102 BindFallbackCallback(callback); |
| 103 } else { | 103 } else { |
| 104 BindFallbackCallback(NULL); | 104 BindFallbackCallback(NULL); |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 // Some fields are protected because some tests depend on accessing them, | 108 // Some fields are protected because some tests depend on accessing them, |
| 109 // but otherwise they should be considered private. | 109 // but otherwise they should be considered private. |
| 110 | 110 |
| 111 typedef std::map<NPIdentifier, CppVariant*> PropertyList; | 111 typedef std::map<NPIdentifier, CppVariant*> PropertyList; |
| 112 typedef std::map<NPIdentifier, Callback*> MethodList; | 112 typedef std::map<NPIdentifier, Callback*> MethodList; |
| 113 // These maps associate names with property and method pointers to be | 113 // These maps associate names with property and method pointers to be |
| 114 // exposed to JavaScript. | 114 // exposed to JavaScript. |
| 115 PropertyList properties_; | 115 PropertyList properties_; |
| 116 MethodList methods_; | 116 MethodList methods_; |
| 117 | 117 |
| 118 // The callback gets invoked when a call is made to an nonexistent method. | 118 // The callback gets invoked when a call is made to an nonexistent method. |
| 119 scoped_ptr<Callback> fallback_callback_; | 119 scoped_ptr<Callback> fallback_callback_; |
| 120 | 120 |
| 121 private: | 121 private: |
| 122 // NPObject callbacks. | 122 // NPObject callbacks. |
| 123 friend struct CppNPObject; | 123 friend struct CppNPObject; |
| 124 bool HasMethod(NPIdentifier ident); | 124 bool HasMethod(NPIdentifier ident) const; |
| 125 bool Invoke(NPIdentifier ident, const NPVariant* args, size_t arg_count, | 125 bool Invoke(NPIdentifier ident, const NPVariant* args, size_t arg_count, |
| 126 NPVariant* result); | 126 NPVariant* result); |
| 127 bool HasProperty(NPIdentifier ident); | 127 bool HasProperty(NPIdentifier ident) const; |
| 128 bool GetProperty(NPIdentifier ident, NPVariant* result); | 128 bool GetProperty(NPIdentifier ident, NPVariant* result) const; |
| 129 bool SetProperty(NPIdentifier ident, const NPVariant* value); | 129 bool SetProperty(NPIdentifier ident, const NPVariant* value); |
| 130 | 130 |
| 131 // A list of all NPObjects we created and bound in BindToJavascript(), so we | 131 // A list of all NPObjects we created and bound in BindToJavascript(), so we |
| 132 // can clean them up when we're destroyed. | 132 // can clean them up when we're destroyed. |
| 133 typedef std::vector<NPObject*> BoundObjectList; | 133 typedef std::vector<NPObject*> BoundObjectList; |
| 134 BoundObjectList bound_objects_; | 134 BoundObjectList bound_objects_; |
| 135 | 135 |
| 136 DISALLOW_EVIL_CONSTRUCTORS(CppBoundClass); | 136 DISALLOW_EVIL_CONSTRUCTORS(CppBoundClass); |
| 137 }; | 137 }; |
| 138 | 138 |
| 139 #endif // CPP_BOUNDCLASS_H__ | 139 #endif // CPP_BOUNDCLASS_H__ |
| 140 | 140 |
| OLD | NEW |