1 // Copyright (c) 2012 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.
5 #include "ppapi/proxy/resource_message_params.h"
7 #include "base/logging.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/proxy/ppapi_messages.h"
14 ResourceMessageParams::SerializedHandles::SerializedHandles()
15 : should_close_(false) {
18 ResourceMessageParams::SerializedHandles::~SerializedHandles() {
20 for (std::vector
<SerializedHandle
>::iterator iter
= data_
.begin();
21 iter
!= data_
.end(); ++iter
) {
27 ResourceMessageParams::ResourceMessageParams()
30 handles_(new SerializedHandles()) {
33 ResourceMessageParams::ResourceMessageParams(PP_Resource resource
,
35 : pp_resource_(resource
),
37 handles_(new SerializedHandles()) {
40 ResourceMessageParams::~ResourceMessageParams() {
43 void ResourceMessageParams::Serialize(IPC::Message
* msg
) const {
48 bool ResourceMessageParams::Deserialize(const IPC::Message
* msg
,
49 PickleIterator
* iter
) {
50 return ReadHeader(msg
, iter
) && ReadHandles(msg
, iter
);
53 void ResourceMessageParams::WriteHeader(IPC::Message
* msg
) const {
54 IPC::ParamTraits
<PP_Resource
>::Write(msg
, pp_resource_
);
55 IPC::ParamTraits
<int32_t>::Write(msg
, sequence_
);
58 void ResourceMessageParams::WriteHandles(IPC::Message
* msg
) const {
59 IPC::ParamTraits
<std::vector
<SerializedHandle
> >::Write(msg
,
63 bool ResourceMessageParams::ReadHeader(const IPC::Message
* msg
,
64 PickleIterator
* iter
) {
65 DCHECK(handles_
->data().empty());
66 handles_
->set_should_close(true);
67 return IPC::ParamTraits
<PP_Resource
>::Read(msg
, iter
, &pp_resource_
) &&
68 IPC::ParamTraits
<int32_t>::Read(msg
, iter
, &sequence_
);
71 bool ResourceMessageParams::ReadHandles(const IPC::Message
* msg
,
72 PickleIterator
* iter
) {
73 return IPC::ParamTraits
<std::vector
<SerializedHandle
> >::Read(
74 msg
, iter
, &handles_
->data());
77 void ResourceMessageParams::ConsumeHandles() const {
78 // Note: we must not invalidate the handles. This is used for converting
79 // handles from the host OS to NaCl, and that conversion will not work if we
80 // invalidate the handles (see HandleConverter).
81 handles_
->set_should_close(false);
84 SerializedHandle
ResourceMessageParams::TakeHandleOfTypeAtIndex(
86 SerializedHandle::Type type
) const {
87 SerializedHandle handle
;
88 std::vector
<SerializedHandle
>& data
= handles_
->data();
89 if (index
< data
.size() && data
[index
].type() == type
) {
91 data
[index
] = SerializedHandle();
96 bool ResourceMessageParams::TakeSharedMemoryHandleAtIndex(
98 base::SharedMemoryHandle
* handle
) const {
99 SerializedHandle serialized
= TakeHandleOfTypeAtIndex(
100 index
, SerializedHandle::SHARED_MEMORY
);
101 if (!serialized
.is_shmem())
103 *handle
= serialized
.shmem();
107 bool ResourceMessageParams::TakeSocketHandleAtIndex(
109 IPC::PlatformFileForTransit
* handle
) const {
110 SerializedHandle serialized
= TakeHandleOfTypeAtIndex(
111 index
, SerializedHandle::SOCKET
);
112 if (!serialized
.is_socket())
114 *handle
= serialized
.descriptor();
118 bool ResourceMessageParams::TakeFileHandleAtIndex(
120 IPC::PlatformFileForTransit
* handle
) const {
121 SerializedHandle serialized
= TakeHandleOfTypeAtIndex(
122 index
, SerializedHandle::FILE);
123 if (!serialized
.is_file())
125 *handle
= serialized
.descriptor();
129 void ResourceMessageParams::TakeAllSharedMemoryHandles(
130 std::vector
<base::SharedMemoryHandle
>* handles
) const {
131 for (size_t i
= 0; i
< handles_
->data().size(); ++i
) {
132 base::SharedMemoryHandle handle
;
133 if (TakeSharedMemoryHandleAtIndex(i
, &handle
))
134 handles
->push_back(handle
);
138 void ResourceMessageParams::AppendHandle(const SerializedHandle
& handle
) const {
139 handles_
->data().push_back(handle
);
142 ResourceMessageCallParams::ResourceMessageCallParams()
143 : ResourceMessageParams(),
147 ResourceMessageCallParams::ResourceMessageCallParams(PP_Resource resource
,
149 : ResourceMessageParams(resource
, sequence
),
153 ResourceMessageCallParams::~ResourceMessageCallParams() {
156 void ResourceMessageCallParams::Serialize(IPC::Message
* msg
) const {
157 ResourceMessageParams::Serialize(msg
);
158 IPC::ParamTraits
<bool>::Write(msg
, has_callback_
);
161 bool ResourceMessageCallParams::Deserialize(const IPC::Message
* msg
,
162 PickleIterator
* iter
) {
163 if (!ResourceMessageParams::Deserialize(msg
, iter
))
165 return IPC::ParamTraits
<bool>::Read(msg
, iter
, &has_callback_
);
168 ResourceMessageReplyParams::ResourceMessageReplyParams()
169 : ResourceMessageParams(),
173 ResourceMessageReplyParams::ResourceMessageReplyParams(PP_Resource resource
,
175 : ResourceMessageParams(resource
, sequence
),
179 ResourceMessageReplyParams::~ResourceMessageReplyParams() {
182 void ResourceMessageReplyParams::Serialize(IPC::Message
* msg
) const {
183 // Rather than serialize all of ResourceMessageParams first, we serialize all
184 // non-handle data first, then the handles. When transferring to NaCl on
185 // Windows, we need to be able to translate Windows-style handles to POSIX-
186 // style handles, and it's easier to put all the regular stuff at the front.
187 WriteReplyHeader(msg
);
191 bool ResourceMessageReplyParams::Deserialize(const IPC::Message
* msg
,
192 PickleIterator
* iter
) {
193 return (ReadHeader(msg
, iter
) &&
194 IPC::ParamTraits
<int32_t>::Read(msg
, iter
, &result_
) &&
195 ReadHandles(msg
, iter
));
198 void ResourceMessageReplyParams::WriteReplyHeader(IPC::Message
* msg
) const {
200 IPC::ParamTraits
<int32_t>::Write(msg
, result_
);