• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/strings/string_util.h"
9 #include "ppapi/shared_impl/array_var.h"
10 #include "ppapi/shared_impl/ppapi_globals.h"
11 #include "ppapi/shared_impl/var_tracker.h"
12 
13 namespace ppapi {
14 
DictionaryVar()15 DictionaryVar::DictionaryVar() {}
16 
~DictionaryVar()17 DictionaryVar::~DictionaryVar() {}
18 
19 // static
FromPPVar(const PP_Var & var)20 DictionaryVar* DictionaryVar::FromPPVar(const PP_Var& var) {
21   if (var.type != PP_VARTYPE_DICTIONARY)
22     return NULL;
23 
24   scoped_refptr<Var> var_object(
25       PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
26   if (!var_object.get())
27     return NULL;
28   return var_object->AsDictionaryVar();
29 }
30 
AsDictionaryVar()31 DictionaryVar* DictionaryVar::AsDictionaryVar() { return this; }
32 
GetType() const33 PP_VarType DictionaryVar::GetType() const { return PP_VARTYPE_DICTIONARY; }
34 
Get(const PP_Var & key) const35 PP_Var DictionaryVar::Get(const PP_Var& key) const {
36   StringVar* string_var = StringVar::FromPPVar(key);
37   if (!string_var)
38     return PP_MakeUndefined();
39 
40   KeyValueMap::const_iterator iter = key_value_map_.find(string_var->value());
41   if (iter != key_value_map_.end()) {
42     if (PpapiGlobals::Get()->GetVarTracker()->AddRefVar(iter->second.get()))
43       return iter->second.get();
44     else
45       return PP_MakeUndefined();
46   } else {
47     return PP_MakeUndefined();
48   }
49 }
50 
Set(const PP_Var & key,const PP_Var & value)51 PP_Bool DictionaryVar::Set(const PP_Var& key, const PP_Var& value) {
52   StringVar* string_var = StringVar::FromPPVar(key);
53   if (!string_var)
54     return PP_FALSE;
55 
56   key_value_map_[string_var->value()] = value;
57   return PP_TRUE;
58 }
59 
Delete(const PP_Var & key)60 void DictionaryVar::Delete(const PP_Var& key) {
61   StringVar* string_var = StringVar::FromPPVar(key);
62   if (!string_var)
63     return;
64 
65   key_value_map_.erase(string_var->value());
66 }
67 
HasKey(const PP_Var & key) const68 PP_Bool DictionaryVar::HasKey(const PP_Var& key) const {
69   StringVar* string_var = StringVar::FromPPVar(key);
70   if (!string_var)
71     return PP_FALSE;
72 
73   bool result =
74       key_value_map_.find(string_var->value()) != key_value_map_.end();
75   return PP_FromBool(result);
76 }
77 
GetKeys() const78 PP_Var DictionaryVar::GetKeys() const {
79   scoped_refptr<ArrayVar> array_var(new ArrayVar());
80   array_var->elements().reserve(key_value_map_.size());
81 
82   for (KeyValueMap::const_iterator iter = key_value_map_.begin();
83        iter != key_value_map_.end();
84        ++iter) {
85     array_var->elements().push_back(ScopedPPVar(
86         ScopedPPVar::PassRef(), StringVar::StringToPPVar(iter->first)));
87   }
88   return array_var->GetPPVar();
89 }
90 
SetWithStringKey(const std::string & utf8_key,const PP_Var & value)91 bool DictionaryVar::SetWithStringKey(const std::string& utf8_key,
92                                      const PP_Var& value) {
93   if (!base::IsStringUTF8(utf8_key))
94     return false;
95 
96   key_value_map_[utf8_key] = value;
97   return true;
98 }
99 
DeleteWithStringKey(const std::string & utf8_key)100 void DictionaryVar::DeleteWithStringKey(const std::string& utf8_key) {
101   key_value_map_.erase(utf8_key);
102 }
103 
104 }  // namespace ppapi
105