Chromium Code Reviews| Index: ppapi/shared_impl/dictionary_var.cc |
| diff --git a/ppapi/shared_impl/dictionary_var.cc b/ppapi/shared_impl/dictionary_var.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..93a39353c786c6ed4d517991b6ac515b1c0991be |
| --- /dev/null |
| +++ b/ppapi/shared_impl/dictionary_var.cc |
| @@ -0,0 +1,99 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ppapi/shared_impl/dictionary_var.h" |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "base/string_util.h" |
| +#include "ppapi/shared_impl/ppapi_globals.h" |
| +#include "ppapi/shared_impl/var_tracker.h" |
| + |
| +namespace ppapi { |
| + |
| +DictionaryVar::DictionaryVar() { |
| +} |
| + |
| +DictionaryVar::~DictionaryVar() { |
| +} |
| + |
| +// static |
| +DictionaryVar* DictionaryVar::FromPPVar(const PP_Var& var) { |
| + if (var.type != PP_VARTYPE_DICTIONARY) |
| + return NULL; |
| + |
| + scoped_refptr<Var> var_object( |
| + PpapiGlobals::Get()->GetVarTracker()->GetVar(var)); |
| + if (!var_object.get()) |
| + return NULL; |
| + return var_object->AsDictionaryVar(); |
| +} |
| + |
| +DictionaryVar* DictionaryVar::AsDictionaryVar() { |
| + return this; |
| +} |
| + |
| +PP_VarType DictionaryVar::GetType() const { |
| + return PP_VARTYPE_DICTIONARY; |
| +} |
| + |
| +PP_Var DictionaryVar::Get(const PP_Var& key) const { |
| + StringVar* string_var = StringVar::FromPPVar(key); |
| + if (!string_var) |
| + return PP_MakeUndefined(); |
| + |
| + KeyValueMap::const_iterator iter = key_value_map_.find(string_var->value()); |
| + if (iter != key_value_map_.end()) { |
| + PpapiGlobals::Get()->GetVarTracker()->AddRefVar(iter->second.get()); |
|
dmichael (off chromium)
2013/03/05 22:01:03
If the plugin mis-manages its ref-count and decrem
yzshen1
2013/03/14 05:38:21
AddRefVar also returns false for those non-ref-cou
dmichael (off chromium)
2013/03/15 17:35:48
Right, that's what I meant.
yzshen1
2013/03/15 22:56:49
I changed to return an undefined var in that case.
|
| + return iter->second.get(); |
| + } else { |
| + return PP_MakeUndefined(); |
| + } |
| +} |
| + |
| +PP_Bool DictionaryVar::Set(const PP_Var& key, const PP_Var& value) { |
| + StringVar* string_var = StringVar::FromPPVar(key); |
| + if (!string_var) |
| + return PP_FALSE; |
| + |
| + key_value_map_[string_var->value()] = value; |
| + return PP_TRUE; |
| +} |
| + |
| +void DictionaryVar::Delete(const PP_Var& key) { |
| + StringVar* string_var = StringVar::FromPPVar(key); |
| + if (!string_var) |
| + return; |
| + |
| + key_value_map_.erase(string_var->value()); |
| +} |
| + |
| +PP_Bool DictionaryVar::HasKey(const PP_Var& key) const { |
| + StringVar* string_var = StringVar::FromPPVar(key); |
| + if (!string_var) |
| + return PP_FALSE; |
| + |
| + bool result = |
| + key_value_map_.find(string_var->value()) != key_value_map_.end(); |
| + return PP_FromBool(result); |
| +} |
| + |
| +PP_Var DictionaryVar::GetKeys() const { |
| + // TODO(yzshen): Implement once array PP_Var is supported. |
| + return PP_MakeNull(); |
| +} |
| + |
| +bool DictionaryVar::SetWithStringKey(const std::string& utf8_key, |
| + const PP_Var& value) { |
| + if (!IsStringUTF8(utf8_key)) |
| + return false; |
| + |
| + key_value_map_[utf8_key] = value; |
| + return true; |
| +} |
| + |
| +void DictionaryVar::DeleteWithStringKey(const std::string& utf8_key) { |
| + key_value_map_.erase(utf8_key); |
| +} |
| + |
| +} // namespace ppapi |