Don't preload rarely seen large images
[chromium-blink-merge.git] / components / nacl / renderer / pnacl_translation_resource_host.cc
blobb65850cc2a3a0962119ed8726dc7896b5b2dfebe
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());
26 sender_ = sender;
29 void PnaclTranslationResourceHost::OnFilterRemoved() {
30 DCHECK(io_task_runner_->BelongsToCurrentThread());
31 sender_ = NULL;
34 void PnaclTranslationResourceHost::OnChannelClosing() {
35 DCHECK(io_task_runner_->BelongsToCurrentThread());
36 sender_ = NULL;
39 bool PnaclTranslationResourceHost::OnMessageReceived(
40 const IPC::Message& message) {
41 DCHECK(io_task_runner_->BelongsToCurrentThread());
42 bool handled = true;
43 IPC_BEGIN_MESSAGE_MAP(PnaclTranslationResourceHost, message)
44 IPC_MESSAGE_HANDLER(NaClViewMsg_NexeTempFileReply, OnNexeTempFileReply)
45 IPC_MESSAGE_UNHANDLED(handled = false)
46 IPC_END_MESSAGE_MAP()
47 return handled;
50 void PnaclTranslationResourceHost::RequestNexeFd(
51 int render_view_id,
52 PP_Instance instance,
53 const nacl::PnaclCacheInfo& cache_info,
54 RequestNexeFdCallback callback) {
55 DCHECK(PpapiGlobals::Get()->
56 GetMainThreadMessageLoop()->BelongsToCurrentThread());
57 io_task_runner_->PostTask(
58 FROM_HERE,
59 base::Bind(&PnaclTranslationResourceHost::SendRequestNexeFd, this,
60 render_view_id, instance, cache_info, callback));
61 return;
64 void PnaclTranslationResourceHost::SendRequestNexeFd(
65 int render_view_id,
66 PP_Instance instance,
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(
73 FROM_HERE,
74 base::Bind(callback,
75 static_cast<int32_t>(PP_ERROR_FAILED),
76 false,
77 PP_kInvalidFileHandle));
78 return;
80 pending_cache_requests_.insert(std::make_pair(instance, callback));
83 void PnaclTranslationResourceHost::ReportTranslationFinished(
84 PP_Instance instance,
85 PP_Bool success) {
86 DCHECK(PpapiGlobals::Get()->
87 GetMainThreadMessageLoop()->BelongsToCurrentThread());
88 io_task_runner_->PostTask(
89 FROM_HERE,
90 base::Bind(&PnaclTranslationResourceHost::SendReportTranslationFinished,
91 this, instance, success));
92 return;
95 void PnaclTranslationResourceHost::SendReportTranslationFinished(
96 PP_Instance instance,
97 PP_Bool success) {
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.
101 if (!sender_)
102 return;
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,
110 bool is_hit,
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();
123 status = PP_OK;
125 PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
126 FROM_HERE,
127 base::Bind(it->second, status, is_hit, file_handle));
128 pending_cache_requests_.erase(it);
129 } else {
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();
138 ++it) {
139 PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
140 FROM_HERE,
141 base::Bind(it->second,
142 static_cast<int32_t>(PP_ERROR_ABORTED),
143 false,
144 PP_kInvalidFileHandle));
146 pending_cache_requests_.clear();