• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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_PROXY_RESOURCE_REPLY_THREAD_REGISTRAR_H_
6 #define PPAPI_PROXY_RESOURCE_REPLY_THREAD_REGISTRAR_H_
7 
8 #include <map>
9 
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/synchronization/lock.h"
13 #include "ppapi/c/pp_resource.h"
14 #include "ppapi/proxy/ppapi_proxy_export.h"
15 
16 
17 namespace base {
18 class MessageLoopProxy;
19 }
20 
21 namespace ppapi {
22 
23 class TrackedCallback;
24 
25 namespace proxy {
26 
27 // ResourceReplyThreadRegistrar records the handling thread for
28 // PpapiPluginMsg_ResourceReply messages.
29 // This class is thread safe.
30 class PPAPI_PROXY_EXPORT ResourceReplyThreadRegistrar
31     : public base::RefCountedThreadSafe<ResourceReplyThreadRegistrar> {
32  public:
33   explicit ResourceReplyThreadRegistrar(
34       scoped_refptr<base::MessageLoopProxy> default_thread);
35 
36   // This method can only be called while holding the Pepper proxy lock; the
37   // other methods can be called with/without the Pepper proxy lock.
38   void Register(PP_Resource resource,
39                 int32_t sequence_number,
40                 scoped_refptr<TrackedCallback> reply_thread_hint);
41 
42   void Unregister(PP_Resource resource);
43 
44   scoped_refptr<base::MessageLoopProxy> GetTargetThreadAndUnregister(
45       PP_Resource resource,
46       int32_t sequence_number);
47 
48  private:
49   friend class base::RefCountedThreadSafe<ResourceReplyThreadRegistrar>;
50 
51   typedef std::map<int32_t, scoped_refptr<base::MessageLoopProxy> >
52       SequenceNumberMap;
53   typedef std::map<PP_Resource, SequenceNumberMap> ResourceMap;
54 
55   ~ResourceReplyThreadRegistrar();
56 
57   // The lock that protects the data members below.
58   // Please note that we should never try to acquire the Pepper proxy lock while
59   // holding |lock_|, otherwise we will cause deadlock.
60   base::Lock lock_;
61   ResourceMap map_;
62   scoped_refptr<base::MessageLoopProxy> default_thread_;
63 
64   DISALLOW_COPY_AND_ASSIGN(ResourceReplyThreadRegistrar);
65 };
66 
67 }  // namespace proxy
68 }  // namespace ppapi
69 
70 #endif   // PPAPI_PROXY_RESOURCE_REPLY_THREAD_REGISTRAR_H_
71