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 /* | 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 */ |
17 | 17 |
18 #ifndef WEBKIT_GLUE_CPP_BOUNDCLASS_H__ | 18 #ifndef WEBKIT_GLUE_CPP_BOUNDCLASS_H__ |
19 #define WEBKIT_GLUE_CPP_BOUNDCLASS_H__ | 19 #define WEBKIT_GLUE_CPP_BOUNDCLASS_H__ |
20 | 20 |
21 #include <map> | 21 #include <map> |
22 #include <vector> | 22 #include <vector> |
23 | 23 |
24 #include "webkit/glue/cpp_variant.h" | 24 #include "webkit/glue/cpp_variant.h" |
25 | 25 |
26 #include "base/callback_old.h" | 26 #include "base/bind.h" |
| 27 #include "base/callback.h" |
27 #include "base/memory/scoped_ptr.h" | 28 #include "base/memory/scoped_ptr.h" |
28 | 29 |
29 namespace WebKit { | 30 namespace WebKit { |
30 class WebFrame; | 31 class WebFrame; |
31 } | 32 } |
32 | 33 |
33 typedef std::vector<CppVariant> CppArgumentList; | 34 typedef std::vector<CppVariant> CppArgumentList; |
34 | 35 |
35 // CppBoundClass lets you map Javascript method calls and property accesses | 36 // CppBoundClass lets you map Javascript method calls and property accesses |
36 // directly to C++ method calls and CppVariant* variable access. | 37 // directly to C++ method calls and CppVariant* variable access. |
(...skipping 24 matching lines...) Expand all Loading... |
61 // Given a WebFrame, BindToJavascript builds the NPObject that will represent | 62 // Given a WebFrame, BindToJavascript builds the NPObject that will represent |
62 // this CppBoundClass object and binds it to the frame's window under the | 63 // this CppBoundClass object and binds it to the frame's window under the |
63 // given name. This should generally be called from the WebView delegate's | 64 // given name. This should generally be called from the WebView delegate's |
64 // WindowObjectCleared(). This CppBoundClass object will be accessible to | 65 // WindowObjectCleared(). This CppBoundClass object will be accessible to |
65 // JavaScript as window.<classname>. The owner of this CppBoundClass object is | 66 // JavaScript as window.<classname>. The owner of this CppBoundClass object is |
66 // responsible for keeping it around while the frame is alive, and for | 67 // responsible for keeping it around while the frame is alive, and for |
67 // destroying it afterwards. | 68 // destroying it afterwards. |
68 void BindToJavascript(WebKit::WebFrame* frame, const std::string& classname); | 69 void BindToJavascript(WebKit::WebFrame* frame, const std::string& classname); |
69 | 70 |
70 // The type of callbacks. | 71 // The type of callbacks. |
71 typedef Callback2<const CppArgumentList&, CppVariant*>::Type Callback; | 72 typedef base::Callback<void(const CppArgumentList&, CppVariant*)> Callback; |
72 typedef Callback1<CppVariant*>::Type GetterCallback; | 73 typedef base::Callback<void(CppVariant*)> GetterCallback; |
73 | 74 |
74 // Used by a test. Returns true if a method with name |name| exists, | 75 // Used by a test. Returns true if a method with name |name| exists, |
75 // regardless of whether a fallback is registered. | 76 // regardless of whether a fallback is registered. |
76 bool IsMethodRegistered(const std::string& name) const; | 77 bool IsMethodRegistered(const std::string& name) const; |
77 | 78 |
78 protected: | 79 protected: |
79 // Bind the Javascript method called |name| to the C++ callback |callback|. | 80 // Bind the Javascript method called |name| to the C++ callback |callback|. |
80 void BindCallback(const std::string& name, Callback* callback); | 81 void BindCallback(const std::string& name, const Callback& callback); |
81 | 82 |
82 // A wrapper for BindCallback, to simplify the common case of binding a | 83 // A wrapper for BindCallback, to simplify the common case of binding a |
83 // method on the current object. Though not verified here, |method| | 84 // method on the current object. Though not verified here, |method| |
84 // must be a method of this CppBoundClass subclass. | 85 // must be a method of this CppBoundClass subclass. |
85 template<typename T> | 86 template<typename T> |
86 void BindMethod(const std::string& name, | 87 void BindMethod(const std::string& name, |
87 void (T::*method)(const CppArgumentList&, CppVariant*)) { | 88 void (T::*method)(const CppArgumentList&, CppVariant*)) { |
88 Callback* callback = | 89 BindCallback(name, |
89 NewCallback<T, const CppArgumentList&, CppVariant*>( | 90 base::Bind(method, base::Unretained(static_cast<T*>(this)))); |
90 static_cast<T*>(this), method); | |
91 BindCallback(name, callback); | |
92 } | 91 } |
93 | 92 |
94 // Bind Javascript property |name| to the C++ getter callback |callback|. | 93 // Bind Javascript property |name| to the C++ getter callback |callback|. |
95 // This can be used to create read-only properties. | 94 // This can be used to create read-only properties. |
96 void BindGetterCallback(const std::string& name, GetterCallback* callback); | 95 void BindGetterCallback(const std::string& name, |
| 96 const GetterCallback& callback); |
97 | 97 |
98 // A wrapper for BindGetterCallback, to simplify the common case of binding a | 98 // A wrapper for BindGetterCallback, to simplify the common case of binding a |
99 // property on the current object. Though not verified here, |method| | 99 // property on the current object. Though not verified here, |method| |
100 // must be a method of this CppBoundClass subclass. | 100 // must be a method of this CppBoundClass subclass. |
101 template<typename T> | 101 template<typename T> |
102 void BindProperty(const std::string& name, void (T::*method)(CppVariant*)) { | 102 void BindProperty(const std::string& name, void (T::*method)(CppVariant*)) { |
103 GetterCallback* callback = | 103 BindGetterCallback( |
104 NewCallback<T, CppVariant*>(static_cast<T*>(this), method); | 104 name, base::Bind(method, base::Unretained(static_cast<T*>(this)))); |
105 BindGetterCallback(name, callback); | |
106 } | 105 } |
107 | 106 |
108 // Bind the Javascript property called |name| to a CppVariant |prop|. | 107 // Bind the Javascript property called |name| to a CppVariant |prop|. |
109 void BindProperty(const std::string& name, CppVariant* prop); | 108 void BindProperty(const std::string& name, CppVariant* prop); |
110 | 109 |
111 // Bind Javascript property called |name| to a PropertyCallback |callback|. | 110 // Bind Javascript property called |name| to a PropertyCallback |callback|. |
112 // CppBoundClass assumes control over the life time of the |callback|. | 111 // CppBoundClass assumes control over the life time of the |callback|. |
113 void BindProperty(const std::string& name, PropertyCallback* callback); | 112 void BindProperty(const std::string& name, PropertyCallback* callback); |
114 | 113 |
115 // Set the fallback callback, which is called when when a callback is | 114 // Set the fallback callback, which is called when when a callback is |
116 // invoked that isn't bound. | 115 // invoked that isn't bound. |
117 // If it is NULL (its default value), a JavaScript exception is thrown in | 116 // If it is NULL (its default value), a JavaScript exception is thrown in |
118 // that case (as normally expected). If non NULL, the fallback method is | 117 // that case (as normally expected). If non NULL, the fallback method is |
119 // invoked and the script continues its execution. | 118 // invoked and the script continues its execution. |
120 // Passing NULL for |callback| clears out any existing binding. | 119 // Passing NULL for |callback| clears out any existing binding. |
121 // It is used for tests and should probably only be used in such cases | 120 // It is used for tests and should probably only be used in such cases |
122 // as it may cause unexpected behaviors (a JavaScript object with a | 121 // as it may cause unexpected behaviors (a JavaScript object with a |
123 // fallback always returns true when checked for a method's | 122 // fallback always returns true when checked for a method's |
124 // existence). | 123 // existence). |
125 void BindFallbackCallback(Callback* fallback_callback) { | 124 void BindFallbackCallback(const Callback& fallback_callback) { |
126 fallback_callback_.reset(fallback_callback); | 125 fallback_callback_ = fallback_callback; |
127 } | 126 } |
128 | 127 |
129 // A wrapper for BindFallbackCallback, to simplify the common case of | 128 // A wrapper for BindFallbackCallback, to simplify the common case of |
130 // binding a method on the current object. Though not verified here, | 129 // binding a method on the current object. Though not verified here, |
131 // |method| must be a method of this CppBoundClass subclass. | 130 // |method| must be a method of this CppBoundClass subclass. |
132 // Passing NULL for |method| clears out any existing binding. | 131 // Passing NULL for |method| clears out any existing binding. |
133 template<typename T> | 132 template<typename T> |
134 void BindFallbackMethod( | 133 void BindFallbackMethod( |
135 void (T::*method)(const CppArgumentList&, CppVariant*)) { | 134 void (T::*method)(const CppArgumentList&, CppVariant*)) { |
136 if (method) { | 135 if (method) { |
137 Callback* callback = | 136 BindFallbackCallback(base::Bind(method, |
138 NewCallback<T, const CppArgumentList&, CppVariant*>( | 137 base::Unretained(static_cast<T*>(this)))); |
139 static_cast<T*>(this), method); | |
140 BindFallbackCallback(callback); | |
141 } else { | 138 } else { |
142 BindFallbackCallback(NULL); | 139 BindFallbackCallback(Callback()); |
143 } | 140 } |
144 } | 141 } |
145 | 142 |
146 // Some fields are protected because some tests depend on accessing them, | 143 // Some fields are protected because some tests depend on accessing them, |
147 // but otherwise they should be considered private. | 144 // but otherwise they should be considered private. |
148 | 145 |
149 typedef std::map<NPIdentifier, PropertyCallback*> PropertyList; | 146 typedef std::map<NPIdentifier, PropertyCallback*> PropertyList; |
150 typedef std::map<NPIdentifier, Callback*> MethodList; | 147 typedef std::map<NPIdentifier, Callback> MethodList; |
151 // These maps associate names with property and method pointers to be | 148 // These maps associate names with property and method pointers to be |
152 // exposed to JavaScript. | 149 // exposed to JavaScript. |
153 PropertyList properties_; | 150 PropertyList properties_; |
154 MethodList methods_; | 151 MethodList methods_; |
155 | 152 |
156 // The callback gets invoked when a call is made to an nonexistent method. | 153 // The callback gets invoked when a call is made to an nonexistent method. |
157 scoped_ptr<Callback> fallback_callback_; | 154 Callback fallback_callback_; |
158 | 155 |
159 private: | 156 private: |
160 // NPObject callbacks. | 157 // NPObject callbacks. |
161 friend struct CppNPObject; | 158 friend struct CppNPObject; |
162 bool HasMethod(NPIdentifier ident) const; | 159 bool HasMethod(NPIdentifier ident) const; |
163 bool Invoke(NPIdentifier ident, const NPVariant* args, size_t arg_count, | 160 bool Invoke(NPIdentifier ident, const NPVariant* args, size_t arg_count, |
164 NPVariant* result); | 161 NPVariant* result); |
165 bool HasProperty(NPIdentifier ident) const; | 162 bool HasProperty(NPIdentifier ident) const; |
166 bool GetProperty(NPIdentifier ident, NPVariant* result) const; | 163 bool GetProperty(NPIdentifier ident, NPVariant* result) const; |
167 bool SetProperty(NPIdentifier ident, const NPVariant* value); | 164 bool SetProperty(NPIdentifier ident, const NPVariant* value); |
168 | 165 |
169 // A lazily-initialized CppVariant representing this class. We retain 1 | 166 // A lazily-initialized CppVariant representing this class. We retain 1 |
170 // reference to this object, and it is released on deletion. | 167 // reference to this object, and it is released on deletion. |
171 CppVariant self_variant_; | 168 CppVariant self_variant_; |
172 | 169 |
173 // True if our np_object has been bound to a WebFrame, in which case it must | 170 // True if our np_object has been bound to a WebFrame, in which case it must |
174 // be unregistered with V8 when we delete it. | 171 // be unregistered with V8 when we delete it. |
175 bool bound_to_frame_; | 172 bool bound_to_frame_; |
176 | 173 |
177 DISALLOW_COPY_AND_ASSIGN(CppBoundClass); | 174 DISALLOW_COPY_AND_ASSIGN(CppBoundClass); |
178 }; | 175 }; |
179 | 176 |
180 #endif // CPP_BOUNDCLASS_H__ | 177 #endif // CPP_BOUNDCLASS_H__ |
OLD | NEW |