| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "pnacl_translation_resource_host.h" | 5 #include "pnacl_translation_resource_host.h" |
| 6 | 6 |
| 7 #include "base/single_thread_task_runner.h" | |
| 8 #include "components/nacl/common/nacl_host_messages.h" | |
| 9 #include "ppapi/c/pp_errors.h" | 7 #include "ppapi/c/pp_errors.h" |
| 10 #include "ppapi/shared_impl/ppapi_globals.h" | 8 #include "ppapi/shared_impl/ppapi_globals.h" |
| 11 | 9 |
| 12 using ppapi::PpapiGlobals; | 10 using ppapi::PpapiGlobals; |
| 13 | 11 |
| 14 PnaclTranslationResourceHost::PnaclTranslationResourceHost( | 12 namespace { |
| 15 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) | 13 |
| 16 : io_task_runner_(io_task_runner), sender_(NULL) { | 14 void OnNexeTempFileReply( |
| 15 const PnaclTranslationResourceHost::RequestNexeFdCallback& callback, |
| 16 PP_Instance instance, |
| 17 base::File file, |
| 18 bool is_hit) { |
| 19 DCHECK(PpapiGlobals::Get() |
| 20 ->GetMainThreadMessageLoop() |
| 21 ->BelongsToCurrentThread()); |
| 22 |
| 23 callback.Run(instance, is_hit, file.TakePlatformFile()); |
| 17 } | 24 } |
| 18 | 25 |
| 19 PnaclTranslationResourceHost::~PnaclTranslationResourceHost() { | 26 } // namespace |
| 20 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 27 |
| 21 CleanupCacheRequests(); | 28 PnaclTranslationResourceHost::PnaclTranslationResourceHost( |
| 29 nacl::mojom::NaClHost* host) |
| 30 : host_(host) { |
| 31 DCHECK(host_); |
| 22 } | 32 } |
| 23 | 33 |
| 24 void PnaclTranslationResourceHost::OnFilterAdded(IPC::Channel* channel) { | 34 PnaclTranslationResourceHost::~PnaclTranslationResourceHost() = default; |
| 25 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 26 sender_ = channel; | |
| 27 } | |
| 28 | |
| 29 void PnaclTranslationResourceHost::OnFilterRemoved() { | |
| 30 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 31 sender_ = NULL; | |
| 32 } | |
| 33 | |
| 34 void PnaclTranslationResourceHost::OnChannelClosing() { | |
| 35 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 36 sender_ = NULL; | |
| 37 } | |
| 38 | |
| 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; | |
| 48 } | |
| 49 | 35 |
| 50 void PnaclTranslationResourceHost::RequestNexeFd( | 36 void PnaclTranslationResourceHost::RequestNexeFd( |
| 51 int render_view_id, | 37 int render_view_id, |
| 52 PP_Instance instance, | 38 PP_Instance instance, |
| 53 const nacl::PnaclCacheInfo& cache_info, | 39 const nacl::PnaclCacheInfo& cache_info, |
| 54 RequestNexeFdCallback callback) { | 40 RequestNexeFdCallback callback) { |
| 55 DCHECK(PpapiGlobals::Get()-> | 41 DCHECK(PpapiGlobals::Get() |
| 56 GetMainThreadMessageLoop()->BelongsToCurrentThread()); | 42 ->GetMainThreadMessageLoop() |
| 57 io_task_runner_->PostTask( | 43 ->BelongsToCurrentThread()); |
| 58 FROM_HERE, | 44 host_->NexeTempFileRequest(render_view_id, instance, cache_info, |
| 59 base::Bind(&PnaclTranslationResourceHost::SendRequestNexeFd, this, | 45 base::Bind(&OnNexeTempFileReply, callback)); |
| 60 render_view_id, instance, cache_info, callback)); | |
| 61 return; | |
| 62 } | |
| 63 | |
| 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; | |
| 79 } | |
| 80 pending_cache_requests_.insert(std::make_pair(instance, callback)); | |
| 81 } | 46 } |
| 82 | 47 |
| 83 void PnaclTranslationResourceHost::ReportTranslationFinished( | 48 void PnaclTranslationResourceHost::ReportTranslationFinished( |
| 84 PP_Instance instance, | 49 PP_Instance instance, |
| 85 PP_Bool success) { | 50 PP_Bool success) { |
| 86 DCHECK(PpapiGlobals::Get()-> | 51 DCHECK(PpapiGlobals::Get()-> |
| 87 GetMainThreadMessageLoop()->BelongsToCurrentThread()); | 52 GetMainThreadMessageLoop()->BelongsToCurrentThread()); |
| 88 io_task_runner_->PostTask( | 53 host_->ReportTranslationFinished(instance, PP_ToBool(success)); |
| 89 FROM_HERE, | |
| 90 base::Bind(&PnaclTranslationResourceHost::SendReportTranslationFinished, | |
| 91 this, instance, success)); | |
| 92 return; | |
| 93 } | 54 } |
| 94 | |
| 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))); | |
| 106 } | |
| 107 | |
| 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"; | |
| 117 } | |
| 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; | |
| 124 } | |
| 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"; | |
| 131 } | |
| 132 } | |
| 133 | |
| 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)); | |
| 145 } | |
| 146 pending_cache_requests_.clear(); | |
| 147 } | |
| OLD | NEW |