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

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: Diff 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 <deque>
9 #include <map>
10 #include <queue>
11 #include <set>
12 #include <string>
13 #include <utility>
14 #include <vector>
15
16 #include "base/basictypes.h"
17 #include "base/callback_forward.h"
18 #include "base/gtest_prod_util.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/synchronization/lock.h"
22 #include "base/time.h"
23 #include "net/cookies/cookie_store.h"
24 #include "net/base/net_export.h"
25
26 class GURL;
27
28 namespace net {
29
30 class ParsedCookie;
31
32 class NET_EXPORT CanonicalCookie {
33 public:
34
35 // These constructors do no validation or canonicalization of their inputs;
36 // the resulting CanonicalCookies should not be relied on to be canonical
37 // unless the caller has done appropriate validation and canonicalization
38 // themselves.
39 CanonicalCookie();
40 CanonicalCookie(const GURL& url,
41 const std::string& name,
42 const std::string& value,
43 const std::string& domain,
44 const std::string& path,
45 const std::string& mac_key,
46 const std::string& mac_algorithm,
47 const base::Time& creation,
48 const base::Time& expiration,
49 const base::Time& last_access,
50 bool secure,
51 bool httponly);
52
53 // This constructor does canonicalization but not validation.
54 // The result of this constructor should not be relied on in contexts
55 // in which pre-validation of the ParsedCookie has not been done.
56 CanonicalCookie(const GURL& url, const ParsedCookie& pc);
57
58 ~CanonicalCookie();
59
60 // Supports the default copy constructor.
61
62 // Creates a canonical cookie from parsed cookie.
63 // Canonicalizes and validates inputs. May return NULL if an attribute
64 // value is invalid.
65 static CanonicalCookie* Create(const GURL& url,
66 const ParsedCookie& pc);
67
68 // Creates a canonical cookie from unparsed attribute values.
69 // Canonicalizes and validates inputs. May return NULL if an attribute
70 // value is invalid.
71 static CanonicalCookie* Create(const GURL& url,
72 const std::string& name,
73 const std::string& value,
74 const std::string& domain,
75 const std::string& path,
76 const std::string& mac_key,
77 const std::string& mac_algorithm,
78 const base::Time& creation,
79 const base::Time& expiration,
80 bool secure,
81 bool http_only);
82
83 const std::string& Source() const { return source_; }
84 const std::string& Name() const { return name_; }
85 const std::string& Value() const { return value_; }
86 const std::string& Domain() const { return domain_; }
87 const std::string& Path() const { return path_; }
88 const std::string& MACKey() const { return mac_key_; }
89 const std::string& MACAlgorithm() const { return mac_algorithm_; }
90 const base::Time& CreationDate() const { return creation_date_; }
91 const base::Time& LastAccessDate() const { return last_access_date_; }
92 bool IsPersistent() const { return !expiry_date_.is_null(); }
93 const base::Time& ExpiryDate() const { return expiry_date_; }
94 bool IsSecure() const { return secure_; }
95 bool IsHttpOnly() const { return httponly_; }
96 bool IsDomainCookie() const {
97 return !domain_.empty() && domain_[0] == '.'; }
98 bool IsHostCookie() const { return !IsDomainCookie(); }
99
100 bool IsExpired(const base::Time& current) {
101 return !expiry_date_.is_null() && current >= expiry_date_;
102 }
103
104 // Are the cookies considered equivalent in the eyes of RFC 2965.
105 // The RFC says that name must match (case-sensitive), domain must
106 // match (case insensitive), and path must match (case sensitive).
107 // For the case insensitive domain compare, we rely on the domain
108 // having been canonicalized (in
109 // GetCookieDomainWithString->CanonicalizeHost).
110 bool IsEquivalent(const CanonicalCookie& ecc) const {
111 // It seems like it would make sense to take secure and httponly into
112 // account, but the RFC doesn't specify this.
113 // NOTE: Keep this logic in-sync with TrimDuplicateCookiesForHost().
114 return (name_ == ecc.Name() && domain_ == ecc.Domain()
115 && path_ == ecc.Path());
116 }
117
118 void SetLastAccessDate(const base::Time& date) {
119 last_access_date_ = date;
120 }
121
122 bool IsOnPath(const std::string& url_path) const;
123 bool IsDomainMatch(const std::string& scheme, const std::string& host) const;
124
125 std::string DebugString() const;
126
127 // Returns the cookie source when cookies are set for |url|. This function
128 // is public for unit test purposes only.
129 static std::string GetCookieSourceFromURL(const GURL& url);
130 static std::string CanonPath(const GURL& url, const ParsedCookie& pc);
131 static base::Time CanonExpiration(const ParsedCookie& pc,
132 const base::Time& current,
133 const base::Time& server_time);
134
135 private:
136 // Gives the session cookie an expiration time if needed
137 void SetSessionCookieExpiryTime();
138
139 // The source member of a canonical cookie is the origin of the URL that tried
140 // to set this cookie, minus the port number if any. This field is not
141 // persistent though; its only used in the in-tab cookies dialog to show the
142 // user the source URL. This is used for both allowed and blocked cookies.
143 // When a CanonicalCookie is constructed from the backing store (common case)
144 // this field will be null. CanonicalCookie consumers should not rely on
145 // this field unless they guarantee that the creator of those
146 // CanonicalCookies properly initialized the field.
147 // TODO(abarth): We might need to make this field persistent for MAC cookies.
148 std::string source_;
149 std::string name_;
150 std::string value_;
151 std::string domain_;
152 std::string path_;
153 std::string mac_key_; // TODO(abarth): Persist to disk.
154 std::string mac_algorithm_; // TODO(abarth): Persist to disk.
155 base::Time creation_date_;
156 base::Time expiry_date_;
157 base::Time last_access_date_;
158 bool secure_;
159 bool httponly_;
160 };
161
162 class CookieList : public std::vector<CanonicalCookie> {
163 };
164
165 } // namespace net
166
167 #endif // NET_COOKIES_CANONICAL_COOKIE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698