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_SERIALIZED_VAR_H_ | 5 #ifndef PPAPI_PROXY_SERIALIZED_VAR_H_ |
6 #define PPAPI_PROXY_SERIALIZED_VAR_H_ | 6 #define PPAPI_PROXY_SERIALIZED_VAR_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/shared_memory.h" | 14 #include "base/shared_memory.h" |
15 #include "ppapi/c/pp_instance.h" | 15 #include "ppapi/c/pp_instance.h" |
16 #include "ppapi/c/pp_var.h" | 16 #include "ppapi/c/pp_var.h" |
17 #include "ppapi/proxy/ppapi_proxy_export.h" | 17 #include "ppapi/proxy/ppapi_proxy_export.h" |
| 18 #include "ppapi/proxy/raw_var_data.h" |
18 #include "ppapi/proxy/serialized_handle.h" | 19 #include "ppapi/proxy/serialized_handle.h" |
19 #include "ppapi/proxy/serialized_structs.h" | 20 #include "ppapi/proxy/serialized_structs.h" |
20 #include "ppapi/proxy/var_serialization_rules.h" | 21 #include "ppapi/proxy/var_serialization_rules.h" |
21 | 22 |
22 class PickleIterator; | 23 class PickleIterator; |
23 | 24 |
24 namespace IPC { | 25 namespace IPC { |
25 class Message; | 26 class Message; |
26 } | 27 } |
27 | 28 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 ~SerializedVar(); | 77 ~SerializedVar(); |
77 | 78 |
78 // Backend implementation for IPC::ParamTraits<SerializedVar>. | 79 // Backend implementation for IPC::ParamTraits<SerializedVar>. |
79 void WriteToMessage(IPC::Message* m) const { | 80 void WriteToMessage(IPC::Message* m) const { |
80 inner_->WriteToMessage(m); | 81 inner_->WriteToMessage(m); |
81 } | 82 } |
82 bool ReadFromMessage(const IPC::Message* m, PickleIterator* iter) { | 83 bool ReadFromMessage(const IPC::Message* m, PickleIterator* iter) { |
83 return inner_->ReadFromMessage(m, iter); | 84 return inner_->ReadFromMessage(m, iter); |
84 } | 85 } |
85 | 86 |
86 // Used by chrome/nacl/nacl_ipc_adapter.cc | 87 RawVarDataGraph* raw_var_data() const { |
87 SerializedHandle* GetPluginShmemHandle() const { | 88 return inner_->raw_var_data(); |
88 return inner_->GetPluginShmemHandle(); | |
89 } | |
90 void WriteRawVarHeader(IPC::Message* m) const { | |
91 inner_->WriteRawVarHeader(m); | |
92 } | 89 } |
93 | 90 |
94 protected: | 91 protected: |
95 friend class SerializedVarReceiveInput; | 92 friend class SerializedVarReceiveInput; |
96 friend class SerializedVarReturnValue; | 93 friend class SerializedVarReturnValue; |
97 friend class SerializedVarOutParam; | 94 friend class SerializedVarOutParam; |
98 friend class SerializedVarSendInput; | 95 friend class SerializedVarSendInput; |
99 friend class SerializedVarSendInputShmem; | 96 friend class SerializedVarSendInputShmem; |
100 friend class SerializedVarTestConstructor; | 97 friend class SerializedVarTestConstructor; |
101 friend class SerializedVarVectorReceiveInput; | 98 friend class SerializedVarVectorReceiveInput; |
102 | 99 |
103 class PPAPI_PROXY_EXPORT Inner : public base::RefCounted<Inner> { | 100 class PPAPI_PROXY_EXPORT Inner : public base::RefCounted<Inner> { |
104 public: | 101 public: |
105 Inner(); | 102 Inner(); |
106 Inner(VarSerializationRules* serialization_rules); | 103 Inner(VarSerializationRules* serialization_rules); |
107 ~Inner(); | 104 ~Inner(); |
108 | 105 |
109 VarSerializationRules* serialization_rules() { | 106 VarSerializationRules* serialization_rules() { |
110 return serialization_rules_; | 107 return serialization_rules_; |
111 } | 108 } |
112 void set_serialization_rules(VarSerializationRules* serialization_rules) { | 109 void set_serialization_rules(VarSerializationRules* serialization_rules) { |
113 serialization_rules_ = serialization_rules; | 110 serialization_rules_ = serialization_rules; |
114 } | 111 } |
115 | 112 |
| 113 RawVarDataGraph* raw_var_data() { |
| 114 return raw_var_data_.get(); |
| 115 } |
| 116 |
116 // See outer class's declarations above. | 117 // See outer class's declarations above. |
117 PP_Var GetVar(); | 118 PP_Var GetVar(); |
118 void SetVar(PP_Var var); | 119 void SetVar(PP_Var var); |
119 void SetInstance(PP_Instance instance); | 120 void SetInstance(PP_Instance instance); |
120 | 121 |
121 // For the SerializedVarTestConstructor, this writes the Var value as if | 122 // For the SerializedVarTestConstructor, this writes the Var value as if |
122 // it was just received off the wire, without any serialization rules. | 123 // it was just received off the wire, without any serialization rules. |
123 void ForceSetVarValueForTest(PP_Var value); | 124 void ForceSetVarValueForTest(PP_Var value); |
124 | 125 |
125 void WriteToMessage(IPC::Message* m) const; | 126 void WriteToMessage(IPC::Message* m) const; |
126 bool ReadFromMessage(const IPC::Message* m, PickleIterator* iter); | 127 bool ReadFromMessage(const IPC::Message* m, PickleIterator* iter); |
127 | 128 |
128 // Sets the cleanup mode. See the CleanupMode enum below. | 129 // Sets the cleanup mode. See the CleanupMode enum below. |
129 void SetCleanupModeToEndSendPassRef(); | 130 void SetCleanupModeToEndSendPassRef(); |
130 void SetCleanupModeToEndReceiveCallerOwned(); | 131 void SetCleanupModeToEndReceiveCallerOwned(); |
131 | 132 |
132 // Returns a handle in the underlying data, if it exists. | |
133 SerializedHandle* GetPluginShmemHandle() const; | |
134 | |
135 // Writes raw var data, excluding handles. | |
136 void WriteRawVarHeader(IPC::Message* m) const; | |
137 | |
138 private: | 133 private: |
139 enum CleanupMode { | 134 enum CleanupMode { |
140 // The serialized var won't do anything special in the destructor | 135 // The serialized var won't do anything special in the destructor |
141 // (default). | 136 // (default). |
142 CLEANUP_NONE, | 137 CLEANUP_NONE, |
143 | 138 |
144 // The serialized var will call EndSendPassRef in the destructor. | 139 // The serialized var will call EndSendPassRef in the destructor. |
145 END_SEND_PASS_REF, | 140 END_SEND_PASS_REF, |
146 | 141 |
147 // The serialized var will call EndReceiveCallerOwned in the destructor. | 142 // The serialized var will call EndReceiveCallerOwned in the destructor. |
148 END_RECEIVE_CALLER_OWNED | 143 END_RECEIVE_CALLER_OWNED |
149 }; | 144 }; |
150 | 145 |
151 // Enum for array buffer message types. | |
152 enum ShmemType { | |
153 ARRAY_BUFFER_NO_SHMEM, | |
154 ARRAY_BUFFER_SHMEM_HOST, | |
155 ARRAY_BUFFER_SHMEM_PLUGIN, | |
156 }; | |
157 | |
158 // ReadFromMessage() may be called on the I/O thread, e.g., when reading the | |
159 // reply to a sync message. We cannot use the var tracker on the I/O thread, | |
160 // which means we cannot create PP_Var for PP_VARTYPE_STRING and | |
161 // PP_VARTYPE_ARRAY_BUFFER in ReadFromMessage(). So we save the raw var data | |
162 // and create PP_Var later when GetVar() is called, which should happen on | |
163 // the main thread. | |
164 struct RawVarData { | |
165 PP_VarType type; | |
166 ShmemType shmem_type; | |
167 std::string data; | |
168 uint32 shmem_size; | |
169 int host_handle_id; | |
170 SerializedHandle plugin_handle; | |
171 }; | |
172 | |
173 // Converts |raw_var_data_| to |var_|. It is a no-op if |raw_var_data_| is | |
174 // NULL. | |
175 void ConvertRawVarData(); | |
176 | |
177 // Rules for serializing and deserializing vars for this process type. | 146 // Rules for serializing and deserializing vars for this process type. |
178 // This may be NULL, but must be set before trying to serialize to IPC when | 147 // This may be NULL, but must be set before trying to serialize to IPC when |
179 // sending, or before converting back to a PP_Var when receiving. | 148 // sending, or before converting back to a PP_Var when receiving. |
180 scoped_refptr<VarSerializationRules> serialization_rules_; | 149 scoped_refptr<VarSerializationRules> serialization_rules_; |
181 | 150 |
182 // If this is set to VARTYPE_STRING and the 'value.id' is 0, then the | 151 // If this is set to VARTYPE_STRING and the 'value.id' is 0, then the |
183 // string_from_ipc_ holds the string. This means that the caller hasn't | 152 // string_from_ipc_ holds the string. This means that the caller hasn't |
184 // called Deserialize with a valid Dispatcher yet, which is how we can | 153 // called Deserialize with a valid Dispatcher yet, which is how we can |
185 // convert the serialized string value to a PP_Var string ID. | 154 // convert the serialized string value to a PP_Var string ID. |
186 // | 155 // |
187 // This var may not be complete until the serialization rules are set when | 156 // This var may not be complete until the serialization rules are set when |
188 // reading from IPC since we'll need that to convert the string_value to | 157 // reading from IPC since we'll need that to convert the string_value to |
189 // a string ID. Before this, the as_id will be 0 for VARTYPE_STRING. | 158 // a string ID. Before this, the as_id will be 0 for VARTYPE_STRING. |
190 PP_Var var_; | 159 PP_Var var_; |
191 | 160 |
192 PP_Instance instance_; | 161 PP_Instance instance_; |
193 | 162 |
194 CleanupMode cleanup_mode_; | 163 CleanupMode cleanup_mode_; |
195 | 164 |
196 #ifndef NDEBUG | 165 #ifndef NDEBUG |
197 // When being sent or received over IPC, we should only be serialized or | 166 // When being sent or received over IPC, we should only be serialized or |
198 // deserialized once. These flags help us assert this is true. | 167 // deserialized once. These flags help us assert this is true. |
199 mutable bool has_been_serialized_; | 168 mutable bool has_been_serialized_; |
200 mutable bool has_been_deserialized_; | 169 mutable bool has_been_deserialized_; |
201 #endif | 170 #endif |
202 | 171 |
203 // It will be non-NULL if there is PP_VARTYPE_STRING or | 172 // ReadFromMessage() may be called on the I/O thread, e.g., when reading the |
204 // PP_VARTYPE_ARRAY_BUFFER data from ReadFromMessage() that hasn't been | 173 // reply to a sync message. We cannot use the var tracker on the I/O thread, |
205 // converted to PP_Var. | 174 // which means we cannot create some types of PP_Var |
206 scoped_ptr<RawVarData> raw_var_data_; | 175 // (e.g. PP_VARTYPE_STRING). The data is stored in |raw_var_data_| and the |
| 176 // PP_Var is constructed when |GetVar()| is called. |
| 177 scoped_ptr<RawVarDataGraph> raw_var_data_; |
207 | 178 |
208 DISALLOW_COPY_AND_ASSIGN(Inner); | 179 DISALLOW_COPY_AND_ASSIGN(Inner); |
209 }; | 180 }; |
210 | 181 |
211 SerializedVar(VarSerializationRules* serialization_rules); | 182 SerializedVar(VarSerializationRules* serialization_rules); |
212 | 183 |
213 mutable scoped_refptr<Inner> inner_; | 184 mutable scoped_refptr<Inner> inner_; |
214 }; | 185 }; |
215 | 186 |
216 // Helpers for message sending side -------------------------------------------- | 187 // Helpers for message sending side -------------------------------------------- |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 | 463 |
493 // For strings. | 464 // For strings. |
494 explicit SerializedVarTestConstructor(const std::string& str); | 465 explicit SerializedVarTestConstructor(const std::string& str); |
495 }; | 466 }; |
496 | 467 |
497 // For tests that want to read what's in a SerializedVar. | 468 // For tests that want to read what's in a SerializedVar. |
498 class PPAPI_PROXY_EXPORT SerializedVarTestReader : public SerializedVar { | 469 class PPAPI_PROXY_EXPORT SerializedVarTestReader : public SerializedVar { |
499 public: | 470 public: |
500 explicit SerializedVarTestReader(const SerializedVar& var); | 471 explicit SerializedVarTestReader(const SerializedVar& var); |
501 | 472 |
502 // The "incomplete" var is the one sent over the wire. Strings and object | |
503 // IDs have not yet been converted, so this is the thing that tests will | |
504 // actually want to check. | |
505 PP_Var GetVar() const { return inner_->GetVar(); } | 473 PP_Var GetVar() const { return inner_->GetVar(); } |
506 }; | 474 }; |
507 | 475 |
508 } // namespace proxy | 476 } // namespace proxy |
509 } // namespace ppapi | 477 } // namespace ppapi |
510 | 478 |
511 #endif // PPAPI_PROXY_SERIALIZED_VAR_H_ | 479 #endif // PPAPI_PROXY_SERIALIZED_VAR_H_ |
OLD | NEW |