OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef PPAPI_CPP_INSTANCE_HANDLE_H_ |
| 6 #define PPAPI_CPP_INSTANCE_HANDLE_H_ |
| 7 |
| 8 #include "ppapi/c/pp_instance.h" |
| 9 |
| 10 namespace pp { |
| 11 |
| 12 class Instance; |
| 13 |
| 14 /// An instance handle identifies an instance in a constructor for a resource. |
| 15 /// Its existence solves two different problems: |
| 16 /// |
| 17 /// A pp::Instance objects' lifetime is managed by the system on the main thread |
| 18 /// of the plugin. This means that it may get destroyed at any time based on |
| 19 /// something that happens on the web page. This means that it's never OK to |
| 20 /// refer to a pp::Instance object on a background thread. So we need to pass |
| 21 /// some kind of identifier instead to resource constructors so that they may |
| 22 /// safely be used on background threads. If the instance becomes invalid, the |
| 23 /// resource creation will fail on the background thread, but it won't crash. |
| 24 /// |
| 25 /// Background: PP_Instance would be a good identifier to use for this case. |
| 26 /// However, using it in the constructor to resources is problematic. |
| 27 /// PP_Instance is just a typedef for an integer, as is a PP_Resource. Many |
| 28 /// resources have alternate constructors that just take an existing |
| 29 /// PP_Resource, so the constructors would be ambiguous. Having this wrapper |
| 30 /// around a PP_Instance prevents this ambiguity, and also gives us a nice |
| 31 /// place to consolidate an implicit conversion from pp::Instance* for prettier |
| 32 /// code on the main thread (you can just pass "this" to resource constructors |
| 33 /// in your instance objects). |
| 34 /// |
| 35 /// So you should always pass InstanceHandles to background threads instead of |
| 36 /// a pp::Instance, and use them in resource constructors and code that may be |
| 37 /// used from background threads. |
| 38 class InstanceHandle { |
| 39 public: |
| 40 /// Implicit constructor for converting a pp::Instance to an instance handle. |
| 41 InstanceHandle(Instance* instance); |
| 42 |
| 43 /// Explicitly convert a PP_Instance to an instance handle. This should not |
| 44 /// be implicit because it can make some resource constructors ambiguous. |
| 45 /// PP_Instance is just a typedef for an integer, as is PP_Resource, so the |
| 46 /// compiler can get confused between the two. |
| 47 explicit InstanceHandle(PP_Instance pp_instance) |
| 48 : pp_instance_(pp_instance) {} |
| 49 |
| 50 PP_Instance pp_instance() const { return pp_instance_; } |
| 51 |
| 52 private: |
| 53 PP_Instance pp_instance_; |
| 54 }; |
| 55 |
| 56 } // namespace pp |
| 57 |
| 58 #endif // PPAPI_CPP_INSTANCE_HANDLE_H_ |
OLD | NEW |