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>()->AddRef(var_); |
brettw
2011/12/07 17:40:54
There are a few other places in this file where we
dmichael (off chromium)
2011/12/07 18:07:38
Done, nice catch.
| |
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() { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |