This source file includes following definitions.
- NotifyRendererOfError
- PnaclDoOpenFile
- DoOpenPnaclFile
- DoRegisterOpenedNaClExecutableFile
- DoOpenNaClExecutableOnThreadPool
- GetReadonlyPnaclFd
- PnaclCanOpenFile
- OpenNaClExecutable
#include "components/nacl/browser/nacl_file_host.h"
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/sequenced_worker_pool.h"
#include "components/nacl/browser/nacl_browser.h"
#include "components/nacl/browser/nacl_browser_delegate.h"
#include "components/nacl/browser/nacl_host_message_filter.h"
#include "components/nacl/common/nacl_host_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/site_instance.h"
#include "ipc/ipc_platform_file.h"
using content::BrowserThread;
namespace {
const char* kExpectedFilePrefix = "pnacl_public_";
const size_t kMaxFileLength = 40;
void NotifyRendererOfError(
nacl::NaClHostMessageFilter* nacl_host_message_filter,
IPC::Message* reply_msg) {
reply_msg->set_reply_error();
nacl_host_message_filter->Send(reply_msg);
}
base::File PnaclDoOpenFile(const base::FilePath& file_to_open) {
return base::File(file_to_open,
base::File::FLAG_OPEN | base::File::FLAG_READ);
}
void DoOpenPnaclFile(
scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter,
const std::string& filename,
IPC::Message* reply_msg) {
DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
base::FilePath full_filepath;
base::FilePath pnacl_dir;
if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) ||
!base::PathExists(pnacl_dir)) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
if (!nacl_file_host::PnaclCanOpenFile(filename, &full_filepath)) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
base::File file_to_open = PnaclDoOpenFile(full_filepath);
if (!file_to_open.IsValid()) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
IPC::PlatformFileForTransit target_desc =
IPC::TakeFileHandleForProcess(file_to_open.Pass(),
nacl_host_message_filter->PeerHandle());
if (target_desc == IPC::InvalidPlatformFileForTransit()) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
NaClHostMsg_GetReadonlyPnaclFD::WriteReplyParams(
reply_msg, target_desc);
nacl_host_message_filter->Send(reply_msg);
}
void DoRegisterOpenedNaClExecutableFile(
scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter,
base::File file,
base::FilePath file_path,
IPC::Message* reply_msg) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
nacl::NaClBrowser* nacl_browser = nacl::NaClBrowser::GetInstance();
uint64 file_token_lo = 0;
uint64 file_token_hi = 0;
nacl_browser->PutFilePath(file_path, &file_token_lo, &file_token_hi);
IPC::PlatformFileForTransit file_desc = IPC::TakeFileHandleForProcess(
file.Pass(),
nacl_host_message_filter->PeerHandle());
NaClHostMsg_OpenNaClExecutable::WriteReplyParams(
reply_msg, file_desc, file_token_lo, file_token_hi);
nacl_host_message_filter->Send(reply_msg);
}
void DoOpenNaClExecutableOnThreadPool(
scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter,
const GURL& file_url,
IPC::Message* reply_msg) {
DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
base::FilePath file_path;
if (!nacl::NaClBrowser::GetDelegate()->MapUrlToLocalFilePath(
file_url, true , &file_path)) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
base::File file = nacl::OpenNaClExecutableImpl(file_path);
if (file.IsValid()) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(
&DoRegisterOpenedNaClExecutableFile,
nacl_host_message_filter,
Passed(file.Pass()), file_path, reply_msg));
} else {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
}
}
namespace nacl_file_host {
void GetReadonlyPnaclFd(
scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter,
const std::string& filename,
IPC::Message* reply_msg) {
if (!BrowserThread::PostBlockingPoolTask(
FROM_HERE,
base::Bind(&DoOpenPnaclFile,
nacl_host_message_filter,
filename,
reply_msg))) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
}
}
bool PnaclCanOpenFile(const std::string& filename,
base::FilePath* file_to_open) {
if (filename.length() > kMaxFileLength)
return false;
if (filename.empty())
return false;
for (size_t i = 0; i < filename.length(); ++i) {
char charAt = filename[i];
if (charAt < 'a' || charAt > 'z')
if (charAt < '0' || charAt > '9')
if (charAt != '_')
return false;
}
base::FilePath pnacl_dir;
if (!nacl::NaClBrowser::GetDelegate()->GetPnaclDirectory(&pnacl_dir) ||
pnacl_dir.empty())
return false;
base::FilePath full_path = pnacl_dir.AppendASCII(
std::string(kExpectedFilePrefix) + filename);
*file_to_open = full_path;
return true;
}
void OpenNaClExecutable(
scoped_refptr<nacl::NaClHostMessageFilter> nacl_host_message_filter,
int render_view_id,
const GURL& file_url,
IPC::Message* reply_msg) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(
&OpenNaClExecutable,
nacl_host_message_filter,
render_view_id, file_url, reply_msg));
return;
}
content::RenderViewHost* rvh = content::RenderViewHost::FromID(
nacl_host_message_filter->render_process_id(), render_view_id);
if (!rvh) {
nacl_host_message_filter->BadMessageReceived();
return;
}
content::SiteInstance* site_instance = rvh->GetSiteInstance();
if (!content::SiteInstance::IsSameWebSite(site_instance->GetBrowserContext(),
site_instance->GetSiteURL(),
file_url)) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
return;
}
if (!BrowserThread::PostBlockingPoolTask(
FROM_HERE,
base::Bind(
&DoOpenNaClExecutableOnThreadPool,
nacl_host_message_filter,
file_url, reply_msg))) {
NotifyRendererOfError(nacl_host_message_filter.get(), reply_msg);
}
}
}