• 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 }
17 
~DictionaryVar()18 DictionaryVar::~DictionaryVar() {
19 }
20 
21 // static
FromPPVar(const PP_Var & var)22 DictionaryVar* DictionaryVar::FromPPVar(const PP_Var& var) {
23   if (var.type != PP_VARTYPE_DICTIONARY)
24     return NULL;
25 
26   scoped_refptr<Var> var_object(
27       PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
28   if (!var_object.get())
29     return NULL;
30   return var_object->AsDictionaryVar();
31 }
32 
AsDictionaryVar()33 DictionaryVar* DictionaryVar::AsDictionaryVar() {
34   return this;
35 }
36 
GetType() const37 PP_VarType DictionaryVar::GetType() const {
38   return PP_VARTYPE_DICTIONARY;
39 }
40 
Get(const PP_Var & key) const41 PP_Var DictionaryVar::Get(const PP_Var& key) const {
42   StringVar* string_var = StringVar::FromPPVar(key);
43   if (!string_var)
44     return PP_MakeUndefined();
45 
46   KeyValueMap::const_iterator iter = key_value_map_.find(string_var->value());
47   if (iter != key_value_map_.end()) {
48     if (PpapiGlobals::Get()->GetVarTracker()->AddRefVar(iter->second.get()))
49       return iter->second.get();
50     else
51       return PP_MakeUndefined();
52   } else {
53     return PP_MakeUndefined();
54   }
55 }
56 
Set(const PP_Var & key,const PP_Var & value)57 PP_Bool DictionaryVar::Set(const PP_Var& key, const PP_Var& value) {
58   StringVar* string_var = StringVar::FromPPVar(key);
59   if (!string_var)
60     return PP_FALSE;
61 
62   key_value_map_[string_var->value()] = value;
63   return PP_TRUE;
64 }
65 
Delete(const PP_Var & key)66 void DictionaryVar::Delete(const PP_Var& key) {
67   StringVar* string_var = StringVar::FromPPVar(key);
68   if (!string_var)
69     return;
70 
71   key_value_map_.erase(string_var->value());
72 }
73 
HasKey(const PP_Var & key) const74 PP_Bool DictionaryVar::HasKey(const PP_Var& key) const {
75   StringVar* string_var = StringVar::FromPPVar(key);
76   if (!string_var)
77     return PP_FALSE;
78 
79   bool result =
80       key_value_map_.find(string_var->value()) != key_value_map_.end();
81   return PP_FromBool(result);
82 }
83 
GetKeys() const84 PP_Var DictionaryVar::GetKeys() const {
85   scoped_refptr<ArrayVar> array_var(new ArrayVar());
86   array_var->elements().reserve(key_value_map_.size());
87 
88   for (KeyValueMap::const_iterator iter = key_value_map_.begin();
89        iter != key_value_map_.end(); ++iter) {
90     array_var->elements().push_back(
91         ScopedPPVar(ScopedPPVar::PassRef(),
92                     StringVar::StringToPPVar(iter->first)));
93   }
94   return array_var->GetPPVar();
95 }
96 
SetWithStringKey(const std::string & utf8_key,const PP_Var & value)97 bool DictionaryVar::SetWithStringKey(const std::string& utf8_key,
98                                      const PP_Var& value) {
99   if (!IsStringUTF8(utf8_key))
100     return false;
101 
102   key_value_map_[utf8_key] = value;
103   return true;
104 }
105 
DeleteWithStringKey(const std::string & utf8_key)106 void DictionaryVar::DeleteWithStringKey(const std::string& utf8_key) {
107   key_value_map_.erase(utf8_key);
108 }
109 
110 }  // namespace ppapi
111