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