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