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 "base/single_thread_task_runner.h"
8 #include "components/nacl/common/nacl_host_messages.h"
9 #include "ppapi/c/pp_errors.h"
10 #include "ppapi/shared_impl/ppapi_globals.h"
12 using ppapi::PpapiGlobals
;
14 PnaclTranslationResourceHost::PnaclTranslationResourceHost(
15 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
)
16 : io_task_runner_(io_task_runner
), sender_(NULL
) {
19 PnaclTranslationResourceHost::~PnaclTranslationResourceHost() {
20 DCHECK(io_task_runner_
->BelongsToCurrentThread());
21 CleanupCacheRequests();
24 void PnaclTranslationResourceHost::OnFilterAdded(IPC::Sender
* sender
) {
25 DCHECK(io_task_runner_
->BelongsToCurrentThread());
29 void PnaclTranslationResourceHost::OnFilterRemoved() {
30 DCHECK(io_task_runner_
->BelongsToCurrentThread());
34 void PnaclTranslationResourceHost::OnChannelClosing() {
35 DCHECK(io_task_runner_
->BelongsToCurrentThread());
39 bool PnaclTranslationResourceHost::OnMessageReceived(
40 const IPC::Message
& message
) {
41 DCHECK(io_task_runner_
->BelongsToCurrentThread());
43 IPC_BEGIN_MESSAGE_MAP(PnaclTranslationResourceHost
, message
)
44 IPC_MESSAGE_HANDLER(NaClViewMsg_NexeTempFileReply
, OnNexeTempFileReply
)
45 IPC_MESSAGE_UNHANDLED(handled
= false)
50 void PnaclTranslationResourceHost::RequestNexeFd(
53 const nacl::PnaclCacheInfo
& cache_info
,
54 RequestNexeFdCallback callback
) {
55 DCHECK(PpapiGlobals::Get()->
56 GetMainThreadMessageLoop()->BelongsToCurrentThread());
57 io_task_runner_
->PostTask(
59 base::Bind(&PnaclTranslationResourceHost::SendRequestNexeFd
, this,
60 render_view_id
, instance
, cache_info
, callback
));
64 void PnaclTranslationResourceHost::SendRequestNexeFd(
67 const nacl::PnaclCacheInfo
& cache_info
,
68 RequestNexeFdCallback callback
) {
69 DCHECK(io_task_runner_
->BelongsToCurrentThread());
70 if (!sender_
|| !sender_
->Send(new NaClHostMsg_NexeTempFileRequest(
71 render_view_id
, instance
, cache_info
))) {
72 PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
75 static_cast<int32_t>(PP_ERROR_FAILED
),
77 PP_kInvalidFileHandle
));
80 pending_cache_requests_
.insert(std::make_pair(instance
, callback
));
83 void PnaclTranslationResourceHost::ReportTranslationFinished(
86 DCHECK(PpapiGlobals::Get()->
87 GetMainThreadMessageLoop()->BelongsToCurrentThread());
88 io_task_runner_
->PostTask(
90 base::Bind(&PnaclTranslationResourceHost::SendReportTranslationFinished
,
91 this, instance
, success
));
95 void PnaclTranslationResourceHost::SendReportTranslationFinished(
98 DCHECK(io_task_runner_
->BelongsToCurrentThread());
99 // If the sender is closed or we have been detached, we are probably shutting
100 // down, so just don't send anything.
103 DCHECK(pending_cache_requests_
.count(instance
) == 0);
104 sender_
->Send(new NaClHostMsg_ReportTranslationFinished(instance
,
105 PP_ToBool(success
)));
108 void PnaclTranslationResourceHost::OnNexeTempFileReply(
109 PP_Instance instance
,
111 IPC::PlatformFileForTransit file
) {
112 DCHECK(io_task_runner_
->BelongsToCurrentThread());
113 base::File base_file
= IPC::PlatformFileForTransitToFile(file
);
114 CacheRequestInfoMap::iterator it
= pending_cache_requests_
.find(instance
);
115 if (!base_file
.IsValid()) {
116 DLOG(ERROR
) << "Got invalid platformfilefortransit";
118 if (it
!= pending_cache_requests_
.end()) {
119 PP_FileHandle file_handle
= PP_kInvalidFileHandle
;
120 int32_t status
= PP_ERROR_FAILED
;
121 if (base_file
.IsValid()) {
122 file_handle
= base_file
.TakePlatformFile();
125 PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
127 base::Bind(it
->second
, status
, is_hit
, file_handle
));
128 pending_cache_requests_
.erase(it
);
130 DLOG(ERROR
) << "Could not find pending request for reply";
134 void PnaclTranslationResourceHost::CleanupCacheRequests() {
135 DCHECK(io_task_runner_
->BelongsToCurrentThread());
136 for (CacheRequestInfoMap::iterator it
= pending_cache_requests_
.begin();
137 it
!= pending_cache_requests_
.end();
139 PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
141 base::Bind(it
->second
,
142 static_cast<int32_t>(PP_ERROR_ABORTED
),
144 PP_kInvalidFileHandle
));
146 pending_cache_requests_
.clear();