OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_COMMON_COOKIE_TRAITS_H_ |
| 6 #define CONTENT_COMMON_COOKIE_TRAITS_H_ |
| 7 |
| 8 #include "content/common/cookie.mojom.h" |
| 9 #include "ipc/ipc_message_utils.h" |
| 10 #include "mojo/public/cpp/bindings/enum_traits.h" |
| 11 #include "net/cookies/canonical_cookie.h" |
| 12 #include "net/cookies/cookie_constants.h" |
| 13 #include "net/cookies/cookie_options.h" |
| 14 #include "net/cookies/cookie_store.h" |
| 15 |
| 16 namespace mojo { |
| 17 |
| 18 template <> |
| 19 struct EnumTraits<content::mojom::CookiePriority, net::CookiePriority> { |
| 20 static content::mojom::CookiePriority ToMojom(net::CookiePriority input); |
| 21 static bool FromMojom(content::mojom::CookiePriority input, |
| 22 net::CookiePriority* output); |
| 23 }; |
| 24 |
| 25 template <> |
| 26 struct EnumTraits<content::mojom::CookieSameSite, net::CookieSameSite> { |
| 27 static content::mojom::CookieSameSite ToMojom(net::CookieSameSite input); |
| 28 static bool FromMojom(content::mojom::CookieSameSite input, |
| 29 net::CookieSameSite* output); |
| 30 }; |
| 31 |
| 32 template <> |
| 33 struct EnumTraits<content::mojom::CookieSameSiteFilter, |
| 34 net::CookieOptions::SameSiteCookieMode> { |
| 35 static content::mojom::CookieSameSiteFilter ToMojom( |
| 36 net::CookieOptions::SameSiteCookieMode input); |
| 37 |
| 38 static bool FromMojom(content::mojom::CookieSameSiteFilter input, |
| 39 net::CookieOptions::SameSiteCookieMode* output); |
| 40 }; |
| 41 |
| 42 template <> |
| 43 struct StructTraits<content::mojom::CookieOptionsDataView, net::CookieOptions> { |
| 44 static bool exclude_httponly(const net::CookieOptions& o) { |
| 45 return o.exclude_httponly(); |
| 46 } |
| 47 static net::CookieOptions::SameSiteCookieMode cookie_same_site_filter( |
| 48 const net::CookieOptions& o) { |
| 49 return o.same_site_cookie_mode(); |
| 50 } |
| 51 static bool update_access_time(const net::CookieOptions& o) { |
| 52 return o.update_access_time(); |
| 53 } |
| 54 static base::Optional<base::Time> server_time(const net::CookieOptions& o) { |
| 55 if (!o.has_server_time()) |
| 56 return base::nullopt; |
| 57 return base::Optional<base::Time>(o.server_time()); |
| 58 } |
| 59 |
| 60 static bool Read(content::mojom::CookieOptionsDataView mojo_options, |
| 61 net::CookieOptions* cookie_options) { |
| 62 if (mojo_options.exclude_httponly()) |
| 63 cookie_options->set_exclude_httponly(); |
| 64 else |
| 65 cookie_options->set_include_httponly(); |
| 66 if (mojo_options.update_access_time()) |
| 67 cookie_options->set_update_access_time(); |
| 68 else |
| 69 cookie_options->set_do_not_update_access_time(); |
| 70 base::Optional<base::Time> optional_server_time; |
| 71 if (!mojo_options.ReadServerTime(&optional_server_time)) |
| 72 return false; |
| 73 if (optional_server_time) { |
| 74 cookie_options->set_server_time( |
| 75 optional_server_time ? *optional_server_time : base::Time()); |
| 76 } |
| 77 return true; |
| 78 } |
| 79 }; |
| 80 |
| 81 template <> |
| 82 struct StructTraits<content::mojom::CanonicalCookieDataView, |
| 83 net::CanonicalCookie> { |
| 84 static const std::string& name(const net::CanonicalCookie& c) { |
| 85 return c.Name(); |
| 86 } |
| 87 static const std::string& value(const net::CanonicalCookie& c) { |
| 88 return c.Value(); |
| 89 } |
| 90 static const std::string& domain(const net::CanonicalCookie& c) { |
| 91 return c.Domain(); |
| 92 } |
| 93 static const std::string& path(const net::CanonicalCookie& c) { |
| 94 return c.Path(); |
| 95 } |
| 96 static base::Optional<base::Time> creation(const net::CanonicalCookie& c) { |
| 97 return c.CreationDate().is_null() |
| 98 ? base::Optional<base::Time>() |
| 99 : base::Optional<base::Time>(c.CreationDate()); |
| 100 } |
| 101 static base::Optional<base::Time> expiry(const net::CanonicalCookie& c) { |
| 102 return c.ExpiryDate().is_null() |
| 103 ? base::Optional<base::Time>() |
| 104 : base::Optional<base::Time>(c.ExpiryDate()); |
| 105 } |
| 106 static base::Optional<base::Time> last_access(const net::CanonicalCookie& c) { |
| 107 return c.LastAccessDate().is_null() |
| 108 ? base::Optional<base::Time>() |
| 109 : base::Optional<base::Time>(c.LastAccessDate()); |
| 110 } |
| 111 static bool secure(const net::CanonicalCookie& c) { return c.IsSecure(); } |
| 112 static bool httponly(const net::CanonicalCookie& c) { return c.IsHttpOnly(); } |
| 113 static net::CookieSameSite site_restrictions(const net::CanonicalCookie& c) { |
| 114 return c.SameSite(); |
| 115 } |
| 116 static net::CookiePriority priority(const net::CanonicalCookie& c) { |
| 117 return c.Priority(); |
| 118 } |
| 119 |
| 120 static bool Read(content::mojom::CanonicalCookieDataView cookie, |
| 121 net::CanonicalCookie* out) { |
| 122 std::string name; |
| 123 if (!cookie.ReadName(&name)) |
| 124 return false; |
| 125 |
| 126 std::string value; |
| 127 if (!cookie.ReadValue(&value)) |
| 128 return false; |
| 129 |
| 130 std::string domain; |
| 131 if (!cookie.ReadDomain(&domain)) |
| 132 return false; |
| 133 |
| 134 std::string path; |
| 135 if (!cookie.ReadPath(&path)) |
| 136 return false; |
| 137 |
| 138 base::Optional<base::Time> creation_time; |
| 139 base::Optional<base::Time> expiry_time; |
| 140 base::Optional<base::Time> last_access_time; |
| 141 if (!cookie.ReadCreation(&creation_time)) |
| 142 return false; |
| 143 |
| 144 if (!cookie.ReadExpiry(&expiry_time)) |
| 145 return false; |
| 146 |
| 147 if (!cookie.ReadLastAccess(&last_access_time)) |
| 148 return false; |
| 149 |
| 150 net::CookieSameSite site_restrictions; |
| 151 if (!cookie.ReadSiteRestrictions(&site_restrictions)) |
| 152 return false; |
| 153 |
| 154 net::CookiePriority priority; |
| 155 if (!cookie.ReadPriority(&priority)) |
| 156 return false; |
| 157 |
| 158 *out = net::CanonicalCookie( |
| 159 name, value, domain, path, |
| 160 creation_time ? *creation_time : base::Time(), |
| 161 expiry_time ? *expiry_time : base::Time(), |
| 162 last_access_time ? *last_access_time : base::Time(), cookie.secure(), |
| 163 cookie.httponly(), site_restrictions, priority); |
| 164 if (!out->IsCanonical()) |
| 165 return false; |
| 166 return true; |
| 167 } |
| 168 }; |
| 169 |
| 170 } // namespace mojo |
| 171 |
| 172 #endif // CONTENT_COMMON_COOKIES_TRAITS_H_ |
OLD | NEW |