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_StringHolder.h>
24 #include <UT/UT_Task.h>
25 #include <UT/UT_Url.h>
26 #include <UT/UT_WorkBuffer.h>
27 
28 #include <SYS/SYS_Compiler.h>
29 
30 #define NO_DISCARD SYS_NO_DISCARD_RESULT
31 
33 {
34 public:
38 
40  {
43  ForcePersistentCookies
44  };
45 
46  enum Mode
47  {
49  Async
50  };
51 
53  virtual ~NET_NetworkCookieStore();
54 
55  // Start the thread to async load and flush pending ops
56  void setPersistentStoragePath(const UT_StringRef &storage_location);
57 
58  virtual bool load();
59  virtual void loadAsync();
60  // Flush any pending operations
61  virtual bool flush();
62 
63  // Delete the cookie from the in memory cookie store and the persistent
64  // store.
65  virtual bool deleteCookie(const NET_NetworkCookie &cookie);
66  // Check if we can update an existing cookie. Otherwise insert the cookie.
67  // In either case pending operations are added to update the persistent
68  // store.
69  virtual bool insertCookie(const NET_NetworkCookie &cookie);
70  // Try to update an existing cookie in the store. This adds a pending
71  // operation if a cookie was added.
72  virtual bool updateCookie(const NET_NetworkCookie &cookie);
73  // Grab all cookies that apply to the passed in url.
74  virtual CookieList cookiesForUrl(const UT_Url &url);
75  // Insert or update existing cookies based on the url.
76  virtual bool setCookiesFromUrl(const CookieList &cookies,
77  const UT_Url &url);
78  // Remove all cookies in our store
79  void deleteAllCookies();
80 
81  void setStoragePolicy(StoragePolicy policy);
82 
83  void setMode(Mode mode);
84 
85  // Useful for debug printing whats currently in the store.
86  void debugPrintStore() const;
87 
88  // The launcher does not depend on FS so the default cookie storage is in
89  // UT which then gets used here so that everyone can share the same cookie
90  // store path.
91  NO_DISCARD static UT_StringHolder defaultCookieStoragePath(
92  UT_LMHost::AppType app_type,
93  const UT_StringRef &name);
94  /// Find a cookie in the cookie store based on the cookie identifier.
96 
97  // Create the directories for the db file.
98  void createDBFileLocationIfNeeded();
99  // Grab a list of all cookies. This is really only meant to be used for
100  // debugging and testing.
101  const CookieList& allCookies() const { return myCookies; }
102 
103  const UT_StringHolder& persistentStorage() const { return myPersistentStorage; }
104 
105  bool isAsync() const { return myMode == Async; }
106 protected:
107  // Clear all existing cookies and pending operations and set the cookie
108  // store to the passed in cookie list.
109  void setAllCookies(const CookieList &cookies);
110  // Validate the cookie is usable for the passed in url
111  NO_DISCARD virtual bool validateCookie(const NET_NetworkCookie &cookie,
112  const UT_Url &url) const;
113 
114 private:
115 
116  // This class allows us to defer an operation to our persistent storage
117  // until we have accumulated enough operations, timeout expired or
118  // we are exiting the application. This also allows us to modify the
119  // persistent storage only when something has changed with that storage
120  class PendingOperation
121  {
122  public:
123  enum Type
124  {
125  INSERT_COOKIE,
126  DELETE_COOKIE,
127  UPDATE_ACCESS
128  };
129 
130  PendingOperation(Type type, const NET_NetworkCookie &cookie)
131  : myType(type), myCookie(cookie)
132  {
133  }
134 
135  Type myType;
136  NET_NetworkCookie myCookie;
137  };
138  using PendingOperationList = UT_Array<PendingOperation>;
139 
140  // Try to update the cookies access time if we already have one in our
141  // store.
142  bool updateCookieAccess(const NET_NetworkCookie& cookie);
143  // Insert the cookie into our in memory store. Returns true if the cookie
144  // was inserted.
145  // WARNING:
146  // Be very careful when you use this function as duplicate checking
147  // is not done and a pending operation is not added. If you need
148  // duplicate checking or to add a pending operation then call
149  // updateCookie() or insertCookie()
150  bool internalInsertCookie(const NET_NetworkCookie &cookie);
151  // Add a cookie and the associated operation to our pending operations to
152  // be processed at a later time.
153  void addPendingOperation(PendingOperation::Type type,
154  const NET_NetworkCookie &cookie);
155  // Every 30s or more we want to update the cookie store. The idea is to lazy
156  // update the store. Its unlikely that the store will be updated constantly
157  // so we only update every so often.
158  void refreshIfRequired();
159  static bool isPersistentCookie(StoragePolicy policy,
160  const NET_NetworkCookie &cookie);
161 
162  mutable UT_Lock myLock;
163  UT_StringHolder myPersistentStorage;
164  StoragePolicy myStoragePolicy;
165  Mode myMode;
166  CookieList myCookies;
167  PendingOperationList myPendingOps;
168  time_t myLastUpdate;
169 };
170 
171 #endif // __NET_NETWORKCOOKIESTORE_H__
172 
GLuint const GLchar * name
Definition: glew.h:1814
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
GLenum mode
Definition: glew.h:2163
hboost::optional< T > UT_Optional
Definition: UT_Optional.h:16
base_iterator< NET_NetworkCookie, true > iterator
Definition: UT_Array.h:788
Definition: UT_Url.h:22
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
Definition: format.h:2104
base_iterator< const NET_NetworkCookie, true > const_iterator
Definition: UT_Array.h:789