Home | History | Annotate | Download | only in proxy
      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 #include "ppapi/proxy/resource_reply_thread_registrar.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/message_loop/message_loop_proxy.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"
     13 
     14 namespace ppapi {
     15 namespace proxy {
     16 
     17 ResourceReplyThreadRegistrar::ResourceReplyThreadRegistrar(
     18     scoped_refptr<base::MessageLoopProxy> main_thread)
     19     : main_thread_(main_thread) {
     20 }
     21 
     22 ResourceReplyThreadRegistrar::~ResourceReplyThreadRegistrar() {
     23 }
     24 
     25 void ResourceReplyThreadRegistrar::Register(
     26     PP_Resource resource,
     27     int32_t sequence_number,
     28     scoped_refptr<TrackedCallback> reply_thread_hint) {
     29   ProxyLock::AssertAcquiredDebugOnly();
     30 
     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;
     34 
     35   DCHECK(reply_thread_hint->target_loop());
     36   scoped_refptr<base::MessageLoopProxy> reply_thread(
     37       reply_thread_hint->target_loop()->GetMessageLoopProxy());
     38   {
     39     base::AutoLock auto_lock(lock_);
     40 
     41     if (reply_thread.get() == main_thread_.get())
     42       return;
     43 
     44     map_[resource][sequence_number] = reply_thread;
     45   }
     46 }
     47 
     48 void ResourceReplyThreadRegistrar::Unregister(PP_Resource resource) {
     49   base::AutoLock auto_lock(lock_);
     50   map_.erase(resource);
     51 }
     52 
     53 void ResourceReplyThreadRegistrar::HandleOnIOThread(uint32 nested_msg_type) {
     54   base::AutoLock auto_lock(lock_);
     55   io_thread_message_types_.insert(nested_msg_type);
     56 }
     57 
     58 scoped_refptr<base::MessageLoopProxy>
     59 ResourceReplyThreadRegistrar::GetTargetThread(
     60     const ResourceMessageReplyParams& reply_params,
     61     const IPC::Message& nested_msg) {
     62   base::AutoLock auto_lock(lock_);
     63   ResourceMap::iterator resource_iter = map_.find(reply_params.pp_resource());
     64   if (resource_iter != map_.end()) {
     65     SequenceThreadMap::iterator sequence_thread_iter =
     66         resource_iter->second.find(reply_params.sequence());
     67     if (sequence_thread_iter != resource_iter->second.end()) {
     68       scoped_refptr<base::MessageLoopProxy> target =
     69           sequence_thread_iter->second;
     70       resource_iter->second.erase(sequence_thread_iter);
     71       return target;
     72     }
     73   }
     74 
     75   if (io_thread_message_types_.count(nested_msg.type()) != 0)
     76     return scoped_refptr<base::MessageLoopProxy>();
     77 
     78   return main_thread_;
     79 }
     80 
     81 }  // namespace proxy
     82 }  // namespace ppapi
     83