electron_api_asar.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "shell/common/asar/archive.h"
  7. #include "shell/common/asar/asar_util.h"
  8. #include "shell/common/gin_converters/file_path_converter.h"
  9. #include "shell/common/gin_helper/dictionary.h"
  10. #include "shell/common/node_includes.h"
  11. #include "shell/common/node_util.h"
  12. namespace {
  13. class Archive : public node::ObjectWrap {
  14. public:
  15. static v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
  16. v8::Isolate* isolate) {
  17. auto tpl = v8::FunctionTemplate::New(isolate, Archive::New);
  18. tpl->SetClassName(
  19. v8::String::NewFromUtf8(isolate, "Archive").ToLocalChecked());
  20. tpl->InstanceTemplate()->SetInternalFieldCount(1);
  21. NODE_SET_PROTOTYPE_METHOD(tpl, "getFileInfo", &Archive::GetFileInfo);
  22. NODE_SET_PROTOTYPE_METHOD(tpl, "stat", &Archive::Stat);
  23. NODE_SET_PROTOTYPE_METHOD(tpl, "readdir", &Archive::Readdir);
  24. NODE_SET_PROTOTYPE_METHOD(tpl, "realpath", &Archive::Realpath);
  25. NODE_SET_PROTOTYPE_METHOD(tpl, "copyFileOut", &Archive::CopyFileOut);
  26. NODE_SET_PROTOTYPE_METHOD(tpl, "getFdAndValidateIntegrityLater",
  27. &Archive::GetFD);
  28. return tpl;
  29. }
  30. // disable copy
  31. Archive(const Archive&) = delete;
  32. Archive& operator=(const Archive&) = delete;
  33. protected:
  34. explicit Archive(std::shared_ptr<asar::Archive> archive)
  35. : archive_(std::move(archive)) {}
  36. static void New(const v8::FunctionCallbackInfo<v8::Value>& args) {
  37. auto* isolate = args.GetIsolate();
  38. base::FilePath path;
  39. if (!gin::ConvertFromV8(isolate, args[0], &path)) {
  40. isolate->ThrowException(v8::Exception::Error(node::FIXED_ONE_BYTE_STRING(
  41. isolate, "failed to convert path to V8")));
  42. return;
  43. }
  44. std::shared_ptr<asar::Archive> archive = asar::GetOrCreateAsarArchive(path);
  45. if (!archive) {
  46. isolate->ThrowException(v8::Exception::Error(node::FIXED_ONE_BYTE_STRING(
  47. isolate, "failed to initialize archive")));
  48. return;
  49. }
  50. auto* archive_wrap = new Archive(std::move(archive));
  51. archive_wrap->Wrap(args.This());
  52. args.GetReturnValue().Set(args.This());
  53. }
  54. // Reads the offset and size of file.
  55. static void GetFileInfo(const v8::FunctionCallbackInfo<v8::Value>& args) {
  56. auto* isolate = args.GetIsolate();
  57. auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
  58. base::FilePath path;
  59. if (!gin::ConvertFromV8(isolate, args[0], &path)) {
  60. args.GetReturnValue().Set(v8::False(isolate));
  61. return;
  62. }
  63. asar::Archive::FileInfo info;
  64. if (!wrap->archive_ || !wrap->archive_->GetFileInfo(path, &info)) {
  65. args.GetReturnValue().Set(v8::False(isolate));
  66. return;
  67. }
  68. gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
  69. dict.Set("size", info.size);
  70. dict.Set("unpacked", info.unpacked);
  71. dict.Set("offset", info.offset);
  72. if (info.integrity.has_value()) {
  73. gin_helper::Dictionary integrity(isolate, v8::Object::New(isolate));
  74. asar::HashAlgorithm algorithm = info.integrity.value().algorithm;
  75. switch (algorithm) {
  76. case asar::HashAlgorithm::kSHA256:
  77. integrity.Set("algorithm", "SHA256");
  78. break;
  79. case asar::HashAlgorithm::kNone:
  80. CHECK(false);
  81. break;
  82. }
  83. integrity.Set("hash", info.integrity.value().hash);
  84. dict.Set("integrity", integrity);
  85. }
  86. args.GetReturnValue().Set(dict.GetHandle());
  87. }
  88. // Returns a fake result of fs.stat(path).
  89. static void Stat(const v8::FunctionCallbackInfo<v8::Value>& args) {
  90. auto* isolate = args.GetIsolate();
  91. auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
  92. base::FilePath path;
  93. if (!gin::ConvertFromV8(isolate, args[0], &path)) {
  94. args.GetReturnValue().Set(v8::False(isolate));
  95. return;
  96. }
  97. asar::Archive::Stats stats;
  98. if (!wrap->archive_ || !wrap->archive_->Stat(path, &stats)) {
  99. args.GetReturnValue().Set(v8::False(isolate));
  100. return;
  101. }
  102. gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
  103. dict.Set("size", stats.size);
  104. dict.Set("offset", stats.offset);
  105. dict.Set("isFile", stats.is_file);
  106. dict.Set("isDirectory", stats.is_directory);
  107. dict.Set("isLink", stats.is_link);
  108. args.GetReturnValue().Set(dict.GetHandle());
  109. }
  110. // Returns all files under a directory.
  111. static void Readdir(const v8::FunctionCallbackInfo<v8::Value>& args) {
  112. auto* isolate = args.GetIsolate();
  113. auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
  114. base::FilePath path;
  115. if (!gin::ConvertFromV8(isolate, args[0], &path)) {
  116. args.GetReturnValue().Set(v8::False(isolate));
  117. return;
  118. }
  119. std::vector<base::FilePath> files;
  120. if (!wrap->archive_ || !wrap->archive_->Readdir(path, &files)) {
  121. args.GetReturnValue().Set(v8::False(isolate));
  122. return;
  123. }
  124. args.GetReturnValue().Set(gin::ConvertToV8(isolate, files));
  125. }
  126. // Returns the path of file with symbol link resolved.
  127. static void Realpath(const v8::FunctionCallbackInfo<v8::Value>& args) {
  128. auto* isolate = args.GetIsolate();
  129. auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
  130. base::FilePath path;
  131. if (!gin::ConvertFromV8(isolate, args[0], &path)) {
  132. args.GetReturnValue().Set(v8::False(isolate));
  133. return;
  134. }
  135. base::FilePath realpath;
  136. if (!wrap->archive_ || !wrap->archive_->Realpath(path, &realpath)) {
  137. args.GetReturnValue().Set(v8::False(isolate));
  138. return;
  139. }
  140. args.GetReturnValue().Set(gin::ConvertToV8(isolate, realpath));
  141. }
  142. // Copy the file out into a temporary file and returns the new path.
  143. static void CopyFileOut(const v8::FunctionCallbackInfo<v8::Value>& args) {
  144. auto* isolate = args.GetIsolate();
  145. auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
  146. base::FilePath path;
  147. if (!gin::ConvertFromV8(isolate, args[0], &path)) {
  148. args.GetReturnValue().Set(v8::False(isolate));
  149. return;
  150. }
  151. base::FilePath new_path;
  152. if (!wrap->archive_ || !wrap->archive_->CopyFileOut(path, &new_path)) {
  153. args.GetReturnValue().Set(v8::False(isolate));
  154. return;
  155. }
  156. args.GetReturnValue().Set(gin::ConvertToV8(isolate, new_path));
  157. }
  158. // Return the file descriptor.
  159. static void GetFD(const v8::FunctionCallbackInfo<v8::Value>& args) {
  160. auto* isolate = args.GetIsolate();
  161. auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
  162. args.GetReturnValue().Set(gin::ConvertToV8(
  163. isolate, wrap->archive_ ? wrap->archive_->GetUnsafeFD() : -1));
  164. }
  165. std::shared_ptr<asar::Archive> archive_;
  166. };
  167. static void InitAsarSupport(const v8::FunctionCallbackInfo<v8::Value>& args) {
  168. auto* isolate = args.GetIsolate();
  169. auto require = args[0];
  170. // Evaluate asar_bundle.js.
  171. std::vector<v8::Local<v8::String>> asar_bundle_params = {
  172. node::FIXED_ONE_BYTE_STRING(isolate, "require")};
  173. std::vector<v8::Local<v8::Value>> asar_bundle_args = {require};
  174. electron::util::CompileAndCall(
  175. isolate->GetCurrentContext(), "electron/js2c/asar_bundle",
  176. &asar_bundle_params, &asar_bundle_args, nullptr);
  177. }
  178. static void SplitPath(const v8::FunctionCallbackInfo<v8::Value>& args) {
  179. auto* isolate = args.GetIsolate();
  180. base::FilePath path;
  181. if (!gin::ConvertFromV8(isolate, args[0], &path)) {
  182. args.GetReturnValue().Set(v8::False(isolate));
  183. return;
  184. }
  185. auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
  186. base::FilePath asar_path, file_path;
  187. if (asar::GetAsarArchivePath(path, &asar_path, &file_path, true)) {
  188. dict.Set("isAsar", true);
  189. dict.Set("asarPath", asar_path);
  190. dict.Set("filePath", file_path);
  191. } else {
  192. dict.Set("isAsar", false);
  193. }
  194. args.GetReturnValue().Set(dict.GetHandle());
  195. }
  196. void Initialize(v8::Local<v8::Object> exports,
  197. v8::Local<v8::Value> unused,
  198. v8::Local<v8::Context> context,
  199. void* priv) {
  200. auto* isolate = exports->GetIsolate();
  201. auto cons = Archive::CreateFunctionTemplate(isolate)
  202. ->GetFunction(context)
  203. .ToLocalChecked();
  204. cons->SetName(node::FIXED_ONE_BYTE_STRING(isolate, "Archive"));
  205. exports->Set(context, node::FIXED_ONE_BYTE_STRING(isolate, "Archive"), cons)
  206. .Check();
  207. NODE_SET_METHOD(exports, "splitPath", &SplitPath);
  208. NODE_SET_METHOD(exports, "initAsarSupport", &InitAsarSupport);
  209. }
  210. } // namespace
  211. NODE_LINKED_BINDING_CONTEXT_AWARE(electron_common_asar, Initialize)