Chromium Code Reviews| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 } else if (has_interface<PPB_Var_1_0>()) { | 48 } else if (has_interface<PPB_Var_1_0>()) { |
| 49 return get_interface<PPB_Var_1_0>()->VarFromUtf8(Module::Get()->pp_module(), | 49 return get_interface<PPB_Var_1_0>()->VarFromUtf8(Module::Get()->pp_module(), |
| 50 utf8_str, | 50 utf8_str, |
| 51 len); | 51 len); |
| 52 } else { | 52 } else { |
| 53 return PP_MakeNull(); | 53 return PP_MakeNull(); |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 } // namespace | 57 } // namespace |
| 58 | 58 |
|
Takashi Toyoshima
2012/09/07 17:18:27
ppapi/cpp/websocket.cc uses Var::pp_var() to get i
| |
| 59 Var::Var() { | 59 Var::Var() { |
| 60 memset(&var_, 0, sizeof(var_)); | 60 memset(&var_, 0, sizeof(var_)); |
| 61 var_.type = PP_VARTYPE_UNDEFINED; | 61 var_.type = PP_VARTYPE_UNDEFINED; |
| 62 needs_release_ = false; | 62 needs_release_ = true; |
|
Takashi Toyoshima
2012/09/07 17:18:27
NeedsRefcounting() tell us if the PP_Var requires
dmichael (off chromium)
2012/09/07 18:36:58
Currently, it only tells you if the Var's *type* r
Takashi Toyoshima
2012/09/10 13:21:27
I meant that we can change the meaning of |needs_r
| |
| 63 } | 63 } |
| 64 | 64 |
| 65 Var::Var(Null) { | 65 Var::Var(Null) { |
| 66 memset(&var_, 0, sizeof(var_)); | 66 memset(&var_, 0, sizeof(var_)); |
| 67 var_.type = PP_VARTYPE_NULL; | 67 var_.type = PP_VARTYPE_NULL; |
| 68 needs_release_ = false; | 68 needs_release_ = true; |
| 69 } | 69 } |
| 70 | 70 |
| 71 Var::Var(bool b) { | 71 Var::Var(bool b) { |
| 72 var_.type = PP_VARTYPE_BOOL; | 72 var_.type = PP_VARTYPE_BOOL; |
| 73 var_.padding = 0; | 73 var_.padding = 0; |
| 74 var_.value.as_bool = PP_FromBool(b); | 74 var_.value.as_bool = PP_FromBool(b); |
| 75 needs_release_ = false; | 75 needs_release_ = true; |
| 76 } | 76 } |
| 77 | 77 |
| 78 Var::Var(int32_t i) { | 78 Var::Var(int32_t i) { |
| 79 var_.type = PP_VARTYPE_INT32; | 79 var_.type = PP_VARTYPE_INT32; |
| 80 var_.padding = 0; | 80 var_.padding = 0; |
| 81 var_.value.as_int = i; | 81 var_.value.as_int = i; |
| 82 needs_release_ = false; | 82 needs_release_ = true; |
| 83 } | 83 } |
| 84 | 84 |
| 85 Var::Var(double d) { | 85 Var::Var(double d) { |
| 86 var_.type = PP_VARTYPE_DOUBLE; | 86 var_.type = PP_VARTYPE_DOUBLE; |
| 87 var_.padding = 0; | 87 var_.padding = 0; |
| 88 var_.value.as_double = d; | 88 var_.value.as_double = d; |
| 89 needs_release_ = false; | 89 needs_release_ = true; |
| 90 } | 90 } |
| 91 | 91 |
| 92 Var::Var(const char* utf8_str) { | 92 Var::Var(const char* utf8_str) { |
| 93 uint32_t len = utf8_str ? static_cast<uint32_t>(strlen(utf8_str)) : 0; | 93 uint32_t len = utf8_str ? static_cast<uint32_t>(strlen(utf8_str)) : 0; |
| 94 var_ = VarFromUtf8Helper(utf8_str, len); | 94 var_ = VarFromUtf8Helper(utf8_str, len); |
| 95 needs_release_ = (var_.type == PP_VARTYPE_STRING); | 95 needs_release_ = (var_.type == PP_VARTYPE_STRING); |
| 96 } | 96 } |
| 97 | 97 |
| 98 Var::Var(const std::string& utf8_str) { | 98 Var::Var(const std::string& utf8_str) { |
| 99 var_ = VarFromUtf8Helper(utf8_str.c_str(), | 99 var_ = VarFromUtf8Helper(utf8_str.c_str(), |
| 100 static_cast<uint32_t>(utf8_str.size())); | 100 static_cast<uint32_t>(utf8_str.size())); |
| 101 needs_release_ = (var_.type == PP_VARTYPE_STRING); | 101 needs_release_ = (var_.type == PP_VARTYPE_STRING); |
| 102 } | 102 } |
| 103 | 103 |
| 104 Var::Var(const Var& other) { | 104 Var::Var(const Var& other) { |
| 105 var_ = other.var_; | 105 var_ = other.var_; |
| 106 needs_release_ = true; | |
| 106 if (NeedsRefcounting(var_)) { | 107 if (NeedsRefcounting(var_)) { |
| 107 if (has_interface<PPB_Var_1_0>()) { | 108 if (has_interface<PPB_Var_1_0>()) |
| 108 needs_release_ = true; | |
| 109 get_interface<PPB_Var_1_0>()->AddRef(var_); | 109 get_interface<PPB_Var_1_0>()->AddRef(var_); |
| 110 } else { | 110 else |
| 111 var_.type = PP_VARTYPE_NULL; | 111 var_.type = PP_VARTYPE_NULL; |
| 112 needs_release_ = false; | |
| 113 } | |
| 114 } else { | |
| 115 needs_release_ = false; | |
| 116 } | 112 } |
| 117 } | 113 } |
| 118 | 114 |
| 119 Var::~Var() { | 115 Var::~Var() { |
| 120 if (needs_release_ && has_interface<PPB_Var_1_0>()) | 116 if (needs_release_ && NeedsRefcounting(var_) && has_interface<PPB_Var_1_0>()) |
|
Takashi Toyoshima
2012/09/07 17:18:27
PP_Var leaked when PP_Var was updated after constr
| |
| 121 get_interface<PPB_Var_1_0>()->Release(var_); | 117 get_interface<PPB_Var_1_0>()->Release(var_); |
| 122 } | 118 } |
| 123 | 119 |
| 124 Var& Var::operator=(const Var& other) { | 120 Var& Var::operator=(const Var& other) { |
| 125 // Early return for self-assignment. Note however, that two distinct vars | 121 // Early return for self-assignment. Note however, that two distinct vars |
| 126 // can refer to the same object, so we still need to be careful about the | 122 // can refer to the same object, so we still need to be careful about the |
| 127 // refcounting below. | 123 // refcounting below. |
| 128 if (this == &other) | 124 if (this == &other) |
| 129 return *this; | 125 return *this; |
| 130 | 126 |
| 131 // Be careful to keep the ref alive for cases where we're assigning an | 127 // Be careful to keep the ref alive for cases where we're assigning an |
| 132 // object to itself by addrefing the new one before releasing the old one. | 128 // object to itself by addrefing the new one before releasing the old one. |
| 133 bool old_needs_release = needs_release_; | 129 bool old_needs_release = needs_release_; |
| 130 needs_release_ = true; | |
| 134 if (NeedsRefcounting(other.var_)) { | 131 if (NeedsRefcounting(other.var_)) { |
| 135 // Assume we already has_interface<PPB_Var_1_0> for refcounted vars or else | 132 // Assume we already has_interface<PPB_Var_1_0> for refcounted vars or else |
| 136 // we couldn't have created them in the first place. | 133 // we couldn't have created them in the first place. |
| 137 needs_release_ = true; | |
| 138 get_interface<PPB_Var_1_0>()->AddRef(other.var_); | 134 get_interface<PPB_Var_1_0>()->AddRef(other.var_); |
| 139 } else { | |
| 140 needs_release_ = false; | |
| 141 } | 135 } |
| 142 if (old_needs_release) | 136 if (old_needs_release && NeedsRefcounting(var_)) |
| 143 get_interface<PPB_Var_1_0>()->Release(var_); | 137 get_interface<PPB_Var_1_0>()->Release(var_); |
| 144 | 138 |
| 145 var_ = other.var_; | 139 var_ = other.var_; |
| 146 return *this; | 140 return *this; |
| 147 } | 141 } |
| 148 | 142 |
| 149 bool Var::operator==(const Var& other) const { | 143 bool Var::operator==(const Var& other) const { |
| 150 if (var_.type != other.var_.type) | 144 if (var_.type != other.var_.type) |
| 151 return false; | 145 return false; |
| 152 switch (var_.type) { | 146 switch (var_.type) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 snprintf(buf, sizeof(buf), format, str.c_str()); | 229 snprintf(buf, sizeof(buf), format, str.c_str()); |
| 236 } else if (is_array_buffer()) { | 230 } else if (is_array_buffer()) { |
| 237 snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)"); | 231 snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)"); |
| 238 } else if (is_object()) { | 232 } else if (is_object()) { |
| 239 snprintf(buf, sizeof(buf), "Var(OBJECT)"); | 233 snprintf(buf, sizeof(buf), "Var(OBJECT)"); |
| 240 } | 234 } |
| 241 return buf; | 235 return buf; |
| 242 } | 236 } |
| 243 | 237 |
| 244 } // namespace pp | 238 } // namespace pp |
| OLD | NEW |