• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2006-2008 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.
4 
5 // This file contains the definition for CppBindingExample, which is used in
6 // cpp_bound_class_unittest.
7 
8 #include "cpp_binding_example.h"
9 
10 namespace {
11 
12 class PropertyCallbackExample : public CppBoundClass::PropertyCallback {
13  public:
GetValue(CppVariant * value)14   virtual bool GetValue(CppVariant* value) {
15     value->Set(value_);
16     return true;
17   }
18 
SetValue(const CppVariant & value)19   virtual bool SetValue(const CppVariant& value) {
20     value_.Set(value);
21     return true;
22   }
23 
24  private:
25   CppVariant value_;
26 };
27 
28 }
29 
CppBindingExample()30 CppBindingExample::CppBindingExample() {
31   // Map properties.  It's recommended, but not required, that the JavaScript
32   // names (used as the keys in this map) match the names of the member
33   // variables exposed through those names.
34   BindProperty("my_value", &my_value);
35   BindProperty("my_other_value", &my_other_value);
36 
37   // Bind property with a callback.
38   BindProperty("my_value_with_callback", new PropertyCallbackExample());
39   // Bind property with a getter callback.
40   BindProperty("same", &CppBindingExample::same);
41 
42   // Map methods.  See comment above about names.
43   BindMethod("echoValue", &CppBindingExample::echoValue);
44   BindMethod("echoType",  &CppBindingExample::echoType);
45   BindMethod("plus",      &CppBindingExample::plus);
46 
47   // The fallback method is called when a nonexistent method is called on an
48   // object. If none is specified, calling a nonexistent method causes an
49   // exception to be thrown and the JavaScript execution is stopped.
50   BindFallbackMethod(&CppBindingExample::fallbackMethod);
51 
52   my_value.Set(10);
53   my_other_value.Set("Reinitialized!");
54 }
55 
echoValue(const CppArgumentList & args,CppVariant * result)56 void CppBindingExample::echoValue(const CppArgumentList& args,
57                                   CppVariant* result) {
58   if (args.size() < 1) {
59     result->SetNull();
60     return;
61   }
62   result->Set(args[0]);
63 }
64 
echoType(const CppArgumentList & args,CppVariant * result)65 void CppBindingExample::echoType(const CppArgumentList& args,
66                                  CppVariant* result) {
67   if (args.size() < 1) {
68     result->SetNull();
69     return;
70   }
71   // Note that if args[0] is a string, the following assignment implicitly
72   // makes a copy of that string, which may have an undesirable impact on
73   // performance.
74   CppVariant arg1 = args[0];
75   if (arg1.isBool())
76     result->Set(true);
77   else if (arg1.isInt32())
78     result->Set(7);
79   else if (arg1.isDouble())
80     result->Set(3.14159);
81   else if (arg1.isString())
82     result->Set("Success!");
83 }
84 
plus(const CppArgumentList & args,CppVariant * result)85 void CppBindingExample::plus(const CppArgumentList& args,
86                              CppVariant* result) {
87   if (args.size() < 2) {
88     result->SetNull();
89     return;
90   }
91 
92   CppVariant arg1 = args[0];
93   CppVariant arg2 = args[1];
94 
95   if (!arg1.isNumber() || !arg2.isNumber()) {
96     result->SetNull();
97     return;
98   }
99 
100   // The value of a CppVariant may be read directly from its NPVariant struct.
101   // (However, it should only be set using one of the Set() functions.)
102   double sum = 0.;
103   if (arg1.isDouble())
104     sum += arg1.value.doubleValue;
105   else if (arg1.isInt32())
106     sum += arg1.value.intValue;
107 
108   if (arg2.isDouble())
109     sum += arg2.value.doubleValue;
110   else if (arg2.isInt32())
111     sum += arg2.value.intValue;
112 
113   result->Set(sum);
114 }
115 
same(CppVariant * result)116 void CppBindingExample::same(CppVariant* result) {
117   result->Set(42);
118 }
119 
fallbackMethod(const CppArgumentList & args,CppVariant * result)120 void CppBindingExample::fallbackMethod(const CppArgumentList& args,
121                                        CppVariant* result) {
122   printf("Error: unknown JavaScript method invoked.\n");
123 }
124