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 #include "NET_NetworkCookie.h"
18 
19 #include <UT/UT_Array.h>
20 #include <UT/UT_LMHost.h>
21 #include <UT/UT_Lock.h>
22 #include <UT/UT_Optional.h>
23 #include <UT/UT_SQL.h>
24 #include <UT/UT_StringHolder.h>
25 #include <UT/UT_UniquePtr.h>
26 #include <UT/UT_Url.h>
27 #include <UT/UT_WorkBuffer.h>
28 
29 #include <SYS/SYS_Compiler.h>
30 #include <SYS/SYS_Hash.h>
31 
32 #define NO_DISCARD SYS_NO_DISCARD_RESULT
33 
35 {
36 public:
40 
42  {
45  ForcePersistentCookies
46  };
47 
48  enum Mode
49  {
51  Async
52  };
53 
55  virtual ~NET_NetworkCookieStore();
56 
57  void init();
58 
59  // Start the thread to async load and flush pending ops
60  void setPersistentStoragePath(const UT_StringRef &storage_location);
61 
62  virtual bool load();
63  virtual void loadAsync();
64  // Flush any pending operations
65  virtual bool flush();
66 
67  // Delete the cookie from the in memory cookie store and the persistent
68  // store.
69  virtual bool deleteCookie(const NET_NetworkCookie &cookie);
70  // Check if we can update an existing cookie. Otherwise insert the cookie.
71  // In either case pending operations are added to update the persistent
72  // store.
73  virtual bool insertCookie(const NET_NetworkCookie &cookie);
74  // Try to update an existing cookie in the store. This adds a pending
75  // operation if a cookie was added.
76  virtual bool updateCookie(const NET_NetworkCookie &cookie);
77  // Grab all cookies that apply to the passed in url.
78  virtual CookieList cookiesForUrl(const UT_Url &url);
79  // Insert or update existing cookies based on the url.
80  virtual bool setCookiesFromUrl(const CookieList &cookies,
81  const UT_Url &url);
82  // Remove all cookies in our store
83  void deleteAllCookies();
84 
85  void setStoragePolicy(StoragePolicy policy);
86 
87  void setMode(Mode mode);
88 
89  // Useful for debug printing whats currently in the store.
90  void debugPrintStore() const;
91 
92  // The launcher does not depend on FS so the default cookie storage is in
93  // UT which then gets used here so that everyone can share the same cookie
94  // store path.
95  NO_DISCARD static UT_StringHolder defaultCookieStoragePath(
96  UT_LMHost::AppType app_type,
97  const UT_StringRef &name);
98  /// Find a cookie in the cookie store based on the cookie identifier.
100 
101  // Create the directories for the db file.
102  void createDBFileLocationIfNeeded();
103  // Grab a list of all cookies. This is really only meant to be used for
104  // debugging and testing.
106  {
107  CookieList cookies;
108  cookies.setCapacity(myCookies.size());
109  for (auto&& it : myCookies)
110  {
111  cookies.emplace_back(it.second);
112  }
113  return cookies;
114  }
115 
116  const UT_StringHolder& persistentStorage() const { return myPersistentStorage; }
117 
118  bool isAsync() const { return myMode == Async; }
119 protected:
120  // Clear all existing cookies and pending operations and set the cookie
121  // store to the passed in cookie list.
122  void setAllCookies(const CookieList &cookies);
123  // Validate the cookie is usable for the passed in url
124  NO_DISCARD virtual bool validateCookie(const NET_NetworkCookie &cookie,
125  const UT_Url &url) const;
126 
127 private:
128 
129  // This class allows us to defer an operation to our persistent storage
130  // until we have accumulated enough operations, timeout expired or
131  // we are exiting the application. This also allows us to modify the
132  // persistent storage only when something has changed with that storage
133  class PendingOperation
134  {
135  public:
136  enum Type
137  {
138  INSERT_COOKIE,
139  DELETE_COOKIE,
140  UPDATE_ACCESS
141  };
142 
143  PendingOperation(Type type, const NET_NetworkCookie &cookie)
144  : myType(type), myCookie(cookie)
145  {
146  }
147 
148  Type myType;
149  NET_NetworkCookie myCookie;
150  };
151  using PendingOperationList = UT_Array<PendingOperation>;
152 
153  // Represents the unique key as defined in the sqlite db.
154  class UniqueKey
155  {
156  public:
157  UniqueKey(const NET_NetworkCookie &cookie)
158  : myDomain(cookie.domain())
159  , myName(cookie.name())
160  , myPath(cookie.path())
161  {
162  }
163 
164  bool operator==(const UniqueKey &other) const
165  {
166  return myDomain == other.myDomain && myName == other.myName
167  && myPath == other.myPath;
168  }
169  bool operator!=(const UniqueKey &other) const
170  {
171  return !(*this == other);
172  }
173 
174  SYS_HashType hash() const
175  {
176  SYS_HashType h = myDomain.hash();
177  SYShashCombine(h, myName.hash());
178  SYShashCombine(h, myPath.hash());
179  return h;
180  }
181 
182  friend std::size_t hash_value(const UniqueKey &key)
183  {
184  return key.hash();
185  }
186 
187  private:
188  UT_StringHolder myDomain;
189  UT_StringHolder myName;
190  UT_StringHolder myPath;
191  };
192 
193  // Try to update the cookies access time if we already have one in our
194  // store.
195  bool updateCookieAccess(const NET_NetworkCookie& cookie);
196  // Insert the cookie into our in memory store. Returns true if the cookie
197  // was inserted.
198  // WARNING:
199  // Be very careful when you use this function as duplicate checking
200  // is not done and a pending operation is not added. If you need
201  // duplicate checking or to add a pending operation then call
202  // updateCookie() or insertCookie()
203  bool internalInsertCookie(const NET_NetworkCookie &cookie);
204  // Add a cookie and the associated operation to our pending operations to
205  // be processed at a later time.
206  void addPendingOperation(PendingOperation::Type type,
207  const NET_NetworkCookie &cookie);
208  // Every 30s or more we want to update the cookie store. The idea is to lazy
209  // update the store. Its unlikely that the store will be updated constantly
210  // so we only update every so often.
211  void refreshIfRequired();
212  static bool isPersistentCookie(StoragePolicy policy,
213  const NET_NetworkCookie &cookie);
214 
215  bool fail(const UT_ErrorCode& ec, const char* msg);
216 
218  mutable UT_Lock myLock;
219  UT_StringHolder myPersistentStorage;
220  StoragePolicy myStoragePolicy;
221  Mode myMode;
223  PendingOperationList myPendingOps;
224  time_t myLastUpdate;
225 };
226 
227 #endif // __NET_NETWORKCOOKIESTORE_H__
228 
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
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
void setCapacity(exint new_capacity)
std::optional< T > UT_Optional
Definition: UT_Optional.h:26
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
#define NET_API
Definition: NET_API.h:9
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
exint emplace_back(S &&...s)
Definition: UT_ArrayImpl.h:769
Definition: UT_Url.h:22
GLuint const GLchar * name
Definition: glcorearb.h:786
GLenum mode
Definition: glcorearb.h:99
std::error_code UT_ErrorCode
Definition: UT_ErrorCode.h:20
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
size_t hash_value(const CH_ChannelRef &ref)
type
Definition: core.h:1059
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089