OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "components/nacl/common/nacl_service.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "content/public/common/mojo_channel_switches.h" |
| 11 #include "content/public/common/service_names.mojom.h" |
| 12 #include "ipc/ipc.mojom.h" |
| 13 #include "mojo/edk/embedder/embedder.h" |
| 14 #include "mojo/edk/embedder/scoped_ipc_support.h" |
| 15 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 16 #include "services/service_manager/public/cpp/interface_registry.h" |
| 17 #include "services/service_manager/public/cpp/service.h" |
| 18 #include "services/service_manager/public/cpp/service_context.h" |
| 19 #include "services/service_manager/public/cpp/service_info.h" |
| 20 |
| 21 #if defined(OS_POSIX) |
| 22 #include "base/posix/global_descriptors.h" |
| 23 #include "content/public/common/content_descriptors.h" |
| 24 #elif defined(OS_WIN) |
| 25 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 26 #endif |
| 27 |
| 28 namespace { |
| 29 |
| 30 void EstablishMojoConnection() { |
| 31 #if defined(OS_WIN) |
| 32 mojo::edk::ScopedPlatformHandle platform_channel( |
| 33 mojo::edk::PlatformChannelPair::PassClientHandleFromParentProcess( |
| 34 *base::CommandLine::ForCurrentProcess())); |
| 35 #else |
| 36 mojo::edk::ScopedPlatformHandle platform_channel(mojo::edk::PlatformHandle( |
| 37 base::GlobalDescriptors::GetInstance()->Get(kMojoIPCChannel))); |
| 38 #endif |
| 39 DCHECK(platform_channel.is_valid()); |
| 40 mojo::edk::SetParentPipeHandle(std::move(platform_channel)); |
| 41 } |
| 42 |
| 43 service_manager::mojom::ServiceRequest ConnectToServiceManager() { |
| 44 const std::string service_request_channel_token = |
| 45 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 46 switches::kServiceRequestChannelToken); |
| 47 DCHECK(!service_request_channel_token.empty()); |
| 48 mojo::ScopedMessagePipeHandle parent_handle = |
| 49 mojo::edk::CreateChildMessagePipe(service_request_channel_token); |
| 50 DCHECK(parent_handle.is_valid()); |
| 51 return mojo::MakeRequest<service_manager::mojom::Service>( |
| 52 std::move(parent_handle)); |
| 53 } |
| 54 |
| 55 void ConnectBootstrapChannel(IPC::mojom::ChannelBootstrapPtrInfo ptr, |
| 56 IPC::mojom::ChannelBootstrapRequest request) { |
| 57 mojo::FuseInterface(std::move(request), std::move(ptr)); |
| 58 } |
| 59 |
| 60 class NaClService : public service_manager::Service { |
| 61 public: |
| 62 NaClService(IPC::mojom::ChannelBootstrapPtrInfo bootstrap, |
| 63 std::unique_ptr<mojo::edk::ScopedIPCSupport> ipc_support); |
| 64 ~NaClService() override; |
| 65 |
| 66 // Service overrides. |
| 67 bool OnConnect(const service_manager::ServiceInfo& remote_info, |
| 68 service_manager::InterfaceRegistry* registry) override; |
| 69 |
| 70 private: |
| 71 IPC::mojom::ChannelBootstrapPtrInfo ipc_channel_bootstrap_; |
| 72 std::unique_ptr<mojo::edk::ScopedIPCSupport> ipc_support_; |
| 73 bool connected_ = false; |
| 74 }; |
| 75 |
| 76 NaClService::NaClService( |
| 77 IPC::mojom::ChannelBootstrapPtrInfo bootstrap, |
| 78 std::unique_ptr<mojo::edk::ScopedIPCSupport> ipc_support) |
| 79 : ipc_channel_bootstrap_(std::move(bootstrap)), |
| 80 ipc_support_(std::move(ipc_support)) {} |
| 81 |
| 82 NaClService::~NaClService() = default; |
| 83 |
| 84 bool NaClService::OnConnect(const service_manager::ServiceInfo& remote_info, |
| 85 service_manager::InterfaceRegistry* registry) { |
| 86 if (remote_info.identity.name() != content::mojom::kBrowserServiceName) |
| 87 return false; |
| 88 |
| 89 if (connected_) |
| 90 return false; |
| 91 |
| 92 connected_ = true; |
| 93 registry->AddInterface(base::Bind(&ConnectBootstrapChannel, |
| 94 base::Passed(&ipc_channel_bootstrap_))); |
| 95 return true; |
| 96 } |
| 97 |
| 98 } // namespace |
| 99 |
| 100 std::unique_ptr<service_manager::ServiceContext> CreateNaClServiceContext( |
| 101 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| 102 mojo::ScopedMessagePipeHandle* ipc_channel) { |
| 103 auto ipc_support = |
| 104 base::MakeUnique<mojo::edk::ScopedIPCSupport>(std::move(io_task_runner)); |
| 105 EstablishMojoConnection(); |
| 106 |
| 107 IPC::mojom::ChannelBootstrapPtr bootstrap; |
| 108 *ipc_channel = mojo::GetProxy(&bootstrap).PassMessagePipe(); |
| 109 return base::MakeUnique<service_manager::ServiceContext>( |
| 110 base::MakeUnique<NaClService>(bootstrap.PassInterface(), |
| 111 std::move(ipc_support)), |
| 112 ConnectToServiceManager()); |
| 113 } |
OLD | NEW |