HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_NetworkCookieStore.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: NET_NetworkCookieStore.h
7  *
8  * COMMENTS:
9  * A Qt QNetworkCookieJar based cookie store that is designed to interface
10  * with QWebEngine. This class is also fully thread safe.
11  */
12 
13 #ifndef __NET_NETWORKCOOKIESTORE_H__
14 #define __NET_NETWORKCOOKIESTORE_H__
15 
16 #include "NET_API.h"
17 
18 
19 #include "NET_NetworkCookie.h"
20 #include "NET_RWLock.h"
21 
22 #include <UT/UT_ArraySet.h>
23 #include <UT/UT_BoostAsio.h>
24 #include <UT/UT_ErrorCode.h>
25 #include <UT/UT_LMHost.h>
26 #include <UT/UT_NonCopyable.h>
27 #include <UT/UT_Optional.h>
28 #include <UT/UT_SQL.h>
29 #include <UT/UT_SharedPtr.h>
30 #include <UT/UT_Lock.h>
31 
32 #define NO_DISCARD SYS_NO_DISCARD_RESULT
33 
34 class UT_SqlDatabase;
35 
37  : public UTenable_shared_from_this<NET_INetworkCookieStoreObserver>
38 {
39 public:
40  virtual ~NET_INetworkCookieStoreObserver() = default;
42 
43  template <typename T, typename... Args>
44  static UT_SharedPtr<T> create(Args&&... args)
45  {
46  return UTmakeShared<T>(std::forward<Args>(args)...);
47  }
48 
49  void notifyInsert(const NET_NetworkCookie &cookie)
50  {
52  }
53  void notifyDelete(const NET_NetworkCookie &cookie)
54  {
56  }
57 
58 protected:
59  virtual void onInsert(const NET_NetworkCookie &cookie) = 0;
60  virtual void onDelete(const NET_NetworkCookie &cookie) = 0;
61 
63  : myExecutor(exc)
64  {
65  }
66 
67 private:
68 
69  using func_ptr_t = void (NET_INetworkCookieStoreObserver::*)(
70  const NET_NetworkCookie &);
71  void notify_(func_ptr_t ptr, const NET_NetworkCookie &cookie)
72  {
73  hboost::asio::post(
74  myExecutor,
75  [weak = weak_from_this(), ptr, cookie]()
76  {
78  = weak.lock();
79  self != nullptr)
80  {
81  NET_INetworkCookieStoreObserver &observer = *self;
82 
83  (observer.*ptr)(cookie);
84  }
85  });
86  }
87 
88  ASIO_AnyExecutor myExecutor;
89 };
90 
92  : public UTenable_shared_from_this<NET_NetworkCookieStore>
93 {
94 protected:
96  {
97  explicit PrivateCtorTag(int) {}
98  };
99 
100 public:
104 
106  {
109  ForcePersistentCookies
110  };
111 
112  // Represents the unique key as defined in the sqlite db.
113  class UniqueKey
114  {
115  public:
116  explicit UniqueKey(const NET_NetworkCookie &cookie)
117  : myDomain(cookie.domain())
118  , myName(cookie.name())
119  , myPath(cookie.path())
120  {
121  }
122 
123  bool operator==(const UniqueKey &other) const
124  {
125  return myDomain == other.myDomain && myName == other.myName
126  && myPath == other.myPath;
127  }
128  bool operator!=(const UniqueKey &other) const
129  {
130  return !(*this == other);
131  }
132 
134  {
135  SYS_HashType h = myDomain.hash();
136  SYShashCombine(h, myName.hash());
137  SYShashCombine(h, myPath.hash());
138  return h;
139  }
140 
141  friend std::size_t hash_value(const UniqueKey &key)
142  {
143  return key.hash();
144  }
145 
146  private:
147  UT_StringHolder myDomain;
148  UT_StringHolder myName;
149  UT_StringHolder myPath;
150  };
151 
153  const PrivateCtorTag &,
154  const ASIO_AnyExecutor &exc)
156  {
157  }
160 
161  template <
162  typename ExecutionContext,
163  typename = typename std::enable_if_t<std::is_convertible_v<
164  ExecutionContext &,
165  hboost::asio::execution_context &>>>
167  ExecutionContext &ctx,
168  const UT_StringHolder &filename,
170  {
171  return create(ctx.get_executor(), filename, policy);
172  }
174  const ASIO_AnyExecutor& exc,
175  const UT_StringHolder &filename,
177  {
178  auto store = UTmakeShared<NET_NetworkCookieStore>(
179  PrivateCtorTag{0}, exc);
180  store->configure(filename, policy);
181  return store;
182  }
183  template <
184  typename ExecutionContext,
185  typename = typename std::enable_if_t<std::is_convertible_v<
186  ExecutionContext &,
187  hboost::asio::execution_context &>>>
189  ExecutionContext &ctx)
190  {
191  return create(ctx.get_executor());
192  }
194  const ASIO_AnyExecutor &exc)
195  {
196  return UTmakeShared<NET_NetworkCookieStore>(PrivateCtorTag{0}, exc);
197  }
198  void configure(const UT_StringHolder &storage, StoragePolicy policy);
199 
200  void shutdown();
201 
202  bool load();
203  // Flush any pending operations
204  bool flush();
205 
206  // Delete the cookie from the in memory cookie store and the persistent
207  // store.
208  bool deleteCookie(const NET_NetworkCookie &cookie);
209  // Check if we can update an existing cookie. Otherwise insert the cookie.
210  // In either case pending operations are added to update the persistent
211  // store.
212  bool insertCookie(const NET_NetworkCookie &cookie);
213  // Notify any observers a cookie was inserted.
214  void notifyInsert_(const NET_NetworkCookie &cookie);
215  // Try to update an existing cookie in the store. This adds a pending
216  // operation if a cookie was added.
217  bool updateCookie(const NET_NetworkCookie &cookie);
218  bool updateCookieAccess(const NET_NetworkCookie& cookie);
219  // Grab all cookies that apply to the passed in url.
220  CookieList cookiesForUrl(const UT_Url &url) const;
221  // Insert or update existing cookies based on the url.
222  bool setCookiesFromUrl(const CookieList &cookies, const UT_Url &url);
223  // Remove all cookies in our store
224  void deleteAllCookies();
225 
226  void setStoragePolicy(StoragePolicy policy);
227 
228  // Useful for debug printing whats currently in the store.
229  void debugPrintStore() const;
230 
231  /// Find a cookie in the cookie store based on the cookie identifier.
233 
234  // Grab a list of all cookies. This is really only meant to be used for
235  // debugging and testing.
236  CookieList allCookies() const;
237 
238  bool filter(NET_CookieList& cookies, const UT_StringRef& name) const;
239 
240  void attachObserver(
242  void detachObserver(
244 
245 private:
246  explicit NET_NetworkCookieStore(const ASIO_AnyExecutor& exc);
247 
248  // This class allows us to defer an operation to our persistent storage
249  // until we have accumulated enough operations, timeout expired or
250  // we are exiting the application. This also allows us to modify the
251  // persistent storage only when something has changed with that storage
252  class PendingOperation
253  {
254  public:
255  enum Type
256  {
257  INSERT_COOKIE,
258  DELETE_COOKIE,
259  UPDATE_ACCESS
260  };
261 
262  PendingOperation(Type type, const NET_NetworkCookie &cookie)
263  : myType(type), myCookie(cookie)
264  {
265  }
266 
267  Type myType;
268  NET_NetworkCookie myCookie;
269  };
270  using PendingOperationList = UT_Array<PendingOperation>;
271 
272 private:
273  void addPendingOperation_(
275  const NET_NetworkCookie &cookie);
276  bool insertCookie_(const NET_NetworkCookie& cookie);
277  bool deleteCookie_(const NET_NetworkCookie &cookie);
278  bool load_(
280  PendingOperationList &pending_ops);
281  bool fail(const UT_ErrorCode& ec, const char* what);
282 
283  UT_Lock myDBLock;
284  UT_SqlDatabase myDB;
285 
286  mutable NET_RWLock myRWLock;
287  UT_StringHolder myPersistentStorage;
288  StoragePolicy myStoragePolicy = StoragePolicy::ForcePersistentCookies;
290  PendingOperationList myPendingOps;
291  ASIO_AnyExecutor myExecutor;
292 
294 };
295 
296 #endif // __NET_NETWORKCOOKIESTORE_H__
typename std::enable_if< B, T >::type enable_if_t
Define Imath::enable_if_t to be std for C++14, equivalent for C++11.
GT_API const UT_StringHolder filename
Unsorted map container.
Definition: UT_Map.h:109
hboost::math::policies::policy< hboost::math::policies::domain_error< hboost::math::policies::ignore_error >, hboost::math::policies::pole_error< hboost::math::policies::ignore_error >, hboost::math::policies::overflow_error< hboost::math::policies::ignore_error >, hboost::math::policies::underflow_error< hboost::math::policies::ignore_error >, hboost::math::policies::denorm_error< hboost::math::policies::ignore_error >, hboost::math::policies::rounding_error< hboost::math::policies::ignore_error >, hboost::math::policies::evaluation_error< hboost::math::policies::ignore_error >, hboost::math::policies::indeterminate_result_error< hboost::math::policies::ignore_error > > policy
Definition: SYS_MathCbrt.h:35
void
Definition: png.h:1083
getFileOption("OpenEXR:storage") storage
Definition: HDK_Image.dox:276
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2138
std::optional< T > UT_Optional
Definition: UT_Optional.h:26
#define NET_API
Definition: NET_API.h:9
std::enable_shared_from_this< T > UTenable_shared_from_this
Definition: UT_SharedPtr.h:39
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
Definition: UT_Url.h:25
hboost::shared_mutex NET_RWLock
Definition: NET_RWLock.h:79
GLuint const GLchar * name
Definition: glcorearb.h:786
std::error_code UT_ErrorCode
Definition: UT_ErrorCode.h:20
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
auto ptr(T p) -> const void *
Definition: format.h:4331
**If you just want to fire and args
Definition: thread.h:618
OIIO_API void shutdown()
hboost::asio::any_io_executor ASIO_AnyExecutor
Definition: UT_BoostAsio.h:78
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1297