OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #include "ppapi/proxy/resource_reply_thread_registrar.h" | 5 #include "ppapi/proxy/resource_reply_thread_registrar.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
| 9 #include "ipc/ipc_message.h" |
| 10 #include "ppapi/proxy/resource_message_params.h" |
9 #include "ppapi/shared_impl/proxy_lock.h" | 11 #include "ppapi/shared_impl/proxy_lock.h" |
10 #include "ppapi/shared_impl/tracked_callback.h" | 12 #include "ppapi/shared_impl/tracked_callback.h" |
11 | 13 |
12 namespace ppapi { | 14 namespace ppapi { |
13 namespace proxy { | 15 namespace proxy { |
14 | 16 |
15 ResourceReplyThreadRegistrar::ResourceReplyThreadRegistrar( | 17 ResourceReplyThreadRegistrar::ResourceReplyThreadRegistrar( |
16 scoped_refptr<base::MessageLoopProxy> default_thread) | 18 scoped_refptr<base::MessageLoopProxy> main_thread) |
17 : default_thread_(default_thread) { | 19 : main_thread_(main_thread) { |
18 } | 20 } |
19 | 21 |
20 ResourceReplyThreadRegistrar::~ResourceReplyThreadRegistrar() { | 22 ResourceReplyThreadRegistrar::~ResourceReplyThreadRegistrar() { |
21 } | 23 } |
22 | 24 |
23 void ResourceReplyThreadRegistrar::Register( | 25 void ResourceReplyThreadRegistrar::Register( |
24 PP_Resource resource, | 26 PP_Resource resource, |
25 int32_t sequence_number, | 27 int32_t sequence_number, |
26 scoped_refptr<TrackedCallback> reply_thread_hint) { | 28 scoped_refptr<TrackedCallback> reply_thread_hint) { |
27 ProxyLock::AssertAcquiredDebugOnly(); | 29 ProxyLock::AssertAcquiredDebugOnly(); |
28 | 30 |
29 // Use the default thread if |reply_thread_hint| is NULL or blocking. | 31 // Use the main thread if |reply_thread_hint| is NULL or blocking. |
30 if (!reply_thread_hint.get() || reply_thread_hint->is_blocking()) | 32 if (!reply_thread_hint.get() || reply_thread_hint->is_blocking()) |
31 return; | 33 return; |
32 | 34 |
33 DCHECK(reply_thread_hint->target_loop()); | 35 DCHECK(reply_thread_hint->target_loop()); |
34 scoped_refptr<base::MessageLoopProxy> reply_thread( | 36 scoped_refptr<base::MessageLoopProxy> reply_thread( |
35 reply_thread_hint->target_loop()->GetMessageLoopProxy()); | 37 reply_thread_hint->target_loop()->GetMessageLoopProxy()); |
36 { | 38 { |
37 base::AutoLock auto_lock(lock_); | 39 base::AutoLock auto_lock(lock_); |
38 | 40 |
39 if (reply_thread.get() == default_thread_.get()) | 41 if (reply_thread.get() == main_thread_.get()) |
40 return; | 42 return; |
41 | 43 |
42 map_[resource][sequence_number] = reply_thread; | 44 map_[resource].thread_map[sequence_number] = reply_thread; |
43 } | 45 } |
44 } | 46 } |
45 | 47 |
| 48 void ResourceReplyThreadRegistrar::HandleOnIOThread( |
| 49 PP_Resource resource, uint32 nested_msg_type) { |
| 50 base::AutoLock auto_lock(lock_); |
| 51 map_[resource].io_thread_message_types.insert(nested_msg_type); |
| 52 } |
| 53 |
46 void ResourceReplyThreadRegistrar::Unregister(PP_Resource resource) { | 54 void ResourceReplyThreadRegistrar::Unregister(PP_Resource resource) { |
47 base::AutoLock auto_lock(lock_); | 55 base::AutoLock auto_lock(lock_); |
48 map_.erase(resource); | 56 map_.erase(resource); |
49 } | 57 } |
50 | 58 |
51 scoped_refptr<base::MessageLoopProxy> | 59 scoped_refptr<base::MessageLoopProxy> |
52 ResourceReplyThreadRegistrar::GetTargetThreadAndUnregister( | 60 ResourceReplyThreadRegistrar::GetTargetThread( |
53 PP_Resource resource, | 61 const ResourceMessageReplyParams& reply_params, |
54 int32_t sequence_number) { | 62 const IPC::Message& nested_msg) { |
55 base::AutoLock auto_lock(lock_); | 63 base::AutoLock auto_lock(lock_); |
56 ResourceMap::iterator resource_iter = map_.find(resource); | 64 ResourceMap::iterator resource_iter = map_.find(reply_params.pp_resource()); |
57 if (resource_iter == map_.end()) | 65 if (resource_iter == map_.end()) |
58 return default_thread_; | 66 return main_thread_; |
59 | 67 |
60 SequenceNumberMap::iterator sequence_number_iter = | 68 ResourceInfo& info = resource_iter->second; |
61 resource_iter->second.find(sequence_number); | 69 SequenceThreadMap::iterator sequence_thread_iter = |
62 if (sequence_number_iter == resource_iter->second.end()) | 70 info.thread_map.find(reply_params.sequence()); |
63 return default_thread_; | 71 if (sequence_thread_iter != info.thread_map.end()) { |
| 72 scoped_refptr<base::MessageLoopProxy> target = sequence_thread_iter->second; |
| 73 info.thread_map.erase(sequence_thread_iter); |
| 74 return target; |
| 75 } |
64 | 76 |
65 scoped_refptr<base::MessageLoopProxy> target = sequence_number_iter->second; | 77 if (info.io_thread_message_types.find(nested_msg.type()) != |
66 resource_iter->second.erase(sequence_number_iter); | 78 info.io_thread_message_types.end()) { |
67 return target; | 79 return scoped_refptr<base::MessageLoopProxy>(); |
| 80 } |
| 81 |
| 82 return main_thread_; |
68 } | 83 } |
69 | 84 |
70 } // namespace proxy | 85 } // namespace proxy |
71 } // namespace ppapi | 86 } // namespace ppapi |
OLD | NEW |