electron_api_net.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 "shell/browser/api/electron_api_url_loader.h"
  11. #include "shell/common/gin_converters/file_path_converter.h"
  12. #include "shell/common/gin_converters/gurl_converter.h"
  13. #include "shell/common/gin_helper/dictionary.h"
  14. #include "shell/common/gin_helper/error_thrower.h"
  15. #include "shell/common/gin_helper/object_template_builder.h"
  16. #include "shell/common/node_includes.h"
  17. namespace {
  18. bool IsOnline() {
  19. return !net::NetworkChangeNotifier::IsOffline();
  20. }
  21. bool IsValidHeaderName(std::string header_name) {
  22. return net::HttpUtil::IsValidHeaderName(header_name);
  23. }
  24. bool IsValidHeaderValue(std::string header_value) {
  25. return net::HttpUtil::IsValidHeaderValue(header_value);
  26. }
  27. base::FilePath FileURLToFilePath(v8::Isolate* isolate, const GURL& url) {
  28. base::FilePath path;
  29. if (!net::FileURLToFilePath(url, &path))
  30. gin_helper::ErrorThrower(isolate).ThrowError(
  31. "Failed to convert URL to file path");
  32. return path;
  33. }
  34. using electron::api::SimpleURLLoaderWrapper;
  35. void Initialize(v8::Local<v8::Object> exports,
  36. v8::Local<v8::Value> unused,
  37. v8::Local<v8::Context> context,
  38. void* priv) {
  39. v8::Isolate* isolate = context->GetIsolate();
  40. gin_helper::Dictionary dict(isolate, exports);
  41. dict.SetMethod("isOnline", &IsOnline);
  42. dict.SetMethod("isValidHeaderName", &IsValidHeaderName);
  43. dict.SetMethod("isValidHeaderValue", &IsValidHeaderValue);
  44. dict.SetMethod("createURLLoader", &SimpleURLLoaderWrapper::Create);
  45. dict.SetMethod("fileURLToFilePath", &FileURLToFilePath);
  46. }
  47. } // namespace
  48. NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_net, Initialize)