OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 10 matching lines...) Expand all Loading... |
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>() { | 28 template <> const char* interface_name<PPB_Var>() { |
29 return PPB_VAR_INTERFACE; | 29 return PPB_VAR_INTERFACE; |
30 } | 30 } |
| 31 template <> const char* interface_name<PPB_Var_1_0>() { |
| 32 return PPB_VAR_INTERFACE_1_0; |
| 33 } |
31 | 34 |
32 // 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 |
33 // 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 |
34 // only do refcounting on the necessary objects. | 37 // only do refcounting on the necessary objects. |
35 inline bool NeedsRefcounting(const PP_Var& var) { | 38 inline bool NeedsRefcounting(const PP_Var& var) { |
36 return var.type > PP_VARTYPE_DOUBLE; | 39 return var.type > PP_VARTYPE_DOUBLE; |
37 } | 40 } |
38 | 41 |
| 42 // This helper function detects whether PPB_Var version 1.1 is available. If so, |
| 43 // it uses it to create a PP_Var for the given string. Otherwise it falls back |
| 44 // to PPB_Var version 1.0. |
| 45 PP_Var VarFromUtf8Helper(const char* utf8_str, uint32_t len) { |
| 46 if (has_interface<PPB_Var>()) { |
| 47 return get_interface<PPB_Var>()->VarFromUtf8(utf8_str, len); |
| 48 } else if (has_interface<PPB_Var_1_0>()) { |
| 49 return get_interface<PPB_Var_1_0>()->VarFromUtf8(Module::Get()->pp_module(), |
| 50 utf8_str, |
| 51 len); |
| 52 } else { |
| 53 return PP_MakeNull(); |
| 54 } |
| 55 } |
| 56 |
39 } // namespace | 57 } // namespace |
40 | 58 |
41 Var::Var() { | 59 Var::Var() { |
42 memset(&var_, 0, sizeof(var_)); | 60 memset(&var_, 0, sizeof(var_)); |
43 var_.type = PP_VARTYPE_UNDEFINED; | 61 var_.type = PP_VARTYPE_UNDEFINED; |
44 needs_release_ = false; | 62 needs_release_ = false; |
45 } | 63 } |
46 | 64 |
47 Var::Var(Null) { | 65 Var::Var(Null) { |
48 memset(&var_, 0, sizeof(var_)); | 66 memset(&var_, 0, sizeof(var_)); |
(...skipping 16 matching lines...) Expand all Loading... |
65 } | 83 } |
66 | 84 |
67 Var::Var(double d) { | 85 Var::Var(double d) { |
68 var_.type = PP_VARTYPE_DOUBLE; | 86 var_.type = PP_VARTYPE_DOUBLE; |
69 var_.padding = 0; | 87 var_.padding = 0; |
70 var_.value.as_double = d; | 88 var_.value.as_double = d; |
71 needs_release_ = false; | 89 needs_release_ = false; |
72 } | 90 } |
73 | 91 |
74 Var::Var(const char* utf8_str) { | 92 Var::Var(const char* utf8_str) { |
75 if (has_interface<PPB_Var>()) { | 93 uint32_t len = utf8_str ? static_cast<uint32_t>(strlen(utf8_str)) : 0; |
76 uint32_t len = utf8_str ? static_cast<uint32_t>(strlen(utf8_str)) : 0; | 94 var_ = VarFromUtf8Helper(utf8_str, len); |
77 var_ = get_interface<PPB_Var>()->VarFromUtf8(Module::Get()->pp_module(), | |
78 utf8_str, len); | |
79 } else { | |
80 var_.type = PP_VARTYPE_NULL; | |
81 var_.padding = 0; | |
82 } | |
83 needs_release_ = (var_.type == PP_VARTYPE_STRING); | 95 needs_release_ = (var_.type == PP_VARTYPE_STRING); |
84 } | 96 } |
85 | 97 |
86 Var::Var(const std::string& utf8_str) { | 98 Var::Var(const std::string& utf8_str) { |
87 if (has_interface<PPB_Var>()) { | 99 var_ = VarFromUtf8Helper(utf8_str.c_str(), |
88 var_ = get_interface<PPB_Var>()->VarFromUtf8( | 100 static_cast<uint32_t>(utf8_str.size())); |
89 Module::Get()->pp_module(), | |
90 utf8_str.c_str(), | |
91 static_cast<uint32_t>(utf8_str.size())); | |
92 } else { | |
93 var_.type = PP_VARTYPE_NULL; | |
94 var_.padding = 0; | |
95 } | |
96 needs_release_ = (var_.type == PP_VARTYPE_STRING); | 101 needs_release_ = (var_.type == PP_VARTYPE_STRING); |
97 } | 102 } |
98 | 103 |
99 Var::Var(const Var& other) { | 104 Var::Var(const Var& other) { |
100 var_ = other.var_; | 105 var_ = other.var_; |
101 if (NeedsRefcounting(var_)) { | 106 if (NeedsRefcounting(var_)) { |
102 if (has_interface<PPB_Var>()) { | 107 if (has_interface<PPB_Var>()) { |
103 needs_release_ = true; | 108 needs_release_ = true; |
104 get_interface<PPB_Var>()->AddRef(var_); | 109 get_interface<PPB_Var_1_0>()->AddRef(var_); |
105 } else { | 110 } else { |
106 var_.type = PP_VARTYPE_NULL; | 111 var_.type = PP_VARTYPE_NULL; |
107 needs_release_ = false; | 112 needs_release_ = false; |
108 } | 113 } |
109 } else { | 114 } else { |
110 needs_release_ = false; | 115 needs_release_ = false; |
111 } | 116 } |
112 } | 117 } |
113 | 118 |
114 Var::~Var() { | 119 Var::~Var() { |
115 if (needs_release_ && has_interface<PPB_Var>()) | 120 if (needs_release_ && has_interface<PPB_Var>()) |
116 get_interface<PPB_Var>()->Release(var_); | 121 get_interface<PPB_Var_1_0>()->Release(var_); |
117 } | 122 } |
118 | 123 |
119 Var& Var::operator=(const Var& other) { | 124 Var& Var::operator=(const Var& other) { |
120 // Early return for self-assignment. Note however, that two distinct vars | 125 // Early return for self-assignment. Note however, that two distinct vars |
121 // can refer to the same object, so we still need to be careful about the | 126 // can refer to the same object, so we still need to be careful about the |
122 // refcounting below. | 127 // refcounting below. |
123 if (this == &other) | 128 if (this == &other) |
124 return *this; | 129 return *this; |
125 | 130 |
126 // Be careful to keep the ref alive for cases where we're assigning an | 131 // Be careful to keep the ref alive for cases where we're assigning an |
127 // object to itself by addrefing the new one before releasing the old one. | 132 // object to itself by addrefing the new one before releasing the old one. |
128 bool old_needs_release = needs_release_; | 133 bool old_needs_release = needs_release_; |
129 if (NeedsRefcounting(other.var_)) { | 134 if (NeedsRefcounting(other.var_)) { |
130 // Assume we already has_interface<PPB_Var> for refcounted vars or else we | 135 // Assume we already has_interface<PPB_Var> for refcounted vars or else we |
131 // couldn't have created them in the first place. | 136 // couldn't have created them in the first place. |
132 needs_release_ = true; | 137 needs_release_ = true; |
133 get_interface<PPB_Var>()->AddRef(other.var_); | 138 get_interface<PPB_Var_1_0>()->AddRef(other.var_); |
134 } else { | 139 } else { |
135 needs_release_ = false; | 140 needs_release_ = false; |
136 } | 141 } |
137 if (old_needs_release) | 142 if (old_needs_release) |
138 get_interface<PPB_Var>()->Release(var_); | 143 get_interface<PPB_Var_1_0>()->Release(var_); |
139 | 144 |
140 var_ = other.var_; | 145 var_ = other.var_; |
141 return *this; | 146 return *this; |
142 } | 147 } |
143 | 148 |
144 bool Var::operator==(const Var& other) const { | 149 bool Var::operator==(const Var& other) const { |
145 if (var_.type != other.var_.type) | 150 if (var_.type != other.var_.type) |
146 return false; | 151 return false; |
147 switch (var_.type) { | 152 switch (var_.type) { |
148 case PP_VARTYPE_UNDEFINED: | 153 case PP_VARTYPE_UNDEFINED: |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 199 |
195 std::string Var::AsString() const { | 200 std::string Var::AsString() const { |
196 if (!is_string()) { | 201 if (!is_string()) { |
197 PP_NOTREACHED(); | 202 PP_NOTREACHED(); |
198 return std::string(); | 203 return std::string(); |
199 } | 204 } |
200 | 205 |
201 if (!has_interface<PPB_Var>()) | 206 if (!has_interface<PPB_Var>()) |
202 return std::string(); | 207 return std::string(); |
203 uint32_t len; | 208 uint32_t len; |
204 const char* str = get_interface<PPB_Var>()->VarToUtf8(var_, &len); | 209 const char* str = get_interface<PPB_Var_1_0>()->VarToUtf8(var_, &len); |
205 return std::string(str, len); | 210 return std::string(str, len); |
206 } | 211 } |
207 | 212 |
208 std::string Var::DebugString() const { | 213 std::string Var::DebugString() const { |
209 char buf[256]; | 214 char buf[256]; |
210 if (is_undefined()) { | 215 if (is_undefined()) { |
211 snprintf(buf, sizeof(buf), "Var<UNDEFINED>"); | 216 snprintf(buf, sizeof(buf), "Var<UNDEFINED>"); |
212 } else if (is_null()) { | 217 } else if (is_null()) { |
213 snprintf(buf, sizeof(buf), "Var<NULL>"); | 218 snprintf(buf, sizeof(buf), "Var<NULL>"); |
214 } else if (is_bool()) { | 219 } else if (is_bool()) { |
(...skipping 15 matching lines...) Expand all Loading... |
230 str.append("..."); | 235 str.append("..."); |
231 } | 236 } |
232 snprintf(buf, sizeof(buf), format, str.c_str()); | 237 snprintf(buf, sizeof(buf), format, str.c_str()); |
233 } else if (is_object()) { | 238 } else if (is_object()) { |
234 snprintf(buf, sizeof(buf), "Var<OBJECT>"); | 239 snprintf(buf, sizeof(buf), "Var<OBJECT>"); |
235 } | 240 } |
236 return buf; | 241 return buf; |
237 } | 242 } |
238 | 243 |
239 } // namespace pp | 244 } // namespace pp |
OLD | NEW |