Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(944)

Side by Side Diff: ppapi/cpp/var.cc

Issue 158563002: [PPAPI] pp::Var uses the latest version of the PPB_Var API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove final else when all if/elseif branches return. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 {
53 return PP_MakeNull();
54 } 51 }
52 return PP_MakeNull();
53 }
54
55 // This helper function uses the latest available version of AddRef.
56 // Returns true on success, false if no appropriate interface was available.
57 bool AddRefHelper(const PP_Var& var) {
58 if (has_interface<PPB_Var_1_1>()) {
59 get_interface<PPB_Var_1_1>()->AddRef(var);
60 return true;
61 } else if (has_interface<PPB_Var_1_0>()) {
62 get_interface<PPB_Var_1_0>()->AddRef(var);
63 return true;
64 }
65 return false;
66 }
67
68 // This helper function uses the latest available version of Release.
69 // Returns true on success, false if no appropriate interface was available.
70 bool ReleaseHelper(const PP_Var& var) {
71 if (has_interface<PPB_Var_1_1>()) {
72 get_interface<PPB_Var_1_1>()->Release(var);
73 return true;
74 } else if (has_interface<PPB_Var_1_0>()) {
75 get_interface<PPB_Var_1_0>()->Release(var);
76 return true;
77 }
78 return false;
55 } 79 }
56 80
57 } // namespace 81 } // namespace
58 82
59 Var::Var() { 83 Var::Var() {
60 memset(&var_, 0, sizeof(var_)); 84 memset(&var_, 0, sizeof(var_));
61 var_.type = PP_VARTYPE_UNDEFINED; 85 var_.type = PP_VARTYPE_UNDEFINED;
62 is_managed_ = true; 86 is_managed_ = true;
63 } 87 }
64 88
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 var_ = VarFromUtf8Helper(utf8_str.c_str(), 123 var_ = VarFromUtf8Helper(utf8_str.c_str(),
100 static_cast<uint32_t>(utf8_str.size())); 124 static_cast<uint32_t>(utf8_str.size()));
101 is_managed_ = true; 125 is_managed_ = true;
102 } 126 }
103 127
104 128
105 Var::Var(const PP_Var& var) { 129 Var::Var(const PP_Var& var) {
106 var_ = var; 130 var_ = var;
107 is_managed_ = true; 131 is_managed_ = true;
108 if (NeedsRefcounting(var_)) { 132 if (NeedsRefcounting(var_)) {
109 if (has_interface<PPB_Var_1_0>()) 133 if (!AddRefHelper(var_))
110 get_interface<PPB_Var_1_0>()->AddRef(var_);
111 else
112 var_.type = PP_VARTYPE_NULL; 134 var_.type = PP_VARTYPE_NULL;
113 } 135 }
114 } 136 }
115 137
116 Var::Var(const Var& other) { 138 Var::Var(const Var& other) {
117 var_ = other.var_; 139 var_ = other.var_;
118 is_managed_ = true; 140 is_managed_ = true;
119 if (NeedsRefcounting(var_)) { 141 if (NeedsRefcounting(var_)) {
120 if (has_interface<PPB_Var_1_0>()) 142 if (!AddRefHelper(var_))
121 get_interface<PPB_Var_1_0>()->AddRef(var_);
122 else
123 var_.type = PP_VARTYPE_NULL; 143 var_.type = PP_VARTYPE_NULL;
124 } 144 }
125 } 145 }
126 146
127 Var::~Var() { 147 Var::~Var() {
128 if (NeedsRefcounting(var_) && 148 if (NeedsRefcounting(var_) && is_managed_)
129 is_managed_ && 149 ReleaseHelper(var_);
130 has_interface<PPB_Var_1_0>())
131 get_interface<PPB_Var_1_0>()->Release(var_);
132 } 150 }
133 151
134 Var& Var::operator=(const Var& other) { 152 Var& Var::operator=(const Var& other) {
135 // Early return for self-assignment. Note however, that two distinct vars 153 // 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 154 // can refer to the same object, so we still need to be careful about the
137 // refcounting below. 155 // refcounting below.
138 if (this == &other) 156 if (this == &other)
139 return *this; 157 return *this;
140 158
141 // Be careful to keep the ref alive for cases where we're assigning an 159 // 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. 160 // object to itself by addrefing the new one before releasing the old one.
143 bool old_is_managed = is_managed_; 161 bool old_is_managed = is_managed_;
144 is_managed_ = true; 162 is_managed_ = true;
145 if (NeedsRefcounting(other.var_)) { 163 if (NeedsRefcounting(other.var_)) {
146 // Assume we already has_interface<PPB_Var_1_0> for refcounted vars or else 164 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 } 165 }
150 if (NeedsRefcounting(var_) && old_is_managed) 166 if (NeedsRefcounting(var_) && old_is_managed)
151 get_interface<PPB_Var_1_0>()->Release(var_); 167 ReleaseHelper(var_);
152 168
153 var_ = other.var_; 169 var_ = other.var_;
154 return *this; 170 return *this;
155 } 171 }
156 172
157 bool Var::operator==(const Var& other) const { 173 bool Var::operator==(const Var& other) const {
158 if (var_.type != other.var_.type) 174 if (var_.type != other.var_.type)
159 return false; 175 return false;
160 switch (var_.type) { 176 switch (var_.type) {
161 case PP_VARTYPE_UNDEFINED: 177 case PP_VARTYPE_UNDEFINED:
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 PP_NOTREACHED(); 221 PP_NOTREACHED();
206 return 0.0; 222 return 0.0;
207 } 223 }
208 224
209 std::string Var::AsString() const { 225 std::string Var::AsString() const {
210 if (!is_string()) { 226 if (!is_string()) {
211 PP_NOTREACHED(); 227 PP_NOTREACHED();
212 return std::string(); 228 return std::string();
213 } 229 }
214 230
215 if (!has_interface<PPB_Var_1_0>()) 231 uint32_t len;
232 const char* str;
233 if (has_interface<PPB_Var_1_1>())
234 str = get_interface<PPB_Var_1_1>()->VarToUtf8(var_, &len);
235 else if (has_interface<PPB_Var_1_0>())
236 str = get_interface<PPB_Var_1_0>()->VarToUtf8(var_, &len);
237 else
216 return std::string(); 238 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); 239 return std::string(str, len);
220 } 240 }
221 241
222 std::string Var::DebugString() const { 242 std::string Var::DebugString() const {
223 char buf[256]; 243 char buf[256];
224 if (is_undefined()) { 244 if (is_undefined()) {
225 snprintf(buf, sizeof(buf), "Var(UNDEFINED)"); 245 snprintf(buf, sizeof(buf), "Var(UNDEFINED)");
226 } else if (is_null()) { 246 } else if (is_null()) {
227 snprintf(buf, sizeof(buf), "Var(NULL)"); 247 snprintf(buf, sizeof(buf), "Var(NULL)");
228 } else if (is_bool()) { 248 } else if (is_bool()) {
(...skipping 20 matching lines...) Expand all
249 snprintf(buf, sizeof(buf), "Var(DICTIONARY)"); 269 snprintf(buf, sizeof(buf), "Var(DICTIONARY)");
250 } else if (is_array_buffer()) { 270 } else if (is_array_buffer()) {
251 snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)"); 271 snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)");
252 } else { 272 } else {
253 buf[0] = '\0'; 273 buf[0] = '\0';
254 } 274 }
255 return buf; 275 return buf;
256 } 276 }
257 277
258 } // namespace pp 278 } // namespace pp
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698