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