| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/mojo/service_registry_js_wrapper.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "content/common/mojo/service_registry_impl.h" | |
| 11 #include "content/public/common/service_registry.h" | |
| 12 #include "mojo/edk/js/handle.h" | |
| 13 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 17 gin::WrapperInfo ServiceRegistryJsWrapper::kWrapperInfo = { | |
| 18 gin::kEmbedderNativeGin}; | |
| 19 const char ServiceRegistryJsWrapper::kPerFrameModuleName[] = | |
| 20 "content/public/renderer/frame_service_registry"; | |
| 21 const char ServiceRegistryJsWrapper::kPerProcessModuleName[] = | |
| 22 "content/public/renderer/service_registry"; | |
| 23 | |
| 24 ServiceRegistryJsWrapper::~ServiceRegistryJsWrapper() { | |
| 25 } | |
| 26 | |
| 27 // static | |
| 28 gin::Handle<ServiceRegistryJsWrapper> ServiceRegistryJsWrapper::Create( | |
| 29 v8::Isolate* isolate, | |
| 30 v8::Handle<v8::Context> context, | |
| 31 ServiceRegistry* service_registry) { | |
| 32 return gin::CreateHandle( | |
| 33 isolate, | |
| 34 new ServiceRegistryJsWrapper( | |
| 35 isolate, context, | |
| 36 static_cast<ServiceRegistryImpl*>(service_registry)->GetWeakPtr())); | |
| 37 } | |
| 38 | |
| 39 gin::ObjectTemplateBuilder ServiceRegistryJsWrapper::GetObjectTemplateBuilder( | |
| 40 v8::Isolate* isolate) { | |
| 41 return Wrappable<ServiceRegistryJsWrapper>::GetObjectTemplateBuilder(isolate) | |
| 42 .SetMethod("connectToService", | |
| 43 &ServiceRegistryJsWrapper::ConnectToService) | |
| 44 .SetMethod("addServiceOverrideForTesting", | |
| 45 &ServiceRegistryJsWrapper::AddServiceOverrideForTesting) | |
| 46 .SetMethod("clearServiceOverridesForTesting", | |
| 47 &ServiceRegistryJsWrapper::ClearServiceOverridesForTesting); | |
| 48 } | |
| 49 | |
| 50 mojo::Handle ServiceRegistryJsWrapper::ConnectToService( | |
| 51 const std::string& service_name) { | |
| 52 mojo::MessagePipe pipe; | |
| 53 if (service_registry_) | |
| 54 service_registry_->ConnectToRemoteService(service_name, | |
| 55 std::move(pipe.handle0)); | |
| 56 return pipe.handle1.release(); | |
| 57 } | |
| 58 | |
| 59 void ServiceRegistryJsWrapper::AddServiceOverrideForTesting( | |
| 60 const std::string& service_name, | |
| 61 v8::Local<v8::Function> service_factory) { | |
| 62 ServiceRegistry* registry = service_registry_.get(); | |
| 63 if (!registry) | |
| 64 return; | |
| 65 ScopedJsFactory factory(v8::Isolate::GetCurrent(), service_factory); | |
| 66 registry->AddServiceOverrideForTesting( | |
| 67 service_name, base::Bind(&ServiceRegistryJsWrapper::CallJsFactory, | |
| 68 weak_factory_.GetWeakPtr(), factory)); | |
| 69 } | |
| 70 | |
| 71 void ServiceRegistryJsWrapper::ClearServiceOverridesForTesting() { | |
| 72 ServiceRegistryImpl* registry = | |
| 73 static_cast<ServiceRegistryImpl*>(service_registry_.get()); | |
| 74 if (!registry) | |
| 75 return; | |
| 76 | |
| 77 registry->ClearServiceOverridesForTesting(); | |
| 78 } | |
| 79 | |
| 80 ServiceRegistryJsWrapper::ServiceRegistryJsWrapper( | |
| 81 v8::Isolate* isolate, | |
| 82 v8::Handle<v8::Context> context, | |
| 83 base::WeakPtr<ServiceRegistry> service_registry) | |
| 84 : isolate_(isolate), | |
| 85 context_(isolate, context), | |
| 86 service_registry_(service_registry), | |
| 87 weak_factory_(this) { | |
| 88 context_.SetWeak(this, &ServiceRegistryJsWrapper::ClearContext, | |
| 89 v8::WeakCallbackType::kParameter); | |
| 90 } | |
| 91 | |
| 92 void ServiceRegistryJsWrapper::CallJsFactory( | |
| 93 const ScopedJsFactory& factory, | |
| 94 mojo::ScopedMessagePipeHandle pipe) { | |
| 95 if (context_.IsEmpty()) | |
| 96 return; | |
| 97 | |
| 98 v8::HandleScope handle_scope(isolate_); | |
| 99 v8::Handle<v8::Context> context = context_.Get(isolate_); | |
| 100 v8::Context::Scope context_scope(context); | |
| 101 v8::Local<v8::Value> argv[] = { | |
| 102 gin::ConvertToV8(isolate_, mojo::Handle(pipe.release().value()))}; | |
| 103 blink::WebLocalFrame::frameForContext(context) | |
| 104 ->callFunctionEvenIfScriptDisabled(factory.Get(isolate_), | |
| 105 v8::Undefined(isolate_), 1, argv); | |
| 106 } | |
| 107 | |
| 108 // static | |
| 109 void ServiceRegistryJsWrapper::ClearContext( | |
| 110 const v8::WeakCallbackInfo<ServiceRegistryJsWrapper>& data) { | |
| 111 ServiceRegistryJsWrapper* service_registry = data.GetParameter(); | |
| 112 service_registry->context_.Reset(); | |
| 113 } | |
| 114 | |
| 115 } // namespace content | |
| OLD | NEW |