1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is Indexed Database.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * The Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2010
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Ben Turner <bent.mozilla@gmail.com>
23 : * Kyle Huey <me@kylehuey.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef mozilla_dom_indexeddb_opendatabasehelper_h__
40 : #define mozilla_dom_indexeddb_opendatabasehelper_h__
41 :
42 : #include "AsyncConnectionHelper.h"
43 : #include "DatabaseInfo.h"
44 : #include "IDBDatabase.h"
45 : #include "IDBRequest.h"
46 :
47 : #include "nsIRunnable.h"
48 :
49 : class mozIStorageConnection;
50 :
51 : BEGIN_INDEXEDDB_NAMESPACE
52 :
53 : class OpenDatabaseHelper : public HelperBase
54 304 : {
55 : public:
56 76 : OpenDatabaseHelper(IDBOpenDBRequest* aRequest,
57 : const nsAString& aName,
58 : const nsACString& aASCIIOrigin,
59 : PRUint64 aRequestedVersion,
60 : bool aForDeletion)
61 : : HelperBase(aRequest), mOpenDBRequest(aRequest), mName(aName),
62 : mASCIIOrigin(aASCIIOrigin), mRequestedVersion(aRequestedVersion),
63 : mForDeletion(aForDeletion), mDatabaseId(nsnull), mCurrentVersion(0),
64 : mLastObjectStoreId(0), mLastIndexId(0), mState(eCreated),
65 76 : mResultCode(NS_OK), mLoadDBMetadata(false)
66 : {
67 76 : NS_ASSERTION(!aForDeletion || !aRequestedVersion,
68 : "Can't be for deletion and request a version!");
69 76 : }
70 :
71 : NS_DECL_ISUPPORTS
72 : NS_DECL_NSIRUNNABLE
73 :
74 : nsresult Init();
75 :
76 : nsresult Dispatch(nsIEventTarget* aDatabaseThread);
77 : nsresult RunImmediately();
78 :
79 1 : void SetError(nsresult rv)
80 : {
81 1 : NS_ASSERTION(NS_FAILED(rv), "Why are you telling me?");
82 1 : mResultCode = rv;
83 1 : }
84 :
85 5 : nsresult GetResultCode()
86 : {
87 5 : return mResultCode;
88 : }
89 :
90 : nsresult NotifySetVersionFinished();
91 : nsresult NotifyDeleteFinished();
92 : void BlockDatabase();
93 :
94 76 : nsIAtom* Id() const
95 : {
96 76 : return mDatabaseId.get();
97 : }
98 :
99 3 : IDBDatabase* Database() const
100 : {
101 3 : NS_ASSERTION(mDatabase, "Calling at the wrong time!");
102 3 : return mDatabase;
103 : }
104 :
105 : static
106 : nsresult CreateDatabaseConnection(const nsAString& aName,
107 : nsIFile* aDBFile,
108 : nsIFile* aFileManagerDirectory,
109 : mozIStorageConnection** aConnection);
110 :
111 : protected:
112 : // Methods only called on the main thread
113 : nsresult EnsureSuccessResult();
114 : nsresult StartSetVersion();
115 : nsresult StartDelete();
116 : nsresult GetSuccessResult(JSContext* aCx,
117 : jsval* aVal);
118 : void DispatchSuccessEvent();
119 : void DispatchErrorEvent();
120 : void ReleaseMainThreadObjects();
121 :
122 : // Methods only called on the DB thread
123 : nsresult DoDatabaseWork();
124 :
125 : private:
126 : // In-params.
127 : nsRefPtr<IDBOpenDBRequest> mOpenDBRequest;
128 : nsString mName;
129 : nsCString mASCIIOrigin;
130 : PRUint64 mRequestedVersion;
131 : bool mForDeletion;
132 : nsCOMPtr<nsIAtom> mDatabaseId;
133 :
134 : // Out-params.
135 : nsTArray<nsRefPtr<ObjectStoreInfo> > mObjectStores;
136 : PRUint64 mCurrentVersion;
137 : nsString mDatabaseFilePath;
138 : PRInt64 mLastObjectStoreId;
139 : PRInt64 mLastIndexId;
140 : nsRefPtr<IDBDatabase> mDatabase;
141 :
142 : // State variables
143 : enum OpenDatabaseState {
144 : eCreated = 0, // Not yet dispatched to the DB thread
145 : eDBWork, // Waiting to do/doing work on the DB thread
146 : eFiringEvents, // Waiting to fire/firing events on the main thread
147 : eSetVersionPending, // Waiting on a SetVersionHelper
148 : eSetVersionCompleted, // SetVersionHelper is done
149 : eDeletePending, // Waiting on a DeleteDatabaseHelper
150 : eDeleteCompleted, // DeleteDatabaseHelper is done
151 : };
152 : OpenDatabaseState mState;
153 : nsresult mResultCode;
154 :
155 : nsRefPtr<FileManager> mFileManager;
156 :
157 : nsRefPtr<DatabaseInfo> mDBInfo;
158 : bool mLoadDBMetadata;
159 : };
160 :
161 : END_INDEXEDDB_NAMESPACE
162 :
163 : #endif // mozilla_dom_indexeddb_opendatabasehelper_h__
|