OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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/browser/renderer_host/pepper/pepper_mojo_host.h" |
| 6 |
| 7 #include "content/public/browser/browser_thread.h" |
| 8 #include "ipc/ipc_platform_file.h" |
| 9 #include "mojo/embedder/platform_channel_pair.h" |
| 10 #include "mojo/examples/echo/echo_service.mojom.h" |
| 11 #include "mojo/public/cpp/system/message_pipe.h" |
| 12 #include "ppapi/c/pp_errors.h" |
| 13 #include "ppapi/c/ppb_file_io.h" |
| 14 #include "ppapi/host/dispatch_host_message.h" |
| 15 #include "ppapi/host/ppapi_host.h" |
| 16 #include "ppapi/proxy/dispatch_reply_message.h" |
| 17 #include "ppapi/proxy/ppapi_messages.h" |
| 18 |
| 19 namespace content { |
| 20 |
| 21 namespace { |
| 22 base::PlatformFile PlatformFileFromScopedPlatformHandle( |
| 23 mojo::embedder::ScopedPlatformHandle handle) { |
| 24 #if defined(OS_POSIX) |
| 25 return handle.release().fd; |
| 26 #elif defined(OS_WIN) |
| 27 return handle.release().handle; |
| 28 #endif |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 |
| 34 PepperMojoHost::PepperMojoHost(BrowserPpapiHost* host, |
| 35 PP_Instance instance, |
| 36 PP_Resource resource) |
| 37 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 38 browser_ppapi_host_(host) { |
| 39 } |
| 40 |
| 41 int32_t PepperMojoHost::OnResourceMessageReceived( |
| 42 const IPC::Message& msg, |
| 43 ppapi::host::HostMessageContext* context) { |
| 44 PPAPI_BEGIN_MESSAGE_MAP(PepperMojoHost, msg) |
| 45 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_Mojo_GetHandle, |
| 46 OnGetHandle) |
| 47 PPAPI_END_MESSAGE_MAP() |
| 48 return PP_ERROR_FAILED; |
| 49 } |
| 50 |
| 51 class EchoServiceImpl |
| 52 : public mojo::InterfaceImpl<mojo::examples::EchoService> { |
| 53 public: |
| 54 virtual void OnConnectionEstablished() OVERRIDE { |
| 55 LOG(INFO) << "EchoServiceImpl::OnConnectionEstablished()"; |
| 56 } |
| 57 |
| 58 virtual void OnConnectionError() OVERRIDE { |
| 59 LOG(INFO) << "EchoServiceImpl::OnConnectionError()"; |
| 60 fprintf(stderr, "on connection error\n"); |
| 61 } |
| 62 |
| 63 virtual void EchoString( |
| 64 const mojo::String& value, |
| 65 const mojo::Callback<void(mojo::String)>& cb) OVERRIDE { |
| 66 LOG(INFO) << "Browser EchoString(), value: " << value.get(); |
| 67 cb.Run(value); |
| 68 } |
| 69 }; |
| 70 |
| 71 void CreateEchoService( |
| 72 mojo::InterfaceRequest<mojo::examples::EchoService> request) { |
| 73 mojo::WeakBindToRequest(new EchoServiceImpl(), &request); |
| 74 } |
| 75 |
| 76 int32_t PepperMojoHost::OnGetHandle(ppapi::host::HostMessageContext* context) { |
| 77 base::ProcessHandle plugin_process_handle = |
| 78 browser_ppapi_host_->GetPluginProcessHandle(); |
| 79 |
| 80 // No support for in-process. |
| 81 if (base::GetProcId(plugin_process_handle) == base::kNullProcessId) { |
| 82 return PP_ERROR_FAILED; |
| 83 } |
| 84 |
| 85 // Note: This code is largely cloned from MojoApplicationHost. |
| 86 mojo::embedder::PlatformChannelPair channel_pair; |
| 87 mojo::ScopedMessagePipeHandle message_pipe = channel_init_.Init( |
| 88 PlatformFileFromScopedPlatformHandle(channel_pair.PassServerHandle()), |
| 89 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
| 90 |
| 91 // TODO(teravest): Support more than one call to GetHandle(); this will |
| 92 // incorrectly bind our service registry to multiple pipes and add services |
| 93 // to it multiple times. |
| 94 service_registry_.BindRemoteServiceProvider(message_pipe.Pass()); |
| 95 |
| 96 // We use the base class here for calling AddService since |
| 97 // ServiceRegistryImpl hides the base class' AddService() call. |
| 98 ServiceRegistry* sr = &service_registry_; |
| 99 sr->AddService<mojo::examples::EchoService>(base::Bind(&CreateEchoService)); |
| 100 |
| 101 base::PlatformFile client_file = |
| 102 PlatformFileFromScopedPlatformHandle(channel_pair.PassClientHandle()); |
| 103 ppapi::host::ReplyMessageContext reply_context = |
| 104 context->MakeReplyMessageContext(); |
| 105 |
| 106 ppapi::proxy::SerializedHandle file_handle; |
| 107 int32_t open_flags = PP_FILEOPENFLAG_READ | PP_FILEOPENFLAG_WRITE; |
| 108 |
| 109 file_handle.set_file_handle( |
| 110 IPC::GetFileHandleForProcess(client_file, |
| 111 plugin_process_handle, |
| 112 true), |
| 113 open_flags, |
| 114 0); |
| 115 |
| 116 reply_context.params.AppendHandle(file_handle); |
| 117 host()->SendReply(reply_context, PpapiPluginMsg_Mojo_GetHandleReply()); |
| 118 |
| 119 return PP_OK_COMPLETIONPENDING; |
| 120 } |
| 121 |
| 122 } // namespace content |
OLD | NEW |