blob: 4d833ae12b247bc57beee738949eebab958d6547 [file] [log] [blame]
[email protected]47ef6142012-01-26 21:04:101// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ce701cd2011-08-01 21:47:042// 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]2bbd2c672011-08-09 23:14:1312#include "ppapi/c/pp_var.h"
[email protected]f0a04c42011-08-26 22:43:2013#include "ppapi/shared_impl/ppapi_shared_export.h"
[email protected]ce701cd2011-08-01 21:47:0414
15namespace ppapi {
16
[email protected]8cc26a42011-12-15 21:22:3117class ArrayBufferVar;
[email protected]ce701cd2011-08-01 21:47:0418class NPObjectVar;
[email protected]2bbd2c672011-08-09 23:14:1319class ProxyObjectVar;
[email protected]ce701cd2011-08-01 21:47:0420class StringVar;
[email protected]c6d03e02011-12-15 05:04:0621class VarTracker;
[email protected]ce701cd2011-08-01 21:47:0422
23// Var -------------------------------------------------------------------------
24
[email protected]872caf562011-12-07 22:50:4325// Represents a non-POD var.
[email protected]f0a04c42011-08-26 22:43:2026class PPAPI_SHARED_EXPORT Var : public base::RefCounted<Var> {
[email protected]ce701cd2011-08-01 21:47:0427 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]ce701cd2011-08-01 21:47:0433 virtual StringVar* AsStringVar();
[email protected]8cc26a42011-12-15 21:22:3134 virtual ArrayBufferVar* AsArrayBufferVar();
[email protected]ce701cd2011-08-01 21:47:0435 virtual NPObjectVar* AsNPObjectVar();
[email protected]2bbd2c672011-08-09 23:14:1336 virtual ProxyObjectVar* AsProxyObjectVar();
[email protected]ce701cd2011-08-01 21:47:0437
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]8cc26a42011-12-15 21:22:3140 PP_Var GetPPVar();
[email protected]ce701cd2011-08-01 21:47:0441
[email protected]2bbd2c672011-08-09 23:14:1342 // Returns the type of this var.
43 virtual PP_VarType GetType() const = 0;
44
[email protected]ce701cd2011-08-01 21:47:0445 // 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]ce701cd2011-08-01 21:47:0453 protected:
[email protected]c6d03e02011-12-15 05:04:0654 friend class VarTracker;
55
[email protected]872caf562011-12-07 22:50:4356 Var();
[email protected]ce701cd2011-08-01 21:47:0457
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]2bbd2c672011-08-09 23:14:1366 // 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]c6d03e02011-12-15 05:04:0670 // Reset the assigned object ID.
71 void ResetVarID() { var_id_ = 0; }
72
[email protected]ce701cd2011-08-01 21:47:0473 private:
[email protected]ce701cd2011-08-01 21:47:0474 // 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]872caf562011-12-07 22:50:4385// return StringVar::StringToPPVar(my_string);
[email protected]ce701cd2011-08-01 21:47:0486//
87// Converting a PP_Var to a string:
[email protected]28cfaed02011-08-22 22:15:5888// StringVar* string = StringVar::FromPPVar(var);
[email protected]ce701cd2011-08-01 21:47:0489// if (!string)
90// return false; // Not a string or an invalid var.
91// DoSomethingWithTheString(string->value());
[email protected]f0a04c42011-08-26 22:43:2092class PPAPI_SHARED_EXPORT StringVar : public Var {
[email protected]ce701cd2011-08-01 21:47:0493 public:
[email protected]872caf562011-12-07 22:50:4394 StringVar(const std::string& str);
95 StringVar(const char* str, uint32 len);
[email protected]ce701cd2011-08-01 21:47:0496 virtual ~StringVar();
97
98 const std::string& value() const { return value_; }
99
100 // Var override.
101 virtual StringVar* AsStringVar() OVERRIDE;
[email protected]2bbd2c672011-08-09 23:14:13102 virtual PP_VarType GetType() const OVERRIDE;
[email protected]ce701cd2011-08-01 21:47:04103
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]872caf562011-12-07 22:50:43109 // 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]ce701cd2011-08-01 21:47:04112
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]28cfaed02011-08-22 22:15:58115 static StringVar* FromPPVar(PP_Var var);
[email protected]ce701cd2011-08-01 21:47:04116
117 private:
118 std::string value_;
119
120 DISALLOW_COPY_AND_ASSIGN(StringVar);
121};
122
[email protected]8cc26a42011-12-15 21:22:31123// 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);
137class PPAPI_SHARED_EXPORT ArrayBufferVar : public Var {
138 public:
139 ArrayBufferVar();
140 virtual ~ArrayBufferVar();
141
142 virtual void* Map() = 0;
[email protected]47ef6142012-01-26 21:04:10143 virtual void Unmap() = 0;
[email protected]8cc26a42011-12-15 21:22:31144 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]ce701cd2011-08-01 21:47:04158} // namespace ppapi
159
160#endif // PPAPI_SHARED_IMPL_VAR_H_