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_COOKIES_TRAITS_H_ | |
6 #define CONTENT_COMMON_COOKIES_TRAITS_H_ | |
7 | |
8 #include "content/common/cookies.mojom.h" | |
9 #include "mojo/public/cpp/bindings/enum_traits.h" | |
10 #include "net/cookies/cookie_constants.h" | |
11 #include "net/cookies/cookie_options.h" | |
12 #include "net/cookies/cookie_store.h" | |
13 | |
14 namespace mojo { | |
15 | |
16 template <> | |
17 struct EnumTraits<content::mojom::CookiePriority, net::CookiePriority> { | |
18 static content::mojom::CookiePriority ToMojom(net::CookiePriority input); | |
19 static bool FromMojom(content::mojom::CookiePriority input, | |
20 net::CookiePriority* output); | |
21 }; | |
22 | |
23 template <> | |
24 struct EnumTraits<content::mojom::CookieSameSite, net::CookieSameSite> { | |
25 static content::mojom::CookieSameSite ToMojom(net::CookieSameSite input); | |
26 static bool FromMojom(content::mojom::CookieSameSite input, | |
27 net::CookieSameSite* output); | |
28 }; | |
29 | |
30 template <> | |
31 struct EnumTraits<content::mojom::CookieSameSiteFilter, | |
32 net::CookieOptions::SameSiteCookieMode> { | |
33 static content::mojom::CookieSameSiteFilter ToMojom( | |
34 net::CookieOptions::SameSiteCookieMode input); | |
35 | |
36 static bool FromMojom(content::mojom::CookieSameSiteFilter input, | |
37 net::CookieOptions::SameSiteCookieMode* output); | |
38 }; | |
39 | |
40 template <> | |
41 struct EnumTraits<content::mojom::CookieChangeCause, | |
42 net::CookieStore::ChangeCause> { | |
43 static content::mojom::CookieChangeCause ToMojom( | |
44 net::CookieStore::ChangeCause input); | |
45 | |
46 static bool FromMojom(content::mojom::CookieChangeCause input, | |
47 net::CookieStore::ChangeCause* output); | |
48 }; | |
49 | |
50 template <> | |
51 struct StructTraits<content::mojom::CookieOptions, net::CookieOptions> { | |
52 static bool exclude_httponly(const net::CookieOptions& o) { | |
53 return o.exclude_httponly(); | |
54 } | |
55 static content::mojom::CookieSameSiteFilter cookie_same_site_filter( | |
yzshen1
2017/06/20 18:33:17
You could directly return net::CookieOptions::Same
| |
56 const net::CookieOptions& o) { | |
57 return EnumTraits<content::mojom::CookieSameSiteFilter, | |
58 net::CookieOptions::SameSiteCookieMode>:: | |
59 ToMojom(o.same_site_cookie_mode()); | |
60 } | |
61 static bool update_access_time(const net::CookieOptions& o) { | |
62 return o.update_access_time(); | |
63 } | |
64 static base::Optional<base::Time> server_time(const net::CookieOptions& o) { | |
65 if (!o.has_server_time()) | |
66 return base::Optional<base::Time>(); | |
yzshen1
2017/06/20 18:33:17
nit: you could use base::nullopt
| |
67 return base::Optional<base::Time>(o.server_time()); | |
68 } | |
69 | |
70 static bool Read(const content::mojom::CookieOptions& mojo_options, | |
71 net::CookieOptions* cookie_options) { | |
72 if (mojo_options.exclude_httponly) | |
73 cookie_options->set_exclude_httponly(); | |
74 else | |
75 cookie_options->set_include_httponly(); | |
76 if (mojo_options.update_access_time) | |
77 cookie_options->set_update_access_time(); | |
78 else | |
79 cookie_options->set_do_not_update_access_time(); | |
80 cookie_options->set_server_time( | |
81 mojo_options.server_time ? *mojo_options.server_time : base::Time()); | |
82 return true; | |
83 } | |
84 }; | |
85 | |
86 template <> | |
87 struct StructTraits<content::mojom::CanonicalCookie, net::CanonicalCookie> { | |
88 static std::string name(const net::CanonicalCookie& c) { return c.Name(); } | |
yzshen1
2017/06/20 18:33:17
It is more efficient to return const& in this case
| |
89 static std::string value(const net::CanonicalCookie& c) { return c.Value(); } | |
90 static std::string domain(const net::CanonicalCookie& c) { | |
91 return c.Domain(); | |
92 } | |
93 static std::string path(const net::CanonicalCookie& c) { return c.Path(); } | |
94 static base::Time creation(const net::CanonicalCookie& c) { | |
yzshen1
2017/06/20 18:33:17
Ditto
| |
95 return c.CreationDate(); | |
96 } | |
97 static base::Time expiry(const net::CanonicalCookie& c) { | |
98 return c.ExpiryDate(); | |
99 } | |
100 static base::Time last_access(const net::CanonicalCookie& c) { | |
101 return c.LastAccessDate(); | |
102 } | |
103 static bool secure(const net::CanonicalCookie& c) { return c.IsSecure(); } | |
104 static bool httponly(const net::CanonicalCookie& c) { return c.IsHttpOnly(); } | |
105 static content::mojom::CookieSameSite site_restrictions( | |
106 const net::CanonicalCookie& c) { | |
107 return EnumTraits<content::mojom::CookieSameSite, | |
108 net::CookieSameSite>::ToMojom(c.SameSite()); | |
109 } | |
110 static content::mojom::CookiePriority priority( | |
111 const net::CanonicalCookie& c) { | |
112 return EnumTraits<content::mojom::CookiePriority, | |
113 net::CookiePriority>::ToMojom(c.Priority()); | |
114 } | |
115 | |
116 static bool Read(const content::mojom::CanonicalCookie& cookie, | |
117 net::CanonicalCookie* out) { | |
118 // TODO(rdsmith): There's no point in allocating this object only to delete | |
119 // it at the end of the function; fix the CC declaration to avoid this. | |
120 std::unique_ptr<net::CanonicalCookie> output_cc( | |
121 net::CanonicalCookie::Create( | |
122 cookie.name, cookie.value, cookie.domain, cookie.path, | |
123 cookie.creation, cookie.expiry, cookie.last_access, cookie.secure, | |
124 cookie.httponly, cookie.site_restrictions, cookie.priority)); | |
125 *out = *output_cc.get(); | |
126 return true; | |
127 } | |
128 }; | |
129 | |
130 } // namespace mojo | |
131 | |
132 #endif // CONTENT_COMMON_COOKIES_TRAITS_H_ | |
OLD | NEW |