Elim cr-checkbox
[chromium-blink-merge.git] / ppapi / proxy / resource_reply_thread_registrar.cc
blob4921b7bcc58e08130a831396d94791457fb5247e
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.
5 #include "ppapi/proxy/resource_reply_thread_registrar.h"
7 #include "base/logging.h"
8 #include "base/single_thread_task_runner.h"
9 #include "ipc/ipc_message.h"
10 #include "ppapi/proxy/resource_message_params.h"
11 #include "ppapi/shared_impl/proxy_lock.h"
12 #include "ppapi/shared_impl/tracked_callback.h"
14 namespace ppapi {
15 namespace proxy {
17 ResourceReplyThreadRegistrar::ResourceReplyThreadRegistrar(
18 scoped_refptr<base::SingleThreadTaskRunner> main_thread)
19 : main_thread_(main_thread) {
22 ResourceReplyThreadRegistrar::~ResourceReplyThreadRegistrar() {
25 void ResourceReplyThreadRegistrar::Register(
26 PP_Resource resource,
27 int32_t sequence_number,
28 scoped_refptr<TrackedCallback> reply_thread_hint) {
29 ProxyLock::AssertAcquiredDebugOnly();
31 // Use the main thread if |reply_thread_hint| is NULL or blocking.
32 if (!reply_thread_hint.get() || reply_thread_hint->is_blocking())
33 return;
35 DCHECK(reply_thread_hint->target_loop());
36 scoped_refptr<base::SingleThreadTaskRunner> reply_thread(
37 reply_thread_hint->target_loop()->GetTaskRunner());
39 base::AutoLock auto_lock(lock_);
41 if (reply_thread.get() == main_thread_.get())
42 return;
44 map_[resource][sequence_number] = reply_thread;
48 void ResourceReplyThreadRegistrar::Unregister(PP_Resource resource) {
49 base::AutoLock auto_lock(lock_);
50 map_.erase(resource);
53 scoped_refptr<base::SingleThreadTaskRunner>
54 ResourceReplyThreadRegistrar::GetTargetThread(
55 const ResourceMessageReplyParams& reply_params,
56 const IPC::Message& nested_msg) {
57 base::AutoLock auto_lock(lock_);
58 ResourceMap::iterator resource_iter = map_.find(reply_params.pp_resource());
59 if (resource_iter != map_.end()) {
60 SequenceThreadMap::iterator sequence_thread_iter =
61 resource_iter->second.find(reply_params.sequence());
62 if (sequence_thread_iter != resource_iter->second.end()) {
63 scoped_refptr<base::SingleThreadTaskRunner> target =
64 sequence_thread_iter->second;
65 resource_iter->second.erase(sequence_thread_iter);
66 return target;
70 return main_thread_;
73 } // namespace proxy
74 } // namespace ppapi