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 #ifndef CHROME_RENDERER_NATIVE_HANDLER_H_ | |
6 #define CHROME_RENDERER_NATIVE_HANDLER_H_ | |
7 | |
8 #include "base/bind.h" | |
9 #include "base/memory/linked_ptr.h" | |
10 #include "v8/include/v8.h" | |
11 | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 // A NativeHandler is a factory for JS objects with functions on them that map | |
16 // to native C++ functions. Subclasses should call RouteFunction() in their | |
17 // constructor to define functions on the created JS objects. | |
18 // | |
19 // NativeHandlers are intended to be used with a ModuleSystem. The ModuleSystem | |
20 // will assume ownership of the NativeHandler, and as a ModuleSystem is tied to | |
21 // a single v8::Context, this implies that NativeHandlers will also be tied to | |
22 // a single v8::context. | |
23 // TODO(koz): Rename this to NativeJavaScriptModule. | |
24 class NativeHandler { | |
25 public: | |
26 explicit NativeHandler(); | |
27 virtual ~NativeHandler(); | |
28 | |
29 // Create an object with bindings to the native functions defined through | |
30 // RouteFunction(). | |
31 virtual v8::Handle<v8::Object> NewInstance(); | |
32 | |
33 protected: | |
34 typedef v8::Handle<v8::Value> (*HandlerFunc)(const v8::Arguments&); | |
35 typedef base::Callback<v8::Handle<v8::Value>(const v8::Arguments&)> | |
36 HandlerFunction; | |
37 | |
38 // Installs a new 'route' from |name| to |handler_function|. This means that | |
39 // NewInstance()s of this NativeHandler will have a property |name| which | |
40 // will be handled by |handler_function|. | |
41 void RouteFunction(const std::string& name, | |
42 const HandlerFunction& handler_function); | |
43 | |
44 void RouteStaticFunction(const std::string& name, | |
45 const HandlerFunc handler_func); | |
46 | |
47 private: | |
48 static v8::Handle<v8::Value> Router(const v8::Arguments& args); | |
49 | |
50 std::vector<linked_ptr<HandlerFunction> > handler_functions_; | |
51 v8::Persistent<v8::ObjectTemplate> object_template_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(NativeHandler); | |
54 }; | |
55 | |
56 #endif // CHROME_RENDERER_NATIVE_HANDLER_H_ | |
OLD | NEW |