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 #ifndef PPAPI_SHARED_IMPL_VAR_H_ | 5 #ifndef PPAPI_SHARED_IMPL_VAR_H_ |
6 #define PPAPI_SHARED_IMPL_VAR_H_ | 6 #define PPAPI_SHARED_IMPL_VAR_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
12 #include "ppapi/c/pp_var.h" | 13 #include "ppapi/c/pp_var.h" |
13 #include "ppapi/shared_impl/ppapi_shared_export.h" | 14 #include "ppapi/shared_impl/ppapi_shared_export.h" |
14 | 15 |
15 namespace ppapi { | 16 namespace ppapi { |
16 | 17 |
17 class ArrayBufferVar; | 18 class ArrayBufferVar; |
18 class NPObjectVar; | 19 class NPObjectVar; |
19 class ProxyObjectVar; | 20 class ProxyObjectVar; |
20 class StringVar; | 21 class StringVar; |
21 class VarTracker; | 22 class VarTracker; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // | 87 // |
87 // Converting a PP_Var to a string: | 88 // Converting a PP_Var to a string: |
88 // StringVar* string = StringVar::FromPPVar(var); | 89 // StringVar* string = StringVar::FromPPVar(var); |
89 // if (!string) | 90 // if (!string) |
90 // return false; // Not a string or an invalid var. | 91 // return false; // Not a string or an invalid var. |
91 // DoSomethingWithTheString(string->value()); | 92 // DoSomethingWithTheString(string->value()); |
92 class PPAPI_SHARED_EXPORT StringVar : public Var { | 93 class PPAPI_SHARED_EXPORT StringVar : public Var { |
93 public: | 94 public: |
94 StringVar(const std::string& str); | 95 StringVar(const std::string& str); |
95 StringVar(const char* str, uint32 len); | 96 StringVar(const char* str, uint32 len); |
| 97 StringVar(scoped_ptr<std::string> str); |
96 virtual ~StringVar(); | 98 virtual ~StringVar(); |
97 | 99 |
98 const std::string& value() const { return value_; } | 100 const std::string& value() const { return value_; } |
| 101 // Return a pointer to the internal string. This allows other objects to |
| 102 // temporarily store a weak pointer to our internal string. Use with care; the |
| 103 // pointer *will* become invalid if this StringVar is removed from the |
| 104 // tracker. (All of this applies to value(), but this one's even easier to use |
| 105 // dangerously). |
| 106 const std::string* ptr() const { return &value_; } |
99 | 107 |
100 // Var override. | 108 // Var override. |
101 virtual StringVar* AsStringVar() OVERRIDE; | 109 virtual StringVar* AsStringVar() OVERRIDE; |
102 virtual PP_VarType GetType() const OVERRIDE; | 110 virtual PP_VarType GetType() const OVERRIDE; |
103 | 111 |
104 // Helper function to create a PP_Var of type string that contains a copy of | 112 // Helper function to create a PP_Var of type string that contains a copy of |
105 // the given string. The input data must be valid UTF-8 encoded text, if it | 113 // the given string. The input data must be valid UTF-8 encoded text, if it |
106 // is not valid UTF-8, a NULL var will be returned. | 114 // is not valid UTF-8, a NULL var will be returned. |
107 // | 115 // |
108 // The return value will have a reference count of 1. Internally, this will | 116 // The return value will have a reference count of 1. Internally, this will |
109 // create a StringVar and return the reference to it in the var. | 117 // create a StringVar and return the reference to it in the var. |
110 static PP_Var StringToPPVar(const std::string& str); | 118 static PP_Var StringToPPVar(const std::string& str); |
111 static PP_Var StringToPPVar(const char* str, uint32 len); | 119 static PP_Var StringToPPVar(const char* str, uint32 len); |
| 120 static PP_Var StringToPPVar(scoped_ptr<std::string> str); |
112 | 121 |
113 // Helper function that converts a PP_Var to a string. This will return NULL | 122 // Helper function that converts a PP_Var to a string. This will return NULL |
114 // if the PP_Var is not of string type or the string is invalid. | 123 // if the PP_Var is not of string type or the string is invalid. |
115 static StringVar* FromPPVar(PP_Var var); | 124 static StringVar* FromPPVar(PP_Var var); |
116 | 125 |
117 private: | 126 private: |
118 std::string value_; | 127 std::string value_; |
119 | 128 |
120 DISALLOW_COPY_AND_ASSIGN(StringVar); | 129 DISALLOW_COPY_AND_ASSIGN(StringVar); |
121 }; | 130 }; |
(...skipping 29 matching lines...) Expand all Loading... |
151 // return NULL if the PP_Var is not of ArrayBuffer type. | 160 // return NULL if the PP_Var is not of ArrayBuffer type. |
152 static ArrayBufferVar* FromPPVar(PP_Var var); | 161 static ArrayBufferVar* FromPPVar(PP_Var var); |
153 | 162 |
154 private: | 163 private: |
155 DISALLOW_COPY_AND_ASSIGN(ArrayBufferVar); | 164 DISALLOW_COPY_AND_ASSIGN(ArrayBufferVar); |
156 }; | 165 }; |
157 | 166 |
158 } // namespace ppapi | 167 } // namespace ppapi |
159 | 168 |
160 #endif // PPAPI_SHARED_IMPL_VAR_H_ | 169 #endif // PPAPI_SHARED_IMPL_VAR_H_ |
OLD | NEW |