1 // Copyright 2012 The Chromium Authors
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 "components/nacl/renderer/plugin/pnacl_resources.h"
6
7 #include <stddef.h>
8
9 #include <vector>
10
11 #include "base/check.h"
12 #include "base/files/file.h"
13 #include "base/notreached.h"
14 #include "components/nacl/renderer/plugin/plugin.h"
15 #include "ppapi/c/pp_errors.h"
16
17 namespace plugin {
18
19 namespace {
20
21 static const char kPnaclBaseUrl[] = "chrome://pnacl-translator/";
22
GetFullUrl(const std::string & partial_url)23 std::string GetFullUrl(const std::string& partial_url) {
24 return std::string(kPnaclBaseUrl) + nacl::PPBNaClPrivate::GetSandboxArch() +
25 "/" + partial_url;
26 }
27
28 } // namespace
29
PnaclResources(Plugin * plugin,bool use_subzero)30 PnaclResources::PnaclResources(Plugin* plugin, bool use_subzero)
31 : plugin_(plugin), use_subzero_(use_subzero) {
32 for (PnaclResourceEntry& entry : resources_) {
33 entry.file_info = kInvalidNaClFileInfo;
34 }
35 }
36
~PnaclResources()37 PnaclResources::~PnaclResources() {
38 for (PnaclResourceEntry& entry : resources_) {
39 base::File closer(entry.file_info.handle);
40 }
41 }
42
GetUrl(ResourceType type) const43 const std::string& PnaclResources::GetUrl(ResourceType type) const {
44 size_t index = static_cast<size_t>(type);
45 DCHECK(index < NUM_TYPES);
46 return resources_[index].tool_name;
47 }
48
TakeFileInfo(ResourceType type)49 PP_NaClFileInfo PnaclResources::TakeFileInfo(ResourceType type) {
50 size_t index = static_cast<size_t>(type);
51 if (index >= NUM_TYPES) {
52 NOTREACHED();
53 return kInvalidNaClFileInfo;
54 }
55 PP_NaClFileInfo to_return = resources_[index].file_info;
56 resources_[index].file_info = kInvalidNaClFileInfo;
57 return to_return;
58 }
59
ReadResourceInfo()60 bool PnaclResources::ReadResourceInfo() {
61 PP_Var pp_llc_tool_name_var;
62 PP_Var pp_ld_tool_name_var;
63 PP_Var pp_subzero_tool_name_var;
64 if (!nacl::PPBNaClPrivate::GetPnaclResourceInfo(
65 plugin_->pp_instance(), &pp_llc_tool_name_var, &pp_ld_tool_name_var,
66 &pp_subzero_tool_name_var)) {
67 return false;
68 }
69 pp::Var llc_tool_name(pp::PASS_REF, pp_llc_tool_name_var);
70 pp::Var ld_tool_name(pp::PASS_REF, pp_ld_tool_name_var);
71 pp::Var subzero_tool_name(pp::PASS_REF, pp_subzero_tool_name_var);
72 resources_[LLC].tool_name = GetFullUrl(llc_tool_name.AsString());
73 resources_[LD].tool_name = GetFullUrl(ld_tool_name.AsString());
74 resources_[SUBZERO].tool_name = GetFullUrl(subzero_tool_name.AsString());
75 return true;
76 }
77
78
StartLoad()79 bool PnaclResources::StartLoad() {
80 // Do a blocking load of each of the resources.
81 std::vector<ResourceType> to_load;
82 if (use_subzero_) {
83 to_load.push_back(SUBZERO);
84 } else {
85 to_load.push_back(LLC);
86 }
87 to_load.push_back(LD);
88 bool all_valid = true;
89 for (ResourceType t : to_load) {
90 nacl::PPBNaClPrivate::GetReadExecPnaclFd(
91 resources_[t].tool_name.c_str(), &resources_[t].file_info);
92 all_valid =
93 all_valid && resources_[t].file_info.handle != PP_kInvalidFileHandle;
94 }
95 return all_valid;
96 }
97
98 } // namespace plugin
99