OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_RENDERER_EXTENSIONS_NATIVE_HANDLER_H_ | 5 #ifndef CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ |
6 #define CHROME_RENDERER_EXTENSIONS_NATIVE_HANDLER_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ |
7 | |
8 #include "base/bind.h" | |
9 #include "base/memory/linked_ptr.h" | |
10 #include "v8/include/v8.h" | |
11 | 7 |
12 #include <string> | 8 #include <string> |
13 #include <vector> | 9 #include <vector> |
14 | 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/memory/linked_ptr.h" |
| 13 #include "chrome/renderer/extensions/native_handler.h" |
| 14 #include "v8/include/v8.h" |
| 15 |
15 namespace extensions { | 16 namespace extensions { |
16 | 17 |
17 // A NativeHandler is a factory for JS objects with functions on them that map | 18 // An ObjectBackedNativeHandler is a factory for JS objects with functions on |
18 // to native C++ functions. Subclasses should call RouteFunction() in their | 19 // them that map to native C++ functions. Subclasses should call RouteFunction() |
19 // constructor to define functions on the created JS objects. | 20 // in their constructor to define functions on the created JS objects. |
20 // | 21 class ObjectBackedNativeHandler : public NativeHandler { |
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. | |
26 class NativeHandler { | |
27 public: | 22 public: |
28 explicit NativeHandler(v8::Isolate* isolate); | 23 explicit ObjectBackedNativeHandler(v8::Isolate* isolate); |
29 virtual ~NativeHandler(); | 24 virtual ~ObjectBackedNativeHandler(); |
30 | 25 |
31 // Create an object with bindings to the native functions defined through | 26 // Create an object with bindings to the native functions defined through |
32 // RouteFunction(). | 27 // RouteFunction(). |
33 virtual v8::Handle<v8::Object> NewInstance(); | 28 virtual v8::Handle<v8::Object> NewInstance() OVERRIDE; |
34 | 29 |
35 protected: | 30 protected: |
36 typedef v8::Handle<v8::Value> (*HandlerFunc)(const v8::Arguments&); | 31 typedef v8::Handle<v8::Value> (*HandlerFunc)(const v8::Arguments&); |
37 typedef base::Callback<v8::Handle<v8::Value>(const v8::Arguments&)> | 32 typedef base::Callback<v8::Handle<v8::Value>(const v8::Arguments&)> |
38 HandlerFunction; | 33 HandlerFunction; |
39 | 34 |
40 // Installs a new 'route' from |name| to |handler_function|. This means that | 35 // Installs a new 'route' from |name| to |handler_function|. This means that |
41 // NewInstance()s of this NativeHandler will have a property |name| which | 36 // NewInstance()s of this ObjectBackedNativeHandler will have a property |
42 // will be handled by |handler_function|. | 37 // |name| which will be handled by |handler_function|. |
43 void RouteFunction(const std::string& name, | 38 void RouteFunction(const std::string& name, |
44 const HandlerFunction& handler_function); | 39 const HandlerFunction& handler_function); |
45 | 40 |
46 void RouteStaticFunction(const std::string& name, | 41 void RouteStaticFunction(const std::string& name, |
47 const HandlerFunc handler_func); | 42 const HandlerFunc handler_func); |
48 | 43 |
49 private: | 44 private: |
50 static v8::Handle<v8::Value> Router(const v8::Arguments& args); | 45 static v8::Handle<v8::Value> Router(const v8::Arguments& args); |
51 | 46 |
52 std::vector<linked_ptr<HandlerFunction> > handler_functions_; | 47 std::vector<linked_ptr<HandlerFunction> > handler_functions_; |
53 v8::Isolate* isolate_; | 48 v8::Isolate* isolate_; |
54 v8::Persistent<v8::ObjectTemplate> object_template_; | 49 v8::Persistent<v8::ObjectTemplate> object_template_; |
55 | 50 |
56 DISALLOW_COPY_AND_ASSIGN(NativeHandler); | 51 DISALLOW_COPY_AND_ASSIGN(ObjectBackedNativeHandler); |
57 }; | 52 }; |
58 | 53 |
59 } // extensions | 54 } // extensions |
60 | 55 |
61 #endif // CHROME_RENDERER_EXTENSIONS_NATIVE_HANDLER_H_ | 56 #endif // CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ |
OLD | NEW |