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 ChromeV8Context* context() { return context_; } |
42 | 43 |
43 virtual void Invalidate() OVERRIDE; | 44 virtual void Invalidate() OVERRIDE; |
44 | 45 |
45 private: | 46 private: |
46 // Callback for RouteFunction which routes the V8 call to the correct | 47 // Callback for RouteFunction which routes the V8 call to the correct |
47 // base::Bound callback. | 48 // base::Bound callback. |
48 static v8::Handle<v8::Value> Router(const v8::Arguments& args); | 49 static v8::Handle<v8::Value> Router(const v8::Arguments& args); |
49 | 50 |
50 // When RouteFunction is called we create a v8::Object to hold the data we | 51 // 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 | 52 // need when handling it in Router() - this is the base::Bound function to |
52 // route to. | 53 // route to. |
53 // | 54 // |
54 // We need a v8::Object because it's possible for v8 to outlive the | 55 // 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 | 56 // 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. | 57 // 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 | 58 // A scenario when v8 will outlive us is if a frame holds onto the |
58 // contentWindow of an iframe after it's removed. | 59 // contentWindow of an iframe after it's removed. |
59 // | 60 // |
60 // So, we use v8::Objects here to hold that data, effectively refcounting | 61 // 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 | 62 // the data. When |this| is destroyed we remove the base::Bound function from |
62 // the object to indicate that it shoudn't be called. | 63 // the object to indicate that it shoudn't be called. |
63 typedef std::vector<v8::Persistent<v8::Object> > RouterData; | 64 typedef std::vector<v8::Persistent<v8::Object> > RouterData; |
64 RouterData router_data_; | 65 RouterData router_data_; |
65 | 66 |
66 // TODO(kalman): Just pass around a ChromeV8Context. It already has a | 67 ChromeV8Context* context_; |
67 // persistent handle to this context. | |
68 ScopedPersistent<v8::Context> v8_context_; | |
69 | 68 |
70 ScopedPersistent<v8::ObjectTemplate> object_template_; | 69 ScopedPersistent<v8::ObjectTemplate> object_template_; |
71 | 70 |
72 DISALLOW_COPY_AND_ASSIGN(ObjectBackedNativeHandler); | 71 DISALLOW_COPY_AND_ASSIGN(ObjectBackedNativeHandler); |
73 }; | 72 }; |
74 | 73 |
75 } // extensions | 74 } // extensions |
76 | 75 |
77 #endif // CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ | 76 #endif // CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ |
OLD | NEW |