electron/shell/common/api/electron_api_asar.cc

172 lines
5.9 KiB
C++
Raw Normal View History

// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
2014-09-24 10:44:00 +00:00
#include <vector>
2020-08-03 21:26:27 +00:00
#include "gin/handle.h"
#include "gin/object_template_builder.h"
#include "gin/wrappable.h"
#include "shell/common/asar/archive.h"
#include "shell/common/asar/asar_util.h"
#include "shell/common/gin_converters/callback_converter.h"
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
#include "shell/common/node_util.h"
2020-08-03 21:26:27 +00:00
namespace {
2020-08-03 21:26:27 +00:00
class Archive : public gin::Wrappable<Archive> {
public:
2020-08-03 21:26:27 +00:00
static gin::Handle<Archive> Create(v8::Isolate* isolate,
2018-04-18 01:55:30 +00:00
const base::FilePath& path) {
auto archive = std::make_unique<asar::Archive>(path);
if (!archive->Init())
2020-08-03 21:26:27 +00:00
return gin::Handle<Archive>();
return gin::CreateHandle(isolate, new Archive(isolate, std::move(archive)));
2016-04-25 01:17:54 +00:00
}
2020-08-03 21:26:27 +00:00
// gin::Wrappable
static gin::WrapperInfo kWrapperInfo;
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override {
return gin::ObjectTemplateBuilder(isolate)
2016-04-25 01:17:54 +00:00
.SetMethod("getFileInfo", &Archive::GetFileInfo)
.SetMethod("stat", &Archive::Stat)
.SetMethod("readdir", &Archive::Readdir)
.SetMethod("realpath", &Archive::Realpath)
.SetMethod("copyFileOut", &Archive::CopyFileOut)
.SetMethod("getFdAndValidateIntegrityLater", &Archive::GetFD);
}
2020-08-03 21:26:27 +00:00
const char* GetTypeName() override { return "Archive"; }
// disable copy
Archive(const Archive&) = delete;
Archive& operator=(const Archive&) = delete;
protected:
2016-05-23 01:59:39 +00:00
Archive(v8::Isolate* isolate, std::unique_ptr<asar::Archive> archive)
2020-08-03 21:26:27 +00:00
: archive_(std::move(archive)) {}
2016-04-25 01:17:54 +00:00
2014-09-24 04:02:33 +00:00
// Reads the offset and size of file.
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> GetFileInfo(v8::Isolate* isolate,
2018-04-18 01:55:30 +00:00
const base::FilePath& path) {
asar::Archive::FileInfo info;
if (!archive_ || !archive_->GetFileInfo(path, &info))
2014-09-24 10:44:00 +00:00
return v8::False(isolate);
gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
dict.Set("size", info.size);
dict.Set("unpacked", info.unpacked);
dict.Set("offset", info.offset);
if (info.integrity.has_value()) {
gin_helper::Dictionary integrity(isolate, v8::Object::New(isolate));
asar::HashAlgorithm algorithm = info.integrity.value().algorithm;
switch (algorithm) {
case asar::HashAlgorithm::SHA256:
integrity.Set("algorithm", "SHA256");
break;
case asar::HashAlgorithm::NONE:
CHECK(false);
break;
}
integrity.Set("hash", info.integrity.value().hash);
dict.Set("integrity", integrity);
}
return dict.GetHandle();
}
2014-09-24 04:02:33 +00:00
// Returns a fake result of fs.stat(path).
2018-04-18 01:55:30 +00:00
v8::Local<v8::Value> Stat(v8::Isolate* isolate, const base::FilePath& path) {
2014-09-24 04:02:33 +00:00
asar::Archive::Stats stats;
if (!archive_ || !archive_->Stat(path, &stats))
2014-09-24 10:44:00 +00:00
return v8::False(isolate);
gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
2014-09-24 04:02:33 +00:00
dict.Set("size", stats.size);
dict.Set("offset", stats.offset);
dict.Set("isFile", stats.is_file);
dict.Set("isDirectory", stats.is_directory);
dict.Set("isLink", stats.is_link);
return dict.GetHandle();
}
2014-09-24 10:44:00 +00:00
// Returns all files under a directory.
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> Readdir(v8::Isolate* isolate,
2018-04-18 01:55:30 +00:00
const base::FilePath& path) {
2014-09-24 10:44:00 +00:00
std::vector<base::FilePath> files;
if (!archive_ || !archive_->Readdir(path, &files))
2014-09-24 10:44:00 +00:00
return v8::False(isolate);
return gin::ConvertToV8(isolate, files);
2014-09-24 10:44:00 +00:00
}
2014-09-30 06:53:41 +00:00
// Returns the path of file with symbol link resolved.
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> Realpath(v8::Isolate* isolate,
2018-04-18 01:55:30 +00:00
const base::FilePath& path) {
2014-09-30 06:53:41 +00:00
base::FilePath realpath;
if (!archive_ || !archive_->Realpath(path, &realpath))
return v8::False(isolate);
return gin::ConvertToV8(isolate, realpath);
2014-09-30 06:53:41 +00:00
}
// Copy the file out into a temporary file and returns the new path.
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> CopyFileOut(v8::Isolate* isolate,
2018-04-18 01:55:30 +00:00
const base::FilePath& path) {
base::FilePath new_path;
if (!archive_ || !archive_->CopyFileOut(path, &new_path))
return v8::False(isolate);
return gin::ConvertToV8(isolate, new_path);
}
// Return the file descriptor.
int GetFD() const {
if (!archive_)
return -1;
return archive_->GetUnsafeFD();
2015-05-11 03:02:17 +00:00
}
private:
std::unique_ptr<asar::Archive> archive_;
};
2020-08-03 21:26:27 +00:00
// static
gin::WrapperInfo Archive::kWrapperInfo = {gin::kEmbedderNativeGin};
void InitAsarSupport(v8::Isolate* isolate, v8::Local<v8::Value> require) {
// Evaluate asar_bundle.js.
std::vector<v8::Local<v8::String>> asar_bundle_params = {
node::FIXED_ONE_BYTE_STRING(isolate, "require")};
std::vector<v8::Local<v8::Value>> asar_bundle_args = {require};
electron::util::CompileAndCall(
isolate->GetCurrentContext(), "electron/js2c/asar_bundle",
&asar_bundle_params, &asar_bundle_args, nullptr);
}
v8::Local<v8::Value> SplitPath(v8::Isolate* isolate,
const base::FilePath& path) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
base::FilePath asar_path, file_path;
if (asar::GetAsarArchivePath(path, &asar_path, &file_path, true)) {
dict.Set("isAsar", true);
dict.Set("asarPath", asar_path);
dict.Set("filePath", file_path);
} else {
dict.Set("isAsar", false);
}
return dict.GetHandle();
}
2018-04-18 01:55:30 +00:00
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
gin_helper::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("createArchive", &Archive::Create);
dict.SetMethod("splitPath", &SplitPath);
dict.SetMethod("initAsarSupport", &InitAsarSupport);
}
} // namespace
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_common_asar, Initialize)