| 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 #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 "ppapi/c/pp_var.h" | 12 #include "ppapi/c/pp_var.h" |
| 13 #include "ppapi/shared_impl/ppapi_shared_export.h" | 13 #include "ppapi/shared_impl/ppapi_shared_export.h" |
| 14 | 14 |
| 15 namespace ppapi { | 15 namespace ppapi { |
| 16 | 16 |
| 17 class ArrayBufferVar; |
| 17 class NPObjectVar; | 18 class NPObjectVar; |
| 18 class ProxyObjectVar; | 19 class ProxyObjectVar; |
| 19 class StringVar; | 20 class StringVar; |
| 20 | 21 |
| 21 // Var ------------------------------------------------------------------------- | 22 // Var ------------------------------------------------------------------------- |
| 22 | 23 |
| 23 // Represents a non-POD var. | 24 // Represents a non-POD var. |
| 24 class PPAPI_SHARED_EXPORT Var : public base::RefCounted<Var> { | 25 class PPAPI_SHARED_EXPORT Var : public base::RefCounted<Var> { |
| 25 public: | 26 public: |
| 26 virtual ~Var(); | 27 virtual ~Var(); |
| 27 | 28 |
| 28 // Returns a string representing the given var for logging purposes. | 29 // Returns a string representing the given var for logging purposes. |
| 29 static std::string PPVarToLogString(PP_Var var); | 30 static std::string PPVarToLogString(PP_Var var); |
| 30 | 31 |
| 31 virtual StringVar* AsStringVar(); | 32 virtual StringVar* AsStringVar(); |
| 33 virtual ArrayBufferVar* AsArrayBufferVar(); |
| 32 virtual NPObjectVar* AsNPObjectVar(); | 34 virtual NPObjectVar* AsNPObjectVar(); |
| 33 virtual ProxyObjectVar* AsProxyObjectVar(); | 35 virtual ProxyObjectVar* AsProxyObjectVar(); |
| 34 | 36 |
| 35 // Creates a PP_Var corresponding to this object. The return value will have | 37 // Creates a PP_Var corresponding to this object. The return value will have |
| 36 // one reference addrefed on behalf of the caller. | 38 // one reference addrefed on behalf of the caller. |
| 37 virtual PP_Var GetPPVar() = 0; | 39 PP_Var GetPPVar(); |
| 38 | 40 |
| 39 // Returns the type of this var. | 41 // Returns the type of this var. |
| 40 virtual PP_VarType GetType() const = 0; | 42 virtual PP_VarType GetType() const = 0; |
| 41 | 43 |
| 42 // Returns the ID corresponing to the string or object if it exists already, | 44 // Returns the ID corresponing to the string or object if it exists already, |
| 43 // or 0 if an ID hasn't been generated for this object (the plugin is holding | 45 // or 0 if an ID hasn't been generated for this object (the plugin is holding |
| 44 // no refs). | 46 // no refs). |
| 45 // | 47 // |
| 46 // Contrast to GetOrCreateVarID which creates the ID and a ref on behalf of | 48 // Contrast to GetOrCreateVarID which creates the ID and a ref on behalf of |
| 47 // the plugin. | 49 // the plugin. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 class PPAPI_SHARED_EXPORT StringVar : public Var { | 86 class PPAPI_SHARED_EXPORT StringVar : public Var { |
| 85 public: | 87 public: |
| 86 StringVar(const std::string& str); | 88 StringVar(const std::string& str); |
| 87 StringVar(const char* str, uint32 len); | 89 StringVar(const char* str, uint32 len); |
| 88 virtual ~StringVar(); | 90 virtual ~StringVar(); |
| 89 | 91 |
| 90 const std::string& value() const { return value_; } | 92 const std::string& value() const { return value_; } |
| 91 | 93 |
| 92 // Var override. | 94 // Var override. |
| 93 virtual StringVar* AsStringVar() OVERRIDE; | 95 virtual StringVar* AsStringVar() OVERRIDE; |
| 94 virtual PP_Var GetPPVar() OVERRIDE; | |
| 95 virtual PP_VarType GetType() const OVERRIDE; | 96 virtual PP_VarType GetType() const OVERRIDE; |
| 96 | 97 |
| 97 // Helper function to create a PP_Var of type string that contains a copy of | 98 // Helper function to create a PP_Var of type string that contains a copy of |
| 98 // the given string. The input data must be valid UTF-8 encoded text, if it | 99 // the given string. The input data must be valid UTF-8 encoded text, if it |
| 99 // is not valid UTF-8, a NULL var will be returned. | 100 // is not valid UTF-8, a NULL var will be returned. |
| 100 // | 101 // |
| 101 // The return value will have a reference count of 1. Internally, this will | 102 // The return value will have a reference count of 1. Internally, this will |
| 102 // create a StringVar and return the reference to it in the var. | 103 // create a StringVar and return the reference to it in the var. |
| 103 static PP_Var StringToPPVar(const std::string& str); | 104 static PP_Var StringToPPVar(const std::string& str); |
| 104 static PP_Var StringToPPVar(const char* str, uint32 len); | 105 static PP_Var StringToPPVar(const char* str, uint32 len); |
| 105 | 106 |
| 106 // Helper function that converts a PP_Var to a string. This will return NULL | 107 // Helper function that converts a PP_Var to a string. This will return NULL |
| 107 // if the PP_Var is not of string type or the string is invalid. | 108 // if the PP_Var is not of string type or the string is invalid. |
| 108 static StringVar* FromPPVar(PP_Var var); | 109 static StringVar* FromPPVar(PP_Var var); |
| 109 | 110 |
| 110 private: | 111 private: |
| 111 std::string value_; | 112 std::string value_; |
| 112 | 113 |
| 113 DISALLOW_COPY_AND_ASSIGN(StringVar); | 114 DISALLOW_COPY_AND_ASSIGN(StringVar); |
| 114 }; | 115 }; |
| 115 | 116 |
| 117 // ArrayBufferVar -------------------------------------------------------------- |
| 118 |
| 119 // Represents an array buffer Var. |
| 120 // |
| 121 // Note this is an abstract class. To create an appropriate concrete one, you |
| 122 // need to use the VarTracker: |
| 123 // VarArrayBuffer* buf = |
| 124 // PpapiGlobals::Get()->GetVarTracker()->CreateArrayBuffer(size); |
| 125 // |
| 126 // Converting a PP_Var to an ArrayBufferVar: |
| 127 // ArrayBufferVar* array = ArrayBufferVar::FromPPVar(var); |
| 128 // if (!array) |
| 129 // return false; // Not an ArrayBuffer or an invalid var. |
| 130 // DoSomethingWithTheBuffer(array); |
| 131 class PPAPI_SHARED_EXPORT ArrayBufferVar : public Var { |
| 132 public: |
| 133 ArrayBufferVar(); |
| 134 virtual ~ArrayBufferVar(); |
| 135 |
| 136 virtual void* Map() = 0; |
| 137 virtual uint32 ByteLength() = 0; |
| 138 |
| 139 // Var override. |
| 140 virtual ArrayBufferVar* AsArrayBufferVar() OVERRIDE; |
| 141 virtual PP_VarType GetType() const OVERRIDE; |
| 142 |
| 143 // Helper function that converts a PP_Var to an ArrayBufferVar. This will |
| 144 // return NULL if the PP_Var is not of ArrayBuffer type. |
| 145 static ArrayBufferVar* FromPPVar(PP_Var var); |
| 146 |
| 147 private: |
| 148 DISALLOW_COPY_AND_ASSIGN(ArrayBufferVar); |
| 149 }; |
| 150 |
| 116 } // namespace ppapi | 151 } // namespace ppapi |
| 117 | 152 |
| 118 #endif // PPAPI_SHARED_IMPL_VAR_H_ | 153 #endif // PPAPI_SHARED_IMPL_VAR_H_ |
| OLD | NEW |