Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: net/cookies/canonical_cookie.h

Issue 10785017: Move CanonicalCookie into separate files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added missing include Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #ifndef NET_COOKIES_CANONICAL_COOKIE_H_
6 #define NET_COOKIES_CANONICAL_COOKIE_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/time.h"
13 #include "net/base/net_export.h"
14
15 class GURL;
16
17 namespace net {
18
19 class ParsedCookie;
20
21 class NET_EXPORT CanonicalCookie {
22 public:
23
24 // These constructors do no validation or canonicalization of their inputs;
25 // the resulting CanonicalCookies should not be relied on to be canonical
26 // unless the caller has done appropriate validation and canonicalization
27 // themselves.
28 CanonicalCookie();
29 CanonicalCookie(const GURL& url,
30 const std::string& name,
31 const std::string& value,
32 const std::string& domain,
33 const std::string& path,
34 const std::string& mac_key,
35 const std::string& mac_algorithm,
36 const base::Time& creation,
37 const base::Time& expiration,
38 const base::Time& last_access,
39 bool secure,
40 bool httponly);
41
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);
46
47 ~CanonicalCookie();
48
49 // Supports the default copy constructor.
50
51 // Creates a canonical cookie from parsed cookie.
52 // Canonicalizes and validates inputs. May return NULL if an attribute
53 // value is invalid.
54 static CanonicalCookie* Create(const GURL& url,
55 const ParsedCookie& pc);
56
57 // Creates a canonical cookie from unparsed attribute values.
58 // Canonicalizes and validates inputs. May return NULL if an attribute
59 // value is invalid.
60 static CanonicalCookie* Create(const GURL& url,
61 const std::string& name,
62 const std::string& value,
63 const std::string& domain,
64 const std::string& path,
65 const std::string& mac_key,
66 const std::string& mac_algorithm,
67 const base::Time& creation,
68 const base::Time& expiration,
69 bool secure,
70 bool http_only);
71
72 const std::string& Source() const { return source_; }
73 const std::string& Name() const { return name_; }
74 const std::string& Value() const { return value_; }
75 const std::string& Domain() const { return domain_; }
76 const std::string& Path() const { return path_; }
77 const std::string& MACKey() const { return mac_key_; }
78 const std::string& MACAlgorithm() const { return mac_algorithm_; }
79 const base::Time& CreationDate() const { return creation_date_; }
80 const base::Time& LastAccessDate() const { return last_access_date_; }
81 bool IsPersistent() const { return !expiry_date_.is_null(); }
82 const base::Time& ExpiryDate() const { return expiry_date_; }
83 bool IsSecure() const { return secure_; }
84 bool IsHttpOnly() const { return httponly_; }
85 bool IsDomainCookie() const {
86 return !domain_.empty() && domain_[0] == '.'; }
87 bool IsHostCookie() const { return !IsDomainCookie(); }
88
89 bool IsExpired(const base::Time& current) {
90 return !expiry_date_.is_null() && current >= expiry_date_;
91 }
92
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());
105 }
106
107 void SetLastAccessDate(const base::Time& date) {
108 last_access_date_ = date;
109 }
110
111 bool IsOnPath(const std::string& url_path) const;
112 bool IsDomainMatch(const std::string& scheme, const std::string& host) const;
113
114 std::string DebugString() const;
115
116 // Returns the cookie source when cookies are set for |url|. This function
117 // is public for unit test purposes only.
118 static std::string GetCookieSourceFromURL(const GURL& url);
119 static std::string CanonPath(const GURL& url, const ParsedCookie& pc);
120 static base::Time CanonExpiration(const ParsedCookie& pc,
121 const base::Time& current,
122 const base::Time& server_time);
123
124 private:
125 // Gives the session cookie an expiration time if needed
126 void SetSessionCookieExpiryTime();
127
128 // The source member of a canonical cookie is the origin of the URL that tried
129 // to set this cookie, minus the port number if any. This field is not
130 // persistent though; its only used in the in-tab cookies dialog to show the
131 // user the source URL. This is used for both allowed and blocked cookies.
132 // When a CanonicalCookie is constructed from the backing store (common case)
133 // this field will be null. CanonicalCookie consumers should not rely on
134 // this field unless they guarantee that the creator of those
135 // CanonicalCookies properly initialized the field.
136 // TODO(abarth): We might need to make this field persistent for MAC cookies.
137 std::string source_;
138 std::string name_;
139 std::string value_;
140 std::string domain_;
141 std::string path_;
142 std::string mac_key_; // TODO(abarth): Persist to disk.
143 std::string mac_algorithm_; // TODO(abarth): Persist to disk.
144 base::Time creation_date_;
145 base::Time expiry_date_;
146 base::Time last_access_date_;
147 bool secure_;
148 bool httponly_;
149 };
150
151 class CookieList : public std::vector<CanonicalCookie> {
152 };
153
154 } // namespace net
155
156 #endif // NET_COOKIES_CANONICAL_COOKIE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698