2016-10-25 10:41:01 +00:00
|
|
|
// Copyright (c) 2016 GitHub, Inc.
|
2016-09-15 13:59:40 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
#include <string>
|
|
|
|
|
2019-10-24 05:47:58 +00:00
|
|
|
#include "gin/handle.h"
|
2022-03-16 23:23:41 +00:00
|
|
|
#include "net/base/filename_util.h"
|
2020-10-21 02:55:06 +00:00
|
|
|
#include "net/base/network_change_notifier.h"
|
2022-02-25 18:17:35 +00:00
|
|
|
#include "net/http/http_util.h"
|
2019-07-04 01:56:19 +00:00
|
|
|
#include "services/network/public/cpp/features.h"
|
2024-01-04 21:20:37 +00:00
|
|
|
#include "services/network/public/mojom/host_resolver.mojom.h"
|
|
|
|
#include "shell/browser/net/resolve_host_function.h"
|
|
|
|
#include "shell/common/api/electron_api_url_loader.h"
|
2022-03-16 23:23:41 +00:00
|
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
|
|
|
#include "shell/common/gin_converters/gurl_converter.h"
|
2024-01-04 21:20:37 +00:00
|
|
|
#include "shell/common/gin_converters/net_converter.h"
|
2019-10-24 05:47:58 +00:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2022-03-16 23:23:41 +00:00
|
|
|
#include "shell/common/gin_helper/error_thrower.h"
|
2019-10-24 05:47:58 +00:00
|
|
|
#include "shell/common/gin_helper/object_template_builder.h"
|
2024-01-04 21:20:37 +00:00
|
|
|
#include "shell/common/gin_helper/promise.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2016-09-15 13:59:40 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-10-21 02:55:06 +00:00
|
|
|
bool IsOnline() {
|
|
|
|
return !net::NetworkChangeNotifier::IsOffline();
|
|
|
|
}
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
bool IsValidHeaderName(std::string header_name) {
|
|
|
|
return net::HttpUtil::IsValidHeaderName(header_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValidHeaderValue(std::string header_value) {
|
|
|
|
return net::HttpUtil::IsValidHeaderValue(header_value);
|
|
|
|
}
|
|
|
|
|
2022-03-16 23:23:41 +00:00
|
|
|
base::FilePath FileURLToFilePath(v8::Isolate* isolate, const GURL& url) {
|
|
|
|
base::FilePath path;
|
|
|
|
if (!net::FileURLToFilePath(url, &path))
|
|
|
|
gin_helper::ErrorThrower(isolate).ThrowError(
|
|
|
|
"Failed to convert URL to file path");
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2024-01-04 21:20:37 +00:00
|
|
|
v8::Local<v8::Promise> ResolveHost(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
std::string host,
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<network::mojom::ResolveHostParametersPtr> params) {
|
2024-01-04 21:20:37 +00:00
|
|
|
gin_helper::Promise<gin_helper::Dictionary> promise(isolate);
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
|
|
|
auto fn = base::MakeRefCounted<electron::ResolveHostFunction>(
|
|
|
|
nullptr, std::move(host), params ? std::move(params.value()) : nullptr,
|
|
|
|
base::BindOnce(
|
|
|
|
[](gin_helper::Promise<gin_helper::Dictionary> promise,
|
2024-01-10 22:23:35 +00:00
|
|
|
int64_t net_error, const std::optional<net::AddressList>& addrs) {
|
2024-01-04 21:20:37 +00:00
|
|
|
if (net_error < 0) {
|
|
|
|
promise.RejectWithErrorMessage(net::ErrorToString(net_error));
|
|
|
|
} else {
|
|
|
|
DCHECK(addrs.has_value() && !addrs->empty());
|
|
|
|
|
|
|
|
v8::HandleScope handle_scope(promise.isolate());
|
|
|
|
auto dict =
|
|
|
|
gin_helper::Dictionary::CreateEmpty(promise.isolate());
|
|
|
|
dict.Set("endpoints", addrs->endpoints());
|
|
|
|
promise.Resolve(dict);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
std::move(promise)));
|
|
|
|
|
|
|
|
fn->Run();
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
using electron::api::SimpleURLLoaderWrapper;
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-10-14 09:50:47 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2016-09-15 13:59:40 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
|
|
|
2019-10-24 05:47:58 +00:00
|
|
|
gin_helper::Dictionary dict(isolate, exports);
|
2020-10-21 02:55:06 +00:00
|
|
|
dict.SetMethod("isOnline", &IsOnline);
|
2020-03-23 20:09:45 +00:00
|
|
|
dict.SetMethod("isValidHeaderName", &IsValidHeaderName);
|
|
|
|
dict.SetMethod("isValidHeaderValue", &IsValidHeaderValue);
|
|
|
|
dict.SetMethod("createURLLoader", &SimpleURLLoaderWrapper::Create);
|
2022-03-16 23:23:41 +00:00
|
|
|
dict.SetMethod("fileURLToFilePath", &FileURLToFilePath);
|
2024-01-04 21:20:37 +00:00
|
|
|
dict.SetMethod("resolveHost", &ResolveHost);
|
2016-09-15 13:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2024-01-04 21:20:37 +00:00
|
|
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_common_net, Initialize)
|