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> |
11 | 11 |
12 #include "ppapi/c/pp_var.h" | 12 #include "ppapi/c/pp_var.h" |
13 #include "ppapi/c/ppb_var.h" | 13 #include "ppapi/c/ppb_var.h" |
14 #include "ppapi/cpp/instance.h" | 14 #include "ppapi/cpp/instance.h" |
15 #include "ppapi/cpp/logging.h" | 15 #include "ppapi/cpp/logging.h" |
16 #include "ppapi/cpp/module.h" | 16 #include "ppapi/cpp/module.h" |
17 #include "ppapi/cpp/module_impl.h" | 17 #include "ppapi/cpp/module_impl.h" |
18 | 18 |
19 // Define equivalent to snprintf on Windows. | 19 // Define equivalent to snprintf on Windows. |
20 #if defined(_MSC_VER) | 20 #if defined(_MSC_VER) |
21 # define snprintf sprintf_s | 21 # define snprintf sprintf_s |
22 #endif | 22 #endif |
23 | 23 |
24 namespace pp { | 24 namespace pp { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 template <> const char* interface_name<PPB_Var_1_2>() { | |
29 return PPB_VAR_INTERFACE_1_2; | |
30 } | |
28 template <> const char* interface_name<PPB_Var_1_1>() { | 31 template <> const char* interface_name<PPB_Var_1_1>() { |
29 return PPB_VAR_INTERFACE_1_1; | 32 return PPB_VAR_INTERFACE_1_1; |
30 } | 33 } |
31 template <> const char* interface_name<PPB_Var_1_0>() { | 34 template <> const char* interface_name<PPB_Var_1_0>() { |
32 return PPB_VAR_INTERFACE_1_0; | 35 return PPB_VAR_INTERFACE_1_0; |
33 } | 36 } |
34 | 37 |
35 // Technically you can call AddRef and Release on any Var, but it may involve | 38 // 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 | 39 // cross-process calls depending on the plugin. This is an optimization so we |
37 // only do refcounting on the necessary objects. | 40 // only do refcounting on the necessary objects. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 var_ = VarFromUtf8Helper(utf8_str, len); | 97 var_ = VarFromUtf8Helper(utf8_str, len); |
95 is_managed_ = true; | 98 is_managed_ = true; |
96 } | 99 } |
97 | 100 |
98 Var::Var(const std::string& utf8_str) { | 101 Var::Var(const std::string& utf8_str) { |
99 var_ = VarFromUtf8Helper(utf8_str.c_str(), | 102 var_ = VarFromUtf8Helper(utf8_str.c_str(), |
100 static_cast<uint32_t>(utf8_str.size())); | 103 static_cast<uint32_t>(utf8_str.size())); |
101 is_managed_ = true; | 104 is_managed_ = true; |
102 } | 105 } |
103 | 106 |
107 Var::Var(const pp::Resource& resource) { | |
108 if (!has_interface<PPB_Var_1_2>()) { | |
109 PP_NOTREACHED(); | |
110 return; | |
111 } | |
112 | |
113 var_ = get_interface<PPB_Var_1_2>()->VarFromResource( | |
114 resource.pp_resource()); | |
115 // Set |is_managed_| to true, so |var_| will be properly released upon | |
116 // destruction. | |
117 is_managed_ = true; | |
118 } | |
119 | |
104 | 120 |
105 Var::Var(const PP_Var& var) { | 121 Var::Var(const PP_Var& var) { |
106 var_ = var; | 122 var_ = var; |
107 is_managed_ = true; | 123 is_managed_ = true; |
108 if (NeedsRefcounting(var_)) { | 124 if (NeedsRefcounting(var_)) { |
109 if (has_interface<PPB_Var_1_0>()) | 125 if (has_interface<PPB_Var_1_0>()) |
110 get_interface<PPB_Var_1_0>()->AddRef(var_); | 126 get_interface<PPB_Var_1_0>()->AddRef(var_); |
dmichael (off chromium)
2014/02/06 17:25:01
pp::Var was done before we decided to change our C
Matt Giuca
2014/02/07 03:11:22
:O is that necessary?
It doesn't seem scalable. I
dmichael (off chromium)
2014/02/07 17:45:28
Yes, we should do it here. It's definitely kind of
| |
111 else | 127 else |
112 var_.type = PP_VARTYPE_NULL; | 128 var_.type = PP_VARTYPE_NULL; |
113 } | 129 } |
114 } | 130 } |
115 | 131 |
116 Var::Var(const Var& other) { | 132 Var::Var(const Var& other) { |
117 var_ = other.var_; | 133 var_ = other.var_; |
118 is_managed_ = true; | 134 is_managed_ = true; |
119 if (NeedsRefcounting(var_)) { | 135 if (NeedsRefcounting(var_)) { |
120 if (has_interface<PPB_Var_1_0>()) | 136 if (has_interface<PPB_Var_1_0>()) |
yzshen1
2014/02/06 18:02:58
In all the other cpp files, we use the latest vers
| |
121 get_interface<PPB_Var_1_0>()->AddRef(var_); | 137 get_interface<PPB_Var_1_0>()->AddRef(var_); |
122 else | 138 else |
123 var_.type = PP_VARTYPE_NULL; | 139 var_.type = PP_VARTYPE_NULL; |
124 } | 140 } |
125 } | 141 } |
126 | 142 |
127 Var::~Var() { | 143 Var::~Var() { |
128 if (NeedsRefcounting(var_) && | 144 if (NeedsRefcounting(var_) && |
129 is_managed_ && | 145 is_managed_ && |
130 has_interface<PPB_Var_1_0>()) | 146 has_interface<PPB_Var_1_0>()) |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 return std::string(); | 228 return std::string(); |
213 } | 229 } |
214 | 230 |
215 if (!has_interface<PPB_Var_1_0>()) | 231 if (!has_interface<PPB_Var_1_0>()) |
216 return std::string(); | 232 return std::string(); |
217 uint32_t len; | 233 uint32_t len; |
218 const char* str = get_interface<PPB_Var_1_0>()->VarToUtf8(var_, &len); | 234 const char* str = get_interface<PPB_Var_1_0>()->VarToUtf8(var_, &len); |
219 return std::string(str, len); | 235 return std::string(str, len); |
220 } | 236 } |
221 | 237 |
238 pp::Resource Var::AsResource() const { | |
yzshen1
2014/02/06 18:02:58
Maybe we should follow AsString to assert that it
Matt Giuca
2014/02/07 03:11:22
Done.
| |
239 if (!has_interface<PPB_Var_1_2>()) | |
240 return pp::Resource(); | |
241 | |
242 return pp::Resource( | |
243 pp::PASS_REF, | |
244 get_interface<PPB_Var_1_2>()->VarToResource(var_)); | |
245 } | |
246 | |
222 std::string Var::DebugString() const { | 247 std::string Var::DebugString() const { |
223 char buf[256]; | 248 char buf[256]; |
224 if (is_undefined()) { | 249 if (is_undefined()) { |
225 snprintf(buf, sizeof(buf), "Var(UNDEFINED)"); | 250 snprintf(buf, sizeof(buf), "Var(UNDEFINED)"); |
226 } else if (is_null()) { | 251 } else if (is_null()) { |
227 snprintf(buf, sizeof(buf), "Var(NULL)"); | 252 snprintf(buf, sizeof(buf), "Var(NULL)"); |
228 } else if (is_bool()) { | 253 } else if (is_bool()) { |
229 snprintf(buf, sizeof(buf), AsBool() ? "Var(true)" : "Var(false)"); | 254 snprintf(buf, sizeof(buf), AsBool() ? "Var(true)" : "Var(false)"); |
230 } else if (is_int()) { | 255 } else if (is_int()) { |
231 snprintf(buf, sizeof(buf), "Var(%d)", static_cast<int>(AsInt())); | 256 snprintf(buf, sizeof(buf), "Var(%d)", static_cast<int>(AsInt())); |
(...skipping 17 matching lines...) Expand all Loading... | |
249 snprintf(buf, sizeof(buf), "Var(DICTIONARY)"); | 274 snprintf(buf, sizeof(buf), "Var(DICTIONARY)"); |
250 } else if (is_array_buffer()) { | 275 } else if (is_array_buffer()) { |
251 snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)"); | 276 snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)"); |
252 } else { | 277 } else { |
253 buf[0] = '\0'; | 278 buf[0] = '\0'; |
254 } | 279 } |
255 return buf; | 280 return buf; |
256 } | 281 } |
257 | 282 |
258 } // namespace pp | 283 } // namespace pp |
OLD | NEW |