1 // Copyright (c) 2012 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.
5 #ifndef NET_COOKIES_CANONICAL_COOKIE_H_
6 #define NET_COOKIES_CANONICAL_COOKIE_H_
11 #include "base/basictypes.h"
12 #include "base/time/time.h"
13 #include "net/base/net_export.h"
14 #include "net/cookies/cookie_constants.h"
15 #include "net/cookies/cookie_options.h"
23 class NET_EXPORT CanonicalCookie
{
25 // These constructors do no validation or canonicalization of their inputs;
26 // the resulting CanonicalCookies should not be relied on to be canonical
27 // unless the caller has done appropriate validation and canonicalization
30 CanonicalCookie(const GURL
& url
,
31 const std::string
& name
,
32 const std::string
& value
,
33 const std::string
& domain
,
34 const std::string
& path
,
35 const base::Time
& creation
,
36 const base::Time
& expiration
,
37 const base::Time
& last_access
,
41 CookiePriority priority
);
43 // This constructor does canonicalization but not validation.
44 // The result of this constructor should not be relied on in contexts
45 // in which pre-validation of the ParsedCookie has not been done.
46 CanonicalCookie(const GURL
& url
, const ParsedCookie
& pc
);
50 // Supports the default copy constructor.
52 // Creates a new |CanonicalCookie| from the |cookie_line| and the
53 // |creation_time|. Canonicalizes and validates inputs. May return NULL if
54 // an attribut value is invalid.
55 static CanonicalCookie
* Create(const GURL
& url
,
56 const std::string
& cookie_line
,
57 const base::Time
& creation_time
,
58 const CookieOptions
& options
);
60 // Creates a canonical cookie from unparsed attribute values.
61 // Canonicalizes and validates inputs. May return NULL if an attribute
63 static CanonicalCookie
* Create(const GURL
& url
,
64 const std::string
& name
,
65 const std::string
& value
,
66 const std::string
& domain
,
67 const std::string
& path
,
68 const base::Time
& creation
,
69 const base::Time
& expiration
,
72 bool first_party_only
,
73 CookiePriority priority
);
75 const GURL
& Source() const { return source_
; }
76 const std::string
& Name() const { return name_
; }
77 const std::string
& Value() const { return value_
; }
78 const std::string
& Domain() const { return domain_
; }
79 const std::string
& Path() const { return path_
; }
80 const base::Time
& CreationDate() const { return creation_date_
; }
81 const base::Time
& LastAccessDate() const { return last_access_date_
; }
82 bool IsPersistent() const { return !expiry_date_
.is_null(); }
83 const base::Time
& ExpiryDate() const { return expiry_date_
; }
84 bool IsSecure() const { return secure_
; }
85 bool IsHttpOnly() const { return httponly_
; }
86 bool IsFirstPartyOnly() const { return first_party_only_
; }
87 CookiePriority
Priority() const { return priority_
; }
88 bool IsDomainCookie() const {
89 return !domain_
.empty() && domain_
[0] == '.'; }
90 bool IsHostCookie() const { return !IsDomainCookie(); }
92 bool IsExpired(const base::Time
& current
) const {
93 return !expiry_date_
.is_null() && current
>= expiry_date_
;
96 // Are the cookies considered equivalent in the eyes of RFC 2965.
97 // The RFC says that name must match (case-sensitive), domain must
98 // match (case insensitive), and path must match (case sensitive).
99 // For the case insensitive domain compare, we rely on the domain
100 // having been canonicalized (in
101 // GetCookieDomainWithString->CanonicalizeHost).
102 bool IsEquivalent(const CanonicalCookie
& ecc
) const {
103 // It seems like it would make sense to take secure and httponly into
104 // account, but the RFC doesn't specify this.
105 // NOTE: Keep this logic in-sync with TrimDuplicateCookiesForHost().
106 return (name_
== ecc
.Name() && domain_
== ecc
.Domain()
107 && path_
== ecc
.Path());
110 void SetLastAccessDate(const base::Time
& date
) {
111 last_access_date_
= date
;
114 // Returns true if the given |url_path| path-matches the cookie-path as
115 // described in section 5.1.4 in RFC 6265.
116 bool IsOnPath(const std::string
& url_path
) const;
118 // Returns true if the cookie domain matches the given |host| as described in
119 // section 5.1.3 of RFC 6265.
120 bool IsDomainMatch(const std::string
& host
) const;
122 // Returns true if the cookie should be included for the given request |url|.
123 // HTTP only cookies can be filter by using appropriate cookie |options|.
124 // PLEASE NOTE that this method does not check whether a cookie is expired or
126 bool IncludeForRequestURL(const GURL
& url
,
127 const CookieOptions
& options
) const;
129 std::string
DebugString() const;
131 static std::string
CanonPath(const GURL
& url
, const ParsedCookie
& pc
);
132 static base::Time
CanonExpiration(const ParsedCookie
& pc
,
133 const base::Time
& current
,
134 const base::Time
& server_time
);
136 // Cookie ordering methods.
138 // Returns true if the cookie is less than |other|, considering only name,
139 // domain and path. In particular, two equivalent cookies (see IsEquivalent())
140 // are identical for PartialCompare().
141 bool PartialCompare(const CanonicalCookie
& other
) const;
143 // Returns true if the cookie is less than |other|, considering all fields.
144 // FullCompare() is consistent with PartialCompare(): cookies sorted using
145 // FullCompare() are also sorted with respect to PartialCompare().
146 bool FullCompare(const CanonicalCookie
& other
) const;
149 // The source member of a canonical cookie is the origin of the URL that tried
150 // to set this cookie. This field is not persistent though; its only used in
151 // the in-tab cookies dialog to show the user the source URL. This is used for
152 // both allowed and blocked cookies.
153 // When a CanonicalCookie is constructed from the backing store (common case)
154 // this field will be null. CanonicalCookie consumers should not rely on
155 // this field unless they guarantee that the creator of those
156 // CanonicalCookies properly initialized the field.
162 base::Time creation_date_
;
163 base::Time expiry_date_
;
164 base::Time last_access_date_
;
167 bool first_party_only_
;
168 CookiePriority priority_
;
171 typedef std::vector
<CanonicalCookie
> CookieList
;
175 #endif // NET_COOKIES_CANONICAL_COOKIE_H_