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" | 12 #include "ppapi/c/pp_module.h" |
13 | 13 #include "ppapi/c/pp_var.h" |
14 struct PP_Var; | |
15 | 14 |
16 namespace ppapi { | 15 namespace ppapi { |
17 | 16 |
18 class NPObjectVar; | 17 class NPObjectVar; |
| 18 class ProxyObjectVar; |
19 class StringVar; | 19 class StringVar; |
20 | 20 |
21 // Var ------------------------------------------------------------------------- | 21 // Var ------------------------------------------------------------------------- |
22 | 22 |
23 // Represents a non-POD var. This is derived from a resource even though it | 23 // Represents a non-POD var. This is derived from a resource even though it |
24 // isn't a resource from the plugin's perspective. This allows us to re-use | 24 // isn't a resource from the plugin's perspective. This allows us to re-use |
25 // the refcounting and the association with the module from the resource code. | 25 // the refcounting and the association with the module from the resource code. |
26 class Var : public base::RefCounted<Var> { | 26 class Var : public base::RefCounted<Var> { |
27 public: | 27 public: |
28 virtual ~Var(); | 28 virtual ~Var(); |
29 | 29 |
30 // Returns a string representing the given var for logging purposes. | 30 // Returns a string representing the given var for logging purposes. |
31 static std::string PPVarToLogString(PP_Var var); | 31 static std::string PPVarToLogString(PP_Var var); |
32 | 32 |
33 // Provides access to the manual refcounting of a PP_Var from the plugin's | |
34 // perspective. This is different than the AddRef/Release on this scoped | |
35 // object. This uses the ResourceTracker, which keeps a separate "plugin | |
36 // refcount" that prevents the plugin from messing up our refcounting or | |
37 // freeing something out from under us. | |
38 // | |
39 // You should not generally need to use these functions. However, if you | |
40 // call a plugin function that returns a var, it will transfer a ref to us | |
41 // (the caller) which in the case of a string or object var will need to | |
42 // be released. | |
43 // | |
44 // Example, assuming we're expecting the plugin to return a string: | |
45 // PP_Var rv = some_ppp_interface->DoSomething(a, b, c); | |
46 // | |
47 // // Get the string value. This will take a reference to the object which | |
48 // // will prevent it from being deleted out from under us when we call | |
49 // // PluginReleasePPVar(). | |
50 // scoped_refptr<StringVar> string(StringVar::FromPPVar(rv)); | |
51 // | |
52 // // Release the reference the plugin gave us when returning the value. | |
53 // // This is legal to do for all types of vars. | |
54 // Var::PluginReleasePPVar(rv); | |
55 // | |
56 // // Use the string. | |
57 // if (!string) | |
58 // return false; // It didn't return a proper string. | |
59 // UseTheString(string->value()); | |
60 static void PluginAddRefPPVar(PP_Var var); | |
61 static void PluginReleasePPVar(PP_Var var); | |
62 | |
63 virtual StringVar* AsStringVar(); | 33 virtual StringVar* AsStringVar(); |
64 virtual NPObjectVar* AsNPObjectVar(); | 34 virtual NPObjectVar* AsNPObjectVar(); |
| 35 virtual ProxyObjectVar* AsProxyObjectVar(); |
65 | 36 |
66 // 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 |
67 // one reference addrefed on behalf of the caller. | 38 // one reference addrefed on behalf of the caller. |
68 virtual PP_Var GetPPVar() = 0; | 39 virtual PP_Var GetPPVar() = 0; |
69 | 40 |
| 41 // Returns the type of this var. |
| 42 virtual PP_VarType GetType() const = 0; |
| 43 |
70 // 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, |
71 // 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 |
72 // no refs). | 46 // no refs). |
73 // | 47 // |
74 // 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 |
75 // the plugin. | 49 // the plugin. |
76 int32 GetExistingVarID() const; | 50 int32 GetExistingVarID() const; |
77 | 51 |
78 PP_Module pp_module() const { return pp_module_; } | 52 PP_Module pp_module() const { return pp_module_; } |
79 | 53 |
80 protected: | 54 protected: |
81 // This can only be constructed as a StringVar or an ObjectVar. | 55 // This can only be constructed as a StringVar or an ObjectVar. |
82 explicit Var(PP_Module module); | 56 explicit Var(PP_Module module); |
83 | 57 |
84 // Returns the unique ID associated with this string or object, creating it | 58 // Returns the unique ID associated with this string or object, creating it |
85 // if necessary. The return value will be 0 if the string or object is | 59 // if necessary. The return value will be 0 if the string or object is |
86 // invalid. | 60 // invalid. |
87 // | 61 // |
88 // This function will take a reference to the var that will be passed to the | 62 // This function will take a reference to the var that will be passed to the |
89 // caller. | 63 // caller. |
90 int32 GetOrCreateVarID(); | 64 int32 GetOrCreateVarID(); |
91 | 65 |
| 66 // 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 |
92 private: | 70 private: |
93 PP_Module pp_module_; | 71 PP_Module pp_module_; |
94 | 72 |
95 // This will be 0 if no ID has been assigned (this happens lazily). | 73 // This will be 0 if no ID has been assigned (this happens lazily). |
96 int32 var_id_; | 74 int32 var_id_; |
97 | 75 |
98 DISALLOW_COPY_AND_ASSIGN(Var); | 76 DISALLOW_COPY_AND_ASSIGN(Var); |
99 }; | 77 }; |
100 | 78 |
101 // StringVar ------------------------------------------------------------------- | 79 // StringVar ------------------------------------------------------------------- |
(...skipping 11 matching lines...) Expand all Loading... |
113 class StringVar : public Var { | 91 class StringVar : public Var { |
114 public: | 92 public: |
115 StringVar(PP_Module module, const char* str, uint32 len); | 93 StringVar(PP_Module module, const char* str, uint32 len); |
116 virtual ~StringVar(); | 94 virtual ~StringVar(); |
117 | 95 |
118 const std::string& value() const { return value_; } | 96 const std::string& value() const { return value_; } |
119 | 97 |
120 // Var override. | 98 // Var override. |
121 virtual StringVar* AsStringVar() OVERRIDE; | 99 virtual StringVar* AsStringVar() OVERRIDE; |
122 virtual PP_Var GetPPVar() OVERRIDE; | 100 virtual PP_Var GetPPVar() OVERRIDE; |
| 101 virtual PP_VarType GetType() const OVERRIDE; |
123 | 102 |
124 // Helper function to create a PP_Var of type string that contains a copy of | 103 // Helper function to create a PP_Var of type string that contains a copy of |
125 // the given string. The input data must be valid UTF-8 encoded text, if it | 104 // the given string. The input data must be valid UTF-8 encoded text, if it |
126 // is not valid UTF-8, a NULL var will be returned. | 105 // is not valid UTF-8, a NULL var will be returned. |
127 // | 106 // |
128 // The return value will have a reference count of 1. Internally, this will | 107 // The return value will have a reference count of 1. Internally, this will |
129 // create a StringVar, associate it with a module, and return the reference | 108 // create a StringVar, associate it with a module, and return the reference |
130 // to it in the var. | 109 // to it in the var. |
131 static PP_Var StringToPPVar(PP_Module module, const std::string& str); | 110 static PP_Var StringToPPVar(PP_Module module, const std::string& str); |
132 static PP_Var StringToPPVar(PP_Module module, const char* str, uint32 len); | 111 static PP_Var StringToPPVar(PP_Module module, const char* str, uint32 len); |
133 | 112 |
134 // Helper function that converts a PP_Var to a string. This will return NULL | 113 // Helper function that converts a PP_Var to a string. This will return NULL |
135 // if the PP_Var is not of string type or the string is invalid. | 114 // if the PP_Var is not of string type or the string is invalid. |
136 static scoped_refptr<StringVar> FromPPVar(PP_Var var); | 115 static scoped_refptr<StringVar> FromPPVar(PP_Var var); |
137 | 116 |
138 private: | 117 private: |
139 std::string value_; | 118 std::string value_; |
140 | 119 |
141 DISALLOW_COPY_AND_ASSIGN(StringVar); | 120 DISALLOW_COPY_AND_ASSIGN(StringVar); |
142 }; | 121 }; |
143 | 122 |
144 } // namespace ppapi | 123 } // namespace ppapi |
145 | 124 |
146 #endif // PPAPI_SHARED_IMPL_VAR_H_ | 125 #endif // PPAPI_SHARED_IMPL_VAR_H_ |
OLD | NEW |