electron_api_net.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) 2016 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 <string>
  5. #include "gin/handle.h"
  6. #include "net/base/filename_util.h"
  7. #include "net/base/network_change_notifier.h"
  8. #include "net/http/http_util.h"
  9. #include "services/network/public/cpp/features.h"
  10. #include "services/network/public/mojom/host_resolver.mojom.h"
  11. #include "shell/browser/net/resolve_host_function.h"
  12. #include "shell/common/api/electron_api_url_loader.h"
  13. #include "shell/common/gin_converters/file_path_converter.h"
  14. #include "shell/common/gin_converters/gurl_converter.h"
  15. #include "shell/common/gin_converters/net_converter.h"
  16. #include "shell/common/gin_helper/dictionary.h"
  17. #include "shell/common/gin_helper/error_thrower.h"
  18. #include "shell/common/gin_helper/object_template_builder.h"
  19. #include "shell/common/gin_helper/promise.h"
  20. #include "shell/common/node_includes.h"
  21. namespace {
  22. bool IsOnline() {
  23. return !net::NetworkChangeNotifier::IsOffline();
  24. }
  25. bool IsValidHeaderName(std::string header_name) {
  26. return net::HttpUtil::IsValidHeaderName(header_name);
  27. }
  28. bool IsValidHeaderValue(std::string header_value) {
  29. return net::HttpUtil::IsValidHeaderValue(header_value);
  30. }
  31. base::FilePath FileURLToFilePath(v8::Isolate* isolate, const GURL& url) {
  32. base::FilePath path;
  33. if (!net::FileURLToFilePath(url, &path))
  34. gin_helper::ErrorThrower(isolate).ThrowError(
  35. "Failed to convert URL to file path");
  36. return path;
  37. }
  38. v8::Local<v8::Promise> ResolveHost(
  39. v8::Isolate* isolate,
  40. std::string host,
  41. std::optional<network::mojom::ResolveHostParametersPtr> params) {
  42. gin_helper::Promise<gin_helper::Dictionary> promise(isolate);
  43. v8::Local<v8::Promise> handle = promise.GetHandle();
  44. auto fn = base::MakeRefCounted<electron::ResolveHostFunction>(
  45. nullptr, std::move(host), params ? std::move(params.value()) : nullptr,
  46. base::BindOnce(
  47. [](gin_helper::Promise<gin_helper::Dictionary> promise,
  48. int64_t net_error, const std::optional<net::AddressList>& addrs) {
  49. if (net_error < 0) {
  50. promise.RejectWithErrorMessage(net::ErrorToString(net_error));
  51. } else {
  52. DCHECK(addrs.has_value() && !addrs->empty());
  53. v8::HandleScope handle_scope(promise.isolate());
  54. auto dict =
  55. gin_helper::Dictionary::CreateEmpty(promise.isolate());
  56. dict.Set("endpoints", addrs->endpoints());
  57. promise.Resolve(dict);
  58. }
  59. },
  60. std::move(promise)));
  61. fn->Run();
  62. return handle;
  63. }
  64. using electron::api::SimpleURLLoaderWrapper;
  65. void Initialize(v8::Local<v8::Object> exports,
  66. v8::Local<v8::Value> unused,
  67. v8::Local<v8::Context> context,
  68. void* priv) {
  69. v8::Isolate* isolate = context->GetIsolate();
  70. gin_helper::Dictionary dict(isolate, exports);
  71. dict.SetMethod("isOnline", &IsOnline);
  72. dict.SetMethod("isValidHeaderName", &IsValidHeaderName);
  73. dict.SetMethod("isValidHeaderValue", &IsValidHeaderValue);
  74. dict.SetMethod("createURLLoader", &SimpleURLLoaderWrapper::Create);
  75. dict.SetMethod("fileURLToFilePath", &FileURLToFilePath);
  76. dict.SetMethod("resolveHost", &ResolveHost);
  77. }
  78. } // namespace
  79. NODE_LINKED_BINDING_CONTEXT_AWARE(electron_common_net, Initialize)