| 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 #include "ppapi/proxy/serialized_var.h" | 5 #include "ppapi/proxy/serialized_var.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "ipc/ipc_message_utils.h" | 8 #include "ipc/ipc_message_utils.h" |
| 9 #include "ppapi/proxy/dispatcher.h" | 9 #include "ppapi/proxy/dispatcher.h" |
| 10 #include "ppapi/proxy/interface_proxy.h" | 10 #include "ppapi/proxy/interface_proxy.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 serialization_rules_->EndSendPassRef(var_); | 43 serialization_rules_->EndSendPassRef(var_); |
| 44 break; | 44 break; |
| 45 case END_RECEIVE_CALLER_OWNED: | 45 case END_RECEIVE_CALLER_OWNED: |
| 46 serialization_rules_->EndReceiveCallerOwned(var_); | 46 serialization_rules_->EndReceiveCallerOwned(var_); |
| 47 break; | 47 break; |
| 48 default: | 48 default: |
| 49 break; | 49 break; |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 | 52 |
| 53 PP_Var SerializedVar::Inner::GetVar() const { | 53 PP_Var SerializedVar::Inner::GetVar() { |
| 54 DCHECK(serialization_rules_); | 54 DCHECK(serialization_rules_); |
| 55 |
| 56 ConvertRawVarData(); |
| 55 return var_; | 57 return var_; |
| 56 } | 58 } |
| 57 | 59 |
| 58 void SerializedVar::Inner::SetVar(PP_Var var) { | 60 void SerializedVar::Inner::SetVar(PP_Var var) { |
| 59 // Sanity check, when updating the var we should have received a | 61 // Sanity check, when updating the var we should have received a |
| 60 // serialization rules pointer already. | 62 // serialization rules pointer already. |
| 61 DCHECK(serialization_rules_); | 63 DCHECK(serialization_rules_); |
| 62 var_ = var; | 64 var_ = var; |
| 65 raw_var_data_.reset(NULL); |
| 63 } | 66 } |
| 64 | 67 |
| 65 void SerializedVar::Inner::ForceSetVarValueForTest(PP_Var value) { | 68 void SerializedVar::Inner::ForceSetVarValueForTest(PP_Var value) { |
| 66 var_ = value; | 69 var_ = value; |
| 70 raw_var_data_.reset(NULL); |
| 67 } | 71 } |
| 68 | 72 |
| 69 void SerializedVar::Inner::WriteToMessage(IPC::Message* m) const { | 73 void SerializedVar::Inner::WriteToMessage(IPC::Message* m) const { |
| 70 // When writing to the IPC messages, a serization rules handler should | 74 // When writing to the IPC messages, a serialization rules handler should |
| 71 // always have been set. | 75 // always have been set. |
| 72 // | 76 // |
| 73 // When sending a message, it should be difficult to trigger this if you're | 77 // When sending a message, it should be difficult to trigger this if you're |
| 74 // using the SerializedVarSendInput class and giving a non-NULL dispatcher. | 78 // using the SerializedVarSendInput class and giving a non-NULL dispatcher. |
| 75 // Make sure you're using the proper "Send" helper class. | 79 // Make sure you're using the proper "Send" helper class. |
| 76 // | 80 // |
| 77 // It should be more common to see this when handling an incoming message | 81 // It should be more common to see this when handling an incoming message |
| 78 // that returns a var. This means the message handler didn't write to the | 82 // that returns a var. This means the message handler didn't write to the |
| 79 // output parameter, or possibly you used the wrong helper class | 83 // output parameter, or possibly you used the wrong helper class |
| 80 // (normally SerializedVarReturnValue). | 84 // (normally SerializedVarReturnValue). |
| 81 DCHECK(serialization_rules_); | 85 DCHECK(serialization_rules_); |
| 82 | 86 |
| 83 #ifndef NDEBUG | 87 #ifndef NDEBUG |
| 84 // We should only be serializing something once. | 88 // We should only be serializing something once. |
| 85 DCHECK(!has_been_serialized_); | 89 DCHECK(!has_been_serialized_); |
| 86 has_been_serialized_ = true; | 90 has_been_serialized_ = true; |
| 87 #endif | 91 #endif |
| 88 | 92 |
| 93 DCHECK(!raw_var_data_.get()); |
| 89 m->WriteInt(static_cast<int>(var_.type)); | 94 m->WriteInt(static_cast<int>(var_.type)); |
| 90 switch (var_.type) { | 95 switch (var_.type) { |
| 91 case PP_VARTYPE_UNDEFINED: | 96 case PP_VARTYPE_UNDEFINED: |
| 92 case PP_VARTYPE_NULL: | 97 case PP_VARTYPE_NULL: |
| 93 // These don't need any data associated with them other than the type we | 98 // These don't need any data associated with them other than the type we |
| 94 // just serialized. | 99 // just serialized. |
| 95 break; | 100 break; |
| 96 case PP_VARTYPE_BOOL: | 101 case PP_VARTYPE_BOOL: |
| 97 m->WriteBool(PP_ToBool(var_.value.as_bool)); | 102 m->WriteBool(PP_ToBool(var_.value.as_bool)); |
| 98 break; | 103 break; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 var_.value.as_bool = PP_FromBool(bool_value); | 180 var_.value.as_bool = PP_FromBool(bool_value); |
| 176 break; | 181 break; |
| 177 } | 182 } |
| 178 case PP_VARTYPE_INT32: | 183 case PP_VARTYPE_INT32: |
| 179 success = m->ReadInt(iter, &var_.value.as_int); | 184 success = m->ReadInt(iter, &var_.value.as_int); |
| 180 break; | 185 break; |
| 181 case PP_VARTYPE_DOUBLE: | 186 case PP_VARTYPE_DOUBLE: |
| 182 success = IPC::ParamTraits<double>::Read(m, iter, &var_.value.as_double); | 187 success = IPC::ParamTraits<double>::Read(m, iter, &var_.value.as_double); |
| 183 break; | 188 break; |
| 184 case PP_VARTYPE_STRING: { | 189 case PP_VARTYPE_STRING: { |
| 185 std::string string_from_ipc; | 190 raw_var_data_.reset(new RawVarData); |
| 186 success = m->ReadString(iter, &string_from_ipc); | 191 raw_var_data_->type = PP_VARTYPE_STRING; |
| 187 var_ = StringVar::SwapValidatedUTF8StringIntoPPVar(&string_from_ipc); | 192 success = m->ReadString(iter, &raw_var_data_->data); |
| 193 if (!success) |
| 194 raw_var_data_.reset(NULL); |
| 188 break; | 195 break; |
| 189 } | 196 } |
| 190 case PP_VARTYPE_ARRAY_BUFFER: { | 197 case PP_VARTYPE_ARRAY_BUFFER: { |
| 191 int length = 0; | 198 int length = 0; |
| 192 const char* message_bytes = NULL; | 199 const char* message_bytes = NULL; |
| 193 success = m->ReadData(iter, &message_bytes, &length); | 200 success = m->ReadData(iter, &message_bytes, &length); |
| 194 if (success) { | 201 if (success) { |
| 195 var_ = PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( | 202 raw_var_data_.reset(new RawVarData); |
| 196 length, message_bytes); | 203 raw_var_data_->type = PP_VARTYPE_ARRAY_BUFFER; |
| 204 raw_var_data_->data.assign(message_bytes, length); |
| 197 } | 205 } |
| 198 break; | 206 break; |
| 199 } | 207 } |
| 200 case PP_VARTYPE_OBJECT: | 208 case PP_VARTYPE_OBJECT: |
| 201 success = m->ReadInt64(iter, &var_.value.as_id); | 209 success = m->ReadInt64(iter, &var_.value.as_id); |
| 202 break; | 210 break; |
| 203 case PP_VARTYPE_ARRAY: | 211 case PP_VARTYPE_ARRAY: |
| 204 case PP_VARTYPE_DICTIONARY: | 212 case PP_VARTYPE_DICTIONARY: |
| 205 // TODO(brettw) when these types are supported, implement this. | 213 // TODO(brettw) when these types are supported, implement this. |
| 206 NOTIMPLEMENTED(); | 214 NOTIMPLEMENTED(); |
| 207 break; | 215 break; |
| 208 default: | 216 default: |
| 209 // Leave success as false. | 217 // Leave success as false. |
| 210 break; | 218 break; |
| 211 } | 219 } |
| 212 | 220 |
| 213 // All success cases get here. We avoid writing the type above so that the | 221 // All success cases get here. We avoid writing the type above so that the |
| 214 // output param is untouched (defaults to VARTYPE_UNDEFINED) even in the | 222 // output param is untouched (defaults to VARTYPE_UNDEFINED) even in the |
| 215 // failure case. | 223 // failure case. |
| 216 if (success) | 224 // We also don't write the type if |raw_var_data_| is set. |var_| will be |
| 225 // updated lazily when GetVar() is called. |
| 226 if (success && !raw_var_data_.get()) |
| 217 var_.type = static_cast<PP_VarType>(type); | 227 var_.type = static_cast<PP_VarType>(type); |
| 218 return success; | 228 return success; |
| 219 } | 229 } |
| 220 | 230 |
| 221 void SerializedVar::Inner::SetCleanupModeToEndSendPassRef() { | 231 void SerializedVar::Inner::SetCleanupModeToEndSendPassRef() { |
| 222 cleanup_mode_ = END_SEND_PASS_REF; | 232 cleanup_mode_ = END_SEND_PASS_REF; |
| 223 } | 233 } |
| 224 | 234 |
| 225 void SerializedVar::Inner::SetCleanupModeToEndReceiveCallerOwned() { | 235 void SerializedVar::Inner::SetCleanupModeToEndReceiveCallerOwned() { |
| 226 cleanup_mode_ = END_RECEIVE_CALLER_OWNED; | 236 cleanup_mode_ = END_RECEIVE_CALLER_OWNED; |
| 227 } | 237 } |
| 228 | 238 |
| 239 void SerializedVar::Inner::ConvertRawVarData() { |
| 240 if (!raw_var_data_.get()) |
| 241 return; |
| 242 |
| 243 DCHECK_EQ(PP_VARTYPE_UNDEFINED, var_.type); |
| 244 switch (raw_var_data_->type) { |
| 245 case PP_VARTYPE_STRING: { |
| 246 var_ = StringVar::SwapValidatedUTF8StringIntoPPVar( |
| 247 &raw_var_data_->data); |
| 248 break; |
| 249 } |
| 250 case PP_VARTYPE_ARRAY_BUFFER: { |
| 251 var_ = PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( |
| 252 raw_var_data_->data.size(), raw_var_data_->data.data()); |
| 253 break; |
| 254 } |
| 255 default: |
| 256 NOTREACHED(); |
| 257 } |
| 258 raw_var_data_.reset(NULL); |
| 259 } |
| 260 |
| 229 // SerializedVar --------------------------------------------------------------- | 261 // SerializedVar --------------------------------------------------------------- |
| 230 | 262 |
| 231 SerializedVar::SerializedVar() : inner_(new Inner) { | 263 SerializedVar::SerializedVar() : inner_(new Inner) { |
| 232 } | 264 } |
| 233 | 265 |
| 234 SerializedVar::SerializedVar(VarSerializationRules* serialization_rules) | 266 SerializedVar::SerializedVar(VarSerializationRules* serialization_rules) |
| 235 : inner_(new Inner(serialization_rules)) { | 267 : inner_(new Inner(serialization_rules)) { |
| 236 } | 268 } |
| 237 | 269 |
| 238 SerializedVar::~SerializedVar() { | 270 SerializedVar::~SerializedVar() { |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 const std::string& str) { | 530 const std::string& str) { |
| 499 inner_->ForceSetVarValueForTest(StringVar::StringToPPVar(str)); | 531 inner_->ForceSetVarValueForTest(StringVar::StringToPPVar(str)); |
| 500 } | 532 } |
| 501 | 533 |
| 502 SerializedVarTestReader::SerializedVarTestReader(const SerializedVar& var) | 534 SerializedVarTestReader::SerializedVarTestReader(const SerializedVar& var) |
| 503 : SerializedVar(var) { | 535 : SerializedVar(var) { |
| 504 } | 536 } |
| 505 | 537 |
| 506 } // namespace proxy | 538 } // namespace proxy |
| 507 } // namespace ppapi | 539 } // namespace ppapi |
| OLD | NEW |