xref: /haiku/headers/private/netservices/NetworkCookieJar.h (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 /*
2  * Copyright 2010-2013 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _B_NETWORK_COOKIE_JAR_H_
6 #define _B_NETWORK_COOKIE_JAR_H_
7 
8 #include <pthread.h>
9 
10 #include <Archivable.h>
11 #include <Flattenable.h>
12 #include <Message.h>
13 #include <NetworkCookie.h>
14 #include <ObjectList.h>
15 #include <String.h>
16 #include <Url.h>
17 
18 
19 #ifndef LIBNETAPI_DEPRECATED
20 namespace BPrivate {
21 
22 namespace Network {
23 #endif
24 
25 class BNetworkCookieList: public BObjectList<const BNetworkCookie> {
26 public:
27 								BNetworkCookieList();
28 								~BNetworkCookieList();
29 
30 			status_t			LockForReading();
31 			status_t			LockForWriting();
32 			status_t			Unlock();
33 private:
34 			pthread_rwlock_t	fLock;
35 };
36 
37 
38 class BNetworkCookieJar : public BArchivable, public BFlattenable {
39 public:
40 			// Nested types
41 			class Iterator;
42 			class UrlIterator;
43 			struct PrivateIterator;
44 			struct PrivateHashMap;
45 
46 public:
47 								BNetworkCookieJar();
48 								BNetworkCookieJar(
49 									const BNetworkCookieJar& other);
50 								BNetworkCookieJar(
51 									const BNetworkCookieList& otherList);
52 								BNetworkCookieJar(BMessage* archive);
53 	virtual						~BNetworkCookieJar();
54 
55 			status_t			AddCookie(const BNetworkCookie& cookie);
56 			status_t			AddCookie(const BString& cookie,
57 									const BUrl& url);
58 			status_t			AddCookie(BNetworkCookie* cookie);
59 			status_t			AddCookies(const BNetworkCookieList& cookies);
60 
61 			uint32				DeleteOutdatedCookies();
62 			uint32				PurgeForExit();
63 
64 	// BArchivable members
65 	virtual	status_t			Archive(BMessage* into,
66 									bool deep = true) const;
67 	static	BArchivable*		Instantiate(BMessage* archive);
68 
69 	// BFlattenable members
70 	virtual	bool				IsFixedSize() const;
71 	virtual	type_code			TypeCode() const;
72 	virtual	ssize_t				FlattenedSize() const;
73 	virtual	status_t			Flatten(void* buffer, ssize_t size)
74 									const;
75 	virtual	bool				AllowsTypeCode(type_code code) const;
76 	virtual	status_t			Unflatten(type_code code,
77 									const void* buffer, ssize_t size);
78 
79 			BNetworkCookieJar&	operator=(const BNetworkCookieJar& other);
80 
81 	// Iterators
82 			Iterator			GetIterator() const;
83 			UrlIterator			GetUrlIterator(const BUrl& url) const;
84 
85 
86 private:
87 			void				_DoFlatten() const;
88 
89 private:
90 	friend	class Iterator;
91 	friend	class UrlIterator;
92 
93 			PrivateHashMap*		fCookieHashMap;
94 	mutable	BString				fFlattened;
95 };
96 
97 
98 class BNetworkCookieJar::Iterator {
99 public:
100 								Iterator(const Iterator& other);
101 								~Iterator();
102 
103 			Iterator& 			operator=(const Iterator& other);
104 
105 			bool 				HasNext() const;
106 	const	BNetworkCookie*		Next();
107 	const	BNetworkCookie*		NextDomain();
108 	const	BNetworkCookie*		Remove();
109 			void				RemoveDomain();
110 
111 private:
112 								Iterator(const BNetworkCookieJar* map);
113 
114 			void 				_FindNext();
115 
116 private:
117 	friend 	class BNetworkCookieJar;
118 
119 			BNetworkCookieJar*	fCookieJar;
120 			PrivateIterator*	fIterator;
121 			BNetworkCookieList* fLastList;
122 			BNetworkCookieList*	fList;
123 	const	BNetworkCookie*		fElement;
124 	const	BNetworkCookie*		fLastElement;
125 			int32				fIndex;
126 };
127 
128 
129 // The copy constructor and assignment operator create new iterators for the
130 // same cookie jar and url. Iteration will start over.
131 class BNetworkCookieJar::UrlIterator {
132 public:
133 								UrlIterator(const UrlIterator& other);
134 								~UrlIterator();
135 
136 			bool 				HasNext() const;
137 	const	BNetworkCookie*		Next();
138 	const	BNetworkCookie*		Remove();
139 			UrlIterator& 		operator=(const UrlIterator& other);
140 
141 private:
142 								UrlIterator(const BNetworkCookieJar* map,
143 									const BUrl& url);
144 
145 			void				_Initialize();
146 			bool				_SuperDomain();
147 			void 				_FindNext();
148 			void				_FindDomain();
149 			bool				_FindPath();
150 
151 private:
152 	friend 	class BNetworkCookieJar;
153 
154 			BNetworkCookieJar*	fCookieJar;
155 			PrivateIterator*	fIterator;
156 			BNetworkCookieList*	fList;
157 			BNetworkCookieList* fLastList;
158 	const	BNetworkCookie*		fElement;
159 	const	BNetworkCookie*		fLastElement;
160 
161 			int32				fIndex;
162 			int32				fLastIndex;
163 
164 			BUrl				fUrl;
165 };
166 
167 #ifndef LIBNETAPI_DEPRECATED
168 } // namespace Network
169 
170 } // namespace BPrivate
171 #endif
172 
173 #endif // _B_NETWORK_COOKIE_JAR_
174