| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <stdint.h> |
| 9 |
| 8 #include <string> | 10 #include <string> |
| 9 | 11 |
| 10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/shared_memory.h" | 15 #include "base/memory/shared_memory.h" |
| 13 #include "ppapi/c/pp_var.h" | 16 #include "ppapi/c/pp_var.h" |
| 14 #include "ppapi/shared_impl/host_resource.h" | 17 #include "ppapi/shared_impl/host_resource.h" |
| 15 #include "ppapi/shared_impl/ppapi_shared_export.h" | 18 #include "ppapi/shared_impl/ppapi_shared_export.h" |
| 16 | 19 |
| 17 namespace ppapi { | 20 namespace ppapi { |
| 18 | 21 |
| 19 class ArrayBufferVar; | 22 class ArrayBufferVar; |
| 20 class ArrayVar; | 23 class ArrayVar; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 47 | 50 |
| 48 // Returns the type of this var. | 51 // Returns the type of this var. |
| 49 virtual PP_VarType GetType() const = 0; | 52 virtual PP_VarType GetType() const = 0; |
| 50 | 53 |
| 51 // Returns the ID corresponing to the string or object if it exists already, | 54 // Returns the ID corresponing to the string or object if it exists already, |
| 52 // or 0 if an ID hasn't been generated for this object (the plugin is holding | 55 // or 0 if an ID hasn't been generated for this object (the plugin is holding |
| 53 // no refs). | 56 // no refs). |
| 54 // | 57 // |
| 55 // Contrast to GetOrCreateVarID which creates the ID and a ref on behalf of | 58 // Contrast to GetOrCreateVarID which creates the ID and a ref on behalf of |
| 56 // the plugin. | 59 // the plugin. |
| 57 int32 GetExistingVarID() const; | 60 int32_t GetExistingVarID() const; |
| 58 | 61 |
| 59 protected: | 62 protected: |
| 60 friend class base::RefCounted<Var>; | 63 friend class base::RefCounted<Var>; |
| 61 friend class VarTracker; | 64 friend class VarTracker; |
| 62 | 65 |
| 63 Var(); | 66 Var(); |
| 64 virtual ~Var(); | 67 virtual ~Var(); |
| 65 | 68 |
| 66 // Returns the unique ID associated with this string or object, creating it | 69 // Returns the unique ID associated with this string or object, creating it |
| 67 // if necessary. The return value will be 0 if the string or object is | 70 // if necessary. The return value will be 0 if the string or object is |
| 68 // invalid. | 71 // invalid. |
| 69 // | 72 // |
| 70 // This function will take a reference to the var that will be passed to the | 73 // This function will take a reference to the var that will be passed to the |
| 71 // caller. | 74 // caller. |
| 72 int32 GetOrCreateVarID(); | 75 int32_t GetOrCreateVarID(); |
| 73 | 76 |
| 74 // Sets the internal object ID. This assumes that the ID hasn't been set | 77 // Sets the internal object ID. This assumes that the ID hasn't been set |
| 75 // before. This is used in cases where the ID is generated externally. | 78 // before. This is used in cases where the ID is generated externally. |
| 76 void AssignVarID(int32 id); | 79 void AssignVarID(int32_t id); |
| 77 | 80 |
| 78 // Reset the assigned object ID. | 81 // Reset the assigned object ID. |
| 79 void ResetVarID() { var_id_ = 0; } | 82 void ResetVarID() { var_id_ = 0; } |
| 80 | 83 |
| 81 private: | 84 private: |
| 82 // This will be 0 if no ID has been assigned (this happens lazily). | 85 // This will be 0 if no ID has been assigned (this happens lazily). |
| 83 int32 var_id_; | 86 int32_t var_id_; |
| 84 | 87 |
| 85 DISALLOW_COPY_AND_ASSIGN(Var); | 88 DISALLOW_COPY_AND_ASSIGN(Var); |
| 86 }; | 89 }; |
| 87 | 90 |
| 88 // StringVar ------------------------------------------------------------------- | 91 // StringVar ------------------------------------------------------------------- |
| 89 | 92 |
| 90 // Represents a string-based Var. | 93 // Represents a string-based Var. |
| 91 // | 94 // |
| 92 // Returning a given string as a PP_Var: | 95 // Returning a given string as a PP_Var: |
| 93 // return StringVar::StringToPPVar(my_string); | 96 // return StringVar::StringToPPVar(my_string); |
| 94 // | 97 // |
| 95 // Converting a PP_Var to a string: | 98 // Converting a PP_Var to a string: |
| 96 // StringVar* string = StringVar::FromPPVar(var); | 99 // StringVar* string = StringVar::FromPPVar(var); |
| 97 // if (!string) | 100 // if (!string) |
| 98 // return false; // Not a string or an invalid var. | 101 // return false; // Not a string or an invalid var. |
| 99 // DoSomethingWithTheString(string->value()); | 102 // DoSomethingWithTheString(string->value()); |
| 100 class PPAPI_SHARED_EXPORT StringVar : public Var { | 103 class PPAPI_SHARED_EXPORT StringVar : public Var { |
| 101 public: | 104 public: |
| 102 explicit StringVar(const std::string& str); | 105 explicit StringVar(const std::string& str); |
| 103 StringVar(const char* str, uint32 len); | 106 StringVar(const char* str, uint32_t len); |
| 104 ~StringVar() override; | 107 ~StringVar() override; |
| 105 | 108 |
| 106 const std::string& value() const { return value_; } | 109 const std::string& value() const { return value_; } |
| 107 // Return a pointer to the internal string. This allows other objects to | 110 // Return a pointer to the internal string. This allows other objects to |
| 108 // temporarily store a weak pointer to our internal string. Use with care; the | 111 // temporarily store a weak pointer to our internal string. Use with care; the |
| 109 // pointer *will* become invalid if this StringVar is removed from the | 112 // pointer *will* become invalid if this StringVar is removed from the |
| 110 // tracker. (All of this applies to value(), but this one's even easier to use | 113 // tracker. (All of this applies to value(), but this one's even easier to use |
| 111 // dangerously). | 114 // dangerously). |
| 112 const std::string* ptr() const { return &value_; } | 115 const std::string* ptr() const { return &value_; } |
| 113 | 116 |
| 114 // Var override. | 117 // Var override. |
| 115 StringVar* AsStringVar() override; | 118 StringVar* AsStringVar() override; |
| 116 PP_VarType GetType() const override; | 119 PP_VarType GetType() const override; |
| 117 | 120 |
| 118 // Helper function to create a PP_Var of type string that contains a copy of | 121 // Helper function to create a PP_Var of type string that contains a copy of |
| 119 // the given string. The input data must be valid UTF-8 encoded text, if it | 122 // the given string. The input data must be valid UTF-8 encoded text, if it |
| 120 // is not valid UTF-8, a NULL var will be returned. | 123 // is not valid UTF-8, a NULL var will be returned. |
| 121 // | 124 // |
| 122 // The return value will have a reference count of 1. Internally, this will | 125 // The return value will have a reference count of 1. Internally, this will |
| 123 // create a StringVar and return the reference to it in the var. | 126 // create a StringVar and return the reference to it in the var. |
| 124 static PP_Var StringToPPVar(const std::string& str); | 127 static PP_Var StringToPPVar(const std::string& str); |
| 125 static PP_Var StringToPPVar(const char* str, uint32 len); | 128 static PP_Var StringToPPVar(const char* str, uint32_t len); |
| 126 | 129 |
| 127 // Same as StringToPPVar but avoids a copy by destructively swapping the | 130 // Same as StringToPPVar but avoids a copy by destructively swapping the |
| 128 // given string into the newly created StringVar. The string must already be | 131 // given string into the newly created StringVar. The string must already be |
| 129 // valid UTF-8. After the call, *src will be empty. | 132 // valid UTF-8. After the call, *src will be empty. |
| 130 static PP_Var SwapValidatedUTF8StringIntoPPVar(std::string* src); | 133 static PP_Var SwapValidatedUTF8StringIntoPPVar(std::string* src); |
| 131 | 134 |
| 132 // Helper function that converts a PP_Var to a string. This will return NULL | 135 // Helper function that converts a PP_Var to a string. This will return NULL |
| 133 // if the PP_Var is not of string type or the string is invalid. | 136 // if the PP_Var is not of string type or the string is invalid. |
| 134 static StringVar* FromPPVar(PP_Var var); | 137 static StringVar* FromPPVar(PP_Var var); |
| 135 | 138 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 155 // if (!array) | 158 // if (!array) |
| 156 // return false; // Not an ArrayBuffer or an invalid var. | 159 // return false; // Not an ArrayBuffer or an invalid var. |
| 157 // DoSomethingWithTheBuffer(array); | 160 // DoSomethingWithTheBuffer(array); |
| 158 class PPAPI_SHARED_EXPORT ArrayBufferVar : public Var { | 161 class PPAPI_SHARED_EXPORT ArrayBufferVar : public Var { |
| 159 public: | 162 public: |
| 160 ArrayBufferVar(); | 163 ArrayBufferVar(); |
| 161 ~ArrayBufferVar() override; | 164 ~ArrayBufferVar() override; |
| 162 | 165 |
| 163 virtual void* Map() = 0; | 166 virtual void* Map() = 0; |
| 164 virtual void Unmap() = 0; | 167 virtual void Unmap() = 0; |
| 165 virtual uint32 ByteLength() = 0; | 168 virtual uint32_t ByteLength() = 0; |
| 166 | 169 |
| 167 // Creates a new shared memory region, and copies the data in the | 170 // Creates a new shared memory region, and copies the data in the |
| 168 // ArrayBufferVar into it. On the plugin side, host_shm_handle_id will be set | 171 // ArrayBufferVar into it. On the plugin side, host_shm_handle_id will be set |
| 169 // to some value that is not -1. On the host side, plugin_shm_handle will be | 172 // to some value that is not -1. On the host side, plugin_shm_handle will be |
| 170 // set to a valid SharedMemoryHandle. | 173 // set to a valid SharedMemoryHandle. |
| 171 // | 174 // |
| 172 // Returns true if creating the shared memory (and copying) is successful, | 175 // Returns true if creating the shared memory (and copying) is successful, |
| 173 // false otherwise. | 176 // false otherwise. |
| 174 virtual bool CopyToNewShmem(PP_Instance instance, | 177 virtual bool CopyToNewShmem(PP_Instance instance, |
| 175 int* host_shm_handle_id, | 178 int* host_shm_handle_id, |
| 176 base::SharedMemoryHandle* plugin_shm_handle) = 0; | 179 base::SharedMemoryHandle* plugin_shm_handle) = 0; |
| 177 | 180 |
| 178 // Var override. | 181 // Var override. |
| 179 ArrayBufferVar* AsArrayBufferVar() override; | 182 ArrayBufferVar* AsArrayBufferVar() override; |
| 180 PP_VarType GetType() const override; | 183 PP_VarType GetType() const override; |
| 181 | 184 |
| 182 // Helper function that converts a PP_Var to an ArrayBufferVar. This will | 185 // Helper function that converts a PP_Var to an ArrayBufferVar. This will |
| 183 // return NULL if the PP_Var is not of ArrayBuffer type. | 186 // return NULL if the PP_Var is not of ArrayBuffer type. |
| 184 static ArrayBufferVar* FromPPVar(PP_Var var); | 187 static ArrayBufferVar* FromPPVar(PP_Var var); |
| 185 | 188 |
| 186 private: | 189 private: |
| 187 DISALLOW_COPY_AND_ASSIGN(ArrayBufferVar); | 190 DISALLOW_COPY_AND_ASSIGN(ArrayBufferVar); |
| 188 }; | 191 }; |
| 189 | 192 |
| 190 } // namespace ppapi | 193 } // namespace ppapi |
| 191 | 194 |
| 192 #endif // PPAPI_SHARED_IMPL_VAR_H_ | 195 #endif // PPAPI_SHARED_IMPL_VAR_H_ |
| OLD | NEW |