Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / glue / cpp_binding_example.cc
blob43b4ede4823445ae9293feae9299d8e0f143945c
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"
10 #include <stdio.h>
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
15 namespace webkit_glue {
17 namespace {
19 class PropertyCallbackExample : public CppBoundClass::PropertyCallback {
20 public:
21 virtual bool GetValue(CppVariant* value) OVERRIDE {
22 value->Set(value_);
23 return true;
26 virtual bool SetValue(const CppVariant& value) OVERRIDE {
27 value_.Set(value);
28 return true;
31 private:
32 CppVariant value_;
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)));
64 my_value.Set(10);
65 my_other_value.Set("Reinitialized!");
68 void CppBindingExample::echoValue(const CppArgumentList& args,
69 CppVariant* result) {
70 if (args.size() < 1) {
71 result->SetNull();
72 return;
74 result->Set(args[0]);
77 void CppBindingExample::echoType(const CppArgumentList& args,
78 CppVariant* result) {
79 if (args.size() < 1) {
80 result->SetNull();
81 return;
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
85 // performance.
86 CppVariant arg1 = args[0];
87 if (arg1.isBool())
88 result->Set(true);
89 else if (arg1.isInt32())
90 result->Set(7);
91 else if (arg1.isDouble())
92 result->Set(3.14159);
93 else if (arg1.isString())
94 result->Set("Success!");
97 void CppBindingExample::plus(const CppArgumentList& args,
98 CppVariant* result) {
99 if (args.size() < 2) {
100 result->SetNull();
101 return;
104 CppVariant arg1 = args[0];
105 CppVariant arg2 = args[1];
107 if (!arg1.isNumber() || !arg2.isNumber()) {
108 result->SetNull();
109 return;
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.)
114 double sum = 0.;
115 if (arg1.isDouble())
116 sum += arg1.value.doubleValue;
117 else if (arg1.isInt32())
118 sum += arg1.value.intValue;
120 if (arg2.isDouble())
121 sum += arg2.value.doubleValue;
122 else if (arg2.isInt32())
123 sum += arg2.value.intValue;
125 result->Set(sum);
128 void CppBindingExample::same(CppVariant* result) {
129 result->Set(42);
132 void CppBindingExample::fallbackMethod(const CppArgumentList& args,
133 CppVariant* result) {
134 printf("Error: unknown JavaScript method invoked.\n");
137 } // namespace webkit_glue