1// Copyright (c) 2012 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 "ppapi/native_client/src/trusted/plugin/pnacl_resources.h"
6
7#include "native_client/src/include/portability_io.h"
8#include "native_client/src/shared/platform/nacl_check.h"
9#include "native_client/src/trusted/desc/nacl_desc_wrapper.h"
10#include "ppapi/c/pp_errors.h"
11#include "ppapi/native_client/src/trusted/plugin/plugin.h"
12#include "ppapi/native_client/src/trusted/plugin/utility.h"
13
14namespace plugin {
15
16namespace {
17
18static const char kPnaclBaseUrl[] = "chrome://pnacl-translator/";
19
20std::string GetFullUrl(const std::string& partial_url) {
21  return std::string(kPnaclBaseUrl) + GetNaClInterface()->GetSandboxArch() +
22         "/" + partial_url;
23}
24
25}  // namespace
26
27PnaclResources::PnaclResources(Plugin* plugin)
28    : plugin_(plugin) {
29  llc_file_info_ = kInvalidNaClFileInfo;
30  ld_file_info_ = kInvalidNaClFileInfo;
31}
32
33PnaclResources::~PnaclResources() {
34  if (llc_file_info_.handle != PP_kInvalidFileHandle)
35    CloseFileHandle(llc_file_info_.handle);
36  if (ld_file_info_.handle != PP_kInvalidFileHandle)
37    CloseFileHandle(ld_file_info_.handle);
38}
39
40bool PnaclResources::ReadResourceInfo() {
41  PP_Var pp_llc_tool_name_var;
42  PP_Var pp_ld_tool_name_var;
43  if (!plugin_->nacl_interface()->GetPnaclResourceInfo(
44          plugin_->pp_instance(),
45          &pp_llc_tool_name_var,
46          &pp_ld_tool_name_var)) {
47    return false;
48  }
49  pp::Var llc_tool_name(pp::PASS_REF, pp_llc_tool_name_var);
50  pp::Var ld_tool_name(pp::PASS_REF, pp_ld_tool_name_var);
51  llc_tool_name_ = GetFullUrl(llc_tool_name.AsString());
52  ld_tool_name_ = GetFullUrl(ld_tool_name.AsString());
53  return true;
54}
55
56PP_NaClFileInfo PnaclResources::TakeLlcFileInfo() {
57  PP_NaClFileInfo to_return = llc_file_info_;
58  llc_file_info_ = kInvalidNaClFileInfo;
59  return to_return;
60}
61
62PP_NaClFileInfo PnaclResources::TakeLdFileInfo() {
63  PP_NaClFileInfo to_return = ld_file_info_;
64  ld_file_info_ = kInvalidNaClFileInfo;
65  return to_return;
66}
67
68bool PnaclResources::StartLoad() {
69  PLUGIN_PRINTF(("PnaclResources::StartLoad\n"));
70
71  // Do a blocking load of each of the resources.
72  plugin_->nacl_interface()->GetReadExecPnaclFd(llc_tool_name_.c_str(),
73                                                &llc_file_info_);
74  plugin_->nacl_interface()->GetReadExecPnaclFd(ld_tool_name_.c_str(),
75                                                &ld_file_info_);
76  return (llc_file_info_.handle != PP_kInvalidFileHandle &&
77          ld_file_info_.handle != PP_kInvalidFileHandle);
78}
79
80}  // namespace plugin
81