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 "pnacl_translation_resource_host.h"
7 #include "components/nacl/common/nacl_host_messages.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/shared_impl/ppapi_globals.h"
11 using ppapi::PpapiGlobals;
13 PnaclTranslationResourceHost::PnaclTranslationResourceHost(
14 const scoped_refptr<base::MessageLoopProxy>& io_message_loop)
15 : io_message_loop_(io_message_loop), sender_(NULL) {}
17 PnaclTranslationResourceHost::~PnaclTranslationResourceHost() {
18 DCHECK(io_message_loop_->BelongsToCurrentThread());
19 CleanupCacheRequests();
22 void PnaclTranslationResourceHost::OnFilterAdded(IPC::Sender* sender) {
23 DCHECK(io_message_loop_->BelongsToCurrentThread());
27 void PnaclTranslationResourceHost::OnFilterRemoved() {
28 DCHECK(io_message_loop_->BelongsToCurrentThread());
32 void PnaclTranslationResourceHost::OnChannelClosing() {
33 DCHECK(io_message_loop_->BelongsToCurrentThread());
37 bool PnaclTranslationResourceHost::OnMessageReceived(
38 const IPC::Message& message) {
39 DCHECK(io_message_loop_->BelongsToCurrentThread());
41 IPC_BEGIN_MESSAGE_MAP(PnaclTranslationResourceHost, message)
42 IPC_MESSAGE_HANDLER(NaClViewMsg_NexeTempFileReply, OnNexeTempFileReply)
43 IPC_MESSAGE_UNHANDLED(handled = false)
48 void PnaclTranslationResourceHost::RequestNexeFd(
51 const nacl::PnaclCacheInfo& cache_info,
52 RequestNexeFdCallback callback) {
53 DCHECK(PpapiGlobals::Get()->
54 GetMainThreadMessageLoop()->BelongsToCurrentThread());
55 io_message_loop_->PostTask(
57 base::Bind(&PnaclTranslationResourceHost::SendRequestNexeFd,
66 void PnaclTranslationResourceHost::SendRequestNexeFd(
69 const nacl::PnaclCacheInfo& cache_info,
70 RequestNexeFdCallback callback) {
71 DCHECK(io_message_loop_->BelongsToCurrentThread());
72 if (!sender_ || !sender_->Send(new NaClHostMsg_NexeTempFileRequest(
73 render_view_id, instance, cache_info))) {
74 PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
77 static_cast<int32_t>(PP_ERROR_FAILED),
79 PP_kInvalidFileHandle));
82 pending_cache_requests_.insert(std::make_pair(instance, callback));
85 void PnaclTranslationResourceHost::ReportTranslationFinished(
88 DCHECK(PpapiGlobals::Get()->
89 GetMainThreadMessageLoop()->BelongsToCurrentThread());
90 io_message_loop_->PostTask(
92 base::Bind(&PnaclTranslationResourceHost::SendReportTranslationFinished,
99 void PnaclTranslationResourceHost::SendReportTranslationFinished(
100 PP_Instance instance,
102 DCHECK(io_message_loop_->BelongsToCurrentThread());
103 // If the sender is closed or we have been detached, we are probably shutting
104 // down, so just don't send anything.
107 DCHECK(pending_cache_requests_.count(instance) == 0);
108 sender_->Send(new NaClHostMsg_ReportTranslationFinished(instance,
109 PP_ToBool(success)));
112 void PnaclTranslationResourceHost::OnNexeTempFileReply(
113 PP_Instance instance,
115 IPC::PlatformFileForTransit file) {
116 DCHECK(io_message_loop_->BelongsToCurrentThread());
117 base::File base_file = IPC::PlatformFileForTransitToFile(file);
118 CacheRequestInfoMap::iterator it = pending_cache_requests_.find(instance);
119 int32_t status = PP_ERROR_FAILED;
120 // Handle the expected successful case first.
121 PP_FileHandle file_handle = PP_kInvalidFileHandle;
122 if (it != pending_cache_requests_.end() && base_file.IsValid()) {
123 file_handle = base_file.TakePlatformFile();
126 if (it == pending_cache_requests_.end()) {
127 DLOG(ERROR) << "Could not find pending request for reply";
129 PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
131 base::Bind(it->second, status, is_hit, file_handle));
132 pending_cache_requests_.erase(it);
134 if (!base_file.IsValid()) {
135 DLOG(ERROR) << "Got invalid platformfilefortransit";
139 void PnaclTranslationResourceHost::CleanupCacheRequests() {
140 DCHECK(io_message_loop_->BelongsToCurrentThread());
141 for (CacheRequestInfoMap::iterator it = pending_cache_requests_.begin();
142 it != pending_cache_requests_.end();
144 PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
146 base::Bind(it->second,
147 static_cast<int32_t>(PP_ERROR_ABORTED),
149 PP_kInvalidFileHandle));
151 pending_cache_requests_.clear();