electron_api_asar.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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("type", static_cast<int>(stats.type));
  106. args.GetReturnValue().Set(dict.GetHandle());
  107. }
  108. // Returns all files under a directory.
  109. static void Readdir(const v8::FunctionCallbackInfo<v8::Value>& args) {
  110. auto* isolate = args.GetIsolate();
  111. auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
  112. base::FilePath path;
  113. if (!gin::ConvertFromV8(isolate, args[0], &path)) {
  114. args.GetReturnValue().Set(v8::False(isolate));
  115. return;
  116. }
  117. std::vector<base::FilePath> files;
  118. if (!wrap->archive_ || !wrap->archive_->Readdir(path, &files)) {
  119. args.GetReturnValue().Set(v8::False(isolate));
  120. return;
  121. }
  122. args.GetReturnValue().Set(gin::ConvertToV8(isolate, files));
  123. }
  124. // Returns the path of file with symbol link resolved.
  125. static void Realpath(const v8::FunctionCallbackInfo<v8::Value>& args) {
  126. auto* isolate = args.GetIsolate();
  127. auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
  128. base::FilePath path;
  129. if (!gin::ConvertFromV8(isolate, args[0], &path)) {
  130. args.GetReturnValue().Set(v8::False(isolate));
  131. return;
  132. }
  133. base::FilePath realpath;
  134. if (!wrap->archive_ || !wrap->archive_->Realpath(path, &realpath)) {
  135. args.GetReturnValue().Set(v8::False(isolate));
  136. return;
  137. }
  138. args.GetReturnValue().Set(gin::ConvertToV8(isolate, realpath));
  139. }
  140. // Copy the file out into a temporary file and returns the new path.
  141. static void CopyFileOut(const v8::FunctionCallbackInfo<v8::Value>& args) {
  142. auto* isolate = args.GetIsolate();
  143. auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
  144. base::FilePath path;
  145. if (!gin::ConvertFromV8(isolate, args[0], &path)) {
  146. args.GetReturnValue().Set(v8::False(isolate));
  147. return;
  148. }
  149. base::FilePath new_path;
  150. if (!wrap->archive_ || !wrap->archive_->CopyFileOut(path, &new_path)) {
  151. args.GetReturnValue().Set(v8::False(isolate));
  152. return;
  153. }
  154. args.GetReturnValue().Set(gin::ConvertToV8(isolate, new_path));
  155. }
  156. // Return the file descriptor.
  157. static void GetFD(const v8::FunctionCallbackInfo<v8::Value>& args) {
  158. auto* isolate = args.GetIsolate();
  159. auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
  160. args.GetReturnValue().Set(gin::ConvertToV8(
  161. isolate, wrap->archive_ ? wrap->archive_->GetUnsafeFD() : -1));
  162. }
  163. std::shared_ptr<asar::Archive> archive_;
  164. };
  165. static void InitAsarSupport(const v8::FunctionCallbackInfo<v8::Value>& args) {
  166. auto* isolate = args.GetIsolate();
  167. auto require = args[0];
  168. // Evaluate asar_bundle.js.
  169. std::vector<v8::Local<v8::String>> asar_bundle_params = {
  170. node::FIXED_ONE_BYTE_STRING(isolate, "require")};
  171. std::vector<v8::Local<v8::Value>> asar_bundle_args = {require};
  172. electron::util::CompileAndCall(isolate->GetCurrentContext(),
  173. "electron/js2c/asar_bundle",
  174. &asar_bundle_params, &asar_bundle_args);
  175. }
  176. static void SplitPath(const v8::FunctionCallbackInfo<v8::Value>& args) {
  177. auto* isolate = args.GetIsolate();
  178. base::FilePath path;
  179. if (!gin::ConvertFromV8(isolate, args[0], &path)) {
  180. args.GetReturnValue().Set(v8::False(isolate));
  181. return;
  182. }
  183. auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
  184. base::FilePath asar_path, file_path;
  185. if (asar::GetAsarArchivePath(path, &asar_path, &file_path, true)) {
  186. dict.Set("isAsar", true);
  187. dict.Set("asarPath", asar_path);
  188. dict.Set("filePath", file_path);
  189. } else {
  190. dict.Set("isAsar", false);
  191. }
  192. args.GetReturnValue().Set(dict.GetHandle());
  193. }
  194. void Initialize(v8::Local<v8::Object> exports,
  195. v8::Local<v8::Value> unused,
  196. v8::Local<v8::Context> context,
  197. void* priv) {
  198. auto* isolate = exports->GetIsolate();
  199. auto cons = Archive::CreateFunctionTemplate(isolate)
  200. ->GetFunction(context)
  201. .ToLocalChecked();
  202. cons->SetName(node::FIXED_ONE_BYTE_STRING(isolate, "Archive"));
  203. exports->Set(context, node::FIXED_ONE_BYTE_STRING(isolate, "Archive"), cons)
  204. .Check();
  205. NODE_SET_METHOD(exports, "splitPath", &SplitPath);
  206. NODE_SET_METHOD(exports, "initAsarSupport", &InitAsarSupport);
  207. }
  208. } // namespace
  209. NODE_LINKED_BINDING_CONTEXT_AWARE(electron_common_asar, Initialize)