Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef mozilla_dom_indexeddb_asyncconnectionhelper_h__ |
michael@0 | 8 | #define mozilla_dom_indexeddb_asyncconnectionhelper_h__ |
michael@0 | 9 | |
michael@0 | 10 | // Only meant to be included in IndexedDB source files, not exported. |
michael@0 | 11 | #include "DatabaseInfo.h" |
michael@0 | 12 | #include "IndexedDatabase.h" |
michael@0 | 13 | #include "IDBDatabase.h" |
michael@0 | 14 | #include "IDBRequest.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "mozIStorageProgressHandler.h" |
michael@0 | 17 | #include "nsIEventTarget.h" |
michael@0 | 18 | #include "nsIRunnable.h" |
michael@0 | 19 | |
michael@0 | 20 | class mozIStorageConnection; |
michael@0 | 21 | |
michael@0 | 22 | BEGIN_INDEXEDDB_NAMESPACE |
michael@0 | 23 | |
michael@0 | 24 | class AutoSetCurrentTransaction; |
michael@0 | 25 | class IDBTransaction; |
michael@0 | 26 | |
michael@0 | 27 | namespace ipc { |
michael@0 | 28 | class ResponseValue; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | // A common base class for AsyncConnectionHelper and OpenDatabaseHelper that |
michael@0 | 32 | // IDBRequest can use. |
michael@0 | 33 | class HelperBase : public nsIRunnable |
michael@0 | 34 | { |
michael@0 | 35 | friend class IDBRequest; |
michael@0 | 36 | |
michael@0 | 37 | public: |
michael@0 | 38 | |
michael@0 | 39 | virtual nsresult GetResultCode() = 0; |
michael@0 | 40 | |
michael@0 | 41 | virtual nsresult GetSuccessResult(JSContext* aCx, |
michael@0 | 42 | JS::MutableHandle<JS::Value> aVal) = 0; |
michael@0 | 43 | |
michael@0 | 44 | IDBRequest* GetRequest() const |
michael@0 | 45 | { |
michael@0 | 46 | return mRequest; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | protected: |
michael@0 | 50 | HelperBase(IDBRequest* aRequest) |
michael@0 | 51 | : mRequest(aRequest) |
michael@0 | 52 | { } |
michael@0 | 53 | |
michael@0 | 54 | virtual ~HelperBase(); |
michael@0 | 55 | |
michael@0 | 56 | /** |
michael@0 | 57 | * Helper to wrap a native into a jsval. Uses the global object of the request |
michael@0 | 58 | * to parent the native. |
michael@0 | 59 | */ |
michael@0 | 60 | nsresult WrapNative(JSContext* aCx, |
michael@0 | 61 | nsISupports* aNative, |
michael@0 | 62 | JS::MutableHandle<JS::Value> aResult); |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * Gives the subclass a chance to release any objects that must be released |
michael@0 | 66 | * on the main thread, regardless of success or failure. Subclasses that |
michael@0 | 67 | * implement this method *MUST* call the base class implementation as well. |
michael@0 | 68 | */ |
michael@0 | 69 | virtual void ReleaseMainThreadObjects(); |
michael@0 | 70 | |
michael@0 | 71 | nsRefPtr<IDBRequest> mRequest; |
michael@0 | 72 | }; |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Must be subclassed. The subclass must implement DoDatabaseWork. It may then |
michael@0 | 76 | * choose to implement OnSuccess and OnError depending on the needs of the |
michael@0 | 77 | * subclass. If the default implementation of OnSuccess is desired then the |
michael@0 | 78 | * subclass can implement GetSuccessResult to properly set the result of the |
michael@0 | 79 | * success event. Call Dispatch to start the database operation. Must be created |
michael@0 | 80 | * and Dispatched from the main thread only. Target thread may not be the main |
michael@0 | 81 | * thread. |
michael@0 | 82 | */ |
michael@0 | 83 | class AsyncConnectionHelper : public HelperBase, |
michael@0 | 84 | public mozIStorageProgressHandler |
michael@0 | 85 | { |
michael@0 | 86 | friend class AutoSetCurrentTransaction; |
michael@0 | 87 | |
michael@0 | 88 | public: |
michael@0 | 89 | typedef ipc::ResponseValue ResponseValue; |
michael@0 | 90 | |
michael@0 | 91 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 92 | NS_DECL_NSIRUNNABLE |
michael@0 | 93 | NS_DECL_MOZISTORAGEPROGRESSHANDLER |
michael@0 | 94 | |
michael@0 | 95 | virtual nsresult Dispatch(nsIEventTarget* aDatabaseThread); |
michael@0 | 96 | |
michael@0 | 97 | // Only for transactions! |
michael@0 | 98 | nsresult DispatchToTransactionPool(); |
michael@0 | 99 | |
michael@0 | 100 | void SetError(nsresult aErrorCode) |
michael@0 | 101 | { |
michael@0 | 102 | NS_ASSERTION(NS_FAILED(aErrorCode), "Not a failure code!"); |
michael@0 | 103 | mResultCode = aErrorCode; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | static IDBTransaction* GetCurrentTransaction(); |
michael@0 | 107 | |
michael@0 | 108 | bool HasTransaction() const |
michael@0 | 109 | { |
michael@0 | 110 | return !!mTransaction; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | IDBTransaction* GetTransaction() const |
michael@0 | 114 | { |
michael@0 | 115 | return mTransaction; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | virtual nsresult GetResultCode() MOZ_OVERRIDE |
michael@0 | 119 | { |
michael@0 | 120 | return mResultCode; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | enum ChildProcessSendResult |
michael@0 | 124 | { |
michael@0 | 125 | // The result was successfully sent to the child process |
michael@0 | 126 | Success_Sent = 0, |
michael@0 | 127 | |
michael@0 | 128 | // The result was not sent, because this is not an out-of-process request. |
michael@0 | 129 | Success_NotSent, |
michael@0 | 130 | |
michael@0 | 131 | // The result was not sent, because the actor has been disconnected |
michael@0 | 132 | // (if the child process has shut down or crashed). |
michael@0 | 133 | Success_ActorDisconnected, |
michael@0 | 134 | |
michael@0 | 135 | // An error occurred. |
michael@0 | 136 | Error |
michael@0 | 137 | }; |
michael@0 | 138 | |
michael@0 | 139 | ChildProcessSendResult |
michael@0 | 140 | MaybeSendResponseToChildProcess(nsresult aResultCode); |
michael@0 | 141 | |
michael@0 | 142 | virtual nsresult OnParentProcessRequestComplete( |
michael@0 | 143 | const ResponseValue& aResponseValue); |
michael@0 | 144 | |
michael@0 | 145 | virtual nsresult |
michael@0 | 146 | UnpackResponseFromParentProcess(const ResponseValue& aResponseValue) = 0; |
michael@0 | 147 | |
michael@0 | 148 | protected: |
michael@0 | 149 | AsyncConnectionHelper(IDBDatabase* aDatabase, |
michael@0 | 150 | IDBRequest* aRequest); |
michael@0 | 151 | |
michael@0 | 152 | AsyncConnectionHelper(IDBTransaction* aTransaction, |
michael@0 | 153 | IDBRequest* aRequest); |
michael@0 | 154 | |
michael@0 | 155 | virtual ~AsyncConnectionHelper(); |
michael@0 | 156 | |
michael@0 | 157 | /** |
michael@0 | 158 | * This is called on the main thread after Dispatch is called but before the |
michael@0 | 159 | * runnable is actually dispatched to the database thread. Allows the subclass |
michael@0 | 160 | * to initialize itself. |
michael@0 | 161 | */ |
michael@0 | 162 | virtual nsresult Init(); |
michael@0 | 163 | |
michael@0 | 164 | /** |
michael@0 | 165 | * This callback is run on the database thread. |
michael@0 | 166 | */ |
michael@0 | 167 | virtual nsresult DoDatabaseWork(mozIStorageConnection* aConnection) = 0; |
michael@0 | 168 | |
michael@0 | 169 | /** |
michael@0 | 170 | * This function returns the event to be dispatched at the request when |
michael@0 | 171 | * OnSuccess is called. A subclass can override this to fire an event other |
michael@0 | 172 | * than "success" at the request. |
michael@0 | 173 | */ |
michael@0 | 174 | virtual already_AddRefed<nsIDOMEvent> CreateSuccessEvent( |
michael@0 | 175 | mozilla::dom::EventTarget* aOwner); |
michael@0 | 176 | |
michael@0 | 177 | /** |
michael@0 | 178 | * This callback is run on the main thread if DoDatabaseWork returned NS_OK. |
michael@0 | 179 | * The default implementation fires a "success" DOM event with its target set |
michael@0 | 180 | * to the request. Returning anything other than NS_OK from the OnSuccess |
michael@0 | 181 | * callback will trigger the OnError callback. |
michael@0 | 182 | */ |
michael@0 | 183 | virtual nsresult OnSuccess(); |
michael@0 | 184 | |
michael@0 | 185 | /** |
michael@0 | 186 | * This callback is run on the main thread if DoDatabaseWork or OnSuccess |
michael@0 | 187 | * returned an error code. The default implementation fires an "error" DOM |
michael@0 | 188 | * event with its target set to the request. |
michael@0 | 189 | */ |
michael@0 | 190 | virtual void OnError(); |
michael@0 | 191 | |
michael@0 | 192 | /** |
michael@0 | 193 | * This function is called by the request on the main thread when script |
michael@0 | 194 | * accesses the result property of the request. |
michael@0 | 195 | */ |
michael@0 | 196 | virtual nsresult GetSuccessResult(JSContext* aCx, |
michael@0 | 197 | JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE; |
michael@0 | 198 | |
michael@0 | 199 | /** |
michael@0 | 200 | * Gives the subclass a chance to release any objects that must be released |
michael@0 | 201 | * on the main thread, regardless of success or failure. Subclasses that |
michael@0 | 202 | * implement this method *MUST* call the base class implementation as well. |
michael@0 | 203 | */ |
michael@0 | 204 | virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE; |
michael@0 | 205 | |
michael@0 | 206 | /** |
michael@0 | 207 | * Helper to make a JS array object out of an array of clone buffers. |
michael@0 | 208 | */ |
michael@0 | 209 | static nsresult ConvertToArrayAndCleanup( |
michael@0 | 210 | JSContext* aCx, |
michael@0 | 211 | nsTArray<StructuredCloneReadInfo>& aReadInfos, |
michael@0 | 212 | JS::MutableHandle<JS::Value> aResult); |
michael@0 | 213 | |
michael@0 | 214 | /** |
michael@0 | 215 | * This should only be called by AutoSetCurrentTransaction. |
michael@0 | 216 | */ |
michael@0 | 217 | static void SetCurrentTransaction(IDBTransaction* aTransaction); |
michael@0 | 218 | |
michael@0 | 219 | /** |
michael@0 | 220 | * Allows the subclass to send its results to the child process. Will only |
michael@0 | 221 | * be called if all of the IPC infrastructure is available (there is an |
michael@0 | 222 | * actor, the child is stil alive and hasn't begun shutting down). |
michael@0 | 223 | */ |
michael@0 | 224 | virtual ChildProcessSendResult |
michael@0 | 225 | SendResponseToChildProcess(nsresult aResultCode) = 0; |
michael@0 | 226 | |
michael@0 | 227 | protected: |
michael@0 | 228 | nsRefPtr<IDBDatabase> mDatabase; |
michael@0 | 229 | nsRefPtr<IDBTransaction> mTransaction; |
michael@0 | 230 | |
michael@0 | 231 | private: |
michael@0 | 232 | nsCOMPtr<mozIStorageProgressHandler> mOldProgressHandler; |
michael@0 | 233 | nsresult mResultCode; |
michael@0 | 234 | bool mDispatched; |
michael@0 | 235 | }; |
michael@0 | 236 | |
michael@0 | 237 | class MOZ_STACK_CLASS StackBasedEventTarget : public nsIEventTarget |
michael@0 | 238 | { |
michael@0 | 239 | public: |
michael@0 | 240 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 241 | }; |
michael@0 | 242 | |
michael@0 | 243 | class MOZ_STACK_CLASS ImmediateRunEventTarget : public StackBasedEventTarget |
michael@0 | 244 | { |
michael@0 | 245 | public: |
michael@0 | 246 | NS_DECL_NSIEVENTTARGET |
michael@0 | 247 | }; |
michael@0 | 248 | |
michael@0 | 249 | class MOZ_STACK_CLASS NoDispatchEventTarget : public StackBasedEventTarget |
michael@0 | 250 | { |
michael@0 | 251 | public: |
michael@0 | 252 | NS_DECL_NSIEVENTTARGET |
michael@0 | 253 | }; |
michael@0 | 254 | |
michael@0 | 255 | END_INDEXEDDB_NAMESPACE |
michael@0 | 256 | |
michael@0 | 257 | #endif // mozilla_dom_indexeddb_asyncconnectionhelper_h__ |