atom_api_net.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "shell/browser/api/atom_api_net.h"
  5. #include <string>
  6. #include "native_mate/dictionary.h"
  7. #include "native_mate/handle.h"
  8. #include "services/network/public/cpp/features.h"
  9. #include "shell/browser/api/atom_api_url_loader.h"
  10. #include "shell/common/node_includes.h"
  11. namespace electron {
  12. namespace api {
  13. Net::Net(v8::Isolate* isolate) {
  14. Init(isolate);
  15. }
  16. Net::~Net() {}
  17. // static
  18. v8::Local<v8::Value> Net::Create(v8::Isolate* isolate) {
  19. return mate::CreateHandle(isolate, new Net(isolate)).ToV8();
  20. }
  21. // static
  22. void Net::BuildPrototype(v8::Isolate* isolate,
  23. v8::Local<v8::FunctionTemplate> prototype) {
  24. prototype->SetClassName(mate::StringToV8(isolate, "Net"));
  25. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  26. .SetProperty("URLLoader", &Net::URLLoader);
  27. }
  28. v8::Local<v8::Value> Net::URLLoader(v8::Isolate* isolate) {
  29. v8::Local<v8::FunctionTemplate> constructor;
  30. constructor = SimpleURLLoaderWrapper::GetConstructor(isolate);
  31. return constructor->GetFunction(isolate->GetCurrentContext())
  32. .ToLocalChecked();
  33. }
  34. } // namespace api
  35. } // namespace electron
  36. namespace {
  37. bool IsValidHeaderName(std::string header_name) {
  38. return net::HttpUtil::IsValidHeaderName(header_name);
  39. }
  40. bool IsValidHeaderValue(std::string header_value) {
  41. return net::HttpUtil::IsValidHeaderValue(header_value);
  42. }
  43. using electron::api::Net;
  44. using electron::api::SimpleURLLoaderWrapper;
  45. void Initialize(v8::Local<v8::Object> exports,
  46. v8::Local<v8::Value> unused,
  47. v8::Local<v8::Context> context,
  48. void* priv) {
  49. v8::Isolate* isolate = context->GetIsolate();
  50. SimpleURLLoaderWrapper::SetConstructor(
  51. isolate, base::BindRepeating(SimpleURLLoaderWrapper::New));
  52. mate::Dictionary dict(isolate, exports);
  53. dict.Set("net", Net::Create(isolate));
  54. dict.Set("Net",
  55. Net::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
  56. dict.SetMethod("_isValidHeaderName", &IsValidHeaderName);
  57. dict.SetMethod("_isValidHeaderValue", &IsValidHeaderValue);
  58. }
  59. } // namespace
  60. NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_net, Initialize)