Chromium Code Reviews| 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_OBJECT_BACKED_NATIVE_HANDLER_H_ | 5 #ifndef CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ |
| 6 #define CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 13 #include "chrome/renderer/extensions/native_handler.h" | 13 #include "chrome/renderer/extensions/native_handler.h" |
| 14 #include "chrome/renderer/extensions/scoped_persistent.h" | 14 #include "chrome/renderer/extensions/scoped_persistent.h" |
| 15 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
| 16 | 16 |
| 17 namespace extensions { | 17 namespace extensions { |
| 18 class ChromeV8Context; | |
| 18 | 19 |
| 19 // An ObjectBackedNativeHandler is a factory for JS objects with functions on | 20 // An ObjectBackedNativeHandler is a factory for JS objects with functions on |
| 20 // them that map to native C++ functions. Subclasses should call RouteFunction() | 21 // them that map to native C++ functions. Subclasses should call RouteFunction() |
| 21 // in their constructor to define functions on the created JS objects. | 22 // in their constructor to define functions on the created JS objects. |
| 22 class ObjectBackedNativeHandler : public NativeHandler { | 23 class ObjectBackedNativeHandler : public NativeHandler { |
| 23 public: | 24 public: |
| 24 explicit ObjectBackedNativeHandler(v8::Handle<v8::Context> context); | 25 explicit ObjectBackedNativeHandler(ChromeV8Context* context); |
| 25 virtual ~ObjectBackedNativeHandler(); | 26 virtual ~ObjectBackedNativeHandler(); |
| 26 | 27 |
| 27 // Create an object with bindings to the native functions defined through | 28 // Create an object with bindings to the native functions defined through |
| 28 // RouteFunction(). | 29 // RouteFunction(). |
| 29 virtual v8::Handle<v8::Object> NewInstance() OVERRIDE; | 30 virtual v8::Handle<v8::Object> NewInstance() OVERRIDE; |
| 30 | 31 |
| 31 protected: | 32 protected: |
| 32 typedef base::Callback<v8::Handle<v8::Value>(const v8::Arguments&)> | 33 typedef base::Callback<v8::Handle<v8::Value>(const v8::Arguments&)> |
| 33 HandlerFunction; | 34 HandlerFunction; |
| 34 | 35 |
| 35 // Installs a new 'route' from |name| to |handler_function|. This means that | 36 // Installs a new 'route' from |name| to |handler_function|. This means that |
| 36 // NewInstance()s of this ObjectBackedNativeHandler will have a property | 37 // NewInstance()s of this ObjectBackedNativeHandler will have a property |
| 37 // |name| which will be handled by |handler_function|. | 38 // |name| which will be handled by |handler_function|. |
| 38 void RouteFunction(const std::string& name, | 39 void RouteFunction(const std::string& name, |
| 39 const HandlerFunction& handler_function); | 40 const HandlerFunction& handler_function); |
| 40 | 41 |
| 41 v8::Handle<v8::Context> v8_context() { return v8_context_.get(); } | 42 v8::Handle<v8::Context> v8_context(); |
|
not at google - send to devlin
2013/05/29 15:55:13
can this be made to return a ChromeV8Context* (and
marja
2013/05/29 16:56:33
Done.
| |
| 42 | 43 |
| 43 virtual void Invalidate() OVERRIDE; | 44 virtual void Invalidate() OVERRIDE; |
| 44 | 45 |
| 46 protected: | |
| 47 ChromeV8Context* context_; | |
|
not at google - send to devlin
2013/05/29 15:55:13
then this can be private
marja
2013/05/29 16:56:33
Done.
| |
| 48 | |
| 45 private: | 49 private: |
| 46 // Callback for RouteFunction which routes the V8 call to the correct | 50 // Callback for RouteFunction which routes the V8 call to the correct |
| 47 // base::Bound callback. | 51 // base::Bound callback. |
| 48 static v8::Handle<v8::Value> Router(const v8::Arguments& args); | 52 static v8::Handle<v8::Value> Router(const v8::Arguments& args); |
| 49 | 53 |
| 50 // When RouteFunction is called we create a v8::Object to hold the data we | 54 // When RouteFunction is called we create a v8::Object to hold the data we |
| 51 // need when handling it in Router() - this is the base::Bound function to | 55 // need when handling it in Router() - this is the base::Bound function to |
| 52 // route to. | 56 // route to. |
| 53 // | 57 // |
| 54 // We need a v8::Object because it's possible for v8 to outlive the | 58 // We need a v8::Object because it's possible for v8 to outlive the |
| 55 // base::Bound function; the lifetime of an ObjectBackedNativeHandler is the | 59 // base::Bound function; the lifetime of an ObjectBackedNativeHandler is the |
| 56 // lifetime of webkit's involvement with it, not the life of the v8 context. | 60 // lifetime of webkit's involvement with it, not the life of the v8 context. |
| 57 // A scenario when v8 will outlive us is if a frame holds onto the | 61 // A scenario when v8 will outlive us is if a frame holds onto the |
| 58 // contentWindow of an iframe after it's removed. | 62 // contentWindow of an iframe after it's removed. |
| 59 // | 63 // |
| 60 // So, we use v8::Objects here to hold that data, effectively refcounting | 64 // So, we use v8::Objects here to hold that data, effectively refcounting |
| 61 // the data. When |this| is destroyed we remove the base::Bound function from | 65 // the data. When |this| is destroyed we remove the base::Bound function from |
| 62 // the object to indicate that it shoudn't be called. | 66 // the object to indicate that it shoudn't be called. |
| 63 typedef std::vector<v8::Persistent<v8::Object> > RouterData; | 67 typedef std::vector<v8::Persistent<v8::Object> > RouterData; |
| 64 RouterData router_data_; | 68 RouterData router_data_; |
| 65 | 69 |
| 66 // TODO(kalman): Just pass around a ChromeV8Context. It already has a | |
| 67 // persistent handle to this context. | |
| 68 ScopedPersistent<v8::Context> v8_context_; | |
| 69 | |
| 70 ScopedPersistent<v8::ObjectTemplate> object_template_; | 70 ScopedPersistent<v8::ObjectTemplate> object_template_; |
| 71 | 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(ObjectBackedNativeHandler); | 72 DISALLOW_COPY_AND_ASSIGN(ObjectBackedNativeHandler); |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 } // extensions | 75 } // extensions |
| 76 | 76 |
| 77 #endif // CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ | 77 #endif // CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ |
| OLD | NEW |