| 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_module.h" | |
| 13 #include "ppapi/c/pp_var.h" | 12 #include "ppapi/c/pp_var.h" |
| 14 #include "ppapi/shared_impl/ppapi_shared_export.h" | 13 #include "ppapi/shared_impl/ppapi_shared_export.h" |
| 15 | 14 |
| 16 namespace ppapi { | 15 namespace ppapi { |
| 17 | 16 |
| 18 class NPObjectVar; | 17 class NPObjectVar; |
| 19 class ProxyObjectVar; | 18 class ProxyObjectVar; |
| 20 class StringVar; | 19 class StringVar; |
| 21 | 20 |
| 22 // Var ------------------------------------------------------------------------- | 21 // Var ------------------------------------------------------------------------- |
| 23 | 22 |
| 24 // Represents a non-POD var. This is derived from a resource even though it | 23 // Represents a non-POD var. |
| 25 // isn't a resource from the plugin's perspective. This allows us to re-use | |
| 26 // the refcounting and the association with the module from the resource code. | |
| 27 class PPAPI_SHARED_EXPORT Var : public base::RefCounted<Var> { | 24 class PPAPI_SHARED_EXPORT Var : public base::RefCounted<Var> { |
| 28 public: | 25 public: |
| 29 virtual ~Var(); | 26 virtual ~Var(); |
| 30 | 27 |
| 31 // Returns a string representing the given var for logging purposes. | 28 // Returns a string representing the given var for logging purposes. |
| 32 static std::string PPVarToLogString(PP_Var var); | 29 static std::string PPVarToLogString(PP_Var var); |
| 33 | 30 |
| 34 virtual StringVar* AsStringVar(); | 31 virtual StringVar* AsStringVar(); |
| 35 virtual NPObjectVar* AsNPObjectVar(); | 32 virtual NPObjectVar* AsNPObjectVar(); |
| 36 virtual ProxyObjectVar* AsProxyObjectVar(); | 33 virtual ProxyObjectVar* AsProxyObjectVar(); |
| 37 | 34 |
| 38 // Creates a PP_Var corresponding to this object. The return value will have | 35 // Creates a PP_Var corresponding to this object. The return value will have |
| 39 // one reference addrefed on behalf of the caller. | 36 // one reference addrefed on behalf of the caller. |
| 40 virtual PP_Var GetPPVar() = 0; | 37 virtual PP_Var GetPPVar() = 0; |
| 41 | 38 |
| 42 // Returns the type of this var. | 39 // Returns the type of this var. |
| 43 virtual PP_VarType GetType() const = 0; | 40 virtual PP_VarType GetType() const = 0; |
| 44 | 41 |
| 45 // Returns the ID corresponing to the string or object if it exists already, | 42 // 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 | 43 // or 0 if an ID hasn't been generated for this object (the plugin is holding |
| 47 // no refs). | 44 // no refs). |
| 48 // | 45 // |
| 49 // Contrast to GetOrCreateVarID which creates the ID and a ref on behalf of | 46 // Contrast to GetOrCreateVarID which creates the ID and a ref on behalf of |
| 50 // the plugin. | 47 // the plugin. |
| 51 int32 GetExistingVarID() const; | 48 int32 GetExistingVarID() const; |
| 52 | 49 |
| 53 PP_Module pp_module() const { return pp_module_; } | |
| 54 | |
| 55 protected: | 50 protected: |
| 56 // This can only be constructed as a StringVar or an ObjectVar. | 51 Var(); |
| 57 explicit Var(PP_Module module); | |
| 58 | 52 |
| 59 // Returns the unique ID associated with this string or object, creating it | 53 // Returns the unique ID associated with this string or object, creating it |
| 60 // if necessary. The return value will be 0 if the string or object is | 54 // if necessary. The return value will be 0 if the string or object is |
| 61 // invalid. | 55 // invalid. |
| 62 // | 56 // |
| 63 // This function will take a reference to the var that will be passed to the | 57 // This function will take a reference to the var that will be passed to the |
| 64 // caller. | 58 // caller. |
| 65 int32 GetOrCreateVarID(); | 59 int32 GetOrCreateVarID(); |
| 66 | 60 |
| 67 // Sets the internal object ID. This assumes that the ID hasn't been set | 61 // Sets the internal object ID. This assumes that the ID hasn't been set |
| 68 // before. This is used in cases where the ID is generated externally. | 62 // before. This is used in cases where the ID is generated externally. |
| 69 void AssignVarID(int32 id); | 63 void AssignVarID(int32 id); |
| 70 | 64 |
| 71 private: | 65 private: |
| 72 PP_Module pp_module_; | |
| 73 | |
| 74 // This will be 0 if no ID has been assigned (this happens lazily). | 66 // This will be 0 if no ID has been assigned (this happens lazily). |
| 75 int32 var_id_; | 67 int32 var_id_; |
| 76 | 68 |
| 77 DISALLOW_COPY_AND_ASSIGN(Var); | 69 DISALLOW_COPY_AND_ASSIGN(Var); |
| 78 }; | 70 }; |
| 79 | 71 |
| 80 // StringVar ------------------------------------------------------------------- | 72 // StringVar ------------------------------------------------------------------- |
| 81 | 73 |
| 82 // Represents a string-based Var. | 74 // Represents a string-based Var. |
| 83 // | 75 // |
| 84 // Returning a given string as a PP_Var: | 76 // Returning a given string as a PP_Var: |
| 85 // return StringVar::StringToPPVar(module, my_string); | 77 // return StringVar::StringToPPVar(my_string); |
| 86 // | 78 // |
| 87 // Converting a PP_Var to a string: | 79 // Converting a PP_Var to a string: |
| 88 // StringVar* string = StringVar::FromPPVar(var); | 80 // StringVar* string = StringVar::FromPPVar(var); |
| 89 // if (!string) | 81 // if (!string) |
| 90 // return false; // Not a string or an invalid var. | 82 // return false; // Not a string or an invalid var. |
| 91 // DoSomethingWithTheString(string->value()); | 83 // DoSomethingWithTheString(string->value()); |
| 92 class PPAPI_SHARED_EXPORT StringVar : public Var { | 84 class PPAPI_SHARED_EXPORT StringVar : public Var { |
| 93 public: | 85 public: |
| 94 StringVar(PP_Module module, const std::string& str); | 86 StringVar(const std::string& str); |
| 95 StringVar(PP_Module module, const char* str, uint32 len); | 87 StringVar(const char* str, uint32 len); |
| 96 virtual ~StringVar(); | 88 virtual ~StringVar(); |
| 97 | 89 |
| 98 const std::string& value() const { return value_; } | 90 const std::string& value() const { return value_; } |
| 99 | 91 |
| 100 // Var override. | 92 // Var override. |
| 101 virtual StringVar* AsStringVar() OVERRIDE; | 93 virtual StringVar* AsStringVar() OVERRIDE; |
| 102 virtual PP_Var GetPPVar() OVERRIDE; | 94 virtual PP_Var GetPPVar() OVERRIDE; |
| 103 virtual PP_VarType GetType() const OVERRIDE; | 95 virtual PP_VarType GetType() const OVERRIDE; |
| 104 | 96 |
| 105 // Helper function to create a PP_Var of type string that contains a copy of | 97 // Helper function to create a PP_Var of type string that contains a copy of |
| 106 // the given string. The input data must be valid UTF-8 encoded text, if it | 98 // the given string. The input data must be valid UTF-8 encoded text, if it |
| 107 // is not valid UTF-8, a NULL var will be returned. | 99 // is not valid UTF-8, a NULL var will be returned. |
| 108 // | 100 // |
| 109 // The return value will have a reference count of 1. Internally, this will | 101 // The return value will have a reference count of 1. Internally, this will |
| 110 // create a StringVar, associate it with a module, and return the reference | 102 // create a StringVar and return the reference to it in the var. |
| 111 // to it in the var. | 103 static PP_Var StringToPPVar(const std::string& str); |
| 112 static PP_Var StringToPPVar(PP_Module module, const std::string& str); | 104 static PP_Var StringToPPVar(const char* str, uint32 len); |
| 113 static PP_Var StringToPPVar(PP_Module module, const char* str, uint32 len); | |
| 114 | 105 |
| 115 // Helper function that converts a PP_Var to a string. This will return NULL | 106 // Helper function that converts a PP_Var to a string. This will return NULL |
| 116 // if the PP_Var is not of string type or the string is invalid. | 107 // if the PP_Var is not of string type or the string is invalid. |
| 117 static StringVar* FromPPVar(PP_Var var); | 108 static StringVar* FromPPVar(PP_Var var); |
| 118 | 109 |
| 119 private: | 110 private: |
| 120 std::string value_; | 111 std::string value_; |
| 121 | 112 |
| 122 DISALLOW_COPY_AND_ASSIGN(StringVar); | 113 DISALLOW_COPY_AND_ASSIGN(StringVar); |
| 123 }; | 114 }; |
| 124 | 115 |
| 125 } // namespace ppapi | 116 } // namespace ppapi |
| 126 | 117 |
| 127 #endif // PPAPI_SHARED_IMPL_VAR_H_ | 118 #endif // PPAPI_SHARED_IMPL_VAR_H_ |
| OLD | NEW |