OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/nacl/renderer/manifest_service_channel.h" | 5 #include "components/nacl/renderer/manifest_service_channel.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/callback.h" | 8 #include "base/callback.h" |
8 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
9 #include "content/public/renderer/render_thread.h" | 10 #include "content/public/renderer/render_thread.h" |
10 #include "ipc/ipc_channel.h" | 11 #include "ipc/ipc_channel.h" |
11 #include "ipc/ipc_sync_channel.h" | 12 #include "ipc/ipc_sync_channel.h" |
12 #include "ppapi/c/pp_errors.h" | 13 #include "ppapi/c/pp_errors.h" |
13 #include "ppapi/proxy/ppapi_messages.h" | 14 #include "ppapi/proxy/ppapi_messages.h" |
14 | 15 |
15 namespace nacl { | 16 namespace nacl { |
16 | 17 |
17 ManifestServiceChannel::ManifestServiceChannel( | 18 ManifestServiceChannel::ManifestServiceChannel( |
18 const IPC::ChannelHandle& handle, | 19 const IPC::ChannelHandle& handle, |
19 const base::Callback<void(int32_t)>& connected_callback, | 20 const base::Callback<void(int32_t)>& connected_callback, |
20 scoped_ptr<Delegate> delegate, | 21 scoped_ptr<Delegate> delegate, |
21 base::WaitableEvent* waitable_event) | 22 base::WaitableEvent* waitable_event) |
22 : connected_callback_(connected_callback), | 23 : connected_callback_(connected_callback), |
23 delegate_(delegate.Pass()), | 24 delegate_(delegate.Pass()), |
24 channel_(new IPC::SyncChannel( | 25 channel_(new IPC::SyncChannel( |
25 handle, IPC::Channel::MODE_CLIENT, this, | 26 handle, IPC::Channel::MODE_CLIENT, this, |
26 content::RenderThread::Get()->GetIOMessageLoopProxy(), | 27 content::RenderThread::Get()->GetIOMessageLoopProxy(), |
27 true, waitable_event)) { | 28 true, waitable_event)), |
| 29 peer_handle_(base::kNullProcessHandle), |
| 30 weak_ptr_factory_(this) { |
28 } | 31 } |
29 | 32 |
30 ManifestServiceChannel::~ManifestServiceChannel() { | 33 ManifestServiceChannel::~ManifestServiceChannel() { |
31 if (!connected_callback_.is_null()) | 34 if (!connected_callback_.is_null()) |
32 base::ResetAndReturn(&connected_callback_).Run(PP_ERROR_FAILED); | 35 base::ResetAndReturn(&connected_callback_).Run(PP_ERROR_FAILED); |
| 36 if (peer_handle_ != base::kNullProcessHandle) |
| 37 base::CloseProcessHandle(peer_handle_); |
| 38 } |
| 39 |
| 40 void ManifestServiceChannel::Send(IPC::Message* message) { |
| 41 channel_->Send(message); |
33 } | 42 } |
34 | 43 |
35 bool ManifestServiceChannel::OnMessageReceived(const IPC::Message& message) { | 44 bool ManifestServiceChannel::OnMessageReceived(const IPC::Message& message) { |
36 // TODO(hidehiko): Implement OpenResource. | |
37 bool handled = true; | 45 bool handled = true; |
38 IPC_BEGIN_MESSAGE_MAP(ManifestServiceChannel, message) | 46 IPC_BEGIN_MESSAGE_MAP(ManifestServiceChannel, message) |
39 IPC_MESSAGE_HANDLER(PpapiHostMsg_StartupInitializationComplete, | 47 IPC_MESSAGE_HANDLER(PpapiHostMsg_StartupInitializationComplete, |
40 OnStartupInitializationComplete) | 48 OnStartupInitializationComplete) |
| 49 IPC_MESSAGE_HANDLER_DELAY_REPLY(PpapiHostMsg_OpenResource, |
| 50 OnOpenResource) |
41 IPC_MESSAGE_UNHANDLED(handled = false) | 51 IPC_MESSAGE_UNHANDLED(handled = false) |
42 IPC_END_MESSAGE_MAP() | 52 IPC_END_MESSAGE_MAP() |
43 return handled; | 53 return handled; |
44 } | 54 } |
45 | 55 |
46 void ManifestServiceChannel::OnChannelConnected(int32 peer_pid) { | 56 void ManifestServiceChannel::OnChannelConnected(int32 peer_pid) { |
47 if (!connected_callback_.is_null()) | 57 if (!connected_callback_.is_null()) |
48 base::ResetAndReturn(&connected_callback_).Run(PP_OK); | 58 base::ResetAndReturn(&connected_callback_).Run(PP_OK); |
49 } | 59 } |
50 | 60 |
51 void ManifestServiceChannel::OnChannelError() { | 61 void ManifestServiceChannel::OnChannelError() { |
52 if (!connected_callback_.is_null()) | 62 if (!connected_callback_.is_null()) |
53 base::ResetAndReturn(&connected_callback_).Run(PP_ERROR_FAILED); | 63 base::ResetAndReturn(&connected_callback_).Run(PP_ERROR_FAILED); |
54 } | 64 } |
55 | 65 |
56 void ManifestServiceChannel::OnStartupInitializationComplete() { | 66 void ManifestServiceChannel::OnStartupInitializationComplete() { |
57 delegate_->StartupInitializationComplete(); | 67 delegate_->StartupInitializationComplete(); |
58 } | 68 } |
59 | 69 |
| 70 void ManifestServiceChannel::OnOpenResource( |
| 71 const std::string& key, IPC::Message* reply) { |
| 72 delegate_->OpenResource(key, |
| 73 base::Bind(&ManifestServiceChannel::DidOpenResource, |
| 74 weak_ptr_factory_.GetWeakPtr(), reply)); |
| 75 } |
| 76 |
| 77 void ManifestServiceChannel::DidOpenResource( |
| 78 IPC::Message* reply, int32_t pp_error, |
| 79 const base::PlatformFile& platform_file) { |
| 80 PpapiHostMsg_OpenResource::WriteReplyParams( |
| 81 reply, |
| 82 ppapi::proxy::SerializedHandle( |
| 83 ppapi::proxy::SerializedHandle::FILE, |
| 84 IPC::GetFileHandleForProcess( |
| 85 platform_file, PeerHandle(), true))); |
| 86 Send(reply); |
| 87 } |
| 88 |
| 89 base::ProcessHandle ManifestServiceChannel::PeerHandle() { |
| 90 // This function is called only from DidOpenResource(), which always runs on |
| 91 // a same thread. |
| 92 if (peer_handle_ == base::kNullProcessHandle) |
| 93 base::OpenPrivilegedProcessHandle(channel_->peer_pid(), &peer_handle_); |
| 94 return peer_handle_; |
| 95 } |
| 96 |
60 } // namespace nacl | 97 } // namespace nacl |
OLD | NEW |