net_converter.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (c) 2019 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_COMMON_GIN_CONVERTERS_NET_CONVERTER_H_
  5. #define ELECTRON_SHELL_COMMON_GIN_CONVERTERS_NET_CONVERTER_H_
  6. #include <utility>
  7. #include <vector>
  8. #include "gin/converter.h"
  9. #include "services/network/public/mojom/fetch_api.mojom.h"
  10. #include "services/network/public/mojom/host_resolver.mojom.h"
  11. #include "services/network/public/mojom/url_request.mojom.h"
  12. #include "shell/browser/net/cert_verifier_client.h"
  13. namespace net {
  14. class AuthChallengeInfo;
  15. class X509Certificate;
  16. class HttpResponseHeaders;
  17. struct CertPrincipal;
  18. class HttpVersion;
  19. } // namespace net
  20. namespace network {
  21. struct ResourceRequest;
  22. }
  23. namespace gin {
  24. template <>
  25. struct Converter<net::AuthChallengeInfo> {
  26. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  27. const net::AuthChallengeInfo& val);
  28. };
  29. template <>
  30. struct Converter<scoped_refptr<net::X509Certificate>> {
  31. static v8::Local<v8::Value> ToV8(
  32. v8::Isolate* isolate,
  33. const scoped_refptr<net::X509Certificate>& val);
  34. static bool FromV8(v8::Isolate* isolate,
  35. v8::Local<v8::Value> val,
  36. scoped_refptr<net::X509Certificate>* out);
  37. };
  38. template <>
  39. struct Converter<net::CertPrincipal> {
  40. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  41. const net::CertPrincipal& val);
  42. };
  43. template <>
  44. struct Converter<net::HttpResponseHeaders*> {
  45. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  46. net::HttpResponseHeaders* headers);
  47. static bool FromV8(v8::Isolate* isolate,
  48. v8::Local<v8::Value> val,
  49. net::HttpResponseHeaders* out);
  50. };
  51. template <>
  52. struct Converter<net::HttpRequestHeaders> {
  53. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  54. const net::HttpRequestHeaders& headers);
  55. static bool FromV8(v8::Isolate* isolate,
  56. v8::Local<v8::Value> val,
  57. net::HttpRequestHeaders* out);
  58. };
  59. template <>
  60. struct Converter<network::ResourceRequestBody> {
  61. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  62. const network::ResourceRequestBody& val);
  63. };
  64. template <>
  65. struct Converter<scoped_refptr<network::ResourceRequestBody>> {
  66. static v8::Local<v8::Value> ToV8(
  67. v8::Isolate* isolate,
  68. const scoped_refptr<network::ResourceRequestBody>& val);
  69. static bool FromV8(v8::Isolate* isolate,
  70. v8::Local<v8::Value> val,
  71. scoped_refptr<network::ResourceRequestBody>* out);
  72. };
  73. template <>
  74. struct Converter<network::ResourceRequest> {
  75. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  76. const network::ResourceRequest& val);
  77. };
  78. template <>
  79. struct Converter<electron::VerifyRequestParams> {
  80. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  81. electron::VerifyRequestParams val);
  82. };
  83. template <>
  84. struct Converter<net::HttpVersion> {
  85. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  86. const net::HttpVersion& val);
  87. };
  88. template <>
  89. struct Converter<net::RedirectInfo> {
  90. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  91. const net::RedirectInfo& val);
  92. };
  93. template <>
  94. struct Converter<net::IPEndPoint> {
  95. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  96. const net::IPEndPoint& val);
  97. };
  98. template <>
  99. struct Converter<net::DnsQueryType> {
  100. static bool FromV8(v8::Isolate* isolate,
  101. v8::Local<v8::Value> val,
  102. net::DnsQueryType* out);
  103. };
  104. template <>
  105. struct Converter<net::HostResolverSource> {
  106. static bool FromV8(v8::Isolate* isolate,
  107. v8::Local<v8::Value> val,
  108. net::HostResolverSource* out);
  109. };
  110. template <>
  111. struct Converter<network::mojom::ResolveHostParameters::CacheUsage> {
  112. static bool FromV8(v8::Isolate* isolate,
  113. v8::Local<v8::Value> val,
  114. network::mojom::ResolveHostParameters::CacheUsage* out);
  115. };
  116. template <>
  117. struct Converter<network::mojom::SecureDnsPolicy> {
  118. static bool FromV8(v8::Isolate* isolate,
  119. v8::Local<v8::Value> val,
  120. network::mojom::SecureDnsPolicy* out);
  121. };
  122. template <>
  123. struct Converter<network::mojom::ResolveHostParametersPtr> {
  124. static bool FromV8(v8::Isolate* isolate,
  125. v8::Local<v8::Value> val,
  126. network::mojom::ResolveHostParametersPtr* out);
  127. };
  128. template <typename K, typename V>
  129. struct Converter<std::vector<std::pair<K, V>>> {
  130. static bool FromV8(v8::Isolate* isolate,
  131. v8::Local<v8::Value> value,
  132. std::vector<std::pair<K, V>>* out) {
  133. if (!value->IsObject())
  134. return false;
  135. out->clear();
  136. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  137. v8::Local<v8::Object> obj = value.As<v8::Object>();
  138. v8::Local<v8::Array> keys = obj->GetPropertyNames(context).ToLocalChecked();
  139. for (uint32_t i = 0; i < keys->Length(); ++i) {
  140. v8::Local<v8::Value> v8key;
  141. if (!keys->Get(context, i).ToLocal(&v8key))
  142. return false;
  143. v8::Local<v8::Value> v8value;
  144. if (!obj->Get(context, v8key).ToLocal(&v8value))
  145. return false;
  146. K key;
  147. V out_value;
  148. if (!ConvertFromV8(isolate, v8key, &key) ||
  149. !ConvertFromV8(isolate, v8value, &out_value))
  150. return false;
  151. (*out).emplace_back(std::move(key), std::move(out_value));
  152. }
  153. return true;
  154. }
  155. };
  156. } // namespace gin
  157. #endif // ELECTRON_SHELL_COMMON_GIN_CONVERTERS_NET_CONVERTER_H_