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
3 // found in the LICENSE file.
5 // This file contains the definition for CppBindingExample, which is used in
6 // cpp_bound_class_unittest.
8 #include "cpp_binding_example.h"
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
15 namespace webkit_glue
{
19 class PropertyCallbackExample
: public CppBoundClass::PropertyCallback
{
21 virtual bool GetValue(CppVariant
* value
) OVERRIDE
{
26 virtual bool SetValue(const CppVariant
& value
) OVERRIDE
{
37 CppBindingExample::CppBindingExample() {
38 // Map properties. It's recommended, but not required, that the JavaScript
39 // names (used as the keys in this map) match the names of the member
40 // variables exposed through those names.
41 BindProperty("my_value", &my_value
);
42 BindProperty("my_other_value", &my_other_value
);
44 // Bind property with a callback.
45 BindProperty("my_value_with_callback", new PropertyCallbackExample());
46 // Bind property with a getter callback.
47 BindGetterCallback("same", base::Bind(&CppBindingExample::same
,
48 base::Unretained(this)));
50 // Map methods. See comment above about names.
51 BindCallback("echoValue", base::Bind(&CppBindingExample::echoValue
,
52 base::Unretained(this)));
53 BindCallback("echoType", base::Bind(&CppBindingExample::echoType
,
54 base::Unretained(this)));
55 BindCallback("plus", base::Bind(&CppBindingExample::plus
,
56 base::Unretained(this)));
58 // The fallback method is called when a nonexistent method is called on an
59 // object. If none is specified, calling a nonexistent method causes an
60 // exception to be thrown and the JavaScript execution is stopped.
61 BindFallbackCallback(base::Bind(&CppBindingExample::fallbackMethod
,
62 base::Unretained(this)));
65 my_other_value
.Set("Reinitialized!");
68 void CppBindingExample::echoValue(const CppArgumentList
& args
,
70 if (args
.size() < 1) {
77 void CppBindingExample::echoType(const CppArgumentList
& args
,
79 if (args
.size() < 1) {
83 // Note that if args[0] is a string, the following assignment implicitly
84 // makes a copy of that string, which may have an undesirable impact on
86 CppVariant arg1
= args
[0];
89 else if (arg1
.isInt32())
91 else if (arg1
.isDouble())
93 else if (arg1
.isString())
94 result
->Set("Success!");
97 void CppBindingExample::plus(const CppArgumentList
& args
,
99 if (args
.size() < 2) {
104 CppVariant arg1
= args
[0];
105 CppVariant arg2
= args
[1];
107 if (!arg1
.isNumber() || !arg2
.isNumber()) {
112 // The value of a CppVariant may be read directly from its NPVariant struct.
113 // (However, it should only be set using one of the Set() functions.)
116 sum
+= arg1
.value
.doubleValue
;
117 else if (arg1
.isInt32())
118 sum
+= arg1
.value
.intValue
;
121 sum
+= arg2
.value
.doubleValue
;
122 else if (arg2
.isInt32())
123 sum
+= arg2
.value
.intValue
;
128 void CppBindingExample::same(CppVariant
* result
) {
132 void CppBindingExample::fallbackMethod(const CppArgumentList
& args
,
133 CppVariant
* result
) {
134 printf("Error: unknown JavaScript method invoked.\n");
137 } // namespace webkit_glue