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_BoostAsio.h>
23 #include <UT/UT_ErrorCode.h>
24 #include <UT/UT_LMHost.h>
25 #include <UT/UT_NonCopyable.h>
26 #include <UT/UT_Optional.h>
27 #include <UT/UT_SQL.h>
28 #include <UT/UT_SharedPtr.h>
29 #include <UT/UT_Lock.h>
30 
31 #define NO_DISCARD SYS_NO_DISCARD_RESULT
32 
33 class UT_SqlDatabase;
34 
36  : public UTenable_shared_from_this<NET_INetworkCookieStoreObserver>
37 {
38 public:
39  virtual ~NET_INetworkCookieStoreObserver() = default;
41 
42  template <typename T, typename... Args>
43  static UT_SharedPtr<T> create(Args&&... args)
44  {
45  return UTmakeShared<T>(std::forward<Args>(args)...);
46  }
47 
48  void notifyInsert(const NET_NetworkCookie &cookie)
49  {
51  }
52  void notifyDelete(const NET_NetworkCookie &cookie)
53  {
55  }
56 
57 protected:
58  virtual void onInsert(const NET_NetworkCookie &cookie) = 0;
59  virtual void onDelete(const NET_NetworkCookie &cookie) = 0;
60 
62  : myExecutor(exc)
63  {
64  }
65 
66 private:
67 
68  using func_ptr_t = void (NET_INetworkCookieStoreObserver::*)(
69  const NET_NetworkCookie &);
70  void notify_(func_ptr_t ptr, const NET_NetworkCookie &cookie)
71  {
72  hboost::asio::post(
73  myExecutor,
74  [weak = weak_from_this(), ptr, cookie]()
75  {
77  = weak.lock();
78  self != nullptr)
79  {
80  NET_INetworkCookieStoreObserver &observer = *self;
81 
82  (observer.*ptr)(cookie);
83  }
84  });
85  }
86 
87  ASIO_AnyExecutor myExecutor;
88 };
89 
91  : public UTenable_shared_from_this<NET_NetworkCookieStore>
92 {
93 protected:
95  {
96  explicit PrivateCtorTag(int) {}
97  };
98 
99 public:
103 
105  {
108  ForcePersistentCookies
109  };
110 
111  // Represents the unique key as defined in the sqlite db.
112  class UniqueKey
113  {
114  public:
115  explicit UniqueKey(const NET_NetworkCookie &cookie)
116  : myDomain(cookie.domain())
117  , myName(cookie.name())
118  , myPath(cookie.path())
119  {
120  }
121 
122  bool operator==(const UniqueKey &other) const
123  {
124  return myDomain == other.myDomain && myName == other.myName
125  && myPath == other.myPath;
126  }
127  bool operator!=(const UniqueKey &other) const
128  {
129  return !(*this == other);
130  }
131 
133  {
134  SYS_HashType h = myDomain.hash();
135  SYShashCombine(h, myName.hash());
136  SYShashCombine(h, myPath.hash());
137  return h;
138  }
139 
140  friend std::size_t hash_value(const UniqueKey &key)
141  {
142  return key.hash();
143  }
144 
145  private:
146  UT_StringHolder myDomain;
147  UT_StringHolder myName;
148  UT_StringHolder myPath;
149  };
150 
152  const PrivateCtorTag &,
153  const ASIO_AnyExecutor &exc)
155  {
156  }
159 
160  template <
161  typename ExecutionContext,
162  typename = typename std::enable_if_t<std::is_convertible_v<
163  ExecutionContext &,
164  hboost::asio::execution_context &>>>
166  ExecutionContext &ctx,
167  const UT_StringHolder &filename,
169  {
170  return create(ctx.get_executor(), filename, policy);
171  }
173  const ASIO_AnyExecutor& exc,
174  const UT_StringHolder &filename,
176  {
177  auto store = UTmakeShared<NET_NetworkCookieStore>(
178  PrivateCtorTag{0}, exc);
179  store->configure(filename, policy);
180  return store;
181  }
182  template <
183  typename ExecutionContext,
184  typename = typename std::enable_if_t<std::is_convertible_v<
185  ExecutionContext &,
186  hboost::asio::execution_context &>>>
188  ExecutionContext &ctx)
189  {
190  return create(ctx.get_executor());
191  }
193  const ASIO_AnyExecutor &exc)
194  {
195  return UTmakeShared<NET_NetworkCookieStore>(PrivateCtorTag{0}, exc);
196  }
197  void configure(const UT_StringHolder &storage, StoragePolicy policy);
198 
199  void shutdown();
200 
201  bool load();
202  // Flush any pending operations
203  bool flush();
204 
205  // Delete the cookie from the in memory cookie store and the persistent
206  // store.
207  bool deleteCookie(const NET_NetworkCookie &cookie);
208  // Check if we can update an existing cookie. Otherwise insert the cookie.
209  // In either case pending operations are added to update the persistent
210  // store.
211  bool insertCookie(const NET_NetworkCookie &cookie);
212  // Notify any observers a cookie was inserted.
213  void notifyInsert_(const NET_NetworkCookie &cookie);
214  // Try to update an existing cookie in the store. This adds a pending
215  // operation if a cookie was added.
216  bool updateCookie(const NET_NetworkCookie &cookie);
217  bool updateCookieAccess(const NET_NetworkCookie& cookie);
218  // Grab all cookies that apply to the passed in url.
219  CookieList cookiesForUrl(const UT_Url &url) const;
220  // Insert or update existing cookies based on the url.
221  bool setCookiesFromUrl(const CookieList &cookies, const UT_Url &url);
222  // Remove all cookies in our store
223  void deleteAllCookies();
224 
225  void setStoragePolicy(StoragePolicy policy);
226 
227  // Useful for debug printing whats currently in the store.
228  void debugPrintStore() const;
229 
230  /// Find a cookie in the cookie store based on the cookie identifier.
232 
233  // Grab a list of all cookies. This is really only meant to be used for
234  // debugging and testing.
235  CookieList allCookies() const;
236 
237  bool filter(NET_CookieList& cookies, const UT_StringRef& name) const;
238 
239  void attachObserver(
241  void detachObserver(
243 
244 private:
245  explicit NET_NetworkCookieStore(const ASIO_AnyExecutor& exc);
246 
247  // This class allows us to defer an operation to our persistent storage
248  // until we have accumulated enough operations, timeout expired or
249  // we are exiting the application. This also allows us to modify the
250  // persistent storage only when something has changed with that storage
251  class PendingOperation
252  {
253  public:
254  enum Type
255  {
256  INSERT_COOKIE,
257  DELETE_COOKIE,
258  UPDATE_ACCESS
259  };
260 
261  PendingOperation(Type type, const NET_NetworkCookie &cookie)
262  : myType(type), myCookie(cookie)
263  {
264  }
265 
266  Type myType;
267  NET_NetworkCookie myCookie;
268  };
269  using PendingOperationList = UT_Array<PendingOperation>;
270 
271 private:
272  void addPendingOperation_(
274  const NET_NetworkCookie &cookie);
275  bool insertCookie_(const NET_NetworkCookie& cookie);
276  bool deleteCookie_(const NET_NetworkCookie &cookie);
277  bool load_(
279  PendingOperationList &pending_ops);
280  bool fail(const UT_ErrorCode& ec, const char* what);
281 
282  UT_Lock myDBLock;
283  UT_SqlDatabase myDB;
284 
285  mutable NET_RWLock myRWLock;
286  UT_StringHolder myPersistentStorage;
287  StoragePolicy myStoragePolicy = StoragePolicy::ForcePersistentCookies;
289  PendingOperationList myPendingOps;
290  ASIO_AnyExecutor myExecutor;
291 
293 };
294 
295 #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:107
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
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
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:22
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:2448
**If you just want to fire and args
Definition: thread.h:609
type
Definition: core.h:1059
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089
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