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_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ |
| 6 #define CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 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 |
| 16 namespace extensions { |
| 17 |
| 18 // An ObjectBackedNativeHandler is a factory for JS objects with functions on |
| 19 // them that map to native C++ functions. Subclasses should call RouteFunction() |
| 20 // in their constructor to define functions on the created JS objects. |
| 21 class ObjectBackedNativeHandler : public NativeHandler { |
| 22 public: |
| 23 explicit ObjectBackedNativeHandler(v8::Handle<v8::Context> context); |
| 24 virtual ~ObjectBackedNativeHandler(); |
| 25 |
| 26 // Create an object with bindings to the native functions defined through |
| 27 // RouteFunction(). |
| 28 virtual v8::Handle<v8::Object> NewInstance() OVERRIDE; |
| 29 |
| 30 protected: |
| 31 typedef v8::Handle<v8::Value> (*HandlerFunc)(const v8::Arguments&); |
| 32 typedef base::Callback<v8::Handle<v8::Value>(const v8::Arguments&)> |
| 33 HandlerFunction; |
| 34 |
| 35 // Installs a new 'route' from |name| to |handler_function|. This means that |
| 36 // NewInstance()s of this ObjectBackedNativeHandler will have a property |
| 37 // |name| which will be handled by |handler_function|. |
| 38 void RouteFunction(const std::string& name, |
| 39 const HandlerFunction& handler_function); |
| 40 |
| 41 void RouteStaticFunction(const std::string& name, |
| 42 const HandlerFunc handler_func); |
| 43 |
| 44 // Invalidate this object so it cannot be used any more. This is needed |
| 45 // because it's possible for this to outlive |v8_context_|. Invalidate must |
| 46 // be called before this happens. |
| 47 // |
| 48 // Subclasses may override to invalidate their own V8 state, but always |
| 49 // call superclass eventually. |
| 50 // |
| 51 // This must be idempotent. Returns true if it was the first time this was |
| 52 // called, false otherwise. |
| 53 virtual bool Invalidate(); |
| 54 |
| 55 v8::Handle<v8::Context> v8_context() { return v8_context_; } |
| 56 |
| 57 private: |
| 58 static v8::Handle<v8::Value> Router(const v8::Arguments& args); |
| 59 |
| 60 std::vector<linked_ptr<HandlerFunction> > handler_functions_; |
| 61 v8::Handle<v8::Context> v8_context_; |
| 62 v8::Persistent<v8::ObjectTemplate> object_template_; |
| 63 bool is_valid_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(ObjectBackedNativeHandler); |
| 66 }; |
| 67 |
| 68 } // extensions |
| 69 |
| 70 #endif // CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ |
OLD | NEW |