| 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_NATIVE_HANDLER_H_ |
| 6 #define CHROME_RENDERER_EXTENSIONS_NATIVE_HANDLER_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_NATIVE_HANDLER_H_ |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/memory/linked_ptr.h" | |
| 10 #include "chrome/renderer/extensions/scoped_persistent.h" | |
| 11 #include "v8/include/v8.h" | 9 #include "v8/include/v8.h" |
| 12 | 10 |
| 13 #include <string> | |
| 14 #include <vector> | |
| 15 | |
| 16 namespace extensions { | 11 namespace extensions { |
| 17 | 12 |
| 18 // A NativeHandler is a factory for JS objects with functions on them that map | |
| 19 // to native C++ functions. Subclasses should call RouteFunction() in their | |
| 20 // constructor to define functions on the created JS objects. | |
| 21 // | |
| 22 // NativeHandlers are intended to be used with a ModuleSystem. The ModuleSystem | 13 // NativeHandlers are intended to be used with a ModuleSystem. The ModuleSystem |
| 23 // will assume ownership of the NativeHandler, and as a ModuleSystem is tied to | 14 // will assume ownership of the NativeHandler, and as a ModuleSystem is tied to |
| 24 // a single v8::Context, this implies that NativeHandlers will also be tied to | 15 // a single v8::Context, this implies that NativeHandlers will also be tied to |
| 25 // a single v8::context. | 16 // a single v8::Context. |
| 26 // TODO(koz): Rename this to NativeJavaScriptModule. | 17 // TODO(koz): Rename this to NativeJavaScriptModule. |
| 27 class NativeHandler { | 18 class NativeHandler { |
| 28 public: | 19 public: |
| 29 explicit NativeHandler(v8::Isolate* isolate); | 20 NativeHandler(); |
| 30 virtual ~NativeHandler(); | 21 virtual ~NativeHandler(); |
| 31 | 22 |
| 32 // Create an object with bindings to the native functions defined through | 23 // Create a new instance of the object this handler specifies. |
| 33 // RouteFunction(). | 24 virtual v8::Handle<v8::Object> NewInstance() = 0; |
| 34 virtual v8::Handle<v8::Object> NewInstance(); | 25 |
| 26 // Invalidate this object so it cannot be used any more. This is needed |
| 27 // because it's possible for this to outlive its owner context. Invalidate |
| 28 // must be called before this happens. |
| 29 // |
| 30 // Subclasses should override to invalidate their own V8 state. If they do |
| 31 // they must call their superclass' Invalidate(). |
| 32 virtual void Invalidate(); |
| 35 | 33 |
| 36 protected: | 34 protected: |
| 37 typedef v8::Handle<v8::Value> (*HandlerFunc)(const v8::Arguments&); | 35 // Allow subclasses to query valid state. |
| 38 typedef base::Callback<v8::Handle<v8::Value>(const v8::Arguments&)> | 36 bool is_valid() { return is_valid_; } |
| 39 HandlerFunction; | |
| 40 | |
| 41 // Installs a new 'route' from |name| to |handler_function|. This means that | |
| 42 // NewInstance()s of this NativeHandler will have a property |name| which | |
| 43 // will be handled by |handler_function|. | |
| 44 void RouteFunction(const std::string& name, | |
| 45 const HandlerFunction& handler_function); | |
| 46 | |
| 47 void RouteStaticFunction(const std::string& name, | |
| 48 const HandlerFunc handler_func); | |
| 49 | 37 |
| 50 private: | 38 private: |
| 51 static v8::Handle<v8::Value> Router(const v8::Arguments& args); | 39 bool is_valid_; |
| 52 | |
| 53 std::vector<linked_ptr<HandlerFunction> > handler_functions_; | |
| 54 ScopedPersistent<v8::ObjectTemplate> object_template_; | |
| 55 | 40 |
| 56 DISALLOW_COPY_AND_ASSIGN(NativeHandler); | 41 DISALLOW_COPY_AND_ASSIGN(NativeHandler); |
| 57 }; | 42 }; |
| 58 | 43 |
| 59 } // extensions | 44 } // extensions |
| 60 | 45 |
| 61 #endif // CHROME_RENDERER_EXTENSIONS_NATIVE_HANDLER_H_ | 46 #endif // CHROME_RENDERER_EXTENSIONS_NATIVE_HANDLER_H_ |
| OLD | NEW |