1 // Copyright 2013 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 #include "content/test/cpp_binding_example.h"
10 #include "base/bind_helpers.h"
12 using webkit_glue::CppArgumentList;
13 using webkit_glue::CppBoundClass;
14 using webkit_glue::CppVariant;
20 class PropertyCallbackExample : public CppBoundClass::PropertyCallback {
22 virtual bool GetValue(CppVariant* value) OVERRIDE {
27 virtual bool SetValue(const CppVariant& value) OVERRIDE {
38 CppBindingExample::CppBindingExample() {
39 // Map properties. It's recommended, but not required, that the JavaScript
40 // names (used as the keys in this map) match the names of the member
41 // variables exposed through those names.
42 BindProperty("my_value", &my_value);
43 BindProperty("my_other_value", &my_other_value);
45 // Bind property with a callback.
46 BindProperty("my_value_with_callback", new PropertyCallbackExample());
47 // Bind property with a getter callback.
48 BindGetterCallback("same", base::Bind(&CppBindingExample::same,
49 base::Unretained(this)));
51 // Map methods. See comment above about names.
52 BindCallback("echoValue", base::Bind(&CppBindingExample::echoValue,
53 base::Unretained(this)));
54 BindCallback("echoType", base::Bind(&CppBindingExample::echoType,
55 base::Unretained(this)));
56 BindCallback("plus", base::Bind(&CppBindingExample::plus,
57 base::Unretained(this)));
59 // The fallback method is called when a nonexistent method is called on an
60 // object. If none is specified, calling a nonexistent method causes an
61 // exception to be thrown and the JavaScript execution is stopped.
62 BindFallbackCallback(base::Bind(&CppBindingExample::fallbackMethod,
63 base::Unretained(this)));
66 my_other_value.Set("Reinitialized!");
69 void CppBindingExample::echoValue(const CppArgumentList& args,
71 if (args.size() < 1) {
78 void CppBindingExample::echoType(const CppArgumentList& args,
80 if (args.size() < 1) {
84 // Note that if args[0] is a string, the following assignment implicitly
85 // makes a copy of that string, which may have an undesirable impact on
87 CppVariant arg1 = args[0];
90 else if (arg1.isInt32())
92 else if (arg1.isDouble())
94 else if (arg1.isString())
95 result->Set("Success!");
98 void CppBindingExample::plus(const CppArgumentList& args,
100 if (args.size() < 2) {
105 CppVariant arg1 = args[0];
106 CppVariant arg2 = args[1];
108 if (!arg1.isNumber() || !arg2.isNumber()) {
113 // The value of a CppVariant may be read directly from its NPVariant struct.
114 // (However, it should only be set using one of the Set() functions.)
117 sum += arg1.value.doubleValue;
118 else if (arg1.isInt32())
119 sum += arg1.value.intValue;
122 sum += arg2.value.doubleValue;
123 else if (arg2.isInt32())
124 sum += arg2.value.intValue;
129 void CppBindingExample::same(CppVariant* result) {
133 void CppBindingExample::fallbackMethod(const CppArgumentList& args,
134 CppVariant* result) {
135 printf("Error: unknown JavaScript method invoked.\n");
138 } // namespace content