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 "content/common/mojo/service_registry_impl.h"
8#include "content/public/common/service_registry.h"
9#include "mojo/bindings/js/handle.h"
10
11namespace content {
12
13gin::WrapperInfo ServiceRegistryJsWrapper::kWrapperInfo = {
14    gin::kEmbedderNativeGin};
15const char ServiceRegistryJsWrapper::kModuleName[] =
16    "content/public/renderer/service_provider";
17
18ServiceRegistryJsWrapper::~ServiceRegistryJsWrapper() {
19}
20
21// static
22gin::Handle<ServiceRegistryJsWrapper> ServiceRegistryJsWrapper::Create(
23    v8::Isolate* isolate,
24    ServiceRegistry* service_registry) {
25  return gin::CreateHandle(
26      isolate,
27      new ServiceRegistryJsWrapper(
28          static_cast<ServiceRegistryImpl*>(service_registry)->GetWeakPtr()));
29}
30
31gin::ObjectTemplateBuilder ServiceRegistryJsWrapper::GetObjectTemplateBuilder(
32    v8::Isolate* isolate) {
33  return Wrappable<ServiceRegistryJsWrapper>::GetObjectTemplateBuilder(isolate).
34      SetMethod("connectToService",
35                &ServiceRegistryJsWrapper::ConnectToService);
36}
37
38mojo::Handle ServiceRegistryJsWrapper::ConnectToService(
39    const std::string& service_name) {
40  mojo::MessagePipe pipe;
41  if (service_registry_)
42    service_registry_->ConnectToRemoteService(service_name,
43                                              pipe.handle0.Pass());
44  return pipe.handle1.release();
45}
46
47ServiceRegistryJsWrapper::ServiceRegistryJsWrapper(
48    base::WeakPtr<ServiceRegistry> service_registry)
49    : service_registry_(service_registry) {
50}
51
52}  // namespace content
53