OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/cpp/var.h" | 5 #include "ppapi/cpp/var.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 21 matching lines...) Expand all Loading... | |
32 return PPB_VAR_INTERFACE_1_0; | 32 return PPB_VAR_INTERFACE_1_0; |
33 } | 33 } |
34 | 34 |
35 // Technically you can call AddRef and Release on any Var, but it may involve | 35 // Technically you can call AddRef and Release on any Var, but it may involve |
36 // cross-process calls depending on the plugin. This is an optimization so we | 36 // cross-process calls depending on the plugin. This is an optimization so we |
37 // only do refcounting on the necessary objects. | 37 // only do refcounting on the necessary objects. |
38 inline bool NeedsRefcounting(const PP_Var& var) { | 38 inline bool NeedsRefcounting(const PP_Var& var) { |
39 return var.type > PP_VARTYPE_DOUBLE; | 39 return var.type > PP_VARTYPE_DOUBLE; |
40 } | 40 } |
41 | 41 |
42 // This helper function detects whether PPB_Var version 1.1 is available. If so, | 42 // This helper function uses the latest available version of VarFromUtf8. Note |
43 // it uses it to create a PP_Var for the given string. Otherwise it falls back | 43 // that version 1.0 of this method has a different API to later versions. |
44 // to PPB_Var version 1.0. | |
45 PP_Var VarFromUtf8Helper(const char* utf8_str, uint32_t len) { | 44 PP_Var VarFromUtf8Helper(const char* utf8_str, uint32_t len) { |
46 if (has_interface<PPB_Var_1_1>()) { | 45 if (has_interface<PPB_Var_1_1>()) { |
47 return get_interface<PPB_Var_1_1>()->VarFromUtf8(utf8_str, len); | 46 return get_interface<PPB_Var_1_1>()->VarFromUtf8(utf8_str, len); |
48 } else if (has_interface<PPB_Var_1_0>()) { | 47 } else if (has_interface<PPB_Var_1_0>()) { |
49 return get_interface<PPB_Var_1_0>()->VarFromUtf8(Module::Get()->pp_module(), | 48 return get_interface<PPB_Var_1_0>()->VarFromUtf8(Module::Get()->pp_module(), |
50 utf8_str, | 49 utf8_str, |
51 len); | 50 len); |
52 } else { | 51 } else { |
53 return PP_MakeNull(); | 52 return PP_MakeNull(); |
54 } | 53 } |
55 } | 54 } |
56 | 55 |
56 // This helper function uses the latest available version of AddRef. | |
57 // Returns true on success, false if no appropriate interface was available. | |
58 bool AddRefHelper(const PP_Var& var) { | |
59 if (has_interface<PPB_Var_1_1>()) { | |
60 get_interface<PPB_Var_1_1>()->AddRef(var); | |
61 return true; | |
62 } else if (has_interface<PPB_Var_1_0>()) { | |
63 get_interface<PPB_Var_1_0>()->AddRef(var); | |
64 return true; | |
65 } else { | |
66 return false; | |
67 } | |
dmichael (off chromium)
2014/02/10 22:53:08
per the style guide (as mentioned in https://coder
Matt Giuca
2014/02/10 23:58:10
Done.
| |
68 } | |
69 | |
70 // This helper function uses the latest available version of Release. | |
71 // Returns true on success, false if no appropriate interface was available. | |
72 bool ReleaseHelper(const PP_Var& var) { | |
73 if (has_interface<PPB_Var_1_1>()) { | |
74 get_interface<PPB_Var_1_1>()->Release(var); | |
75 return true; | |
76 } else if (has_interface<PPB_Var_1_0>()) { | |
77 get_interface<PPB_Var_1_0>()->Release(var); | |
78 return true; | |
79 } else { | |
80 return false; | |
81 } | |
82 } | |
83 | |
57 } // namespace | 84 } // namespace |
58 | 85 |
59 Var::Var() { | 86 Var::Var() { |
60 memset(&var_, 0, sizeof(var_)); | 87 memset(&var_, 0, sizeof(var_)); |
61 var_.type = PP_VARTYPE_UNDEFINED; | 88 var_.type = PP_VARTYPE_UNDEFINED; |
62 is_managed_ = true; | 89 is_managed_ = true; |
63 } | 90 } |
64 | 91 |
65 Var::Var(Null) { | 92 Var::Var(Null) { |
66 memset(&var_, 0, sizeof(var_)); | 93 memset(&var_, 0, sizeof(var_)); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 var_ = VarFromUtf8Helper(utf8_str.c_str(), | 126 var_ = VarFromUtf8Helper(utf8_str.c_str(), |
100 static_cast<uint32_t>(utf8_str.size())); | 127 static_cast<uint32_t>(utf8_str.size())); |
101 is_managed_ = true; | 128 is_managed_ = true; |
102 } | 129 } |
103 | 130 |
104 | 131 |
105 Var::Var(const PP_Var& var) { | 132 Var::Var(const PP_Var& var) { |
106 var_ = var; | 133 var_ = var; |
107 is_managed_ = true; | 134 is_managed_ = true; |
108 if (NeedsRefcounting(var_)) { | 135 if (NeedsRefcounting(var_)) { |
109 if (has_interface<PPB_Var_1_0>()) | 136 if (!AddRefHelper(var_)) |
110 get_interface<PPB_Var_1_0>()->AddRef(var_); | |
111 else | |
112 var_.type = PP_VARTYPE_NULL; | 137 var_.type = PP_VARTYPE_NULL; |
113 } | 138 } |
114 } | 139 } |
115 | 140 |
116 Var::Var(const Var& other) { | 141 Var::Var(const Var& other) { |
117 var_ = other.var_; | 142 var_ = other.var_; |
118 is_managed_ = true; | 143 is_managed_ = true; |
119 if (NeedsRefcounting(var_)) { | 144 if (NeedsRefcounting(var_)) { |
120 if (has_interface<PPB_Var_1_0>()) | 145 if (!AddRefHelper(var_)) |
121 get_interface<PPB_Var_1_0>()->AddRef(var_); | |
122 else | |
123 var_.type = PP_VARTYPE_NULL; | 146 var_.type = PP_VARTYPE_NULL; |
124 } | 147 } |
125 } | 148 } |
126 | 149 |
127 Var::~Var() { | 150 Var::~Var() { |
128 if (NeedsRefcounting(var_) && | 151 if (NeedsRefcounting(var_) && is_managed_) |
129 is_managed_ && | 152 ReleaseHelper(var_); |
130 has_interface<PPB_Var_1_0>()) | |
131 get_interface<PPB_Var_1_0>()->Release(var_); | |
132 } | 153 } |
133 | 154 |
134 Var& Var::operator=(const Var& other) { | 155 Var& Var::operator=(const Var& other) { |
135 // Early return for self-assignment. Note however, that two distinct vars | 156 // Early return for self-assignment. Note however, that two distinct vars |
136 // can refer to the same object, so we still need to be careful about the | 157 // can refer to the same object, so we still need to be careful about the |
137 // refcounting below. | 158 // refcounting below. |
138 if (this == &other) | 159 if (this == &other) |
139 return *this; | 160 return *this; |
140 | 161 |
141 // Be careful to keep the ref alive for cases where we're assigning an | 162 // Be careful to keep the ref alive for cases where we're assigning an |
142 // object to itself by addrefing the new one before releasing the old one. | 163 // object to itself by addrefing the new one before releasing the old one. |
143 bool old_is_managed = is_managed_; | 164 bool old_is_managed = is_managed_; |
144 is_managed_ = true; | 165 is_managed_ = true; |
145 if (NeedsRefcounting(other.var_)) { | 166 if (NeedsRefcounting(other.var_)) { |
146 // Assume we already has_interface<PPB_Var_1_0> for refcounted vars or else | 167 AddRefHelper(other.var_); |
147 // we couldn't have created them in the first place. | |
148 get_interface<PPB_Var_1_0>()->AddRef(other.var_); | |
149 } | 168 } |
150 if (NeedsRefcounting(var_) && old_is_managed) | 169 if (NeedsRefcounting(var_) && old_is_managed) |
151 get_interface<PPB_Var_1_0>()->Release(var_); | 170 ReleaseHelper(var_); |
152 | 171 |
153 var_ = other.var_; | 172 var_ = other.var_; |
154 return *this; | 173 return *this; |
155 } | 174 } |
156 | 175 |
157 bool Var::operator==(const Var& other) const { | 176 bool Var::operator==(const Var& other) const { |
158 if (var_.type != other.var_.type) | 177 if (var_.type != other.var_.type) |
159 return false; | 178 return false; |
160 switch (var_.type) { | 179 switch (var_.type) { |
161 case PP_VARTYPE_UNDEFINED: | 180 case PP_VARTYPE_UNDEFINED: |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 PP_NOTREACHED(); | 224 PP_NOTREACHED(); |
206 return 0.0; | 225 return 0.0; |
207 } | 226 } |
208 | 227 |
209 std::string Var::AsString() const { | 228 std::string Var::AsString() const { |
210 if (!is_string()) { | 229 if (!is_string()) { |
211 PP_NOTREACHED(); | 230 PP_NOTREACHED(); |
212 return std::string(); | 231 return std::string(); |
213 } | 232 } |
214 | 233 |
215 if (!has_interface<PPB_Var_1_0>()) | 234 uint32_t len; |
235 const char* str; | |
236 if (has_interface<PPB_Var_1_1>()) | |
237 str = get_interface<PPB_Var_1_1>()->VarToUtf8(var_, &len); | |
238 else if (has_interface<PPB_Var_1_0>()) | |
239 str = get_interface<PPB_Var_1_0>()->VarToUtf8(var_, &len); | |
240 else | |
216 return std::string(); | 241 return std::string(); |
217 uint32_t len; | |
218 const char* str = get_interface<PPB_Var_1_0>()->VarToUtf8(var_, &len); | |
219 return std::string(str, len); | 242 return std::string(str, len); |
220 } | 243 } |
221 | 244 |
222 std::string Var::DebugString() const { | 245 std::string Var::DebugString() const { |
223 char buf[256]; | 246 char buf[256]; |
224 if (is_undefined()) { | 247 if (is_undefined()) { |
225 snprintf(buf, sizeof(buf), "Var(UNDEFINED)"); | 248 snprintf(buf, sizeof(buf), "Var(UNDEFINED)"); |
226 } else if (is_null()) { | 249 } else if (is_null()) { |
227 snprintf(buf, sizeof(buf), "Var(NULL)"); | 250 snprintf(buf, sizeof(buf), "Var(NULL)"); |
228 } else if (is_bool()) { | 251 } else if (is_bool()) { |
(...skipping 20 matching lines...) Expand all Loading... | |
249 snprintf(buf, sizeof(buf), "Var(DICTIONARY)"); | 272 snprintf(buf, sizeof(buf), "Var(DICTIONARY)"); |
250 } else if (is_array_buffer()) { | 273 } else if (is_array_buffer()) { |
251 snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)"); | 274 snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)"); |
252 } else { | 275 } else { |
253 buf[0] = '\0'; | 276 buf[0] = '\0'; |
254 } | 277 } |
255 return buf; | 278 return buf; |
256 } | 279 } |
257 | 280 |
258 } // namespace pp | 281 } // namespace pp |
OLD | NEW |