| 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 "ppapi/c/pp_var.h" | 14 #include "ppapi/c/pp_var.h" |
| 15 #include "ppapi/proxy/ppapi_proxy_export.h" | 15 #include "ppapi/proxy/ppapi_proxy_export.h" |
| 16 #include "ppapi/proxy/var_serialization_rules.h" |
| 16 | 17 |
| 17 class PickleIterator; | 18 class PickleIterator; |
| 18 | 19 |
| 19 namespace IPC { | 20 namespace IPC { |
| 20 class Message; | 21 class Message; |
| 21 } | 22 } |
| 22 | 23 |
| 23 namespace ppapi { | 24 namespace ppapi { |
| 24 namespace proxy { | 25 namespace proxy { |
| 25 | 26 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 PP_Var GetVar() const; | 104 PP_Var GetVar() const; |
| 104 void SetVar(PP_Var var); | 105 void SetVar(PP_Var var); |
| 105 | 106 |
| 106 // For the SerializedVarTestConstructor, this writes the Var value as if | 107 // For the SerializedVarTestConstructor, this writes the Var value as if |
| 107 // it was just received off the wire, without any serialization rules. | 108 // it was just received off the wire, without any serialization rules. |
| 108 void ForceSetVarValueForTest(PP_Var value); | 109 void ForceSetVarValueForTest(PP_Var value); |
| 109 | 110 |
| 110 void WriteToMessage(IPC::Message* m) const; | 111 void WriteToMessage(IPC::Message* m) const; |
| 111 bool ReadFromMessage(const IPC::Message* m, PickleIterator* iter); | 112 bool ReadFromMessage(const IPC::Message* m, PickleIterator* iter); |
| 112 | 113 |
| 113 // Sets the cleanup mode. See the CleanupMode enum below. These functions | 114 // Sets the cleanup mode. See the CleanupMode enum below. |
| 114 // are not just a simple setter in order to require that the appropriate | 115 void SetCleanupModeToEndSendPassRef(); |
| 115 // data is set along with the corresponding mode. | |
| 116 void SetCleanupModeToEndSendPassRef(Dispatcher* dispatcher); | |
| 117 void SetCleanupModeToEndReceiveCallerOwned(); | 116 void SetCleanupModeToEndReceiveCallerOwned(); |
| 118 | 117 |
| 119 private: | 118 private: |
| 120 enum CleanupMode { | 119 enum CleanupMode { |
| 121 // The serialized var won't do anything special in the destructor | 120 // The serialized var won't do anything special in the destructor |
| 122 // (default). | 121 // (default). |
| 123 CLEANUP_NONE, | 122 CLEANUP_NONE, |
| 124 | 123 |
| 125 // The serialized var will call EndSendPassRef in the destructor. | 124 // The serialized var will call EndSendPassRef in the destructor. |
| 126 END_SEND_PASS_REF, | 125 END_SEND_PASS_REF, |
| 127 | 126 |
| 128 // The serialized var will call EndReceiveCallerOwned in the destructor. | 127 // The serialized var will call EndReceiveCallerOwned in the destructor. |
| 129 END_RECEIVE_CALLER_OWNED | 128 END_RECEIVE_CALLER_OWNED |
| 130 }; | 129 }; |
| 131 | 130 |
| 132 // Rules for serializing and deserializing vars for this process type. | 131 // Rules for serializing and deserializing vars for this process type. |
| 133 // This may be NULL, but must be set before trying to serialize to IPC when | 132 // This may be NULL, but must be set before trying to serialize to IPC when |
| 134 // sending, or before converting back to a PP_Var when receiving. | 133 // sending, or before converting back to a PP_Var when receiving. |
| 135 VarSerializationRules* serialization_rules_; | 134 scoped_refptr<VarSerializationRules> serialization_rules_; |
| 136 | 135 |
| 137 // If this is set to VARTYPE_STRING and the 'value.id' is 0, then the | 136 // If this is set to VARTYPE_STRING and the 'value.id' is 0, then the |
| 138 // string_from_ipc_ holds the string. This means that the caller hasn't | 137 // string_from_ipc_ holds the string. This means that the caller hasn't |
| 139 // called Deserialize with a valid Dispatcher yet, which is how we can | 138 // called Deserialize with a valid Dispatcher yet, which is how we can |
| 140 // convert the serialized string value to a PP_Var string ID. | 139 // convert the serialized string value to a PP_Var string ID. |
| 141 // | 140 // |
| 142 // This var may not be complete until the serialization rules are set when | 141 // This var may not be complete until the serialization rules are set when |
| 143 // reading from IPC since we'll need that to convert the string_value to | 142 // reading from IPC since we'll need that to convert the string_value to |
| 144 // a string ID. Before this, the as_id will be 0 for VARTYPE_STRING. | 143 // a string ID. Before this, the as_id will be 0 for VARTYPE_STRING. |
| 145 PP_Var var_; | 144 PP_Var var_; |
| 146 | 145 |
| 147 CleanupMode cleanup_mode_; | 146 CleanupMode cleanup_mode_; |
| 148 | 147 |
| 149 // The dispatcher saved for the call to EndSendPassRef for the cleanup. | |
| 150 // This is only valid when cleanup_mode == END_SEND_PASS_REF. | |
| 151 Dispatcher* dispatcher_for_end_send_pass_ref_; | |
| 152 | |
| 153 #ifndef NDEBUG | 148 #ifndef NDEBUG |
| 154 // When being sent or received over IPC, we should only be serialized or | 149 // When being sent or received over IPC, we should only be serialized or |
| 155 // deserialized once. These flags help us assert this is true. | 150 // deserialized once. These flags help us assert this is true. |
| 156 mutable bool has_been_serialized_; | 151 mutable bool has_been_serialized_; |
| 157 mutable bool has_been_deserialized_; | 152 mutable bool has_been_deserialized_; |
| 158 #endif | 153 #endif |
| 159 | 154 |
| 160 DISALLOW_COPY_AND_ASSIGN(Inner); | 155 DISALLOW_COPY_AND_ASSIGN(Inner); |
| 161 }; | 156 }; |
| 162 | 157 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 // Example for API: | 194 // Example for API: |
| 200 // PP_Var MyFunction() | 195 // PP_Var MyFunction() |
| 201 // IPC message: | 196 // IPC message: |
| 202 // IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, SerializedVar); | 197 // IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, SerializedVar); |
| 203 // Message handler would be: | 198 // Message handler would be: |
| 204 // PP_Var MyFunctionProxy() { | 199 // PP_Var MyFunctionProxy() { |
| 205 // ReceiveSerializedVarReturnValue result; | 200 // ReceiveSerializedVarReturnValue result; |
| 206 // Send(new MyFunctionMsg(&result)); | 201 // Send(new MyFunctionMsg(&result)); |
| 207 // return result.Return(dispatcher()); | 202 // return result.Return(dispatcher()); |
| 208 // } | 203 // } |
| 204 // |
| 205 // TODO(yzshen): Move the dispatcher parameter to the constructor and store a |
| 206 // VarSerializationRules reference instead, in case the dispatcher is destroyed |
| 207 // while waiting for reply to the sync message. |
| 209 class PPAPI_PROXY_EXPORT ReceiveSerializedVarReturnValue | 208 class PPAPI_PROXY_EXPORT ReceiveSerializedVarReturnValue |
| 210 : public SerializedVar { | 209 : public SerializedVar { |
| 211 public: | 210 public: |
| 212 // Note that we can't set the dispatcher in the constructor because the | 211 // Note that we can't set the dispatcher in the constructor because the |
| 213 // data will be overridden when the return value is set. This constructor is | 212 // data will be overridden when the return value is set. This constructor is |
| 214 // normally used in the pattern above (operator= will be implicitly invoked | 213 // normally used in the pattern above (operator= will be implicitly invoked |
| 215 // when the sync message writes the output values). | 214 // when the sync message writes the output values). |
| 216 ReceiveSerializedVarReturnValue(); | 215 ReceiveSerializedVarReturnValue(); |
| 217 | 216 |
| 218 // This constructor can be used when deserializing manually. This is useful | 217 // This constructor can be used when deserializing manually. This is useful |
| (...skipping 20 matching lines...) Expand all Loading... |
| 239 class PPAPI_PROXY_EXPORT ReceiveSerializedException : public SerializedVar { | 238 class PPAPI_PROXY_EXPORT ReceiveSerializedException : public SerializedVar { |
| 240 public: | 239 public: |
| 241 ReceiveSerializedException(Dispatcher* dispatcher, PP_Var* exception); | 240 ReceiveSerializedException(Dispatcher* dispatcher, PP_Var* exception); |
| 242 ~ReceiveSerializedException(); | 241 ~ReceiveSerializedException(); |
| 243 | 242 |
| 244 // Returns true if the exception passed in the constructor is set. Check | 243 // Returns true if the exception passed in the constructor is set. Check |
| 245 // this before actually issuing the IPC. | 244 // this before actually issuing the IPC. |
| 246 bool IsThrown() const; | 245 bool IsThrown() const; |
| 247 | 246 |
| 248 private: | 247 private: |
| 249 Dispatcher* dispatcher_; | |
| 250 | |
| 251 // The input/output exception we're wrapping. May be NULL. | 248 // The input/output exception we're wrapping. May be NULL. |
| 252 PP_Var* exception_; | 249 PP_Var* exception_; |
| 253 | 250 |
| 254 DISALLOW_IMPLICIT_CONSTRUCTORS(ReceiveSerializedException); | 251 DISALLOW_IMPLICIT_CONSTRUCTORS(ReceiveSerializedException); |
| 255 }; | 252 }; |
| 256 | 253 |
| 257 // Helper class for when we're returning a vector of Vars. When it goes out | 254 // Helper class for when we're returning a vector of Vars. When it goes out |
| 258 // of scope it will automatically convert the vector filled by the IPC layer | 255 // of scope it will automatically convert the vector filled by the IPC layer |
| 259 // into the array specified by the constructor params. | 256 // into the array specified by the constructor params. |
| 260 // | 257 // |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 // We rely on the implicit constructor here since the IPC layer will call | 300 // We rely on the implicit constructor here since the IPC layer will call |
| 304 // us with a SerializedVar. Pass this object by value, the copy constructor | 301 // us with a SerializedVar. Pass this object by value, the copy constructor |
| 305 // will pass along the pointer (as cheap as passing a pointer arg). | 302 // will pass along the pointer (as cheap as passing a pointer arg). |
| 306 SerializedVarReceiveInput(const SerializedVar& serialized); | 303 SerializedVarReceiveInput(const SerializedVar& serialized); |
| 307 ~SerializedVarReceiveInput(); | 304 ~SerializedVarReceiveInput(); |
| 308 | 305 |
| 309 PP_Var Get(Dispatcher* dispatcher); | 306 PP_Var Get(Dispatcher* dispatcher); |
| 310 | 307 |
| 311 private: | 308 private: |
| 312 const SerializedVar& serialized_; | 309 const SerializedVar& serialized_; |
| 313 | |
| 314 // Since the SerializedVar is const, we can't set its dispatcher (which is | |
| 315 // OK since we don't need to). But since we need it for our own uses, we | |
| 316 // track it here. Will be NULL before Get() is called. | |
| 317 Dispatcher* dispatcher_; | |
| 318 PP_Var var_; | |
| 319 }; | 310 }; |
| 320 | 311 |
| 321 // For receiving an input vector of vars from the remote side. | 312 // For receiving an input vector of vars from the remote side. |
| 322 // | 313 // |
| 323 // Example: | 314 // Example: |
| 324 // OnMsgMyFunction(SerializedVarVectorReceiveInput vector) { | 315 // OnMsgMyFunction(SerializedVarVectorReceiveInput vector) { |
| 325 // uint32_t size; | 316 // uint32_t size; |
| 326 // PP_Var* array = vector.Get(dispatcher, &size); | 317 // PP_Var* array = vector.Get(dispatcher, &size); |
| 327 // MyFunction(size, array); | 318 // MyFunction(size, array); |
| 328 // } | 319 // } |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 // The "incomplete" var is the one sent over the wire. Strings and object | 435 // The "incomplete" var is the one sent over the wire. Strings and object |
| 445 // IDs have not yet been converted, so this is the thing that tests will | 436 // IDs have not yet been converted, so this is the thing that tests will |
| 446 // actually want to check. | 437 // actually want to check. |
| 447 PP_Var GetVar() const { return inner_->GetVar(); } | 438 PP_Var GetVar() const { return inner_->GetVar(); } |
| 448 }; | 439 }; |
| 449 | 440 |
| 450 } // namespace proxy | 441 } // namespace proxy |
| 451 } // namespace ppapi | 442 } // namespace ppapi |
| 452 | 443 |
| 453 #endif // PPAPI_PROXY_SERIALIZED_VAR_H_ | 444 #endif // PPAPI_PROXY_SERIALIZED_VAR_H_ |
| 454 | |
| OLD | NEW |