| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ppapi/c/pp_var.h" | 13 #include "ppapi/c/pp_var.h" |
| 14 #include "ppapi/proxy/ppapi_proxy_export.h" |
| 14 | 15 |
| 15 namespace IPC { | 16 namespace IPC { |
| 16 class Message; | 17 class Message; |
| 17 } | 18 } |
| 18 | 19 |
| 19 namespace ppapi { | 20 namespace ppapi { |
| 20 namespace proxy { | 21 namespace proxy { |
| 21 | 22 |
| 22 class Dispatcher; | 23 class Dispatcher; |
| 23 class VarSerializationRules; | 24 class VarSerializationRules; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 54 // Constness | 55 // Constness |
| 55 // --------- | 56 // --------- |
| 56 // SerializedVar basically doesn't support const. Everything is mutable and | 57 // SerializedVar basically doesn't support const. Everything is mutable and |
| 57 // most functions are declared const. This unfortunateness is because of the | 58 // most functions are declared const. This unfortunateness is because of the |
| 58 // way the IPC system works. When deserializing, it will have a const | 59 // way the IPC system works. When deserializing, it will have a const |
| 59 // SerializedVar in a Tuple and this will be given to the function. We kind of | 60 // SerializedVar in a Tuple and this will be given to the function. We kind of |
| 60 // want to modify that to convert strings and do refcounting. | 61 // want to modify that to convert strings and do refcounting. |
| 61 // | 62 // |
| 62 // The helper classes used for accessing the SerializedVar have more reasonable | 63 // The helper classes used for accessing the SerializedVar have more reasonable |
| 63 // behavior and will enforce that you don't do stupid things. | 64 // behavior and will enforce that you don't do stupid things. |
| 64 class SerializedVar { | 65 class PPAPI_PROXY_EXPORT SerializedVar { |
| 65 public: | 66 public: |
| 66 SerializedVar(); | 67 SerializedVar(); |
| 67 ~SerializedVar(); | 68 ~SerializedVar(); |
| 68 | 69 |
| 69 // Backend implementation for IPC::ParamTraits<SerializedVar>. | 70 // Backend implementation for IPC::ParamTraits<SerializedVar>. |
| 70 void WriteToMessage(IPC::Message* m) const { | 71 void WriteToMessage(IPC::Message* m) const { |
| 71 inner_->WriteToMessage(m); | 72 inner_->WriteToMessage(m); |
| 72 } | 73 } |
| 73 bool ReadFromMessage(const IPC::Message* m, void** iter) { | 74 bool ReadFromMessage(const IPC::Message* m, void** iter) { |
| 74 return inner_->ReadFromMessage(m, iter); | 75 return inner_->ReadFromMessage(m, iter); |
| 75 } | 76 } |
| 76 | 77 |
| 77 protected: | 78 protected: |
| 78 friend class SerializedVarReceiveInput; | 79 friend class SerializedVarReceiveInput; |
| 79 friend class SerializedVarReturnValue; | 80 friend class SerializedVarReturnValue; |
| 80 friend class SerializedVarOutParam; | 81 friend class SerializedVarOutParam; |
| 81 friend class SerializedVarSendInput; | 82 friend class SerializedVarSendInput; |
| 82 friend class SerializedVarTestConstructor; | 83 friend class SerializedVarTestConstructor; |
| 83 friend class SerializedVarVectorReceiveInput; | 84 friend class SerializedVarVectorReceiveInput; |
| 84 | 85 |
| 85 class Inner : public base::RefCounted<Inner> { | 86 class PPAPI_PROXY_EXPORT Inner : public base::RefCounted<Inner> { |
| 86 public: | 87 public: |
| 87 Inner(); | 88 Inner(); |
| 88 Inner(VarSerializationRules* serialization_rules); | 89 Inner(VarSerializationRules* serialization_rules); |
| 89 Inner(VarSerializationRules* serialization_rules, const PP_Var& var); | 90 Inner(VarSerializationRules* serialization_rules, const PP_Var& var); |
| 90 ~Inner(); | 91 ~Inner(); |
| 91 | 92 |
| 92 VarSerializationRules* serialization_rules() { | 93 VarSerializationRules* serialization_rules() { |
| 93 return serialization_rules_; | 94 return serialization_rules_; |
| 94 } | 95 } |
| 95 void set_serialization_rules(VarSerializationRules* serialization_rules) { | 96 void set_serialization_rules(VarSerializationRules* serialization_rules) { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 // Example for API: | 206 // Example for API: |
| 206 // PP_Var MyFunction() | 207 // PP_Var MyFunction() |
| 207 // IPC message: | 208 // IPC message: |
| 208 // IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, SerializedVar); | 209 // IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, SerializedVar); |
| 209 // Message handler would be: | 210 // Message handler would be: |
| 210 // PP_Var MyFunctionProxy() { | 211 // PP_Var MyFunctionProxy() { |
| 211 // ReceiveSerializedVarReturnValue result; | 212 // ReceiveSerializedVarReturnValue result; |
| 212 // Send(new MyFunctionMsg(&result)); | 213 // Send(new MyFunctionMsg(&result)); |
| 213 // return result.Return(dispatcher()); | 214 // return result.Return(dispatcher()); |
| 214 // } | 215 // } |
| 215 class ReceiveSerializedVarReturnValue : public SerializedVar { | 216 class PPAPI_PROXY_EXPORT ReceiveSerializedVarReturnValue |
| 217 : public SerializedVar { |
| 216 public: | 218 public: |
| 217 // Note that we can't set the dispatcher in the constructor because the | 219 // Note that we can't set the dispatcher in the constructor because the |
| 218 // data will be overridden when the return value is set. This constructor is | 220 // data will be overridden when the return value is set. This constructor is |
| 219 // normally used in the pattern above (operator= will be implicitly invoked | 221 // normally used in the pattern above (operator= will be implicitly invoked |
| 220 // when the sync message writes the output values). | 222 // when the sync message writes the output values). |
| 221 ReceiveSerializedVarReturnValue(); | 223 ReceiveSerializedVarReturnValue(); |
| 222 | 224 |
| 223 // This constructor can be used when deserializing manually. This is useful | 225 // This constructor can be used when deserializing manually. This is useful |
| 224 // when you're getting strings "returned" via a struct and need to manually | 226 // when you're getting strings "returned" via a struct and need to manually |
| 225 // get the PP_Vars out. In this case just do: | 227 // get the PP_Vars out. In this case just do: |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 // For receiving a value from the remote side. | 298 // For receiving a value from the remote side. |
| 297 // | 299 // |
| 298 // Example for API: | 300 // Example for API: |
| 299 // void MyFunction(PP_Var) | 301 // void MyFunction(PP_Var) |
| 300 // IPC message: | 302 // IPC message: |
| 301 // IPC_MESSAGE_ROUTED1(MyFunction, SerializedVar); | 303 // IPC_MESSAGE_ROUTED1(MyFunction, SerializedVar); |
| 302 // Message handler would be: | 304 // Message handler would be: |
| 303 // void OnMsgMyFunction(SerializedVarReceiveInput param) { | 305 // void OnMsgMyFunction(SerializedVarReceiveInput param) { |
| 304 // MyFunction(param.Get()); | 306 // MyFunction(param.Get()); |
| 305 // } | 307 // } |
| 306 class SerializedVarReceiveInput { | 308 class PPAPI_PROXY_EXPORT SerializedVarReceiveInput { |
| 307 public: | 309 public: |
| 308 // We rely on the implicit constructor here since the IPC layer will call | 310 // We rely on the implicit constructor here since the IPC layer will call |
| 309 // us with a SerializedVar. Pass this object by value, the copy constructor | 311 // us with a SerializedVar. Pass this object by value, the copy constructor |
| 310 // will pass along the pointer (as cheap as passing a pointer arg). | 312 // will pass along the pointer (as cheap as passing a pointer arg). |
| 311 SerializedVarReceiveInput(const SerializedVar& serialized); | 313 SerializedVarReceiveInput(const SerializedVar& serialized); |
| 312 ~SerializedVarReceiveInput(); | 314 ~SerializedVarReceiveInput(); |
| 313 | 315 |
| 314 PP_Var Get(Dispatcher* dispatcher); | 316 PP_Var Get(Dispatcher* dispatcher); |
| 315 | 317 |
| 316 private: | 318 private: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 // ReceiveSerializedVarReturnValue. | 354 // ReceiveSerializedVarReturnValue. |
| 353 // | 355 // |
| 354 // Example for API: | 356 // Example for API: |
| 355 // PP_Var MyFunction() | 357 // PP_Var MyFunction() |
| 356 // IPC message: | 358 // IPC message: |
| 357 // IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, SerializedVar); | 359 // IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, SerializedVar); |
| 358 // Message handler would be: | 360 // Message handler would be: |
| 359 // void OnMsgMyFunction(SerializedVarReturnValue result) { | 361 // void OnMsgMyFunction(SerializedVarReturnValue result) { |
| 360 // result.Return(dispatcher(), MyFunction()); | 362 // result.Return(dispatcher(), MyFunction()); |
| 361 // } | 363 // } |
| 362 class SerializedVarReturnValue { | 364 class PPAPI_PROXY_EXPORT SerializedVarReturnValue { |
| 363 public: | 365 public: |
| 364 // We rely on the implicit constructor here since the IPC layer will call | 366 // We rely on the implicit constructor here since the IPC layer will call |
| 365 // us with a SerializedVar*. Pass this object by value, the copy constructor | 367 // us with a SerializedVar*. Pass this object by value, the copy constructor |
| 366 // will pass along the pointer (as cheap as passing a pointer arg). | 368 // will pass along the pointer (as cheap as passing a pointer arg). |
| 367 SerializedVarReturnValue(SerializedVar* serialized); | 369 SerializedVarReturnValue(SerializedVar* serialized); |
| 368 | 370 |
| 369 void Return(Dispatcher* dispatcher, const PP_Var& var); | 371 void Return(Dispatcher* dispatcher, const PP_Var& var); |
| 370 | 372 |
| 371 // Helper function for code that doesn't use the pattern above, but gets | 373 // Helper function for code that doesn't use the pattern above, but gets |
| 372 // a return value from the remote side via a struct. You can pass in the | 374 // a return value from the remote side via a struct. You can pass in the |
| 373 // SerializedVar and a PP_Var will be created with return value semantics. | 375 // SerializedVar and a PP_Var will be created with return value semantics. |
| 374 static SerializedVar Convert(Dispatcher* dispatcher, const PP_Var& var); | 376 static SerializedVar Convert(Dispatcher* dispatcher, const PP_Var& var); |
| 375 | 377 |
| 376 private: | 378 private: |
| 377 SerializedVar* serialized_; | 379 SerializedVar* serialized_; |
| 378 }; | 380 }; |
| 379 | 381 |
| 380 // For writing an out param to the remote side. | 382 // For writing an out param to the remote side. |
| 381 // | 383 // |
| 382 // Example for API: | 384 // Example for API: |
| 383 // "void MyFunction(PP_Var* out);" | 385 // "void MyFunction(PP_Var* out);" |
| 384 // IPC message: | 386 // IPC message: |
| 385 // IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, SerializedVar); | 387 // IPC_SYNC_MESSAGE_ROUTED0_1(MyFunction, SerializedVar); |
| 386 // Message handler would be: | 388 // Message handler would be: |
| 387 // void OnMsgMyFunction(SerializedVarOutParam out_param) { | 389 // void OnMsgMyFunction(SerializedVarOutParam out_param) { |
| 388 // MyFunction(out_param.OutParam(dispatcher())); | 390 // MyFunction(out_param.OutParam(dispatcher())); |
| 389 // } | 391 // } |
| 390 class SerializedVarOutParam { | 392 class PPAPI_PROXY_EXPORT SerializedVarOutParam { |
| 391 public: | 393 public: |
| 392 // We rely on the implicit constructor here since the IPC layer will call | 394 // We rely on the implicit constructor here since the IPC layer will call |
| 393 // us with a SerializedVar*. Pass this object by value, the copy constructor | 395 // us with a SerializedVar*. Pass this object by value, the copy constructor |
| 394 // will pass along the pointer (as cheap as passing a pointer arg). | 396 // will pass along the pointer (as cheap as passing a pointer arg). |
| 395 SerializedVarOutParam(SerializedVar* serialized); | 397 SerializedVarOutParam(SerializedVar* serialized); |
| 396 ~SerializedVarOutParam(); | 398 ~SerializedVarOutParam(); |
| 397 | 399 |
| 398 // Call this function only once. The caller should write its result to the | 400 // Call this function only once. The caller should write its result to the |
| 399 // returned var pointer before this class goes out of scope. The var's | 401 // returned var pointer before this class goes out of scope. The var's |
| 400 // initial value will be VARTYPE_UNDEFINED. | 402 // initial value will be VARTYPE_UNDEFINED. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 425 Dispatcher* dispatcher_; | 427 Dispatcher* dispatcher_; |
| 426 std::vector<SerializedVar>* serialized_; | 428 std::vector<SerializedVar>* serialized_; |
| 427 | 429 |
| 428 uint32_t count_; | 430 uint32_t count_; |
| 429 PP_Var* array_; | 431 PP_Var* array_; |
| 430 }; | 432 }; |
| 431 | 433 |
| 432 // For tests that just want to construct a SerializedVar for giving it to one | 434 // For tests that just want to construct a SerializedVar for giving it to one |
| 433 // of the other classes. This emulates a SerializedVar just received over the | 435 // of the other classes. This emulates a SerializedVar just received over the |
| 434 // wire from another process. | 436 // wire from another process. |
| 435 class SerializedVarTestConstructor : public SerializedVar { | 437 class PPAPI_PROXY_EXPORT SerializedVarTestConstructor : public SerializedVar { |
| 436 public: | 438 public: |
| 437 // For POD-types and objects. | 439 // For POD-types and objects. |
| 438 explicit SerializedVarTestConstructor(const PP_Var& pod_var); | 440 explicit SerializedVarTestConstructor(const PP_Var& pod_var); |
| 439 | 441 |
| 440 // For strings. | 442 // For strings. |
| 441 explicit SerializedVarTestConstructor(const std::string& str); | 443 explicit SerializedVarTestConstructor(const std::string& str); |
| 442 }; | 444 }; |
| 443 | 445 |
| 444 // For tests that want to read what's in a SerializedVar. | 446 // For tests that want to read what's in a SerializedVar. |
| 445 class SerializedVarTestReader : public SerializedVar { | 447 class PPAPI_PROXY_EXPORT SerializedVarTestReader : public SerializedVar { |
| 446 public: | 448 public: |
| 447 explicit SerializedVarTestReader(const SerializedVar& var); | 449 explicit SerializedVarTestReader(const SerializedVar& var); |
| 448 | 450 |
| 449 // The "incomplete" var is the one sent over the wire. Strings and object | 451 // The "incomplete" var is the one sent over the wire. Strings and object |
| 450 // IDs have not yet been converted, so this is the thing that tests will | 452 // IDs have not yet been converted, so this is the thing that tests will |
| 451 // actually want to check. | 453 // actually want to check. |
| 452 PP_Var GetIncompleteVar() const { return inner_->GetIncompleteVar(); } | 454 PP_Var GetIncompleteVar() const { return inner_->GetIncompleteVar(); } |
| 453 | 455 |
| 454 const std::string& GetString() const { return inner_->GetString(); } | 456 const std::string& GetString() const { return inner_->GetString(); } |
| 455 }; | 457 }; |
| 456 | 458 |
| 457 } // namespace proxy | 459 } // namespace proxy |
| 458 } // namespace ppapi | 460 } // namespace ppapi |
| 459 | 461 |
| 460 #endif // PPAPI_PROXY_SERIALIZED_VAR_H_ | 462 #endif // PPAPI_PROXY_SERIALIZED_VAR_H_ |
| 461 | 463 |
| OLD | NEW |