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
,
40 CookiePriority priority
);
42 // This constructor does canonicalization but not validation.
43 // The result of this constructor should not be relied on in contexts
44 // in which pre-validation of the ParsedCookie has not been done.
45 CanonicalCookie(const GURL
& url
, const ParsedCookie
& pc
);
49 // Supports the default copy constructor.
51 // Creates a new |CanonicalCookie| from the |cookie_line| and the
52 // |creation_time|. Canonicalizes and validates inputs. May return NULL if
53 // an attribut value is invalid.
54 static CanonicalCookie
* Create(const GURL
& url
,
55 const std::string
& cookie_line
,
56 const base::Time
& creation_time
,
57 const CookieOptions
& options
);
59 // Creates a canonical cookie from unparsed attribute values.
60 // Canonicalizes and validates inputs. May return NULL if an attribute
62 static CanonicalCookie
* Create(const GURL
& url
,
63 const std::string
& name
,
64 const std::string
& value
,
65 const std::string
& domain
,
66 const std::string
& path
,
67 const base::Time
& creation
,
68 const base::Time
& expiration
,
71 CookiePriority priority
);
73 const std::string
& Source() const { return source_
; }
74 const std::string
& Name() const { return name_
; }
75 const std::string
& Value() const { return value_
; }
76 const std::string
& Domain() const { return domain_
; }
77 const std::string
& Path() const { return path_
; }
78 const base::Time
& CreationDate() const { return creation_date_
; }
79 const base::Time
& LastAccessDate() const { return last_access_date_
; }
80 bool IsPersistent() const { return !expiry_date_
.is_null(); }
81 const base::Time
& ExpiryDate() const { return expiry_date_
; }
82 bool IsSecure() const { return secure_
; }
83 bool IsHttpOnly() const { return httponly_
; }
84 CookiePriority
Priority() const { return priority_
; }
85 bool IsDomainCookie() const {
86 return !domain_
.empty() && domain_
[0] == '.'; }
87 bool IsHostCookie() const { return !IsDomainCookie(); }
89 bool IsExpired(const base::Time
& current
) const {
90 return !expiry_date_
.is_null() && current
>= expiry_date_
;
93 // Are the cookies considered equivalent in the eyes of RFC 2965.
94 // The RFC says that name must match (case-sensitive), domain must
95 // match (case insensitive), and path must match (case sensitive).
96 // For the case insensitive domain compare, we rely on the domain
97 // having been canonicalized (in
98 // GetCookieDomainWithString->CanonicalizeHost).
99 bool IsEquivalent(const CanonicalCookie
& ecc
) const {
100 // It seems like it would make sense to take secure and httponly into
101 // account, but the RFC doesn't specify this.
102 // NOTE: Keep this logic in-sync with TrimDuplicateCookiesForHost().
103 return (name_
== ecc
.Name() && domain_
== ecc
.Domain()
104 && path_
== ecc
.Path());
107 void SetLastAccessDate(const base::Time
& date
) {
108 last_access_date_
= date
;
111 // Returns true if the given |url_path| path-matches the cookie-path as
112 // described in section 5.1.4 in RFC 6265.
113 bool IsOnPath(const std::string
& url_path
) const;
115 // Returns true if the cookie domain matches the given |host| as described in
116 // section 5.1.3 of RFC 6265.
117 bool IsDomainMatch(const std::string
& host
) const;
119 // Returns true if the cookie should be included for the given request |url|.
120 // HTTP only cookies can be filter by using appropriate cookie |options|.
121 // PLEASE NOTE that this method does not check whether a cookie is expired or
123 bool IncludeForRequestURL(const GURL
& url
,
124 const CookieOptions
& options
) const;
126 std::string
DebugString() const;
128 // Returns a duplicate of this cookie.
129 CanonicalCookie
* Duplicate();
131 // Returns the cookie source when cookies are set for |url|. This function
132 // is public for unit test purposes only.
133 static std::string
GetCookieSourceFromURL(const GURL
& url
);
134 static std::string
CanonPath(const GURL
& url
, const ParsedCookie
& pc
);
135 static base::Time
CanonExpiration(const ParsedCookie
& pc
,
136 const base::Time
& current
,
137 const base::Time
& server_time
);
140 // NOTE: When any new members are added below, the implementation of
141 // Duplicate() must be updated to copy the new member accordingly.
143 // The source member of a canonical cookie is the origin of the URL that tried
144 // to set this cookie, minus the port number if any. This field is not
145 // persistent though; its only used in the in-tab cookies dialog to show the
146 // user the source URL. This is used for both allowed and blocked cookies.
147 // When a CanonicalCookie is constructed from the backing store (common case)
148 // this field will be null. CanonicalCookie consumers should not rely on
149 // this field unless they guarantee that the creator of those
150 // CanonicalCookies properly initialized the field.
156 base::Time creation_date_
;
157 base::Time expiry_date_
;
158 base::Time last_access_date_
;
161 CookiePriority priority_
;
162 // NOTE: When any new members are added above this comment, the
163 // implementation of Duplicate() must be updated to copy the new member
167 typedef std::vector
<CanonicalCookie
> CookieList
;
171 #endif // NET_COOKIES_CANONICAL_COOKIE_H_