| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/shared_impl/var_value_conversions.h" | 5 #include "ppapi/shared_impl/var_value_conversions.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <stack> | 9 #include <stack> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.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/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "ppapi/c/pp_bool.h" | 16 #include "ppapi/c/pp_bool.h" |
| 17 #include "ppapi/c/pp_stdint.h" | 17 #include "ppapi/c/pp_stdint.h" |
| 18 #include "ppapi/shared_impl/array_var.h" |
| 18 #include "ppapi/shared_impl/dictionary_var.h" | 19 #include "ppapi/shared_impl/dictionary_var.h" |
| 19 #include "ppapi/shared_impl/ppapi_globals.h" | 20 #include "ppapi/shared_impl/ppapi_globals.h" |
| 20 #include "ppapi/shared_impl/scoped_pp_var.h" | 21 #include "ppapi/shared_impl/scoped_pp_var.h" |
| 21 #include "ppapi/shared_impl/var.h" | 22 #include "ppapi/shared_impl/var.h" |
| 22 #include "ppapi/shared_impl/var_tracker.h" | 23 #include "ppapi/shared_impl/var_tracker.h" |
| 23 | 24 |
| 24 namespace ppapi { | 25 namespace ppapi { |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 if (!string_var) | 92 if (!string_var) |
| 92 return false; | 93 return false; |
| 93 | 94 |
| 94 value->reset(new base::StringValue(string_var->value())); | 95 value->reset(new base::StringValue(string_var->value())); |
| 95 return true; | 96 return true; |
| 96 } | 97 } |
| 97 case PP_VARTYPE_OBJECT: { | 98 case PP_VARTYPE_OBJECT: { |
| 98 return false; | 99 return false; |
| 99 } | 100 } |
| 100 case PP_VARTYPE_ARRAY: { | 101 case PP_VARTYPE_ARRAY: { |
| 101 // TODO(yzshen): Implement it once array var is supported. | 102 if (ContainsKey(parent_ids, var.value.as_id)) { |
| 102 return false; | 103 // A circular reference is found. |
| 104 return false; |
| 105 } |
| 106 |
| 107 value->reset(new base::ListValue()); |
| 108 state->push(VarNode(var, value->get())); |
| 109 return true; |
| 103 } | 110 } |
| 104 case PP_VARTYPE_DICTIONARY: { | 111 case PP_VARTYPE_DICTIONARY: { |
| 105 if (ContainsKey(parent_ids, var.value.as_id)) { | 112 if (ContainsKey(parent_ids, var.value.as_id)) { |
| 106 // A circular reference is found. | 113 // A circular reference is found. |
| 107 return false; | 114 return false; |
| 108 } | 115 } |
| 109 | 116 |
| 110 value->reset(new base::DictionaryValue()); | 117 value->reset(new base::DictionaryValue()); |
| 111 state->push(VarNode(var, value->get())); | 118 state->push(VarNode(var, value->get())); |
| 112 return true; | 119 return true; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 } | 200 } |
| 194 return false; | 201 return false; |
| 195 } | 202 } |
| 196 case base::Value::TYPE_DICTIONARY: { | 203 case base::Value::TYPE_DICTIONARY: { |
| 197 scoped_refptr<DictionaryVar> dict_var(new DictionaryVar()); | 204 scoped_refptr<DictionaryVar> dict_var(new DictionaryVar()); |
| 198 *var = ScopedPPVar(ScopedPPVar::PassRef(), dict_var->GetPPVar()); | 205 *var = ScopedPPVar(ScopedPPVar::PassRef(), dict_var->GetPPVar()); |
| 199 state->push(ValueNode(var->get(), &value)); | 206 state->push(ValueNode(var->get(), &value)); |
| 200 return true; | 207 return true; |
| 201 } | 208 } |
| 202 case base::Value::TYPE_LIST: { | 209 case base::Value::TYPE_LIST: { |
| 203 // TODO(yzshen): Add support once array var is supported. | 210 scoped_refptr<ArrayVar> array_var(new ArrayVar()); |
| 204 return false; | 211 *var = ScopedPPVar(ScopedPPVar::PassRef(), array_var->GetPPVar()); |
| 212 state->push(ValueNode(var->get(), &value)); |
| 213 return true; |
| 205 } | 214 } |
| 206 } | 215 } |
| 207 NOTREACHED(); | 216 NOTREACHED(); |
| 208 return false; | 217 return false; |
| 209 } | 218 } |
| 210 | 219 |
| 211 } // namespace | 220 } // namespace |
| 212 | 221 |
| 213 base::Value* CreateValueFromVar(const PP_Var& var) { | 222 base::Value* CreateValueFromVar(const PP_Var& var) { |
| 214 // Used to detect circular references. | 223 // Used to detect circular references. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 245 continue; | 254 continue; |
| 246 | 255 |
| 247 scoped_ptr<base::Value> child_value; | 256 scoped_ptr<base::Value> child_value; |
| 248 if (!CreateValueFromVarHelper(parent_ids, iter->second.get(), | 257 if (!CreateValueFromVarHelper(parent_ids, iter->second.get(), |
| 249 &child_value, &state)) { | 258 &child_value, &state)) { |
| 250 return NULL; | 259 return NULL; |
| 251 } | 260 } |
| 252 | 261 |
| 253 dict_value->SetWithoutPathExpansion(iter->first, child_value.release()); | 262 dict_value->SetWithoutPathExpansion(iter->first, child_value.release()); |
| 254 } | 263 } |
| 264 } else if (top.var.type == PP_VARTYPE_ARRAY) { |
| 265 parent_ids.insert(top.var.value.as_id); |
| 266 top.sentinel = true; |
| 267 |
| 268 ArrayVar* array_var = ArrayVar::FromPPVar(top.var); |
| 269 if (!array_var) |
| 270 return NULL; |
| 271 |
| 272 DCHECK(top.value->GetType() == base::Value::TYPE_LIST); |
| 273 base::ListValue* list_value = static_cast<base::ListValue*>(top.value); |
| 274 |
| 275 for (ArrayVar::ElementVector::const_iterator iter = |
| 276 array_var->elements().begin(); |
| 277 iter != array_var->elements().end(); |
| 278 ++iter) { |
| 279 scoped_ptr<base::Value> child_value; |
| 280 if (!CreateValueFromVarHelper(parent_ids, iter->get(), &child_value, |
| 281 &state)) { |
| 282 return NULL; |
| 283 } |
| 284 |
| 285 list_value->Append(child_value.release()); |
| 286 } |
| 255 } else { | 287 } else { |
| 256 NOTREACHED(); | 288 NOTREACHED(); |
| 257 return NULL; | 289 return NULL; |
| 258 } | 290 } |
| 259 } | 291 } |
| 260 DCHECK(parent_ids.empty()); | 292 DCHECK(parent_ids.empty()); |
| 261 return root_value.release(); | 293 return root_value.release(); |
| 262 } | 294 } |
| 263 | 295 |
| 264 PP_Var CreateVarFromValue(const base::Value& value) { | 296 PP_Var CreateVarFromValue(const base::Value& value) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 279 DCHECK(dict_var); | 311 DCHECK(dict_var); |
| 280 for (base::DictionaryValue::Iterator iter(*dict_value); | 312 for (base::DictionaryValue::Iterator iter(*dict_value); |
| 281 !iter.IsAtEnd(); | 313 !iter.IsAtEnd(); |
| 282 iter.Advance()) { | 314 iter.Advance()) { |
| 283 ScopedPPVar child_var; | 315 ScopedPPVar child_var; |
| 284 if (!CreateVarFromValueHelper(iter.value(), &child_var, &state) || | 316 if (!CreateVarFromValueHelper(iter.value(), &child_var, &state) || |
| 285 !dict_var->SetWithStringKey(iter.key(), child_var.get())) { | 317 !dict_var->SetWithStringKey(iter.key(), child_var.get())) { |
| 286 return PP_MakeUndefined(); | 318 return PP_MakeUndefined(); |
| 287 } | 319 } |
| 288 } | 320 } |
| 321 } else if (top.value->GetType() == base::Value::TYPE_LIST) { |
| 322 const base::ListValue* list_value = |
| 323 static_cast<const base::ListValue*>(top.value); |
| 324 ArrayVar* array_var = ArrayVar::FromPPVar(top.var); |
| 325 DCHECK(array_var); |
| 326 for (base::ListValue::const_iterator iter = list_value->begin(); |
| 327 iter != list_value->end(); |
| 328 ++iter) { |
| 329 ScopedPPVar child_var; |
| 330 if (!CreateVarFromValueHelper(**iter, &child_var, &state)) |
| 331 return PP_MakeUndefined(); |
| 332 |
| 333 array_var->elements().push_back(child_var); |
| 334 } |
| 289 } else { | 335 } else { |
| 290 NOTREACHED(); | 336 NOTREACHED(); |
| 291 return PP_MakeUndefined(); | 337 return PP_MakeUndefined(); |
| 292 } | 338 } |
| 293 } | 339 } |
| 294 | 340 |
| 295 return root_var.Release(); | 341 return root_var.Release(); |
| 296 } | 342 } |
| 297 } // namespace ppapi | 343 } // namespace ppapi |
| 298 | 344 |
| OLD | NEW |