From 0d09143a77e6741cc83de5a7cdf172eb1499d0a9 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 24 Sep 2014 11:10:07 +0800 Subject: [PATCH] Add JavaScript bindings of asar::Archive. --- atom.gyp | 1 + atom/common/api/atom_api_asar.cc | 64 ++++++++++++++++++++++++++++++++ atom/common/asar/archive.h | 1 + atom/common/node_bindings.cc | 1 + vendor/native_mate | 2 +- 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 atom/common/api/atom_api_asar.cc diff --git a/atom.gyp b/atom.gyp index 46960695c32..907219da59f 100644 --- a/atom.gyp +++ b/atom.gyp @@ -178,6 +178,7 @@ 'atom/browser/window_list.h', 'atom/browser/window_list_observer.h', 'atom/common/api/api_messages.h', + 'atom/common/api/atom_api_asar.cc', 'atom/common/api/atom_api_clipboard.cc', 'atom/common/api/atom_api_crash_reporter.cc', 'atom/common/api/atom_api_id_weak_map.cc', diff --git a/atom/common/api/atom_api_asar.cc b/atom/common/api/atom_api_asar.cc new file mode 100644 index 00000000000..06cab4a6ee8 --- /dev/null +++ b/atom/common/api/atom_api_asar.cc @@ -0,0 +1,64 @@ +// Copyright (c) 2014 GitHub, Inc. All rights reserved. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/common/asar/archive.h" +#include "atom/common/asar/archive_factory.h" +#include "atom/common/native_mate_converters/file_path_converter.h" +#include "native_mate/arguments.h" +#include "native_mate/dictionary.h" +#include "native_mate/object_template_builder.h" +#include "native_mate/wrappable.h" + +#include "atom/common/node_includes.h" + +namespace { + +class Archive : public mate::Wrappable { + public: + static v8::Handle Create(mate::Arguments* args, + const base::FilePath& path) { + static asar::ArchiveFactory archive_factory; + scoped_refptr archive = archive_factory.GetOrCreate(path); + if (!archive) + return args->ThrowError("Invalid asar archive"); + return (new Archive(archive))->GetWrapper(args->isolate()); + } + + protected: + explicit Archive(scoped_refptr archive) : archive_(archive) {} + virtual ~Archive() {} + + v8::Handle GetFileInfo(mate::Arguments* args, + const base::FilePath& path) { + asar::Archive::FileInfo info; + if (!archive_->GetFileInfo(path, &info)) + return args->ThrowError("Can not find file"); + mate::Dictionary dict(args->isolate(), v8::Object::New(args->isolate())); + dict.Set("size", info.size); + dict.Set("offset", info.offset); + return dict.GetHandle(); + } + + // mate::Wrappable: + mate::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) { + return mate::ObjectTemplateBuilder(isolate) + .SetValue("path", archive_->path()) + .SetMethod("getFileInfo", &Archive::GetFileInfo); + } + + private: + scoped_refptr archive_; + + DISALLOW_COPY_AND_ASSIGN(Archive); +}; + +void Initialize(v8::Handle exports, v8::Handle unused, + v8::Handle context, void* priv) { + mate::Dictionary dict(context->GetIsolate(), exports); + dict.SetMethod("createArchive", &Archive::Create); +} + +} // namespace + +NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_asar, Initialize) diff --git a/atom/common/asar/archive.h b/atom/common/asar/archive.h index b4619acf33e..fe8df60b436 100644 --- a/atom/common/asar/archive.h +++ b/atom/common/asar/archive.h @@ -31,6 +31,7 @@ class Archive : public base::RefCounted { bool GetFileInfo(const base::FilePath& path, FileInfo* info); base::FilePath path() const { return path_; } + base::DictionaryValue* header() const { return header_.get(); } private: friend class base::RefCounted; diff --git a/atom/common/node_bindings.cc b/atom/common/node_bindings.cc index a4dec37f3bd..a6f3659a227 100644 --- a/atom/common/node_bindings.cc +++ b/atom/common/node_bindings.cc @@ -69,6 +69,7 @@ REFERENCE_MODULE(atom_browser_protocol); REFERENCE_MODULE(atom_browser_global_shortcut); REFERENCE_MODULE(atom_browser_tray); REFERENCE_MODULE(atom_browser_window); +REFERENCE_MODULE(atom_common_asar); REFERENCE_MODULE(atom_common_clipboard); REFERENCE_MODULE(atom_common_crash_reporter); REFERENCE_MODULE(atom_common_id_weak_map); diff --git a/vendor/native_mate b/vendor/native_mate index 12f4e9b7ea0..c5b39126ee7 160000 --- a/vendor/native_mate +++ b/vendor/native_mate @@ -1 +1 @@ -Subproject commit 12f4e9b7ea0038e58e52839142eff0a4d17069bf +Subproject commit c5b39126ee7388acc61a25ac6b5fefb7a2cd6262