1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=2 et sw=2 tw=80: */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Indexed Database.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * The Mozilla Foundation.
20 : * Portions created by the Initial Developer are Copyright (C) 2010
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Ben Turner <bent.mozilla@gmail.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef mozilla_dom_indexeddb_asyncconnectionhelper_h__
41 : #define mozilla_dom_indexeddb_asyncconnectionhelper_h__
42 :
43 : // Only meant to be included in IndexedDB source files, not exported.
44 : #include "DatabaseInfo.h"
45 : #include "IndexedDatabase.h"
46 : #include "IDBDatabase.h"
47 : #include "IDBRequest.h"
48 :
49 : #include "mozIStorageProgressHandler.h"
50 : #include "nsIRunnable.h"
51 :
52 : #include "nsDOMEvent.h"
53 :
54 : class mozIStorageConnection;
55 : class nsIEventTarget;
56 :
57 : BEGIN_INDEXEDDB_NAMESPACE
58 :
59 : class IDBTransaction;
60 :
61 : // A common base class for AsyncConnectionHelper and OpenDatabaseHelper that
62 : // IDBRequest can use.
63 : class HelperBase : public nsIRunnable
64 : {
65 : friend class IDBRequest;
66 : public:
67 : virtual nsresult GetResultCode() = 0;
68 :
69 : virtual nsresult GetSuccessResult(JSContext* aCx,
70 : jsval* aVal) = 0;
71 :
72 : protected:
73 4746 : HelperBase(IDBRequest* aRequest)
74 4746 : : mRequest(aRequest)
75 4746 : { }
76 :
77 : virtual ~HelperBase();
78 :
79 : /**
80 : * Helper to wrap a native into a jsval. Uses the global object of the request
81 : * to parent the native.
82 : */
83 : nsresult WrapNative(JSContext* aCx,
84 : nsISupports* aNative,
85 : jsval* aResult);
86 :
87 : /**
88 : * Gives the subclass a chance to release any objects that must be released
89 : * on the main thread, regardless of success or failure. Subclasses that
90 : * implement this method *MUST* call the base class implementation as well.
91 : */
92 : virtual void ReleaseMainThreadObjects();
93 :
94 : nsRefPtr<IDBRequest> mRequest;
95 : };
96 :
97 : /**
98 : * Must be subclassed. The subclass must implement DoDatabaseWork. It may then
99 : * choose to implement OnSuccess and OnError depending on the needs of the
100 : * subclass. If the default implementation of OnSuccess is desired then the
101 : * subclass can implement GetSuccessResult to properly set the result of the
102 : * success event. Call Dispatch to start the database operation. Must be created
103 : * and Dispatched from the main thread only. Target thread may not be the main
104 : * thread.
105 : */
106 : class AsyncConnectionHelper : public HelperBase,
107 : public mozIStorageProgressHandler
108 : {
109 : public:
110 : NS_DECL_ISUPPORTS
111 : NS_DECL_NSIRUNNABLE
112 : NS_DECL_MOZISTORAGEPROGRESSHANDLER
113 :
114 : nsresult Dispatch(nsIEventTarget* aDatabaseThread);
115 :
116 : // Only for transactions!
117 : nsresult DispatchToTransactionPool();
118 :
119 : void SetError(nsresult aErrorCode)
120 : {
121 : NS_ASSERTION(NS_FAILED(aErrorCode), "Not a failure code!");
122 : mResultCode = aErrorCode;
123 : }
124 :
125 : static IDBTransaction* GetCurrentTransaction();
126 :
127 71 : bool HasTransaction()
128 : {
129 71 : return mTransaction;
130 : }
131 :
132 : nsISupports* GetSource()
133 : {
134 : return mRequest ? mRequest->Source() : nsnull;
135 : }
136 :
137 4308 : nsresult GetResultCode()
138 : {
139 4308 : return mResultCode;
140 : }
141 :
142 : protected:
143 : AsyncConnectionHelper(IDBDatabase* aDatabase,
144 : IDBRequest* aRequest);
145 :
146 : AsyncConnectionHelper(IDBTransaction* aTransaction,
147 : IDBRequest* aRequest);
148 :
149 : virtual ~AsyncConnectionHelper();
150 :
151 : /**
152 : * This is called on the main thread after Dispatch is called but before the
153 : * runnable is actually dispatched to the database thread. Allows the subclass
154 : * to initialize itself.
155 : */
156 : virtual nsresult Init();
157 :
158 : /**
159 : * This callback is run on the database thread.
160 : */
161 : virtual nsresult DoDatabaseWork(mozIStorageConnection* aConnection) = 0;
162 :
163 : /**
164 : * This function returns the event to be dispatched at the request when
165 : * OnSuccess is called. A subclass can override this to fire an event other
166 : * than "success" at the request.
167 : */
168 : virtual already_AddRefed<nsDOMEvent> CreateSuccessEvent();
169 :
170 : /**
171 : * This callback is run on the main thread if DoDatabaseWork returned NS_OK.
172 : * The default implementation fires a "success" DOM event with its target set
173 : * to the request. Returning anything other than NS_OK from the OnSuccess
174 : * callback will trigger the OnError callback.
175 : */
176 : virtual nsresult OnSuccess();
177 :
178 : /**
179 : * This callback is run on the main thread if DoDatabaseWork or OnSuccess
180 : * returned an error code. The default implementation fires an "error" DOM
181 : * event with its target set to the request.
182 : */
183 : virtual void OnError();
184 :
185 : /**
186 : * This function is called by the request on the main thread when script
187 : * accesses the result property of the request.
188 : */
189 : virtual nsresult GetSuccessResult(JSContext* aCx,
190 : jsval* aVal);
191 :
192 : /**
193 : * Gives the subclass a chance to release any objects that must be released
194 : * on the main thread, regardless of success or failure. Subclasses that
195 : * implement this method *MUST* call the base class implementation as well.
196 : */
197 : virtual void ReleaseMainThreadObjects();
198 :
199 : /**
200 : * Helper to make a JS array object out of an array of clone buffers.
201 : */
202 : static nsresult ConvertCloneReadInfosToArray(
203 : JSContext* aCx,
204 : nsTArray<StructuredCloneReadInfo>& aReadInfos,
205 : jsval* aResult);
206 :
207 : protected:
208 : nsRefPtr<IDBDatabase> mDatabase;
209 : nsRefPtr<IDBTransaction> mTransaction;
210 :
211 : private:
212 : nsCOMPtr<mozIStorageProgressHandler> mOldProgressHandler;
213 : nsresult mResultCode;
214 : bool mDispatched;
215 : };
216 :
217 : END_INDEXEDDB_NAMESPACE
218 :
219 : #endif // mozilla_dom_indexeddb_asyncconnectionhelper_h__
|