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 "chrome/renderer/extensions/scoped_persistent.h" |
| 15 #include "v8/include/v8.h" |
| 16 |
| 17 namespace extensions { |
| 18 |
| 19 // An ObjectBackedNativeHandler is a factory for JS objects with functions on |
| 20 // them that map to native C++ functions. Subclasses should call RouteFunction() |
| 21 // in their constructor to define functions on the created JS objects. |
| 22 class ObjectBackedNativeHandler : public NativeHandler { |
| 23 public: |
| 24 explicit ObjectBackedNativeHandler(v8::Handle<v8::Context> context); |
| 25 virtual ~ObjectBackedNativeHandler(); |
| 26 |
| 27 // Create an object with bindings to the native functions defined through |
| 28 // RouteFunction(). |
| 29 virtual v8::Handle<v8::Object> NewInstance() OVERRIDE; |
| 30 |
| 31 protected: |
| 32 typedef v8::Handle<v8::Value> (*HandlerFunc)(const v8::Arguments&); |
| 33 typedef base::Callback<v8::Handle<v8::Value>(const v8::Arguments&)> |
| 34 HandlerFunction; |
| 35 |
| 36 // Installs a new 'route' from |name| to |handler_function|. This means that |
| 37 // NewInstance()s of this ObjectBackedNativeHandler will have a property |
| 38 // |name| which will be handled by |handler_function|. |
| 39 void RouteFunction(const std::string& name, |
| 40 const HandlerFunction& handler_function); |
| 41 |
| 42 void RouteStaticFunction(const std::string& name, |
| 43 const HandlerFunc handler_func); |
| 44 |
| 45 v8::Handle<v8::Context> v8_context() { return v8_context_.get(); } |
| 46 |
| 47 virtual void Invalidate() OVERRIDE; |
| 48 |
| 49 private: |
| 50 |
| 51 static v8::Handle<v8::Value> Router(const v8::Arguments& args); |
| 52 |
| 53 struct RouterData; |
| 54 std::vector<linked_ptr<RouterData> > router_data_; |
| 55 |
| 56 // TODO(kalman): Just pass around a ChromeV8Context. It already has a |
| 57 // persistent handle to this context. |
| 58 ScopedPersistent<v8::Context> v8_context_; |
| 59 |
| 60 ScopedPersistent<v8::ObjectTemplate> object_template_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(ObjectBackedNativeHandler); |
| 63 }; |
| 64 |
| 65 } // extensions |
| 66 |
| 67 #endif // CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ |
OLD | NEW |