OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef PPAPI_PROXY_RESOURCE_MESSAGE_PARAMS_H_ | 5 #ifndef PPAPI_PROXY_RESOURCE_MESSAGE_PARAMS_H_ |
6 #define PPAPI_PROXY_RESOURCE_MESSAGE_PARAMS_H_ | 6 #define PPAPI_PROXY_RESOURCE_MESSAGE_PARAMS_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
| 10 #include "base/memory/ref_counted.h" |
10 #include "ipc/ipc_message_utils.h" | 11 #include "ipc/ipc_message_utils.h" |
11 #include "ppapi/c/pp_resource.h" | 12 #include "ppapi/c/pp_resource.h" |
12 #include "ppapi/proxy/ppapi_proxy_export.h" | 13 #include "ppapi/proxy/ppapi_proxy_export.h" |
13 #include "ppapi/proxy/serialized_structs.h" | 14 #include "ppapi/proxy/serialized_structs.h" |
14 | 15 |
15 namespace ppapi { | 16 namespace ppapi { |
16 namespace proxy { | 17 namespace proxy { |
17 | 18 |
18 // Common parameters for resource call and reply params structures below. | 19 // Common parameters for resource call and reply params structures below. |
19 class PPAPI_PROXY_EXPORT ResourceMessageParams { | 20 class PPAPI_PROXY_EXPORT ResourceMessageParams { |
20 public: | 21 public: |
21 virtual ~ResourceMessageParams(); | 22 virtual ~ResourceMessageParams(); |
22 | 23 |
23 PP_Resource pp_resource() const { return pp_resource_; } | 24 PP_Resource pp_resource() const { return pp_resource_; } |
24 int32_t sequence() const { return sequence_; } | 25 int32_t sequence() const { return sequence_; } |
25 | 26 |
26 const std::vector<SerializedHandle> handles() const { return handles_; } | 27 // Note that the caller doesn't take ownership of the returned handles. |
| 28 const std::vector<SerializedHandle>& handles() const { |
| 29 return handles_->data(); |
| 30 } |
27 | 31 |
28 // Returns a pointer to the handle at the given index if it exists and is of | 32 // Returns the handle at the given index if it exists and is of the given |
29 // the given type. If the index doesn't exist or the handle isn't of the | 33 // type. The corresponding slot in the list is set to an invalid handle. |
30 // given type, returns NULL. Note that the pointer will be into an internal | 34 // If the index doesn't exist or the handle isn't of the given type, returns |
31 // vector so will be invalidated if the params are mutated. | 35 // an invalid handle. |
32 const SerializedHandle* GetHandleOfTypeAtIndex( | 36 // Note that the caller is responsible for closing the returned handle, if it |
33 size_t index, | 37 // is valid. |
34 SerializedHandle::Type type) const; | 38 SerializedHandle TakeHandleOfTypeAtIndex(size_t index, |
| 39 SerializedHandle::Type type) const; |
35 | 40 |
36 // Helper functions to return shared memory handles passed in the params | 41 // Helper functions to return shared memory or socket handles passed in the |
37 // struct. If the index has a valid handle of the given type, it will be | 42 // params struct. |
38 // placed in the output parameter and the function will return true. If the | 43 // If the index has a valid handle of the given type, it will be placed in the |
39 // handle doesn't exist or is a different type, the functions will return | 44 // output parameter, the corresponding slot in the list will be set to an |
40 // false and the output parameter will be untouched. | 45 // invalid handle, and the function will return true. If the handle doesn't |
| 46 // exist or is a different type, the functions will return false and the |
| 47 // output parameter will be untouched. |
41 // | 48 // |
42 // Note that the handle could still be a "null" or invalid handle of | 49 // Note: 1) the handle could still be a "null" or invalid handle of the right |
43 // the right type and the functions will succeed. | 50 // type and the functions will succeed. |
44 bool GetSharedMemoryHandleAtIndex(size_t index, | 51 // 2) the caller is responsible for closing the returned handle, if it |
45 base::SharedMemoryHandle* handle) const; | 52 // is valid. |
46 bool GetSocketHandleAtIndex(size_t index, | 53 bool TakeSharedMemoryHandleAtIndex(size_t index, |
47 IPC::PlatformFileForTransit* handle) const; | 54 base::SharedMemoryHandle* handle) const; |
| 55 bool TakeSocketHandleAtIndex(size_t index, |
| 56 IPC::PlatformFileForTransit* handle) const; |
48 | 57 |
49 // Appends the given handle to the list of handles sent with the call or | 58 // Appends the given handle to the list of handles sent with the call or |
50 // reply. | 59 // reply. |
51 void AppendHandle(const SerializedHandle& handle); | 60 void AppendHandle(const SerializedHandle& handle) const; |
52 | 61 |
53 protected: | 62 protected: |
54 ResourceMessageParams(); | 63 ResourceMessageParams(); |
55 ResourceMessageParams(PP_Resource resource, int32_t sequence); | 64 ResourceMessageParams(PP_Resource resource, int32_t sequence); |
56 | 65 |
57 virtual void Serialize(IPC::Message* msg) const; | 66 virtual void Serialize(IPC::Message* msg) const; |
58 virtual bool Deserialize(const IPC::Message* msg, PickleIterator* iter); | 67 virtual bool Deserialize(const IPC::Message* msg, PickleIterator* iter); |
59 | 68 |
60 private: | 69 private: |
| 70 class SerializedHandles : public base::RefCounted<SerializedHandles> { |
| 71 public: |
| 72 SerializedHandles(); |
| 73 ~SerializedHandles(); |
| 74 |
| 75 void set_should_close(bool value) { should_close_ = value; } |
| 76 std::vector<SerializedHandle>& data() { return data_; } |
| 77 |
| 78 private: |
| 79 friend class base::RefCounted<SerializedHandles>; |
| 80 |
| 81 // Whether the handles stored in |data_| should be closed when this object |
| 82 // goes away. |
| 83 // |
| 84 // It is set to true by ResourceMessageParams::Deserialize(), so that the |
| 85 // receiving side of the params (the host side for |
| 86 // ResourceMessageCallParams; the plugin side for |
| 87 // ResourceMessageReplyParams) will close those handles which haven't been |
| 88 // taken using any of the Take*() methods. |
| 89 bool should_close_; |
| 90 std::vector<SerializedHandle> data_; |
| 91 }; |
| 92 |
61 PP_Resource pp_resource_; | 93 PP_Resource pp_resource_; |
62 | 94 |
63 // Identifier for this message. Sequence numbers are quasi-unique within a | 95 // Identifier for this message. Sequence numbers are quasi-unique within a |
64 // resource, but will overlap between different resource objects. | 96 // resource, but will overlap between different resource objects. |
65 // | 97 // |
66 // If you send a lot of messages, the ID may wrap around. This is OK. All IDs | 98 // If you send a lot of messages, the ID may wrap around. This is OK. All IDs |
67 // are valid and 0 and -1 aren't special, so those cases won't confuse us. | 99 // are valid and 0 and -1 aren't special, so those cases won't confuse us. |
68 // In practice, if you send more than 4 billion messages for a resource, the | 100 // In practice, if you send more than 4 billion messages for a resource, the |
69 // old ones will be long gone and there will be no collisions. | 101 // old ones will be long gone and there will be no collisions. |
70 // | 102 // |
71 // If there is a malicious plugin (or exceptionally bad luck) that causes a | 103 // If there is a malicious plugin (or exceptionally bad luck) that causes a |
72 // wraparound and collision the worst that will happen is that we can get | 104 // wraparound and collision the worst that will happen is that we can get |
73 // confused between different callbacks. But since these can only cause | 105 // confused between different callbacks. But since these can only cause |
74 // confusion within the plugin and within callbacks on the same resource, | 106 // confusion within the plugin and within callbacks on the same resource, |
75 // there shouldn't be a security problem. | 107 // there shouldn't be a security problem. |
76 int32_t sequence_; | 108 int32_t sequence_; |
77 | 109 |
78 // A list of all handles transferred in the message. Handles go here so that | 110 // A list of all handles transferred in the message. Handles go here so that |
79 // the NaCl adapter can extract them generally when it rewrites them to | 111 // the NaCl adapter can extract them generally when it rewrites them to |
80 // go between Windows and NaCl (Posix) apps. | 112 // go between Windows and NaCl (Posix) apps. |
81 std::vector<SerializedHandle> handles_; | 113 // TODO(yzshen): Mark it as mutable so that we can take/append handles using a |
| 114 // const reference. We need to change all the callers and make it not mutable. |
| 115 mutable scoped_refptr<SerializedHandles> handles_; |
82 }; | 116 }; |
83 | 117 |
84 // Parameters common to all ResourceMessage "Call" requests. | 118 // Parameters common to all ResourceMessage "Call" requests. |
85 class PPAPI_PROXY_EXPORT ResourceMessageCallParams | 119 class PPAPI_PROXY_EXPORT ResourceMessageCallParams |
86 : public ResourceMessageParams { | 120 : public ResourceMessageParams { |
87 public: | 121 public: |
88 ResourceMessageCallParams(); | 122 ResourceMessageCallParams(); |
89 ResourceMessageCallParams(PP_Resource resource, int32_t sequence); | 123 ResourceMessageCallParams(PP_Resource resource, int32_t sequence); |
90 virtual ~ResourceMessageCallParams(); | 124 virtual ~ResourceMessageCallParams(); |
91 | 125 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { | 181 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
148 return r->Deserialize(m, iter); | 182 return r->Deserialize(m, iter); |
149 } | 183 } |
150 static void Log(const param_type& p, std::string* l) { | 184 static void Log(const param_type& p, std::string* l) { |
151 } | 185 } |
152 }; | 186 }; |
153 | 187 |
154 } // namespace IPC | 188 } // namespace IPC |
155 | 189 |
156 #endif // PPAPI_PROXY_RESOURCE_MESSAGE_PARAMS_H_ | 190 #endif // PPAPI_PROXY_RESOURCE_MESSAGE_PARAMS_H_ |
OLD | NEW |