Get foreground tab on Android
[chromium-blink-merge.git] / content / test / cpp_binding_example.cc
blob3c7630d622a5c37b9b571b6aeb4f159a1e75090c
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"
7 #include <stdio.h>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
12 using webkit_glue::CppArgumentList;
13 using webkit_glue::CppBoundClass;
14 using webkit_glue::CppVariant;
16 namespace content {
18 namespace {
20 class PropertyCallbackExample : public CppBoundClass::PropertyCallback {
21 public:
22 virtual bool GetValue(CppVariant* value) OVERRIDE {
23 value->Set(value_);
24 return true;
27 virtual bool SetValue(const CppVariant& value) OVERRIDE {
28 value_.Set(value);
29 return true;
32 private:
33 CppVariant value_;
36 } // namespace
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)));
65 my_value.Set(10);
66 my_other_value.Set("Reinitialized!");
69 void CppBindingExample::echoValue(const CppArgumentList& args,
70 CppVariant* result) {
71 if (args.size() < 1) {
72 result->SetNull();
73 return;
75 result->Set(args[0]);
78 void CppBindingExample::echoType(const CppArgumentList& args,
79 CppVariant* result) {
80 if (args.size() < 1) {
81 result->SetNull();
82 return;
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
86 // performance.
87 CppVariant arg1 = args[0];
88 if (arg1.isBool())
89 result->Set(true);
90 else if (arg1.isInt32())
91 result->Set(7);
92 else if (arg1.isDouble())
93 result->Set(3.14159);
94 else if (arg1.isString())
95 result->Set("Success!");
98 void CppBindingExample::plus(const CppArgumentList& args,
99 CppVariant* result) {
100 if (args.size() < 2) {
101 result->SetNull();
102 return;
105 CppVariant arg1 = args[0];
106 CppVariant arg2 = args[1];
108 if (!arg1.isNumber() || !arg2.isNumber()) {
109 result->SetNull();
110 return;
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.)
115 double sum = 0.;
116 if (arg1.isDouble())
117 sum += arg1.value.doubleValue;
118 else if (arg1.isInt32())
119 sum += arg1.value.intValue;
121 if (arg2.isDouble())
122 sum += arg2.value.doubleValue;
123 else if (arg2.isInt32())
124 sum += arg2.value.intValue;
126 result->Set(sum);
129 void CppBindingExample::same(CppVariant* result) {
130 result->Set(42);
133 void CppBindingExample::fallbackMethod(const CppArgumentList& args,
134 CppVariant* result) {
135 printf("Error: unknown JavaScript method invoked.\n");
138 } // namespace content