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