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_THUNK_ENTER_H_ | 5 #ifndef PPAPI_THUNK_ENTER_H_ |
6 #define PPAPI_THUNK_ENTER_H_ | 6 #define PPAPI_THUNK_ENTER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "ppapi/c/pp_resource.h" | 9 #include "ppapi/c/pp_resource.h" |
10 #include "ppapi/shared_impl/api_id.h" | 10 #include "ppapi/shared_impl/api_id.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 template <> | 59 template <> |
60 struct LockOnEntry<true> { | 60 struct LockOnEntry<true> { |
61 LockOnEntry() { | 61 LockOnEntry() { |
62 ppapi::ProxyLock::Acquire(); | 62 ppapi::ProxyLock::Acquire(); |
63 } | 63 } |
64 ~LockOnEntry() { | 64 ~LockOnEntry() { |
65 ppapi::ProxyLock::Release(); | 65 ppapi::ProxyLock::Release(); |
66 } | 66 } |
67 }; | 67 }; |
68 | 68 |
| 69 // Keep non-templatized since we need non-inline functions here. |
| 70 class PPAPI_THUNK_EXPORT EnterBase { |
| 71 public: |
| 72 EnterBase(); |
| 73 explicit EnterBase(const PP_CompletionCallback& callback); |
| 74 virtual ~EnterBase(); |
| 75 |
| 76 // Sets the result. |
| 77 // |
| 78 // Returns the "retval()". This is to support the typical usage of |
| 79 // return enter.SetResult(...); |
| 80 // without having to write a separate "return enter.retval();" line. |
| 81 int32_t SetResult(int32_t result); |
| 82 |
| 83 // Use this value as the return value for the function. |
| 84 int32_t retval() const { return retval_; } |
| 85 |
| 86 protected: |
| 87 // Helper function to return a function group from a PP_Instance. Having this |
| 88 // code be in the non-templatized base keeps us from having to instantiate |
| 89 // it in every template. |
| 90 FunctionGroupBase* GetFunctions(PP_Instance instance, ApiID id) const; |
| 91 |
| 92 // Helper function to return a Resource from a PP_Resource. Having this |
| 93 // code be in the non-templatized base keeps us from having to instantiate |
| 94 // it in every template. |
| 95 Resource* GetResource(PP_Resource resource) const; |
| 96 |
| 97 // Does error handling associated with entering a resource. The resource_base |
| 98 // is the result of looking up the given pp_resource. The object is the |
| 99 // result of converting the base to the desired object (converted back to a |
| 100 // Resource* so this function doesn't have to be templatized). The reason for |
| 101 // passing both resource_base and object is that we can differentiate "bad |
| 102 // resource ID" from "valid resource ID not of the currect type." |
| 103 // |
| 104 // This will set retval_ = PP_ERROR_BADRESOURCE if the object is invalid, and |
| 105 // if report_error is set, log a message to the programmer. |
| 106 void SetStateForResourceError(PP_Resource pp_resource, |
| 107 Resource* resource_base, |
| 108 void* object, |
| 109 bool report_error); |
| 110 |
| 111 private: |
| 112 // Holds the callback. The function will only be non-NULL when the |
| 113 // callback is requried. Optional callbacks don't require any special |
| 114 // handling from us at this layer. |
| 115 PP_CompletionCallback callback_; |
| 116 |
| 117 int32_t retval_; |
| 118 }; |
| 119 |
69 } // namespace subtle | 120 } // namespace subtle |
70 | 121 |
71 | 122 |
72 template<typename FunctionsT, bool lock_on_entry = true> | 123 template<typename FunctionsT, bool lock_on_entry = true> |
73 class EnterFunction : subtle::LockOnEntry<lock_on_entry> { | 124 class EnterFunction : public subtle::EnterBase, |
| 125 public subtle::LockOnEntry<lock_on_entry> { |
74 public: | 126 public: |
75 EnterFunction(PP_Instance instance, bool report_error) | 127 EnterFunction(PP_Instance instance, bool report_error) |
76 : functions_(NULL) { | 128 : functions_(NULL) { |
77 FunctionGroupBase* base = PpapiGlobals::Get()->GetFunctionAPI( | 129 FunctionGroupBase* base = GetFunctions(instance, FunctionsT::kApiID); |
78 instance, FunctionsT::kApiID); | |
79 if (base) | 130 if (base) |
80 functions_ = base->GetAs<FunctionsT>(); | 131 functions_ = base->GetAs<FunctionsT>(); |
81 // TODO(brettw) check error and if report_error is set, do something. | 132 // TODO(brettw) check error and if report_error is set, do something. |
82 } | 133 } |
83 | 134 |
84 ~EnterFunction() {} | 135 ~EnterFunction() {} |
85 | 136 |
86 bool succeeded() const { return !!functions_; } | 137 bool succeeded() const { return !!functions_; } |
87 bool failed() const { return !functions_; } | 138 bool failed() const { return !functions_; } |
88 | 139 |
(...skipping 28 matching lines...) Expand all Loading... |
117 static PP_Instance GetInstanceForResource(PP_Resource resource) { | 168 static PP_Instance GetInstanceForResource(PP_Resource resource) { |
118 Resource* object = | 169 Resource* object = |
119 PpapiGlobals::Get()->GetResourceTracker()->GetResource(resource); | 170 PpapiGlobals::Get()->GetResourceTracker()->GetResource(resource); |
120 return object ? object->pp_instance() : 0; | 171 return object ? object->pp_instance() : 0; |
121 } | 172 } |
122 }; | 173 }; |
123 | 174 |
124 // EnterResource --------------------------------------------------------------- | 175 // EnterResource --------------------------------------------------------------- |
125 | 176 |
126 template<typename ResourceT, bool lock_on_entry = true> | 177 template<typename ResourceT, bool lock_on_entry = true> |
127 class EnterResource : subtle::LockOnEntry<lock_on_entry> { | 178 class EnterResource : public subtle::EnterBase, |
| 179 public subtle::LockOnEntry<lock_on_entry> { |
128 public: | 180 public: |
129 EnterResource(PP_Resource resource, bool report_error) | 181 EnterResource(PP_Resource resource, bool report_error) |
130 : object_(NULL) { | 182 : EnterBase() { |
131 resource_ = | 183 Init(resource, report_error); |
132 PpapiGlobals::Get()->GetResourceTracker()->GetResource(resource); | 184 } |
133 if (resource_) | 185 EnterResource(PP_Resource resource, |
134 object_ = resource_->GetAs<ResourceT>(); | 186 const PP_CompletionCallback& callback, |
135 // TODO(brettw) check error and if report_error is set, do something. | 187 bool report_error) |
| 188 : EnterBase(callback) { |
| 189 Init(resource, report_error); |
136 } | 190 } |
137 ~EnterResource() {} | 191 ~EnterResource() {} |
138 | 192 |
139 bool succeeded() const { return !!object_; } | 193 bool succeeded() const { return !!object_; } |
140 bool failed() const { return !object_; } | 194 bool failed() const { return !object_; } |
141 | 195 |
142 ResourceT* object() { return object_; } | 196 ResourceT* object() { return object_; } |
143 Resource* resource() { return resource_; } | 197 Resource* resource() { return resource_; } |
144 | 198 |
145 private: | 199 private: |
| 200 void Init(PP_Resource resource, bool report_error) { |
| 201 resource_ = GetResource(resource); |
| 202 if (resource_) |
| 203 object_ = resource_->GetAs<ResourceT>(); |
| 204 else |
| 205 object_ = NULL; |
| 206 SetStateForResourceError(resource, resource_, object_, report_error); |
| 207 } |
| 208 |
146 Resource* resource_; | 209 Resource* resource_; |
147 ResourceT* object_; | 210 ResourceT* object_; |
148 | 211 |
149 DISALLOW_COPY_AND_ASSIGN(EnterResource); | 212 DISALLOW_COPY_AND_ASSIGN(EnterResource); |
150 }; | 213 }; |
151 | 214 |
152 // Like EnterResource but assumes the lock is already held. | 215 // Like EnterResource but assumes the lock is already held. |
153 template<typename ResourceT> | 216 template<typename ResourceT> |
154 class EnterResourceNoLock : public EnterResource<ResourceT, false> { | 217 class EnterResourceNoLock : public EnterResource<ResourceT, false> { |
155 public: | 218 public: |
(...skipping 19 matching lines...) Expand all Loading... |
175 : public EnterFunctionNoLock<PPB_Instance_FunctionAPI> { | 238 : public EnterFunctionNoLock<PPB_Instance_FunctionAPI> { |
176 public: | 239 public: |
177 EnterInstance(PP_Instance instance); | 240 EnterInstance(PP_Instance instance); |
178 ~EnterInstance(); | 241 ~EnterInstance(); |
179 }; | 242 }; |
180 | 243 |
181 } // namespace thunk | 244 } // namespace thunk |
182 } // namespace ppapi | 245 } // namespace ppapi |
183 | 246 |
184 #endif // PPAPI_THUNK_ENTER_H_ | 247 #endif // PPAPI_THUNK_ENTER_H_ |
OLD | NEW |