Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 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 "chrome/browser/nacl_host/nacl_host_message_filter.h" |
| 6 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 7 | #include "chrome/browser/extensions/extension_info_map.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 8 | #include "chrome/browser/nacl_host/nacl_browser.h" |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 9 | #include "chrome/browser/nacl_host/nacl_file_host.h" |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 10 | #include "chrome/browser/nacl_host/nacl_process_host.h" |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 11 | #include "chrome/browser/nacl_host/pnacl_host.h" |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 12 | #include "components/nacl/common/nacl_host_messages.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 13 | #include "extensions/common/constants.h" |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 14 | #include "net/url_request/url_request_context.h" |
| 15 | #include "net/url_request/url_request_context_getter.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 16 | |
| 17 | static base::FilePath GetManifestPath( |
| 18 | ExtensionInfoMap* extension_info_map, const std::string& manifest) { |
| 19 | GURL manifest_url(manifest); |
| 20 | const extensions::Extension* extension = extension_info_map->extensions() |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 21 | .GetExtensionOrAppByURL(manifest_url); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 22 | if (extension != NULL && |
| 23 | manifest_url.SchemeIs(extensions::kExtensionScheme)) { |
| 24 | std::string path = manifest_url.path(); |
| 25 | TrimString(path, "/", &path); // Remove first slash |
| 26 | return extension->path().AppendASCII(path); |
| 27 | } |
| 28 | return base::FilePath(); |
| 29 | } |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 30 | |
| 31 | NaClHostMessageFilter::NaClHostMessageFilter( |
| 32 | int render_process_id, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 33 | bool is_off_the_record, |
| 34 | const base::FilePath& profile_directory, |
| 35 | ExtensionInfoMap* extension_info_map, |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 36 | net::URLRequestContextGetter* request_context) |
| 37 | : render_process_id_(render_process_id), |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 38 | off_the_record_(is_off_the_record), |
| 39 | profile_directory_(profile_directory), |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 40 | request_context_(request_context), |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 41 | extension_info_map_(extension_info_map), |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 42 | weak_ptr_factory_(this) { |
| 43 | } |
| 44 | |
| 45 | NaClHostMessageFilter::~NaClHostMessageFilter() { |
| 46 | } |
| 47 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 48 | void NaClHostMessageFilter::OnChannelClosing() { |
| 49 | PnaclHost::GetInstance()->RendererClosing(render_process_id_); |
| 50 | BrowserMessageFilter::OnChannelClosing(); |
| 51 | } |
| 52 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 53 | bool NaClHostMessageFilter::OnMessageReceived(const IPC::Message& message, |
| 54 | bool* message_was_ok) { |
| 55 | bool handled = true; |
| 56 | IPC_BEGIN_MESSAGE_MAP_EX(NaClHostMessageFilter, message, *message_was_ok) |
| 57 | #if !defined(DISABLE_NACL) |
| 58 | IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClHostMsg_LaunchNaCl, OnLaunchNaCl) |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 59 | IPC_MESSAGE_HANDLER(NaClHostMsg_EnsurePnaclInstalled, |
| 60 | OnEnsurePnaclInstalled) |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 61 | IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClHostMsg_GetReadonlyPnaclFD, |
| 62 | OnGetReadonlyPnaclFd) |
| 63 | IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClHostMsg_NaClCreateTemporaryFile, |
| 64 | OnNaClCreateTemporaryFile) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 65 | IPC_MESSAGE_HANDLER(NaClHostMsg_NexeTempFileRequest, |
| 66 | OnGetNexeFd) |
| 67 | IPC_MESSAGE_HANDLER(NaClHostMsg_ReportTranslationFinished, |
| 68 | OnTranslationFinished) |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 69 | IPC_MESSAGE_HANDLER(NaClHostMsg_NaClErrorStatus, OnNaClErrorStatus) |
| 70 | IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClHostMsg_OpenNaClExecutable, |
| 71 | OnOpenNaClExecutable) |
| 72 | #endif |
| 73 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 74 | IPC_END_MESSAGE_MAP() |
| 75 | |
| 76 | return handled; |
| 77 | } |
| 78 | |
| 79 | net::HostResolver* NaClHostMessageFilter::GetHostResolver() { |
| 80 | return request_context_->GetURLRequestContext()->host_resolver(); |
| 81 | } |
| 82 | |
| 83 | #if !defined(DISABLE_NACL) |
| 84 | void NaClHostMessageFilter::OnLaunchNaCl( |
| 85 | const nacl::NaClLaunchParams& launch_params, |
| 86 | IPC::Message* reply_msg) { |
| 87 | NaClProcessHost* host = new NaClProcessHost( |
| 88 | GURL(launch_params.manifest_url), |
| 89 | launch_params.render_view_id, |
| 90 | launch_params.permission_bits, |
| 91 | launch_params.uses_irt, |
| 92 | launch_params.enable_dyncode_syscalls, |
| 93 | launch_params.enable_exception_handling, |
| 94 | off_the_record_, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 95 | profile_directory_); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 96 | base::FilePath manifest_url = |
| 97 | GetManifestPath(extension_info_map_.get(), launch_params.manifest_url); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 98 | host->Launch(this, reply_msg, manifest_url); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 101 | void NaClHostMessageFilter::ReplyEnsurePnaclInstalled( |
| 102 | int instance, |
| 103 | bool success) { |
| 104 | Send(new NaClViewMsg_EnsurePnaclInstalledReply(instance, success)); |
| 105 | } |
| 106 | |
| 107 | void NaClHostMessageFilter::SendProgressEnsurePnaclInstalled( |
| 108 | int instance, |
| 109 | const nacl::PnaclInstallProgress& progress) { |
| 110 | // TODO(jvoung): actually send an IPC. |
| 111 | } |
| 112 | |
| 113 | void NaClHostMessageFilter::OnEnsurePnaclInstalled( |
| 114 | int instance) { |
| 115 | nacl_file_host::EnsurePnaclInstalled( |
| 116 | base::Bind(&NaClHostMessageFilter::ReplyEnsurePnaclInstalled, |
| 117 | this, instance), |
| 118 | base::Bind(&NaClHostMessageFilter::SendProgressEnsurePnaclInstalled, |
| 119 | this, instance)); |
| 120 | } |
| 121 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 122 | void NaClHostMessageFilter::OnGetReadonlyPnaclFd( |
| 123 | const std::string& filename, IPC::Message* reply_msg) { |
| 124 | // This posts a task to another thread, but the renderer will |
| 125 | // block until the reply is sent. |
| 126 | nacl_file_host::GetReadonlyPnaclFd(this, filename, reply_msg); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 127 | |
| 128 | // This is the first message we receive from the renderer once it knows we |
| 129 | // want to use PNaCl, so start the translation cache initialization here. |
| 130 | PnaclHost::GetInstance()->Init(); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 133 | // Return the temporary file via a reply to the |
| 134 | // NaClHostMsg_NaClCreateTemporaryFile sync message. |
| 135 | void NaClHostMessageFilter::SyncReturnTemporaryFile( |
| 136 | IPC::Message* reply_msg, |
| 137 | IPC::PlatformFileForTransit fd) { |
| 138 | if (fd == IPC::InvalidPlatformFileForTransit()) { |
| 139 | reply_msg->set_reply_error(); |
| 140 | } else { |
| 141 | NaClHostMsg_NaClCreateTemporaryFile::WriteReplyParams( |
| 142 | reply_msg, fd); |
| 143 | } |
| 144 | Send(reply_msg); |
| 145 | } |
| 146 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 147 | void NaClHostMessageFilter::OnNaClCreateTemporaryFile( |
| 148 | IPC::Message* reply_msg) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 149 | PnaclHost::GetInstance()->CreateTemporaryFile( |
| 150 | PeerHandle(), |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 151 | base::Bind(&NaClHostMessageFilter::SyncReturnTemporaryFile, |
| 152 | this, |
| 153 | reply_msg)); |
| 154 | } |
| 155 | |
| 156 | // For now, GetNexeFd cache requests always set is_hit to false and returns |
| 157 | // a new temporary file via a NaClViewMsg_NexeTempFileReply message. |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 158 | // A future CL will implement the cache lookup logic (and use the currently- |
| 159 | // unused parameters) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 160 | // See also https://code.google.com/p/nativeclient/issues/detail?id=3372 |
| 161 | void NaClHostMessageFilter::AsyncReturnTemporaryFile( |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 162 | int pp_instance, |
| 163 | IPC::PlatformFileForTransit fd, |
| 164 | bool is_hit) { |
| 165 | Send(new NaClViewMsg_NexeTempFileReply(pp_instance, is_hit, fd)); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void NaClHostMessageFilter::OnGetNexeFd( |
| 169 | int render_view_id, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 170 | int pp_instance, |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 171 | const nacl::PnaclCacheInfo& cache_info) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 172 | if (!cache_info.pexe_url.is_valid()) { |
| 173 | LOG(ERROR) << "Bad URL received from GetNexeFd: " << |
| 174 | cache_info.pexe_url.possibly_invalid_spec(); |
| 175 | BadMessageReceived(); |
| 176 | return; |
| 177 | } |
| 178 | PnaclHost::GetInstance()->GetNexeFd( |
| 179 | render_process_id_, |
| 180 | PeerHandle(), |
| 181 | render_view_id, |
| 182 | pp_instance, |
| 183 | cache_info, |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 184 | base::Bind(&NaClHostMessageFilter::AsyncReturnTemporaryFile, |
| 185 | this, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 186 | pp_instance)); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 189 | void NaClHostMessageFilter::OnTranslationFinished(int instance, bool success) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 190 | PnaclHost::GetInstance()->TranslationFinished( |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 191 | render_process_id_, instance, success); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void NaClHostMessageFilter::OnNaClErrorStatus(int render_view_id, |
| 195 | int error_id) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 196 | NaClBrowser::GetDelegate()->ShowNaClInfobar(render_process_id_, |
| 197 | render_view_id, error_id); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void NaClHostMessageFilter::OnOpenNaClExecutable(int render_view_id, |
| 201 | const GURL& file_url, |
| 202 | IPC::Message* reply_msg) { |
| 203 | nacl_file_host::OpenNaClExecutable(this, extension_info_map_, |
| 204 | render_view_id, file_url, reply_msg); |
| 205 | } |
| 206 | #endif |