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 |
(...skipping 22 matching lines...) Expand all Loading... |
33 typedef base::Callback<v8::Handle<v8::Value>(const v8::Arguments&)> | 33 typedef base::Callback<v8::Handle<v8::Value>(const v8::Arguments&)> |
34 HandlerFunction; | 34 HandlerFunction; |
35 | 35 |
36 // 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 |
37 // NewInstance()s of this ObjectBackedNativeHandler will have a property | 37 // NewInstance()s of this ObjectBackedNativeHandler will have a property |
38 // |name| which will be handled by |handler_function|. | 38 // |name| which will be handled by |handler_function|. |
39 void RouteFunction(const std::string& name, | 39 void RouteFunction(const std::string& name, |
40 const HandlerFunction& handler_function); | 40 const HandlerFunction& handler_function); |
41 | 41 |
42 void RouteStaticFunction(const std::string& name, | 42 void RouteStaticFunction(const std::string& name, |
43 const HandlerFunc handler_func); | 43 const HandlerFunc& handler_func); |
44 | 44 |
45 v8::Handle<v8::Context> v8_context() { return v8_context_.get(); } | 45 v8::Handle<v8::Context> v8_context() { return v8_context_.get(); } |
46 | 46 |
47 virtual void Invalidate() OVERRIDE; | 47 virtual void Invalidate() OVERRIDE; |
48 | 48 |
49 private: | 49 private: |
50 | 50 // Callback for RouteFunction which routes the V8 call to the correct |
| 51 // base::Bound callback. |
51 static v8::Handle<v8::Value> Router(const v8::Arguments& args); | 52 static v8::Handle<v8::Value> Router(const v8::Arguments& args); |
52 | 53 |
53 struct RouterData; | 54 // When RouteFunction is called we create a v8::Object to hold the data we |
54 std::vector<linked_ptr<RouterData> > router_data_; | 55 // need when handling it in Router() - this is the base::Bound function to |
| 56 // route to. |
| 57 // |
| 58 // We need a v8::Object because it's possible for v8 to outlive the |
| 59 // base::Bound function; the lifetime of an ObjectBackedNativeHandler is the |
| 60 // lifetime of webkit's involvement with it, not the life of the v8 context. |
| 61 // A scenario when v8 will outlive us is if a frame holds onto the |
| 62 // contentWindow of an iframe after it's removed. |
| 63 // |
| 64 // So, we use v8::Objects here to hold that data, effectively refcounting |
| 65 // the data. When |this| is destroyed we remove the base::Bound function from |
| 66 // the object to indicate that it shoudn't be called. |
| 67 typedef std::vector<v8::Persistent<v8::Object> > RouterData; |
| 68 RouterData router_data_; |
55 | 69 |
56 // TODO(kalman): Just pass around a ChromeV8Context. It already has a | 70 // TODO(kalman): Just pass around a ChromeV8Context. It already has a |
57 // persistent handle to this context. | 71 // persistent handle to this context. |
58 ScopedPersistent<v8::Context> v8_context_; | 72 ScopedPersistent<v8::Context> v8_context_; |
59 | 73 |
60 ScopedPersistent<v8::ObjectTemplate> object_template_; | 74 ScopedPersistent<v8::ObjectTemplate> object_template_; |
61 | 75 |
62 DISALLOW_COPY_AND_ASSIGN(ObjectBackedNativeHandler); | 76 DISALLOW_COPY_AND_ASSIGN(ObjectBackedNativeHandler); |
63 }; | 77 }; |
64 | 78 |
65 } // extensions | 79 } // extensions |
66 | 80 |
67 #endif // CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ | 81 #endif // CHROME_RENDERER_EXTENSIONS_OBJECT_BACKED_NATIVE_HANDLER_H_ |
OLD | NEW |