[email protected] | 47ef614 | 2012-01-26 21:04:10 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #ifndef PPAPI_SHARED_IMPL_VAR_H_ | ||||
6 | #define PPAPI_SHARED_IMPL_VAR_H_ | ||||
7 | |||||
8 | #include <string> | ||||
9 | |||||
10 | #include "base/compiler_specific.h" | ||||
11 | #include "base/memory/ref_counted.h" | ||||
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 12 | #include "ppapi/c/pp_var.h" |
[email protected] | f0a04c4 | 2011-08-26 22:43:20 | [diff] [blame] | 13 | #include "ppapi/shared_impl/ppapi_shared_export.h" |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 14 | |
15 | namespace ppapi { | ||||
16 | |||||
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 17 | class ArrayBufferVar; |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 18 | class NPObjectVar; |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 19 | class ProxyObjectVar; |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 20 | class StringVar; |
[email protected] | c6d03e0 | 2011-12-15 05:04:06 | [diff] [blame] | 21 | class VarTracker; |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 22 | |
23 | // Var ------------------------------------------------------------------------- | ||||
24 | |||||
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 25 | // Represents a non-POD var. |
[email protected] | f0a04c4 | 2011-08-26 22:43:20 | [diff] [blame] | 26 | class PPAPI_SHARED_EXPORT Var : public base::RefCounted<Var> { |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 27 | public: |
28 | virtual ~Var(); | ||||
29 | |||||
30 | // Returns a string representing the given var for logging purposes. | ||||
31 | static std::string PPVarToLogString(PP_Var var); | ||||
32 | |||||
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 33 | virtual StringVar* AsStringVar(); |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 34 | virtual ArrayBufferVar* AsArrayBufferVar(); |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 35 | virtual NPObjectVar* AsNPObjectVar(); |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 36 | virtual ProxyObjectVar* AsProxyObjectVar(); |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 37 | |
38 | // Creates a PP_Var corresponding to this object. The return value will have | ||||
39 | // one reference addrefed on behalf of the caller. | ||||
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 40 | PP_Var GetPPVar(); |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 41 | |
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 42 | // Returns the type of this var. |
43 | virtual PP_VarType GetType() const = 0; | ||||
44 | |||||
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 45 | // Returns the ID corresponing to the string or object if it exists already, |
46 | // or 0 if an ID hasn't been generated for this object (the plugin is holding | ||||
47 | // no refs). | ||||
48 | // | ||||
49 | // Contrast to GetOrCreateVarID which creates the ID and a ref on behalf of | ||||
50 | // the plugin. | ||||
51 | int32 GetExistingVarID() const; | ||||
52 | |||||
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 53 | protected: |
[email protected] | c6d03e0 | 2011-12-15 05:04:06 | [diff] [blame] | 54 | friend class VarTracker; |
55 | |||||
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 56 | Var(); |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 57 | |
58 | // Returns the unique ID associated with this string or object, creating it | ||||
59 | // if necessary. The return value will be 0 if the string or object is | ||||
60 | // invalid. | ||||
61 | // | ||||
62 | // This function will take a reference to the var that will be passed to the | ||||
63 | // caller. | ||||
64 | int32 GetOrCreateVarID(); | ||||
65 | |||||
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 66 | // Sets the internal object ID. This assumes that the ID hasn't been set |
67 | // before. This is used in cases where the ID is generated externally. | ||||
68 | void AssignVarID(int32 id); | ||||
69 | |||||
[email protected] | c6d03e0 | 2011-12-15 05:04:06 | [diff] [blame] | 70 | // Reset the assigned object ID. |
71 | void ResetVarID() { var_id_ = 0; } | ||||
72 | |||||
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 73 | private: |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 74 | // This will be 0 if no ID has been assigned (this happens lazily). |
75 | int32 var_id_; | ||||
76 | |||||
77 | DISALLOW_COPY_AND_ASSIGN(Var); | ||||
78 | }; | ||||
79 | |||||
80 | // StringVar ------------------------------------------------------------------- | ||||
81 | |||||
82 | // Represents a string-based Var. | ||||
83 | // | ||||
84 | // Returning a given string as a PP_Var: | ||||
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 85 | // return StringVar::StringToPPVar(my_string); |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 86 | // |
87 | // Converting a PP_Var to a string: | ||||
[email protected] | 28cfaed0 | 2011-08-22 22:15:58 | [diff] [blame] | 88 | // StringVar* string = StringVar::FromPPVar(var); |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 89 | // if (!string) |
90 | // return false; // Not a string or an invalid var. | ||||
91 | // DoSomethingWithTheString(string->value()); | ||||
[email protected] | f0a04c4 | 2011-08-26 22:43:20 | [diff] [blame] | 92 | class PPAPI_SHARED_EXPORT StringVar : public Var { |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 93 | public: |
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 94 | StringVar(const std::string& str); |
95 | StringVar(const char* str, uint32 len); | ||||
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 96 | virtual ~StringVar(); |
97 | |||||
98 | const std::string& value() const { return value_; } | ||||
99 | |||||
100 | // Var override. | ||||
101 | virtual StringVar* AsStringVar() OVERRIDE; | ||||
[email protected] | 2bbd2c67 | 2011-08-09 23:14:13 | [diff] [blame] | 102 | virtual PP_VarType GetType() const OVERRIDE; |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 103 | |
104 | // 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 | ||||
106 | // is not valid UTF-8, a NULL var will be returned. | ||||
107 | // | ||||
108 | // The return value will have a reference count of 1. Internally, this will | ||||
[email protected] | 872caf56 | 2011-12-07 22:50:43 | [diff] [blame] | 109 | // create a StringVar and return the reference to it in the var. |
110 | static PP_Var StringToPPVar(const std::string& str); | ||||
111 | static PP_Var StringToPPVar(const char* str, uint32 len); | ||||
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 112 | |
113 | // 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. | ||||
[email protected] | 28cfaed0 | 2011-08-22 22:15:58 | [diff] [blame] | 115 | static StringVar* FromPPVar(PP_Var var); |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 116 | |
117 | private: | ||||
118 | std::string value_; | ||||
119 | |||||
120 | DISALLOW_COPY_AND_ASSIGN(StringVar); | ||||
121 | }; | ||||
122 | |||||
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 123 | // ArrayBufferVar -------------------------------------------------------------- |
124 | |||||
125 | // Represents an array buffer Var. | ||||
126 | // | ||||
127 | // Note this is an abstract class. To create an appropriate concrete one, you | ||||
128 | // need to use the VarTracker: | ||||
129 | // VarArrayBuffer* buf = | ||||
130 | // PpapiGlobals::Get()->GetVarTracker()->CreateArrayBuffer(size); | ||||
131 | // | ||||
132 | // Converting a PP_Var to an ArrayBufferVar: | ||||
133 | // ArrayBufferVar* array = ArrayBufferVar::FromPPVar(var); | ||||
134 | // if (!array) | ||||
135 | // return false; // Not an ArrayBuffer or an invalid var. | ||||
136 | // DoSomethingWithTheBuffer(array); | ||||
137 | class PPAPI_SHARED_EXPORT ArrayBufferVar : public Var { | ||||
138 | public: | ||||
139 | ArrayBufferVar(); | ||||
140 | virtual ~ArrayBufferVar(); | ||||
141 | |||||
142 | virtual void* Map() = 0; | ||||
[email protected] | 47ef614 | 2012-01-26 21:04:10 | [diff] [blame] | 143 | virtual void Unmap() = 0; |
[email protected] | 8cc26a4 | 2011-12-15 21:22:31 | [diff] [blame] | 144 | virtual uint32 ByteLength() = 0; |
145 | |||||
146 | // Var override. | ||||
147 | virtual ArrayBufferVar* AsArrayBufferVar() OVERRIDE; | ||||
148 | virtual PP_VarType GetType() const OVERRIDE; | ||||
149 | |||||
150 | // Helper function that converts a PP_Var to an ArrayBufferVar. This will | ||||
151 | // return NULL if the PP_Var is not of ArrayBuffer type. | ||||
152 | static ArrayBufferVar* FromPPVar(PP_Var var); | ||||
153 | |||||
154 | private: | ||||
155 | DISALLOW_COPY_AND_ASSIGN(ArrayBufferVar); | ||||
156 | }; | ||||
157 | |||||
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 158 | } // namespace ppapi |
159 | |||||
160 | #endif // PPAPI_SHARED_IMPL_VAR_H_ |