Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 5 | #ifndef CONTENT_TEST_CPP_BINDING_EXAMPLE_H_ |
| 6 | #define CONTENT_TEST_CPP_BINDING_EXAMPLE_H_ |
| 7 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 8 | /* |
| 9 | CppBindingExample class: |
| 10 | This provides an example of how to use the CppBoundClass to create methods |
| 11 | and properties that can be exposed to JavaScript by an appropriately built |
| 12 | embedding client. It is also used by the CppBoundClass unit test. |
| 13 | |
| 14 | Typically, a class intended to be bound to JavaScript will define a |
| 15 | constructor, any methods and properties to be exposed, and optionally a |
| 16 | destructor. An embedding client can then bind the class to a JavaScript |
| 17 | object in a frame's window using the CppBoundClass::BindToJavascript() method, |
| 18 | generally called from the WebFrameClient's DidClearWindowObject(). |
| 19 | |
| 20 | Once this class has been bound, say to the name "example", it might be called |
| 21 | from JavaScript in the following way: |
| 22 | |
| 23 | <script> |
| 24 | if (window.example) { |
| 25 | document.writeln(example.echoValue(false)); |
| 26 | document.writeln(example.echoType("Hello world!")); |
| 27 | document.writeln(example.plus(2, 3.1)); |
| 28 | |
| 29 | example.my_value = 15; |
| 30 | example.my_other_value = 2.1; |
| 31 | document.writeln(example.plus(example.my_value, example.my_other_value)); |
| 32 | } |
| 33 | </script> |
| 34 | */ |
| 35 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 36 | #include "webkit/renderer/cpp_bound_class.h" |
| 37 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 38 | namespace content { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 39 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 40 | class CppBindingExample : public webkit_glue::CppBoundClass { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 41 | public: |
| 42 | // The default constructor initializes the property and method lists needed |
| 43 | // to bind this class to a JS object. |
| 44 | CppBindingExample(); |
| 45 | |
| 46 | // |
| 47 | // These public member variables and methods implement the methods and |
| 48 | // properties that will be exposed to JavaScript. If needed, the class could |
| 49 | // also contain other methods or variables, which will be hidden from JS |
| 50 | // as long as they're not mapped in the property and method lists created in |
| 51 | // the constructor. |
| 52 | // |
| 53 | // The signatures of any methods to be bound must match |
| 54 | // CppBoundClass::Callback. |
| 55 | // |
| 56 | |
| 57 | // Returns the value that was passed in as its first (only) argument. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 58 | void echoValue(const webkit_glue::CppArgumentList& args, |
| 59 | webkit_glue::CppVariant* result); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 60 | |
| 61 | // Returns a hard-coded value of the same type (bool, number (double), |
| 62 | // string, or null) that was passed in as an argument. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 63 | void echoType(const webkit_glue::CppArgumentList& args, |
| 64 | webkit_glue::CppVariant* result); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 65 | |
| 66 | // Returns the sum of the (first) two arguments as a double, if they are both |
| 67 | // numbers (integers or doubles). Otherwise returns null. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 68 | void plus(const webkit_glue::CppArgumentList& args, |
| 69 | webkit_glue::CppVariant* result); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 70 | |
| 71 | // Always returns the same value -- an example of a read-only property. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 72 | void same(webkit_glue::CppVariant* result); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 73 | |
| 74 | // Invoked when a nonexistent method is called on this example object, this |
| 75 | // prints an error message. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 76 | void fallbackMethod(const webkit_glue::CppArgumentList& args, |
| 77 | webkit_glue::CppVariant* result); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 78 | |
| 79 | // These properties will also be exposed to JavaScript. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 80 | webkit_glue::CppVariant my_value; |
| 81 | webkit_glue::CppVariant my_other_value; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 84 | } // namespace content |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 85 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 86 | #endif // CONTENT_TEST_CPP_BINDING_EXAMPLE_H_ |