[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 5 | #ifndef EXTENSIONS_RENDERER_OBJECT_BACKED_NATIVE_HANDLER_H_ |
6 | #define EXTENSIONS_RENDERER_OBJECT_BACKED_NATIVE_HANDLER_H_ | ||||
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 7 | |
8 | #include <string> | ||||
9 | #include <vector> | ||||
10 | |||||
11 | #include "base/bind.h" | ||||
12 | #include "base/memory/linked_ptr.h" | ||||
[email protected] | b8ce52f | 2014-04-04 22:45:15 | [diff] [blame] | 13 | #include "extensions/renderer/native_handler.h" |
[email protected] | b61750a | 2014-04-04 18:27:53 | [diff] [blame] | 14 | #include "v8/include/v8-util.h" |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 15 | #include "v8/include/v8.h" |
16 | |||||
17 | namespace extensions { | ||||
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 18 | class ScriptContext; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 19 | |
20 | // An ObjectBackedNativeHandler is a factory for JS objects with functions on | ||||
21 | // them that map to native C++ functions. Subclasses should call RouteFunction() | ||||
22 | // in their constructor to define functions on the created JS objects. | ||||
23 | class ObjectBackedNativeHandler : public NativeHandler { | ||||
24 | public: | ||||
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 25 | explicit ObjectBackedNativeHandler(ScriptContext* context); |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 26 | ~ObjectBackedNativeHandler() override; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 27 | |
28 | // Create an object with bindings to the native functions defined through | ||||
29 | // RouteFunction(). | ||||
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 30 | v8::Handle<v8::Object> NewInstance() override; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 31 | |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 32 | v8::Isolate* GetIsolate() const; |
33 | |||||
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 34 | protected: |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 35 | typedef base::Callback<void(const v8::FunctionCallbackInfo<v8::Value>&)> |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 36 | HandlerFunction; |
37 | |||||
38 | // Installs a new 'route' from |name| to |handler_function|. This means that | ||||
39 | // NewInstance()s of this ObjectBackedNativeHandler will have a property | ||||
40 | // |name| which will be handled by |handler_function|. | ||||
kalman | b0c1c50 | 2015-04-15 00:25:06 | [diff] [blame^] | 41 | // |
42 | // Routed functions are destroyed along with the destruction of this class, | ||||
43 | // and are never called back into, therefore it's safe for |handler_function| | ||||
44 | // to bind to base::Unretained. | ||||
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 45 | void RouteFunction(const std::string& name, |
46 | const HandlerFunction& handler_function); | ||||
47 | |||||
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 48 | ScriptContext* context() const { return context_; } |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 49 | |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 50 | void Invalidate() override; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 51 | |
52 | private: | ||||
[email protected] | d876ffdf | 2013-03-14 03:40:26 | [diff] [blame] | 53 | // Callback for RouteFunction which routes the V8 call to the correct |
54 | // base::Bound callback. | ||||
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 55 | static void Router(const v8::FunctionCallbackInfo<v8::Value>& args); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 56 | |
[email protected] | d876ffdf | 2013-03-14 03:40:26 | [diff] [blame] | 57 | // When RouteFunction is called we create a v8::Object to hold the data we |
58 | // need when handling it in Router() - this is the base::Bound function to | ||||
59 | // route to. | ||||
60 | // | ||||
61 | // We need a v8::Object because it's possible for v8 to outlive the | ||||
62 | // base::Bound function; the lifetime of an ObjectBackedNativeHandler is the | ||||
63 | // lifetime of webkit's involvement with it, not the life of the v8 context. | ||||
64 | // A scenario when v8 will outlive us is if a frame holds onto the | ||||
65 | // contentWindow of an iframe after it's removed. | ||||
66 | // | ||||
67 | // So, we use v8::Objects here to hold that data, effectively refcounting | ||||
68 | // the data. When |this| is destroyed we remove the base::Bound function from | ||||
69 | // the object to indicate that it shoudn't be called. | ||||
[email protected] | b61750a | 2014-04-04 18:27:53 | [diff] [blame] | 70 | typedef v8::PersistentValueVector<v8::Object> RouterData; |
[email protected] | d876ffdf | 2013-03-14 03:40:26 | [diff] [blame] | 71 | RouterData router_data_; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 72 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 73 | ScriptContext* context_; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 74 | |
kalman | 83292b8 | 2015-03-12 16:40:25 | [diff] [blame] | 75 | v8::Global<v8::ObjectTemplate> object_template_; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 76 | |
77 | DISALLOW_COPY_AND_ASSIGN(ObjectBackedNativeHandler); | ||||
78 | }; | ||||
79 | |||||
[email protected] | 1002d343 | 2013-06-13 09:04:54 | [diff] [blame] | 80 | } // namespace extensions |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 81 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 82 | #endif // EXTENSIONS_RENDERER_OBJECT_BACKED_NATIVE_HANDLER_H_ |