blob: f7af7d9355d8baeceed8317ca0924977b83ef903 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
// 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/array_var.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()) {
if (PpapiGlobals::Get()->GetVarTracker()->AddRefVar(iter->second.get()))
return iter->second.get();
else
return PP_MakeUndefined();
} 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 {
scoped_refptr<ArrayVar> array_var(new ArrayVar());
array_var->elements().reserve(key_value_map_.size());
for (KeyValueMap::const_iterator iter = key_value_map_.begin();
iter != key_value_map_.end(); ++iter) {
array_var->elements().push_back(
ScopedPPVar(ScopedPPVar::PassRef(),
StringVar::StringToPPVar(iter->first)));
}
return array_var->GetPPVar();
}
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
|