electron_api_asar.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include <vector>
  5. #include "gin/handle.h"
  6. #include "gin/object_template_builder.h"
  7. #include "gin/wrappable.h"
  8. #include "shell/common/asar/archive.h"
  9. #include "shell/common/asar/asar_util.h"
  10. #include "shell/common/gin_converters/callback_converter.h"
  11. #include "shell/common/gin_converters/file_path_converter.h"
  12. #include "shell/common/gin_helper/dictionary.h"
  13. #include "shell/common/node_includes.h"
  14. #include "shell/common/node_util.h"
  15. namespace {
  16. class Archive : public gin::Wrappable<Archive> {
  17. public:
  18. static gin::Handle<Archive> Create(v8::Isolate* isolate,
  19. const base::FilePath& path) {
  20. auto archive = std::make_unique<asar::Archive>(path);
  21. if (!archive->Init())
  22. return gin::Handle<Archive>();
  23. return gin::CreateHandle(isolate, new Archive(isolate, std::move(archive)));
  24. }
  25. // gin::Wrappable
  26. static gin::WrapperInfo kWrapperInfo;
  27. gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
  28. v8::Isolate* isolate) override {
  29. return gin::ObjectTemplateBuilder(isolate)
  30. .SetProperty("path", &Archive::GetPath)
  31. .SetMethod("getFileInfo", &Archive::GetFileInfo)
  32. .SetMethod("stat", &Archive::Stat)
  33. .SetMethod("readdir", &Archive::Readdir)
  34. .SetMethod("realpath", &Archive::Realpath)
  35. .SetMethod("copyFileOut", &Archive::CopyFileOut)
  36. .SetMethod("getFd", &Archive::GetFD);
  37. }
  38. const char* GetTypeName() override { return "Archive"; }
  39. protected:
  40. Archive(v8::Isolate* isolate, std::unique_ptr<asar::Archive> archive)
  41. : archive_(std::move(archive)) {}
  42. // Returns the path of the file.
  43. base::FilePath GetPath() { return archive_->path(); }
  44. // Reads the offset and size of file.
  45. v8::Local<v8::Value> GetFileInfo(v8::Isolate* isolate,
  46. const base::FilePath& path) {
  47. asar::Archive::FileInfo info;
  48. if (!archive_ || !archive_->GetFileInfo(path, &info))
  49. return v8::False(isolate);
  50. gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
  51. dict.Set("size", info.size);
  52. dict.Set("unpacked", info.unpacked);
  53. dict.Set("offset", info.offset);
  54. return dict.GetHandle();
  55. }
  56. // Returns a fake result of fs.stat(path).
  57. v8::Local<v8::Value> Stat(v8::Isolate* isolate, const base::FilePath& path) {
  58. asar::Archive::Stats stats;
  59. if (!archive_ || !archive_->Stat(path, &stats))
  60. return v8::False(isolate);
  61. gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
  62. dict.Set("size", stats.size);
  63. dict.Set("offset", stats.offset);
  64. dict.Set("isFile", stats.is_file);
  65. dict.Set("isDirectory", stats.is_directory);
  66. dict.Set("isLink", stats.is_link);
  67. return dict.GetHandle();
  68. }
  69. // Returns all files under a directory.
  70. v8::Local<v8::Value> Readdir(v8::Isolate* isolate,
  71. const base::FilePath& path) {
  72. std::vector<base::FilePath> files;
  73. if (!archive_ || !archive_->Readdir(path, &files))
  74. return v8::False(isolate);
  75. return gin::ConvertToV8(isolate, files);
  76. }
  77. // Returns the path of file with symbol link resolved.
  78. v8::Local<v8::Value> Realpath(v8::Isolate* isolate,
  79. const base::FilePath& path) {
  80. base::FilePath realpath;
  81. if (!archive_ || !archive_->Realpath(path, &realpath))
  82. return v8::False(isolate);
  83. return gin::ConvertToV8(isolate, realpath);
  84. }
  85. // Copy the file out into a temporary file and returns the new path.
  86. v8::Local<v8::Value> CopyFileOut(v8::Isolate* isolate,
  87. const base::FilePath& path) {
  88. base::FilePath new_path;
  89. if (!archive_ || !archive_->CopyFileOut(path, &new_path))
  90. return v8::False(isolate);
  91. return gin::ConvertToV8(isolate, new_path);
  92. }
  93. // Return the file descriptor.
  94. int GetFD() const {
  95. if (!archive_)
  96. return -1;
  97. return archive_->GetFD();
  98. }
  99. private:
  100. std::unique_ptr<asar::Archive> archive_;
  101. DISALLOW_COPY_AND_ASSIGN(Archive);
  102. };
  103. // static
  104. gin::WrapperInfo Archive::kWrapperInfo = {gin::kEmbedderNativeGin};
  105. void InitAsarSupport(v8::Isolate* isolate, v8::Local<v8::Value> require) {
  106. // Evaluate asar_bundle.js.
  107. std::vector<v8::Local<v8::String>> asar_bundle_params = {
  108. node::FIXED_ONE_BYTE_STRING(isolate, "require")};
  109. std::vector<v8::Local<v8::Value>> asar_bundle_args = {require};
  110. electron::util::CompileAndCall(
  111. isolate->GetCurrentContext(), "electron/js2c/asar_bundle",
  112. &asar_bundle_params, &asar_bundle_args, nullptr);
  113. }
  114. v8::Local<v8::Value> SplitPath(v8::Isolate* isolate,
  115. const base::FilePath& path) {
  116. gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
  117. base::FilePath asar_path, file_path;
  118. if (asar::GetAsarArchivePath(path, &asar_path, &file_path, true)) {
  119. dict.Set("isAsar", true);
  120. dict.Set("asarPath", asar_path);
  121. dict.Set("filePath", file_path);
  122. } else {
  123. dict.Set("isAsar", false);
  124. }
  125. return dict.GetHandle();
  126. }
  127. void Initialize(v8::Local<v8::Object> exports,
  128. v8::Local<v8::Value> unused,
  129. v8::Local<v8::Context> context,
  130. void* priv) {
  131. gin_helper::Dictionary dict(context->GetIsolate(), exports);
  132. dict.SetMethod("createArchive", &Archive::Create);
  133. dict.SetMethod("splitPath", &SplitPath);
  134. dict.SetMethod("initAsarSupport", &InitAsarSupport);
  135. }
  136. } // namespace
  137. NODE_LINKED_MODULE_CONTEXT_AWARE(electron_common_asar, Initialize)