OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ppapi/shared_impl/dictionary_var.h" |
| 6 |
| 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/string_util.h" |
| 9 #include "ppapi/shared_impl/ppapi_globals.h" |
| 10 #include "ppapi/shared_impl/var_tracker.h" |
| 11 |
| 12 namespace ppapi { |
| 13 |
| 14 DictionaryVar::DictionaryVar() { |
| 15 } |
| 16 |
| 17 DictionaryVar::~DictionaryVar() { |
| 18 } |
| 19 |
| 20 // static |
| 21 DictionaryVar* DictionaryVar::FromPPVar(const PP_Var& var) { |
| 22 if (var.type != PP_VARTYPE_DICTIONARY) |
| 23 return NULL; |
| 24 |
| 25 scoped_refptr<Var> var_object( |
| 26 PpapiGlobals::Get()->GetVarTracker()->GetVar(var)); |
| 27 if (!var_object.get()) |
| 28 return NULL; |
| 29 return var_object->AsDictionaryVar(); |
| 30 } |
| 31 |
| 32 DictionaryVar* DictionaryVar::AsDictionaryVar() { |
| 33 return this; |
| 34 } |
| 35 |
| 36 PP_VarType DictionaryVar::GetType() const { |
| 37 return PP_VARTYPE_DICTIONARY; |
| 38 } |
| 39 |
| 40 PP_Var DictionaryVar::Get(const PP_Var& key) const { |
| 41 StringVar* string_var = StringVar::FromPPVar(key); |
| 42 if (!string_var) |
| 43 return PP_MakeUndefined(); |
| 44 |
| 45 KeyValueMap::const_iterator iter = key_value_map_.find(string_var->value()); |
| 46 if (iter != key_value_map_.end()) { |
| 47 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(iter->second.get()); |
| 48 return iter->second.get(); |
| 49 } else { |
| 50 return PP_MakeUndefined(); |
| 51 } |
| 52 } |
| 53 |
| 54 PP_Bool DictionaryVar::Set(const PP_Var& key, const PP_Var& value) { |
| 55 StringVar* string_var = StringVar::FromPPVar(key); |
| 56 if (!string_var) |
| 57 return PP_FALSE; |
| 58 |
| 59 key_value_map_[string_var->value()] = value; |
| 60 return PP_TRUE; |
| 61 } |
| 62 |
| 63 void DictionaryVar::Delete(const PP_Var& key) { |
| 64 StringVar* string_var = StringVar::FromPPVar(key); |
| 65 if (!string_var) |
| 66 return; |
| 67 |
| 68 key_value_map_.erase(string_var->value()); |
| 69 } |
| 70 |
| 71 PP_Bool DictionaryVar::HasKey(const PP_Var& key) const { |
| 72 StringVar* string_var = StringVar::FromPPVar(key); |
| 73 if (!string_var) |
| 74 return PP_FALSE; |
| 75 |
| 76 bool result = |
| 77 key_value_map_.find(string_var->value()) != key_value_map_.end(); |
| 78 return PP_FromBool(result); |
| 79 } |
| 80 |
| 81 PP_Var DictionaryVar::GetKeys() const { |
| 82 // TODO(yzshen): Implement once array PP_Var is supported. |
| 83 return PP_MakeNull(); |
| 84 } |
| 85 |
| 86 bool DictionaryVar::SetWithStringKey(const std::string& utf8_key, |
| 87 const PP_Var& value) { |
| 88 if (!IsStringUTF8(utf8_key)) |
| 89 return false; |
| 90 |
| 91 key_value_map_[utf8_key] = value; |
| 92 return true; |
| 93 } |
| 94 |
| 95 void DictionaryVar::DeleteWithStringKey(const std::string& utf8_key) { |
| 96 key_value_map_.erase(utf8_key); |
| 97 } |
| 98 |
| 99 } // namespace ppapi |
OLD | NEW |