blob: c7c79358b27498435f60354f65f69b9408c163b0 [file] [log] [blame]
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +01001// 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)7d4cd472013-06-19 11:58:07 +01007#include "chrome/browser/extensions/extension_info_map.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01008#include "chrome/browser/nacl_host/nacl_browser.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +01009#include "chrome/browser/nacl_host/nacl_file_host.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010010#include "chrome/browser/nacl_host/nacl_process_host.h"
Ben Murdochca12bfa2013-07-23 11:17:05 +010011#include "chrome/browser/nacl_host/pnacl_host.h"
Ben Murdocha3f7b4e2013-07-24 10:36:34 +010012#include "components/nacl/common/nacl_host_messages.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010013#include "extensions/common/constants.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010014#include "net/url_request/url_request_context.h"
15#include "net/url_request/url_request_context_getter.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010016
17static 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 Murdochca12bfa2013-07-23 11:17:05 +010021 .GetExtensionOrAppByURL(manifest_url);
Ben Murdocheb525c52013-07-10 11:40:50 +010022 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)7d4cd472013-06-19 11:58:07 +010030
31NaClHostMessageFilter::NaClHostMessageFilter(
32 int render_process_id,
Ben Murdocheb525c52013-07-10 11:40:50 +010033 bool is_off_the_record,
34 const base::FilePath& profile_directory,
35 ExtensionInfoMap* extension_info_map,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010036 net::URLRequestContextGetter* request_context)
37 : render_process_id_(render_process_id),
Ben Murdocheb525c52013-07-10 11:40:50 +010038 off_the_record_(is_off_the_record),
39 profile_directory_(profile_directory),
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010040 request_context_(request_context),
Ben Murdocheb525c52013-07-10 11:40:50 +010041 extension_info_map_(extension_info_map),
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010042 weak_ptr_factory_(this) {
43}
44
45NaClHostMessageFilter::~NaClHostMessageFilter() {
46}
47
Ben Murdochca12bfa2013-07-23 11:17:05 +010048void NaClHostMessageFilter::OnChannelClosing() {
49 PnaclHost::GetInstance()->RendererClosing(render_process_id_);
50 BrowserMessageFilter::OnChannelClosing();
51}
52
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010053bool 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 Murdoch2385ea32013-08-06 11:01:04 +010059 IPC_MESSAGE_HANDLER(NaClHostMsg_EnsurePnaclInstalled,
60 OnEnsurePnaclInstalled)
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010061 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClHostMsg_GetReadonlyPnaclFD,
62 OnGetReadonlyPnaclFd)
63 IPC_MESSAGE_HANDLER_DELAY_REPLY(NaClHostMsg_NaClCreateTemporaryFile,
64 OnNaClCreateTemporaryFile)
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010065 IPC_MESSAGE_HANDLER(NaClHostMsg_NexeTempFileRequest,
66 OnGetNexeFd)
67 IPC_MESSAGE_HANDLER(NaClHostMsg_ReportTranslationFinished,
68 OnTranslationFinished)
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010069 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
79net::HostResolver* NaClHostMessageFilter::GetHostResolver() {
80 return request_context_->GetURLRequestContext()->host_resolver();
81}
82
83#if !defined(DISABLE_NACL)
84void 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 Murdocheb525c52013-07-10 11:40:50 +010095 profile_directory_);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010096 base::FilePath manifest_url =
97 GetManifestPath(extension_info_map_.get(), launch_params.manifest_url);
Ben Murdocheb525c52013-07-10 11:40:50 +010098 host->Launch(this, reply_msg, manifest_url);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010099}
100
Ben Murdoch2385ea32013-08-06 11:01:04 +0100101void NaClHostMessageFilter::ReplyEnsurePnaclInstalled(
102 int instance,
103 bool success) {
104 Send(new NaClViewMsg_EnsurePnaclInstalledReply(instance, success));
105}
106
107void NaClHostMessageFilter::SendProgressEnsurePnaclInstalled(
108 int instance,
109 const nacl::PnaclInstallProgress& progress) {
110 // TODO(jvoung): actually send an IPC.
111}
112
113void 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)7d4cd472013-06-19 11:58:07 +0100122void 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 Murdochca12bfa2013-07-23 11:17:05 +0100127
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)7d4cd472013-06-19 11:58:07 +0100131}
132
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100133// Return the temporary file via a reply to the
134// NaClHostMsg_NaClCreateTemporaryFile sync message.
135void 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)7d4cd472013-06-19 11:58:07 +0100147void NaClHostMessageFilter::OnNaClCreateTemporaryFile(
148 IPC::Message* reply_msg) {
Ben Murdochca12bfa2013-07-23 11:17:05 +0100149 PnaclHost::GetInstance()->CreateTemporaryFile(
150 PeerHandle(),
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100151 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 Murdochca12bfa2013-07-23 11:17:05 +0100158// A future CL will implement the cache lookup logic (and use the currently-
159// unused parameters)
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100160// See also https://code.google.com/p/nativeclient/issues/detail?id=3372
161void NaClHostMessageFilter::AsyncReturnTemporaryFile(
Ben Murdochca12bfa2013-07-23 11:17:05 +0100162 int pp_instance,
163 IPC::PlatformFileForTransit fd,
164 bool is_hit) {
165 Send(new NaClViewMsg_NexeTempFileReply(pp_instance, is_hit, fd));
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100166}
167
168void NaClHostMessageFilter::OnGetNexeFd(
169 int render_view_id,
Ben Murdochca12bfa2013-07-23 11:17:05 +0100170 int pp_instance,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100171 const nacl::PnaclCacheInfo& cache_info) {
Ben Murdochca12bfa2013-07-23 11:17:05 +0100172 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 Murdoch7dbb3d52013-07-17 14:55:54 +0100184 base::Bind(&NaClHostMessageFilter::AsyncReturnTemporaryFile,
185 this,
Ben Murdochca12bfa2013-07-23 11:17:05 +0100186 pp_instance));
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100187}
188
Ben Murdochbb1529c2013-08-08 10:24:53 +0100189void NaClHostMessageFilter::OnTranslationFinished(int instance, bool success) {
Ben Murdochca12bfa2013-07-23 11:17:05 +0100190 PnaclHost::GetInstance()->TranslationFinished(
Ben Murdochbb1529c2013-08-08 10:24:53 +0100191 render_process_id_, instance, success);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100192}
193
194void NaClHostMessageFilter::OnNaClErrorStatus(int render_view_id,
195 int error_id) {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100196 NaClBrowser::GetDelegate()->ShowNaClInfobar(render_process_id_,
197 render_view_id, error_id);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100198}
199
200void 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