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
3 // found in the LICENSE file.
5 #ifndef PPAPI_SHARED_IMPL_VAR_H_
6 #define PPAPI_SHARED_IMPL_VAR_H_
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/shared_memory.h"
13 #include "base/platform_file.h"
14 #include "ppapi/c/pp_var.h"
15 #include "ppapi/shared_impl/host_resource.h"
16 #include "ppapi/shared_impl/ppapi_shared_export.h"
29 // Var -------------------------------------------------------------------------
31 // Represents a non-POD var.
32 class PPAPI_SHARED_EXPORT Var
: public base::RefCounted
<Var
> {
34 // Returns a string representing the given var for logging purposes.
35 static std::string
PPVarToLogString(PP_Var var
);
37 virtual StringVar
* AsStringVar();
38 virtual ArrayBufferVar
* AsArrayBufferVar();
39 virtual NPObjectVar
* AsNPObjectVar();
40 virtual ProxyObjectVar
* AsProxyObjectVar();
41 virtual ArrayVar
* AsArrayVar();
42 virtual DictionaryVar
* AsDictionaryVar();
43 virtual ResourceVar
* AsResourceVar();
45 // Creates a PP_Var corresponding to this object. The return value will have
46 // one reference addrefed on behalf of the caller.
49 // Returns the type of this var.
50 virtual PP_VarType
GetType() const = 0;
52 // Returns the ID corresponing to the string or object if it exists already,
53 // or 0 if an ID hasn't been generated for this object (the plugin is holding
56 // Contrast to GetOrCreateVarID which creates the ID and a ref on behalf of
58 int32
GetExistingVarID() const;
61 friend class base::RefCounted
<Var
>;
62 friend class VarTracker
;
67 // Returns the unique ID associated with this string or object, creating it
68 // if necessary. The return value will be 0 if the string or object is
71 // This function will take a reference to the var that will be passed to the
73 int32
GetOrCreateVarID();
75 // Sets the internal object ID. This assumes that the ID hasn't been set
76 // before. This is used in cases where the ID is generated externally.
77 void AssignVarID(int32 id
);
79 // Reset the assigned object ID.
80 void ResetVarID() { var_id_
= 0; }
83 // This will be 0 if no ID has been assigned (this happens lazily).
86 DISALLOW_COPY_AND_ASSIGN(Var
);
89 // StringVar -------------------------------------------------------------------
91 // Represents a string-based Var.
93 // Returning a given string as a PP_Var:
94 // return StringVar::StringToPPVar(my_string);
96 // Converting a PP_Var to a string:
97 // StringVar* string = StringVar::FromPPVar(var);
99 // return false; // Not a string or an invalid var.
100 // DoSomethingWithTheString(string->value());
101 class PPAPI_SHARED_EXPORT StringVar
: public Var
{
103 explicit StringVar(const std::string
& str
);
104 StringVar(const char* str
, uint32 len
);
105 virtual ~StringVar();
107 const std::string
& value() const { return value_
; }
108 // Return a pointer to the internal string. This allows other objects to
109 // temporarily store a weak pointer to our internal string. Use with care; the
110 // pointer *will* become invalid if this StringVar is removed from the
111 // tracker. (All of this applies to value(), but this one's even easier to use
113 const std::string
* ptr() const { return &value_
; }
116 virtual StringVar
* AsStringVar() OVERRIDE
;
117 virtual PP_VarType
GetType() const OVERRIDE
;
119 // Helper function to create a PP_Var of type string that contains a copy of
120 // the given string. The input data must be valid UTF-8 encoded text, if it
121 // is not valid UTF-8, a NULL var will be returned.
123 // The return value will have a reference count of 1. Internally, this will
124 // create a StringVar and return the reference to it in the var.
125 static PP_Var
StringToPPVar(const std::string
& str
);
126 static PP_Var
StringToPPVar(const char* str
, uint32 len
);
128 // Same as StringToPPVar but avoids a copy by destructively swapping the
129 // given string into the newly created StringVar. The string must already be
130 // valid UTF-8. After the call, *src will be empty.
131 static PP_Var
SwapValidatedUTF8StringIntoPPVar(std::string
* src
);
133 // Helper function that converts a PP_Var to a string. This will return NULL
134 // if the PP_Var is not of string type or the string is invalid.
135 static StringVar
* FromPPVar(PP_Var var
);
138 StringVar(); // Makes an empty string.
142 DISALLOW_COPY_AND_ASSIGN(StringVar
);
145 // ArrayBufferVar --------------------------------------------------------------
147 // Represents an array buffer Var.
149 // Note this is an abstract class. To create an appropriate concrete one, you
150 // need to use the VarTracker:
151 // VarArrayBuffer* buf =
152 // PpapiGlobals::Get()->GetVarTracker()->CreateArrayBuffer(size);
154 // Converting a PP_Var to an ArrayBufferVar:
155 // ArrayBufferVar* array = ArrayBufferVar::FromPPVar(var);
157 // return false; // Not an ArrayBuffer or an invalid var.
158 // DoSomethingWithTheBuffer(array);
159 class PPAPI_SHARED_EXPORT ArrayBufferVar
: public Var
{
162 virtual ~ArrayBufferVar();
164 virtual void* Map() = 0;
165 virtual void Unmap() = 0;
166 virtual uint32
ByteLength() = 0;
168 // Creates a new shared memory region, and copies the data in the
169 // ArrayBufferVar into it. On the plugin side, host_shm_handle_id will be set
170 // to some value that is not -1. On the host side, plugin_shm_handle will be
171 // set to a valid SharedMemoryHandle.
173 // Returns true if creating the shared memory (and copying) is successful,
175 virtual bool CopyToNewShmem(PP_Instance instance
,
176 int* host_shm_handle_id
,
177 base::SharedMemoryHandle
* plugin_shm_handle
) = 0;
180 virtual ArrayBufferVar
* AsArrayBufferVar() OVERRIDE
;
181 virtual PP_VarType
GetType() const OVERRIDE
;
183 // Helper function that converts a PP_Var to an ArrayBufferVar. This will
184 // return NULL if the PP_Var is not of ArrayBuffer type.
185 static ArrayBufferVar
* FromPPVar(PP_Var var
);
188 DISALLOW_COPY_AND_ASSIGN(ArrayBufferVar
);
193 #endif // PPAPI_SHARED_IMPL_VAR_H_