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 #ifndef PPAPI_PROXY_RESOURCE_MESSAGE_PARAMS_H_
6 #define PPAPI_PROXY_RESOURCE_MESSAGE_PARAMS_H_
10 #include "base/memory/ref_counted.h"
11 #include "ipc/ipc_message_utils.h"
12 #include "ppapi/c/pp_resource.h"
13 #include "ppapi/proxy/ppapi_proxy_export.h"
14 #include "ppapi/proxy/serialized_handle.h"
19 // Common parameters for resource call and reply params structures below.
20 class PPAPI_PROXY_EXPORT ResourceMessageParams
{
22 virtual ~ResourceMessageParams();
24 PP_Resource
pp_resource() const { return pp_resource_
; }
25 int32_t sequence() const { return sequence_
; }
27 // Note that the caller doesn't take ownership of the returned handles.
28 const std::vector
<SerializedHandle
>& handles() const {
29 return handles_
->data();
32 // Makes ResourceMessageParams leave its handles open, even if they weren't
33 // taken using a Take.* function. After this call, no Take.* calls are
35 void ConsumeHandles() const;
37 // Returns the handle at the given index if it exists and is of the given
38 // type. The corresponding slot in the list is set to an invalid handle.
39 // If the index doesn't exist or the handle isn't of the given type, returns
41 // Note that the caller is responsible for closing the returned handle, if it
43 SerializedHandle
TakeHandleOfTypeAtIndex(size_t index
,
44 SerializedHandle::Type type
) const;
46 // Helper functions to return shared memory, socket or file handles passed in
48 // If the index has a valid handle of the given type, it will be placed in the
49 // output parameter, the corresponding slot in the list will be set to an
50 // invalid handle, and the function will return true. If the handle doesn't
51 // exist or is a different type, the functions will return false and the
52 // output parameter will be untouched.
54 // Note: 1) the handle could still be a "null" or invalid handle of the right
55 // type and the functions will succeed.
56 // 2) the caller is responsible for closing the returned handle, if it
58 bool TakeSharedMemoryHandleAtIndex(size_t index
,
59 base::SharedMemoryHandle
* handle
) const;
60 bool TakeSocketHandleAtIndex(size_t index
,
61 IPC::PlatformFileForTransit
* handle
) const;
62 bool TakeFileHandleAtIndex(size_t index
,
63 IPC::PlatformFileForTransit
* handle
) const;
64 void TakeAllSharedMemoryHandles(
65 std::vector
<base::SharedMemoryHandle
>* handles
) const;
67 // Appends the given handle to the list of handles sent with the call or
69 void AppendHandle(const SerializedHandle
& handle
) const;
72 ResourceMessageParams();
73 ResourceMessageParams(PP_Resource resource
, int32_t sequence
);
75 virtual void Serialize(IPC::Message
* msg
) const;
76 virtual bool Deserialize(const IPC::Message
* msg
, PickleIterator
* iter
);
78 // Writes everything except the handles to |msg|.
79 void WriteHeader(IPC::Message
* msg
) const;
80 // Writes the handles to |msg|.
81 void WriteHandles(IPC::Message
* msg
) const;
82 // Matching deserialize helpers.
83 bool ReadHeader(const IPC::Message
* msg
, PickleIterator
* iter
);
84 bool ReadHandles(const IPC::Message
* msg
, PickleIterator
* iter
);
87 class PPAPI_PROXY_EXPORT SerializedHandles
88 : public base::RefCountedThreadSafe
<SerializedHandles
> {
93 void set_should_close(bool value
) { should_close_
= value
; }
94 std::vector
<SerializedHandle
>& data() { return data_
; }
97 friend class base::RefCountedThreadSafe
<SerializedHandles
>;
99 // Whether the handles stored in |data_| should be closed when this object
102 // It is set to true by ResourceMessageParams::Deserialize(), so that the
103 // receiving side of the params (the host side for
104 // ResourceMessageCallParams; the plugin side for
105 // ResourceMessageReplyParams) will close those handles which haven't been
106 // taken using any of the Take*() methods.
108 std::vector
<SerializedHandle
> data_
;
111 PP_Resource pp_resource_
;
113 // Identifier for this message. Sequence numbers are quasi-unique within a
114 // resource, but will overlap between different resource objects.
116 // If you send a lot of messages, the ID may wrap around. This is OK. All IDs
117 // are valid and 0 and -1 aren't special, so those cases won't confuse us.
118 // In practice, if you send more than 4 billion messages for a resource, the
119 // old ones will be long gone and there will be no collisions.
121 // If there is a malicious plugin (or exceptionally bad luck) that causes a
122 // wraparound and collision the worst that will happen is that we can get
123 // confused between different callbacks. But since these can only cause
124 // confusion within the plugin and within callbacks on the same resource,
125 // there shouldn't be a security problem.
128 // A list of all handles transferred in the message. Handles go here so that
129 // the NaCl adapter can extract them generally when it rewrites them to
130 // go between Windows and NaCl (Posix) apps.
131 // TODO(yzshen): Mark it as mutable so that we can take/append handles using a
132 // const reference. We need to change all the callers and make it not mutable.
133 mutable scoped_refptr
<SerializedHandles
> handles_
;
136 // Parameters common to all ResourceMessage "Call" requests.
137 class PPAPI_PROXY_EXPORT ResourceMessageCallParams
138 : public ResourceMessageParams
{
140 ResourceMessageCallParams();
141 ResourceMessageCallParams(PP_Resource resource
, int32_t sequence
);
142 ~ResourceMessageCallParams() override
;
144 void set_has_callback() { has_callback_
= true; }
145 bool has_callback() const { return has_callback_
; }
147 void Serialize(IPC::Message
* msg
) const override
;
148 bool Deserialize(const IPC::Message
* msg
, PickleIterator
* iter
) override
;
154 // Parameters common to all ResourceMessage "Reply" requests.
155 class PPAPI_PROXY_EXPORT ResourceMessageReplyParams
156 : public ResourceMessageParams
{
158 ResourceMessageReplyParams();
159 ResourceMessageReplyParams(PP_Resource resource
, int32_t sequence
);
160 ~ResourceMessageReplyParams() override
;
162 void set_result(int32_t r
) { result_
= r
; }
163 int32_t result() const { return result_
; }
165 void Serialize(IPC::Message
* msg
) const override
;
166 bool Deserialize(const IPC::Message
* msg
, PickleIterator
* iter
) override
;
168 // Writes everything except the handles to |msg|.
169 void WriteReplyHeader(IPC::Message
* msg
) const;
172 // Pepper "result code" for the callback.
181 template <> struct PPAPI_PROXY_EXPORT
182 ParamTraits
<ppapi::proxy::ResourceMessageCallParams
> {
183 typedef ppapi::proxy::ResourceMessageCallParams param_type
;
184 static void Write(Message
* m
, const param_type
& p
) {
187 static bool Read(const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
188 return r
->Deserialize(m
, iter
);
190 static void Log(const param_type
& p
, std::string
* l
) {
194 template <> struct PPAPI_PROXY_EXPORT
195 ParamTraits
<ppapi::proxy::ResourceMessageReplyParams
> {
196 typedef ppapi::proxy::ResourceMessageReplyParams param_type
;
197 static void Write(Message
* m
, const param_type
& p
) {
200 static bool Read(const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
201 return r
->Deserialize(m
, iter
);
203 static void Log(const param_type
& p
, std::string
* l
) {
209 #endif // PPAPI_PROXY_RESOURCE_MESSAGE_PARAMS_H_