Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / nacl / renderer / plugin / pnacl_resources.cc
blob9e65d6974d4e90e2155c3ddf0509c23ef1839fe1
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.
5 #include "components/nacl/renderer/plugin/pnacl_resources.h"
7 #include <vector>
9 #include "components/nacl/renderer/plugin/plugin.h"
10 #include "components/nacl/renderer/plugin/utility.h"
11 #include "native_client/src/include/portability_io.h"
12 #include "native_client/src/shared/platform/nacl_check.h"
13 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h"
14 #include "ppapi/c/pp_errors.h"
16 namespace plugin {
18 namespace {
20 static const char kPnaclBaseUrl[] = "chrome://pnacl-translator/";
22 std::string GetFullUrl(const std::string& partial_url) {
23 return std::string(kPnaclBaseUrl) + GetNaClInterface()->GetSandboxArch() +
24 "/" + partial_url;
27 } // namespace
29 PnaclResources::PnaclResources(Plugin* plugin, bool use_subzero)
30 : plugin_(plugin), use_subzero_(use_subzero) {
31 for (PnaclResourceEntry& entry : resources_) {
32 entry.file_info = kInvalidNaClFileInfo;
36 PnaclResources::~PnaclResources() {
37 for (PnaclResourceEntry& entry : resources_) {
38 if (entry.file_info.handle != PP_kInvalidFileHandle)
39 CloseFileHandle(entry.file_info.handle);
43 const std::string& PnaclResources::GetUrl(ResourceType type) const {
44 size_t index = static_cast<size_t>(type);
45 if (index < NUM_TYPES) {
46 return resources_[index].tool_name;
48 // TODO(jvoung): Use NOTREACHED() from base/logging.h once
49 // we are able to use base/logging.h without conflicting
50 // with NaCl macros.
51 DCHECK(false && "Index out of bounds");
52 // Return a dummy tool name.
53 return resources_[index].tool_name;
56 PP_NaClFileInfo PnaclResources::TakeFileInfo(ResourceType type) {
57 size_t index = static_cast<size_t>(type);
58 if (index >= NUM_TYPES) {
59 DCHECK(false && "Index out of bounds");
60 return kInvalidNaClFileInfo;
62 PP_NaClFileInfo to_return = resources_[index].file_info;
63 resources_[index].file_info = kInvalidNaClFileInfo;
64 return to_return;
67 bool PnaclResources::ReadResourceInfo() {
68 PP_Var pp_llc_tool_name_var;
69 PP_Var pp_ld_tool_name_var;
70 PP_Var pp_subzero_tool_name_var;
71 if (!plugin_->nacl_interface()->GetPnaclResourceInfo(
72 plugin_->pp_instance(), &pp_llc_tool_name_var, &pp_ld_tool_name_var,
73 &pp_subzero_tool_name_var)) {
74 return false;
76 pp::Var llc_tool_name(pp::PASS_REF, pp_llc_tool_name_var);
77 pp::Var ld_tool_name(pp::PASS_REF, pp_ld_tool_name_var);
78 pp::Var subzero_tool_name(pp::PASS_REF, pp_subzero_tool_name_var);
79 resources_[LLC].tool_name = GetFullUrl(llc_tool_name.AsString());
80 resources_[LD].tool_name = GetFullUrl(ld_tool_name.AsString());
81 resources_[SUBZERO].tool_name = GetFullUrl(subzero_tool_name.AsString());
82 return true;
86 bool PnaclResources::StartLoad() {
87 PLUGIN_PRINTF(("PnaclResources::StartLoad\n"));
89 // Do a blocking load of each of the resources.
90 std::vector<ResourceType> to_load;
91 if (use_subzero_) {
92 to_load.push_back(SUBZERO);
93 } else {
94 to_load.push_back(LLC);
96 to_load.push_back(LD);
97 bool all_valid = true;
98 for (ResourceType t : to_load) {
99 plugin_->nacl_interface()->GetReadExecPnaclFd(
100 resources_[t].tool_name.c_str(), &resources_[t].file_info);
101 all_valid =
102 all_valid && resources_[t].file_info.handle != PP_kInvalidFileHandle;
104 return all_valid;
107 } // namespace plugin