OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 CONTENT_RENDERER_MOJO_INTERFACE_PROVIDER_JS_WRAPPER_H_ | 5 #ifndef CONTENT_RENDERER_MOJO_INTERFACE_PROVIDER_JS_WRAPPER_H_ |
6 #define CONTENT_RENDERER_MOJO_INTERFACE_PROVIDER_JS_WRAPPER_H_ | 6 #define CONTENT_RENDERER_MOJO_INTERFACE_PROVIDER_JS_WRAPPER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "content/common/content_export.h" | 11 #include "content/common/content_export.h" |
12 #include "gin/handle.h" | 12 #include "gin/handle.h" |
13 #include "gin/object_template_builder.h" | 13 #include "gin/object_template_builder.h" |
14 #include "gin/wrappable.h" | 14 #include "gin/wrappable.h" |
15 #include "mojo/public/cpp/system/message_pipe.h" | 15 #include "mojo/public/cpp/system/message_pipe.h" |
16 #include "v8/include/v8.h" | 16 #include "v8/include/v8.h" |
17 | 17 |
18 namespace service_manager { | 18 namespace service_manager { |
| 19 class Connector; |
19 class InterfaceProvider; | 20 class InterfaceProvider; |
20 } | 21 } |
21 | 22 |
22 namespace content { | 23 namespace content { |
23 | 24 |
24 // A JS wrapper around service_manager::InterfaceProvider that allows connecting | 25 // A JS wrapper around service_manager::Connector/InterfaceProvider that allows |
25 // to | 26 // script to bind interfaces exposed by the browser. |
26 // remote services. | |
27 class CONTENT_EXPORT InterfaceProviderJsWrapper | 27 class CONTENT_EXPORT InterfaceProviderJsWrapper |
28 : public gin::Wrappable<InterfaceProviderJsWrapper> { | 28 : public gin::Wrappable<InterfaceProviderJsWrapper> { |
29 public: | 29 public: |
30 ~InterfaceProviderJsWrapper() override; | 30 ~InterfaceProviderJsWrapper() override; |
31 static gin::Handle<InterfaceProviderJsWrapper> Create( | 31 static gin::Handle<InterfaceProviderJsWrapper> Create( |
32 v8::Isolate* isolate, | 32 v8::Isolate* isolate, |
33 v8::Handle<v8::Context> context, | 33 v8::Handle<v8::Context> context, |
| 34 service_manager::Connector* connector); |
| 35 static gin::Handle<InterfaceProviderJsWrapper> Create( |
| 36 v8::Isolate* isolate, |
| 37 v8::Handle<v8::Context> context, |
34 service_manager::InterfaceProvider* remote_interfaces); | 38 service_manager::InterfaceProvider* remote_interfaces); |
35 | 39 |
36 // gin::Wrappable<InterfaceProviderJsWrapper> overrides. | 40 // gin::Wrappable<InterfaceProviderJsWrapper> overrides. |
37 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | 41 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
38 v8::Isolate* isolate) override; | 42 v8::Isolate* isolate) override; |
39 | 43 |
40 // JS interface implementation. | 44 // JS interface implementation. |
41 void AddOverrideForTesting(const std::string& interface_name, | 45 void AddOverrideForTesting(const std::string& interface_name, |
42 v8::Local<v8::Function> interface_factory); | 46 v8::Local<v8::Function> interface_factory); |
43 void ClearOverridesForTesting(); | 47 void ClearOverridesForTesting(); |
44 mojo::Handle GetInterface(const std::string& interface_name); | 48 mojo::Handle GetInterface(const std::string& interface_name); |
45 | 49 |
46 static gin::WrapperInfo kWrapperInfo; | 50 static gin::WrapperInfo kWrapperInfo; |
47 static const char kPerFrameModuleName[]; | 51 static const char kPerFrameModuleName[]; |
48 static const char kPerProcessModuleName[]; | 52 static const char kPerProcessModuleName[]; |
49 | 53 |
50 private: | 54 private: |
51 using ScopedJsFactory = | 55 using ScopedJsFactory = |
52 v8::Persistent<v8::Function, v8::CopyablePersistentTraits<v8::Function>>; | 56 v8::Persistent<v8::Function, v8::CopyablePersistentTraits<v8::Function>>; |
| 57 using BindCallback = base::Callback<void(mojo::ScopedMessagePipeHandle)>; |
53 | 58 |
54 InterfaceProviderJsWrapper( | 59 InterfaceProviderJsWrapper( |
55 v8::Isolate* isolate, | 60 v8::Isolate* isolate, |
56 v8::Handle<v8::Context> context, | 61 v8::Handle<v8::Context> context, |
| 62 base::WeakPtr<service_manager::Connector> connector); |
| 63 InterfaceProviderJsWrapper( |
| 64 v8::Isolate* isolate, |
| 65 v8::Handle<v8::Context> context, |
57 base::WeakPtr<service_manager::InterfaceProvider> remote_interfaces); | 66 base::WeakPtr<service_manager::InterfaceProvider> remote_interfaces); |
58 | 67 |
59 void CallJsFactory(const ScopedJsFactory& factory, | 68 void CallJsFactory(const ScopedJsFactory& factory, |
60 mojo::ScopedMessagePipeHandle pipe); | 69 mojo::ScopedMessagePipeHandle pipe); |
61 | 70 |
62 static void ClearContext( | 71 static void ClearContext( |
63 const v8::WeakCallbackInfo<InterfaceProviderJsWrapper>& data); | 72 const v8::WeakCallbackInfo<InterfaceProviderJsWrapper>& data); |
64 | 73 |
65 v8::Isolate* isolate_; | 74 v8::Isolate* isolate_; |
66 v8::Global<v8::Context> context_; | 75 v8::Global<v8::Context> context_; |
| 76 // Either one of the following two fields will be non-null. |
| 77 base::WeakPtr<service_manager::Connector> connector_; |
67 base::WeakPtr<service_manager::InterfaceProvider> remote_interfaces_; | 78 base::WeakPtr<service_manager::InterfaceProvider> remote_interfaces_; |
68 | 79 |
69 base::WeakPtrFactory<InterfaceProviderJsWrapper> weak_factory_; | 80 base::WeakPtrFactory<InterfaceProviderJsWrapper> weak_factory_; |
70 | 81 |
71 DISALLOW_COPY_AND_ASSIGN(InterfaceProviderJsWrapper); | 82 DISALLOW_COPY_AND_ASSIGN(InterfaceProviderJsWrapper); |
72 }; | 83 }; |
73 | 84 |
74 } // namespace content | 85 } // namespace content |
75 | 86 |
76 #endif // CONTENT_RENDERER_MOJO_INTERFACE_PROVIDER_JS_WRAPPER_H_ | 87 #endif // CONTENT_RENDERER_MOJO_INTERFACE_PROVIDER_JS_WRAPPER_H_ |
OLD | NEW |