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