Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / ppapi / proxy / resource_message_params.h
blob500ebb908241efe74cd6010c8eca0eea5376eb3c
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_
8 #include <vector>
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"
16 namespace ppapi {
17 namespace proxy {
19 // Common parameters for resource call and reply params structures below.
20 class PPAPI_PROXY_EXPORT ResourceMessageParams {
21 public:
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
34 // allowed.
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
40 // an invalid handle.
41 // Note that the caller is responsible for closing the returned handle, if it
42 // is valid.
43 SerializedHandle TakeHandleOfTypeAtIndex(size_t index,
44 SerializedHandle::Type type) const;
46 // Helper functions to return shared memory, socket or file handles passed in
47 // the params struct.
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
57 // is valid.
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
68 // reply.
69 void AppendHandle(const SerializedHandle& handle) const;
71 protected:
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, base::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, base::PickleIterator* iter);
84 bool ReadHandles(const IPC::Message* msg, base::PickleIterator* iter);
86 private:
87 class PPAPI_PROXY_EXPORT SerializedHandles
88 : public base::RefCountedThreadSafe<SerializedHandles> {
89 public:
90 SerializedHandles();
91 ~SerializedHandles();
93 void set_should_close(bool value) { should_close_ = value; }
94 std::vector<SerializedHandle>& data() { return data_; }
96 private:
97 friend class base::RefCountedThreadSafe<SerializedHandles>;
99 // Whether the handles stored in |data_| should be closed when this object
100 // goes away.
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.
107 bool should_close_;
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.
126 int32_t sequence_;
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 {
139 public:
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,
149 base::PickleIterator* iter) override;
151 private:
152 bool has_callback_;
155 // Parameters common to all ResourceMessage "Reply" requests.
156 class PPAPI_PROXY_EXPORT ResourceMessageReplyParams
157 : public ResourceMessageParams {
158 public:
159 ResourceMessageReplyParams();
160 ResourceMessageReplyParams(PP_Resource resource, int32_t sequence);
161 ~ResourceMessageReplyParams() override;
163 void set_result(int32_t r) { result_ = r; }
164 int32_t result() const { return result_; }
166 void Serialize(IPC::Message* msg) const override;
167 bool Deserialize(const IPC::Message* msg,
168 base::PickleIterator* iter) override;
170 // Writes everything except the handles to |msg|.
171 void WriteReplyHeader(IPC::Message* msg) const;
173 private:
174 // Pepper "result code" for the callback.
175 int32_t result_;
178 } // namespace proxy
179 } // namespace ppapi
181 namespace IPC {
183 template <> struct PPAPI_PROXY_EXPORT
184 ParamTraits<ppapi::proxy::ResourceMessageCallParams> {
185 typedef ppapi::proxy::ResourceMessageCallParams param_type;
186 static void Write(Message* m, const param_type& p) {
187 p.Serialize(m);
189 static bool Read(const Message* m, base::PickleIterator* iter,
190 param_type* r) {
191 return r->Deserialize(m, iter);
193 static void Log(const param_type& p, std::string* l) {
197 template <> struct PPAPI_PROXY_EXPORT
198 ParamTraits<ppapi::proxy::ResourceMessageReplyParams> {
199 typedef ppapi::proxy::ResourceMessageReplyParams param_type;
200 static void Write(Message* m, const param_type& p) {
201 p.Serialize(m);
203 static bool Read(const Message* m, base::PickleIterator* iter,
204 param_type* r) {
205 return r->Deserialize(m, iter);
207 static void Log(const param_type& p, std::string* l) {
211 } // namespace IPC
213 #endif // PPAPI_PROXY_RESOURCE_MESSAGE_PARAMS_H_