electron_api_net.cc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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/network_change_notifier.h"
  7. #include "services/network/public/cpp/features.h"
  8. #include "shell/browser/api/electron_api_url_loader.h"
  9. #include "shell/common/gin_helper/dictionary.h"
  10. #include "shell/common/gin_helper/object_template_builder.h"
  11. #include "shell/common/node_includes.h"
  12. namespace {
  13. bool IsOnline() {
  14. return !net::NetworkChangeNotifier::IsOffline();
  15. }
  16. bool IsValidHeaderName(std::string header_name) {
  17. return net::HttpUtil::IsValidHeaderName(header_name);
  18. }
  19. bool IsValidHeaderValue(std::string header_value) {
  20. return net::HttpUtil::IsValidHeaderValue(header_value);
  21. }
  22. using electron::api::SimpleURLLoaderWrapper;
  23. void Initialize(v8::Local<v8::Object> exports,
  24. v8::Local<v8::Value> unused,
  25. v8::Local<v8::Context> context,
  26. void* priv) {
  27. v8::Isolate* isolate = context->GetIsolate();
  28. gin_helper::Dictionary dict(isolate, exports);
  29. dict.SetMethod("isOnline", &IsOnline);
  30. dict.SetMethod("isValidHeaderName", &IsValidHeaderName);
  31. dict.SetMethod("isValidHeaderValue", &IsValidHeaderValue);
  32. dict.SetMethod("createURLLoader", &SimpleURLLoaderWrapper::Create);
  33. }
  34. } // namespace
  35. NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_net, Initialize)