OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/mojo/blink_connector_js_wrapper.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "content/renderer/mojo/blink_connector_impl.h" |
| 11 #include "mojo/edk/js/handle.h" |
| 12 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 gin::WrapperInfo BlinkConnectorJsWrapper::kWrapperInfo = { |
| 17 gin::kEmbedderNativeGin}; |
| 18 const char BlinkConnectorJsWrapper::kModuleName[] = |
| 19 "content/public/renderer/connector"; |
| 20 |
| 21 BlinkConnectorJsWrapper::~BlinkConnectorJsWrapper() {} |
| 22 |
| 23 // static |
| 24 gin::Handle<BlinkConnectorJsWrapper> BlinkConnectorJsWrapper::Create( |
| 25 v8::Isolate* isolate, |
| 26 v8::Handle<v8::Context> context, |
| 27 BlinkConnectorImpl* connector) { |
| 28 return gin::CreateHandle( |
| 29 isolate, |
| 30 new BlinkConnectorJsWrapper(isolate, context, connector->GetWeakPtr())); |
| 31 } |
| 32 |
| 33 gin::ObjectTemplateBuilder BlinkConnectorJsWrapper::GetObjectTemplateBuilder( |
| 34 v8::Isolate* isolate) { |
| 35 return Wrappable<BlinkConnectorJsWrapper>::GetObjectTemplateBuilder(isolate) |
| 36 .SetMethod("bindInterface", &BlinkConnectorJsWrapper::BindInterface) |
| 37 .SetMethod("addInterfaceOverrideForTesting", |
| 38 &BlinkConnectorJsWrapper::AddOverrideForTesting) |
| 39 .SetMethod("clearInterfaceOverridesForTesting", |
| 40 &BlinkConnectorJsWrapper::ClearOverridesForTesting); |
| 41 } |
| 42 |
| 43 mojo::Handle BlinkConnectorJsWrapper::BindInterface( |
| 44 const std::string& service_name, |
| 45 const std::string& interface_name) { |
| 46 mojo::MessagePipe pipe; |
| 47 if (connector_) { |
| 48 connector_->bindInterface(service_name.c_str(), interface_name.c_str(), |
| 49 std::move(pipe.handle0)); |
| 50 } |
| 51 return pipe.handle1.release(); |
| 52 } |
| 53 |
| 54 void BlinkConnectorJsWrapper::AddOverrideForTesting( |
| 55 const std::string& service_name, |
| 56 const std::string& interface_name, |
| 57 v8::Local<v8::Function> service_factory) { |
| 58 ScopedJsFactory factory(v8::Isolate::GetCurrent(), service_factory); |
| 59 connector_->AddOverrideForTesting( |
| 60 service_name, interface_name, |
| 61 base::Bind(&BlinkConnectorJsWrapper::CallJsFactory, |
| 62 weak_factory_.GetWeakPtr(), factory)); |
| 63 } |
| 64 |
| 65 void BlinkConnectorJsWrapper::ClearOverridesForTesting() { |
| 66 connector_->ClearOverridesForTesting(); |
| 67 } |
| 68 |
| 69 BlinkConnectorJsWrapper::BlinkConnectorJsWrapper( |
| 70 v8::Isolate* isolate, |
| 71 v8::Handle<v8::Context> context, |
| 72 base::WeakPtr<BlinkConnectorImpl> connector) |
| 73 : isolate_(isolate), |
| 74 context_(isolate, context), |
| 75 connector_(connector), |
| 76 weak_factory_(this) { |
| 77 context_.SetWeak(this, &BlinkConnectorJsWrapper::ClearContext, |
| 78 v8::WeakCallbackType::kParameter); |
| 79 } |
| 80 |
| 81 void BlinkConnectorJsWrapper::CallJsFactory( |
| 82 const ScopedJsFactory& factory, |
| 83 mojo::ScopedMessagePipeHandle pipe) { |
| 84 if (context_.IsEmpty()) |
| 85 return; |
| 86 |
| 87 v8::HandleScope handle_scope(isolate_); |
| 88 v8::Handle<v8::Context> context = context_.Get(isolate_); |
| 89 v8::Context::Scope context_scope(context); |
| 90 v8::Local<v8::Value> argv[] = { |
| 91 gin::ConvertToV8(isolate_, mojo::Handle(pipe.release().value()))}; |
| 92 blink::WebLocalFrame::frameForContext(context) |
| 93 ->callFunctionEvenIfScriptDisabled(factory.Get(isolate_), |
| 94 v8::Undefined(isolate_), 1, argv); |
| 95 } |
| 96 |
| 97 // static |
| 98 void BlinkConnectorJsWrapper::ClearContext( |
| 99 const v8::WeakCallbackInfo<BlinkConnectorJsWrapper>& data) { |
| 100 BlinkConnectorJsWrapper* registry = data.GetParameter(); |
| 101 registry->context_.Reset(); |
| 102 } |
| 103 |
| 104 } // namespace content |
OLD | NEW |