2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-09-24 03:10:07 +00:00
|
|
|
// 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-04 18:48:04 +00:00
|
|
|
#include "base/numerics/safe_math.h"
|
2020-08-03 21:26:27 +00:00
|
|
|
#include "gin/handle.h"
|
|
|
|
#include "gin/object_template_builder.h"
|
|
|
|
#include "gin/wrappable.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/asar/archive.h"
|
2019-10-02 04:33:07 +00:00
|
|
|
#include "shell/common/asar/asar_util.h"
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2020-08-04 18:48:04 +00:00
|
|
|
#include "shell/common/gin_helper/error_thrower.h"
|
|
|
|
#include "shell/common/gin_helper/function_template_extensions.h"
|
|
|
|
#include "shell/common/gin_helper/promise.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2019-10-23 16:26:32 +00:00
|
|
|
#include "shell/common/node_util.h"
|
2020-08-03 21:26:27 +00:00
|
|
|
|
2014-09-24 03:10:07 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-08-03 21:26:27 +00:00
|
|
|
class Archive : public gin::Wrappable<Archive> {
|
2014-09-24 03:10:07 +00:00
|
|
|
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) {
|
2018-06-18 07:32:55 +00:00
|
|
|
auto archive = std::make_unique<asar::Archive>(path);
|
2014-09-25 12:48:32 +00:00
|
|
|
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
|
|
|
.SetProperty("path", &Archive::GetPath)
|
|
|
|
.SetMethod("getFileInfo", &Archive::GetFileInfo)
|
|
|
|
.SetMethod("stat", &Archive::Stat)
|
|
|
|
.SetMethod("readdir", &Archive::Readdir)
|
|
|
|
.SetMethod("realpath", &Archive::Realpath)
|
|
|
|
.SetMethod("copyFileOut", &Archive::CopyFileOut)
|
2020-08-04 18:48:04 +00:00
|
|
|
.SetMethod("read", &Archive::Read)
|
|
|
|
.SetMethod("readSync", &Archive::ReadSync);
|
2014-09-24 03:10:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 21:26:27 +00:00
|
|
|
const char* GetTypeName() override { return "Archive"; }
|
|
|
|
|
2014-09-24 03:10:07 +00:00
|
|
|
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
|
|
|
|
|
|
|
// Returns the path of the file.
|
2018-04-18 01:55:30 +00:00
|
|
|
base::FilePath GetPath() { return archive_->path(); }
|
2014-09-24 03:10:07 +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) {
|
2014-09-24 03:10:07 +00:00
|
|
|
asar::Archive::FileInfo info;
|
2014-09-25 12:38:12 +00:00
|
|
|
if (!archive_ || !archive_->GetFileInfo(path, &info))
|
2014-09-24 10:44:00 +00:00
|
|
|
return v8::False(isolate);
|
2019-09-06 05:52:54 +00:00
|
|
|
gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
|
2014-09-24 03:10:07 +00:00
|
|
|
dict.Set("size", info.size);
|
2015-03-20 12:34:58 +00:00
|
|
|
dict.Set("unpacked", info.unpacked);
|
2014-09-24 03:10:07 +00:00
|
|
|
dict.Set("offset", info.offset);
|
|
|
|
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;
|
2014-09-25 12:38:12 +00:00
|
|
|
if (!archive_ || !archive_->Stat(path, &stats))
|
2014-09-24 10:44:00 +00:00
|
|
|
return v8::False(isolate);
|
2019-09-06 05:52:54 +00:00
|
|
|
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;
|
2014-09-25 12:38:12 +00:00
|
|
|
if (!archive_ || !archive_->Readdir(path, &files))
|
2014-09-24 10:44:00 +00:00
|
|
|
return v8::False(isolate);
|
2019-10-31 07:56:00 +00:00
|
|
|
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);
|
2019-10-31 07:56:00 +00:00
|
|
|
return gin::ConvertToV8(isolate, realpath);
|
2014-09-30 06:53:41 +00:00
|
|
|
}
|
|
|
|
|
2014-09-25 08:56:50 +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) {
|
2014-09-25 08:56:50 +00:00
|
|
|
base::FilePath new_path;
|
2014-09-25 12:38:12 +00:00
|
|
|
if (!archive_ || !archive_->CopyFileOut(path, &new_path))
|
2014-09-25 08:56:50 +00:00
|
|
|
return v8::False(isolate);
|
2019-10-31 07:56:00 +00:00
|
|
|
return gin::ConvertToV8(isolate, new_path);
|
2014-09-25 08:56:50 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 18:48:04 +00:00
|
|
|
v8::Local<v8::ArrayBuffer> ReadSync(gin_helper::ErrorThrower thrower,
|
|
|
|
uint64_t offset,
|
|
|
|
uint64_t length) {
|
|
|
|
base::CheckedNumeric<uint64_t> safe_offset(offset);
|
|
|
|
base::CheckedNumeric<uint64_t> safe_end = safe_offset + length;
|
|
|
|
if (!safe_end.IsValid() ||
|
|
|
|
safe_end.ValueOrDie() > archive_->file()->length()) {
|
|
|
|
thrower.ThrowError("Out of bounds read");
|
|
|
|
return v8::Local<v8::ArrayBuffer>();
|
|
|
|
}
|
|
|
|
auto array_buffer = v8::ArrayBuffer::New(thrower.isolate(), length);
|
|
|
|
auto backing_store = array_buffer->GetBackingStore();
|
|
|
|
memcpy(backing_store->Data(), archive_->file()->data() + offset, length);
|
|
|
|
return array_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Promise> Read(v8::Isolate* isolate,
|
|
|
|
uint64_t offset,
|
|
|
|
uint64_t length) {
|
|
|
|
gin_helper::Promise<v8::Local<v8::ArrayBuffer>> promise(isolate);
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
|
|
|
base::CheckedNumeric<uint64_t> safe_offset(offset);
|
|
|
|
base::CheckedNumeric<uint64_t> safe_end = safe_offset + length;
|
|
|
|
if (!safe_end.IsValid() ||
|
|
|
|
safe_end.ValueOrDie() > archive_->file()->length()) {
|
|
|
|
promise.RejectWithErrorMessage("Out of bounds read");
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto backing_store = v8::ArrayBuffer::NewBackingStore(isolate, length);
|
|
|
|
base::ThreadPool::PostTaskAndReplyWithResult(
|
|
|
|
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
|
|
|
|
base::BindOnce(&Archive::ReadOnIO, isolate, archive_,
|
|
|
|
std::move(backing_store), offset, length),
|
|
|
|
base::BindOnce(&Archive::ResolveReadOnUI, std::move(promise)));
|
|
|
|
|
|
|
|
return handle;
|
2015-05-11 03:02:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 03:10:07 +00:00
|
|
|
private:
|
2020-08-04 18:48:04 +00:00
|
|
|
static std::unique_ptr<v8::BackingStore> ReadOnIO(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
std::shared_ptr<asar::Archive> archive,
|
|
|
|
std::unique_ptr<v8::BackingStore> backing_store,
|
|
|
|
uint64_t offset,
|
|
|
|
uint64_t length) {
|
|
|
|
memcpy(backing_store->Data(), archive->file()->data() + offset, length);
|
|
|
|
return backing_store;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ResolveReadOnUI(
|
|
|
|
gin_helper::Promise<v8::Local<v8::ArrayBuffer>> promise,
|
|
|
|
std::unique_ptr<v8::BackingStore> backing_store) {
|
|
|
|
v8::HandleScope scope(promise.isolate());
|
|
|
|
v8::Context::Scope context_scope(promise.GetContext());
|
|
|
|
auto array_buffer =
|
|
|
|
v8::ArrayBuffer::New(promise.isolate(), std::move(backing_store));
|
|
|
|
promise.Resolve(array_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<asar::Archive> archive_;
|
2014-09-24 03:10:07 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Archive);
|
|
|
|
};
|
|
|
|
|
2020-08-03 21:26:27 +00:00
|
|
|
// static
|
|
|
|
gin::WrapperInfo Archive::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
2019-04-26 09:55:12 +00:00
|
|
|
void InitAsarSupport(v8::Isolate* isolate, v8::Local<v8::Value> require) {
|
2020-07-16 18:38:31 +00:00
|
|
|
// Evaluate asar_bundle.js.
|
|
|
|
std::vector<v8::Local<v8::String>> asar_bundle_params = {
|
2019-01-12 01:00:43 +00:00
|
|
|
node::FIXED_ONE_BYTE_STRING(isolate, "require")};
|
2020-07-16 18:38:31 +00:00
|
|
|
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);
|
2015-02-04 18:45:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 04:33:07 +00:00
|
|
|
v8::Local<v8::Value> SplitPath(v8::Isolate* isolate,
|
|
|
|
const base::FilePath& path) {
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
2019-10-02 04:33:07 +00:00
|
|
|
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) {
|
2019-09-06 05:52:54 +00:00
|
|
|
gin_helper::Dictionary dict(context->GetIsolate(), exports);
|
2014-09-24 03:10:07 +00:00
|
|
|
dict.SetMethod("createArchive", &Archive::Create);
|
2019-10-02 04:33:07 +00:00
|
|
|
dict.SetMethod("splitPath", &SplitPath);
|
2015-02-04 18:45:17 +00:00
|
|
|
dict.SetMethod("initAsarSupport", &InitAsarSupport);
|
2014-09-24 03:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-02-14 11:25:39 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_common_asar, Initialize)
|