| OLD | NEW |
| (Empty) |
| 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. | |
| 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 #include <stdio.h> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/bind_helpers.h" | |
| 14 | |
| 15 namespace webkit_glue { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 class PropertyCallbackExample : public CppBoundClass::PropertyCallback { | |
| 20 public: | |
| 21 virtual bool GetValue(CppVariant* value) OVERRIDE { | |
| 22 value->Set(value_); | |
| 23 return true; | |
| 24 } | |
| 25 | |
| 26 virtual bool SetValue(const CppVariant& value) OVERRIDE { | |
| 27 value_.Set(value); | |
| 28 return true; | |
| 29 } | |
| 30 | |
| 31 private: | |
| 32 CppVariant value_; | |
| 33 }; | |
| 34 | |
| 35 } | |
| 36 | |
| 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); | |
| 43 | |
| 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))); | |
| 49 | |
| 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))); | |
| 57 | |
| 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))); | |
| 63 | |
| 64 my_value.Set(10); | |
| 65 my_other_value.Set("Reinitialized!"); | |
| 66 } | |
| 67 | |
| 68 void CppBindingExample::echoValue(const CppArgumentList& args, | |
| 69 CppVariant* result) { | |
| 70 if (args.size() < 1) { | |
| 71 result->SetNull(); | |
| 72 return; | |
| 73 } | |
| 74 result->Set(args[0]); | |
| 75 } | |
| 76 | |
| 77 void CppBindingExample::echoType(const CppArgumentList& args, | |
| 78 CppVariant* result) { | |
| 79 if (args.size() < 1) { | |
| 80 result->SetNull(); | |
| 81 return; | |
| 82 } | |
| 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!"); | |
| 95 } | |
| 96 | |
| 97 void CppBindingExample::plus(const CppArgumentList& args, | |
| 98 CppVariant* result) { | |
| 99 if (args.size() < 2) { | |
| 100 result->SetNull(); | |
| 101 return; | |
| 102 } | |
| 103 | |
| 104 CppVariant arg1 = args[0]; | |
| 105 CppVariant arg2 = args[1]; | |
| 106 | |
| 107 if (!arg1.isNumber() || !arg2.isNumber()) { | |
| 108 result->SetNull(); | |
| 109 return; | |
| 110 } | |
| 111 | |
| 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; | |
| 119 | |
| 120 if (arg2.isDouble()) | |
| 121 sum += arg2.value.doubleValue; | |
| 122 else if (arg2.isInt32()) | |
| 123 sum += arg2.value.intValue; | |
| 124 | |
| 125 result->Set(sum); | |
| 126 } | |
| 127 | |
| 128 void CppBindingExample::same(CppVariant* result) { | |
| 129 result->Set(42); | |
| 130 } | |
| 131 | |
| 132 void CppBindingExample::fallbackMethod(const CppArgumentList& args, | |
| 133 CppVariant* result) { | |
| 134 printf("Error: unknown JavaScript method invoked.\n"); | |
| 135 } | |
| 136 | |
| 137 } // namespace webkit_glue | |
| OLD | NEW |