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
14 namespace plugin {
15
16 namespace {
17
18 static const char kPnaclBaseUrl[] = "chrome://pnacl-translator/";
19
GetFullUrl(const nacl::string & partial_url)20 nacl::string GetFullUrl(const nacl::string& partial_url) {
21 return nacl::string(kPnaclBaseUrl) + GetNaClInterface()->GetSandboxArch() +
22 "/" + partial_url;
23 }
24
25 } // namespace
26
~PnaclResources()27 PnaclResources::~PnaclResources() {
28 if (llc_file_handle_ != PP_kInvalidFileHandle)
29 CloseFileHandle(llc_file_handle_);
30 if (ld_file_handle_ != PP_kInvalidFileHandle)
31 CloseFileHandle(ld_file_handle_);
32 }
33
ReadResourceInfo(const pp::CompletionCallback & resource_info_read_cb)34 void PnaclResources::ReadResourceInfo(
35 const pp::CompletionCallback& resource_info_read_cb) {
36 PP_Var pp_llc_tool_name_var;
37 PP_Var pp_ld_tool_name_var;
38 if (!plugin_->nacl_interface()->GetPnaclResourceInfo(
39 plugin_->pp_instance(),
40 "chrome://pnacl-translator/pnacl.json",
41 &pp_llc_tool_name_var,
42 &pp_ld_tool_name_var)) {
43 pp::Module::Get()->core()->CallOnMainThread(0,
44 resource_info_read_cb,
45 PP_ERROR_FAILED);
46 return;
47 }
48 pp::Var llc_tool_name(pp::PASS_REF, pp_llc_tool_name_var);
49 pp::Var ld_tool_name(pp::PASS_REF, pp_ld_tool_name_var);
50 llc_tool_name_ = GetFullUrl(llc_tool_name.AsString());
51 ld_tool_name_ = GetFullUrl(ld_tool_name.AsString());
52 pp::Module::Get()->core()->CallOnMainThread(0, resource_info_read_cb, PP_OK);
53 }
54
TakeLlcFileHandle()55 PP_FileHandle PnaclResources::TakeLlcFileHandle() {
56 PP_FileHandle to_return = llc_file_handle_;
57 llc_file_handle_ = PP_kInvalidFileHandle;
58 return to_return;
59 }
60
TakeLdFileHandle()61 PP_FileHandle PnaclResources::TakeLdFileHandle() {
62 PP_FileHandle to_return = ld_file_handle_;
63 ld_file_handle_ = PP_kInvalidFileHandle;
64 return to_return;
65 }
66
StartLoad(const pp::CompletionCallback & all_loaded_callback)67 void PnaclResources::StartLoad(
68 const pp::CompletionCallback& all_loaded_callback) {
69 PLUGIN_PRINTF(("PnaclResources::StartLoad\n"));
70
71 // Do a blocking load of each of the resources.
72 llc_file_handle_ =
73 plugin_->nacl_interface()->GetReadonlyPnaclFd(llc_tool_name_.c_str());
74 ld_file_handle_ =
75 plugin_->nacl_interface()->GetReadonlyPnaclFd(ld_tool_name_.c_str());
76
77 int32_t result = PP_OK;
78 if (llc_file_handle_ == PP_kInvalidFileHandle ||
79 ld_file_handle_ == PP_kInvalidFileHandle) {
80 result = PP_ERROR_FILENOTFOUND;
81 }
82 pp::Module::Get()->core()->CallOnMainThread(0, all_loaded_callback, result);
83 }
84
85 } // namespace plugin
86