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