Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: ppapi/cpp/instance_handle.h

Issue 9381010: Convert resources to take an instance key instead of an Instance*. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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. I ts
dmichael (off chromium) 2012/02/21 23:38:02 >80 char
15 /// existence solves two different problems:
16 ///
17 /// pp::Instance objects' lifetime is managed by the system on the main thread
dmichael (off chromium) 2012/02/21 23:38:02 nit: "A pp::Instance object's"?
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 /// PP_Instance would be a good identifier to use for this case. However, using
26 /// it in the constructor to resources is problematic. PP_Instance is just a
27 /// typedef for an integer, as is a PP_Resource. Many resources have alternate
28 /// constructors that just take an existing PP_Resource, so the constructors
29 /// would be ambiguous. Having this wrapper around a PP_Instance prevents this
30 /// ambiguity, and also gives us a nice place to consolidate an implicit
31 /// conversion from pp::Instance* for prettier code on the main thread (you can
32 /// just pass "this" to resource constructors in your instance objects).
dmichael (off chromium) 2012/02/21 23:38:02 This paragraph is good for our use but may not add
brettw 2012/02/22 20:24:36 I added a "background" label to make it more clear
33 ///
34 /// So you should always pass InstanceHandles to background threads instead of
35 /// a pp::Instance, and use them in resource constructors and code that may be
36 /// used from background threads.
37 class InstanceHandle {
38 public:
39 /// Implicit constructor for converting a pp::Instance to an instance handle.
40 InstanceHandle(Instance* instance);
41
42 /// Explicitly convert a PP_Instance to an instance handle. This should not
43 /// be implicit because it can make some resource constructors ambiguous.
44 /// PP_Instance is just a typedef for an integer, as is PP_Resource, so the
45 /// compiler can get confused between the two.
46 explicit InstanceHandle(PP_Instance pp_instance)
47 : pp_instance_(pp_instance) {}
48
49 PP_Instance pp_instance() const { return pp_instance_; }
50
51 private:
52 PP_Instance pp_instance_;
53 };
54
55 } // namespace pp
56
57 #endif // PPAPI_CPP_INSTANCE_HANDLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698