Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / NativeClient / scripting_bridge.h
1 // Copyright (c) 2011 The Native Client 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 #ifndef EXAMPLES_TUMBLER_SCRIPTING_BRIDGE_H_
6 #define EXAMPLES_TUMBLER_SCRIPTING_BRIDGE_H_
7
8 #include <map>
9 #include <string>
10 #include <tr1/memory>
11 #include <vector>
12
13 #include "callback.h"
14 #include "ppapi/cpp/var.h"
15
16 namespace tumbler {
17
18 class MethodCallbackExecutor;
19
20 // This class handles the interface between the browser and the NaCl module.
21 // There is a single point of entry from the browser: postMessage().  The
22 // string passed to postMessage() has this format:
23 //     'function_name arg_name0:arg_0 arg_name1:arg1 ...'
24 // The arguments have undetermined type; they are placed in a map of argument
25 // names and values.  Values are all strings, it is up to the target code to
26 // do any type coercion.
27 // Methods called by the scripting bridge must have a signature like this:
28 //     void Method(const ScriptingBridge& bridge,
29 //                 const ParameterDictionary&);
30 class ScriptingBridge {
31  public:
32   // Shared pointer type used in the method map.
33   typedef std::tr1::shared_ptr<MethodCallbackExecutor>
34       SharedMethodCallbackExecutor;
35
36   virtual ~ScriptingBridge() {}
37
38   // Causes |method_name| to be published as a method that can be called via
39   // postMessage() from the browser.  Associates this method with |method|.
40   bool AddMethodNamed(const std::string& method_name,
41                       SharedMethodCallbackExecutor method);
42
43   bool InvokeMethod(const std::string& method);
44
45  private:
46   typedef std::map<std::string, SharedMethodCallbackExecutor> MethodDictionary;
47
48   MethodDictionary method_dictionary_;
49 };
50
51 }  // namespace tumbler
52 #endif  // EXAMPLES_TUMBLER_SCRIPTING_BRIDGE_H_