atom_api_net.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 "atom/browser/api/atom_api_net.h"
  5. #include "atom/browser/api/atom_api_url_request.h"
  6. #include "atom/common/node_includes.h"
  7. #include "native_mate/dictionary.h"
  8. namespace atom {
  9. namespace api {
  10. Net::Net(v8::Isolate* isolate) {
  11. Init(isolate);
  12. }
  13. Net::~Net() {}
  14. // static
  15. v8::Local<v8::Value> Net::Create(v8::Isolate* isolate) {
  16. return mate::CreateHandle(isolate, new Net(isolate)).ToV8();
  17. }
  18. // static
  19. void Net::BuildPrototype(v8::Isolate* isolate,
  20. v8::Local<v8::FunctionTemplate> prototype) {
  21. prototype->SetClassName(mate::StringToV8(isolate, "Net"));
  22. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  23. .SetProperty("URLRequest", &Net::URLRequest);
  24. }
  25. v8::Local<v8::Value> Net::URLRequest(v8::Isolate* isolate) {
  26. return URLRequest::GetConstructor(isolate)
  27. ->GetFunction(isolate->GetCurrentContext())
  28. .ToLocalChecked();
  29. }
  30. } // namespace api
  31. } // namespace atom
  32. namespace {
  33. using atom::api::Net;
  34. using atom::api::URLRequest;
  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. URLRequest::SetConstructor(isolate, base::Bind(URLRequest::New));
  41. mate::Dictionary dict(isolate, exports);
  42. dict.Set("net", Net::Create(isolate));
  43. dict.Set("Net",
  44. Net::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
  45. }
  46. } // namespace
  47. NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_net, Initialize)