|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #ifndef mozilla_dom_indexeddb_opendatabasehelper_h__ |
|
6 #define mozilla_dom_indexeddb_opendatabasehelper_h__ |
|
7 |
|
8 #include "AsyncConnectionHelper.h" |
|
9 |
|
10 #include "nsIRunnable.h" |
|
11 |
|
12 #include "mozilla/dom/quota/StoragePrivilege.h" |
|
13 |
|
14 #include "DatabaseInfo.h" |
|
15 #include "IDBDatabase.h" |
|
16 #include "IDBRequest.h" |
|
17 |
|
18 class mozIStorageConnection; |
|
19 |
|
20 namespace mozilla { |
|
21 namespace dom { |
|
22 class ContentParent; |
|
23 } |
|
24 } |
|
25 |
|
26 BEGIN_INDEXEDDB_NAMESPACE |
|
27 |
|
28 class CheckPermissionsHelper; |
|
29 |
|
30 class OpenDatabaseHelper : public HelperBase |
|
31 { |
|
32 friend class CheckPermissionsHelper; |
|
33 |
|
34 typedef mozilla::dom::quota::PersistenceType PersistenceType; |
|
35 typedef mozilla::dom::quota::StoragePrivilege StoragePrivilege; |
|
36 |
|
37 public: |
|
38 OpenDatabaseHelper(IDBOpenDBRequest* aRequest, |
|
39 const nsAString& aName, |
|
40 const nsACString& aGroup, |
|
41 const nsACString& aASCIIOrigin, |
|
42 uint64_t aRequestedVersion, |
|
43 PersistenceType aPersistenceType, |
|
44 bool aForDeletion, |
|
45 mozilla::dom::ContentParent* aContentParent, |
|
46 StoragePrivilege aPrivilege) |
|
47 : HelperBase(aRequest), mOpenDBRequest(aRequest), mName(aName), |
|
48 mGroup(aGroup), mASCIIOrigin(aASCIIOrigin), |
|
49 mRequestedVersion(aRequestedVersion), mPersistenceType(aPersistenceType), |
|
50 mForDeletion(aForDeletion), mPrivilege(aPrivilege), |
|
51 mContentParent(aContentParent), mCurrentVersion(0), mLastObjectStoreId(0), |
|
52 mLastIndexId(0), mState(eCreated), mResultCode(NS_OK), |
|
53 mLoadDBMetadata(false), |
|
54 mTrackingQuota(aPrivilege != mozilla::dom::quota::Chrome) |
|
55 { |
|
56 NS_ASSERTION(!aForDeletion || !aRequestedVersion, |
|
57 "Can't be for deletion and request a version!"); |
|
58 } |
|
59 |
|
60 NS_DECL_THREADSAFE_ISUPPORTS |
|
61 NS_DECL_NSIRUNNABLE |
|
62 |
|
63 nsresult Init(); |
|
64 |
|
65 nsresult WaitForOpenAllowed(); |
|
66 nsresult Dispatch(nsIEventTarget* aDatabaseThread); |
|
67 nsresult DispatchToIOThread(); |
|
68 nsresult RunImmediately(); |
|
69 |
|
70 void SetError(nsresult rv) |
|
71 { |
|
72 NS_ASSERTION(NS_FAILED(rv), "Why are you telling me?"); |
|
73 mResultCode = rv; |
|
74 } |
|
75 |
|
76 virtual nsresult GetResultCode() MOZ_OVERRIDE |
|
77 { |
|
78 return mResultCode; |
|
79 } |
|
80 |
|
81 nsresult NotifySetVersionFinished(); |
|
82 nsresult NotifyDeleteFinished(); |
|
83 void BlockDatabase(); |
|
84 |
|
85 const nsACString& Id() const |
|
86 { |
|
87 return mDatabaseId; |
|
88 } |
|
89 |
|
90 IDBDatabase* Database() const |
|
91 { |
|
92 NS_ASSERTION(mDatabase, "Calling at the wrong time!"); |
|
93 return mDatabase; |
|
94 } |
|
95 |
|
96 const StoragePrivilege& Privilege() const |
|
97 { |
|
98 return mPrivilege; |
|
99 } |
|
100 |
|
101 static |
|
102 nsresult CreateDatabaseConnection(nsIFile* aDBFile, |
|
103 nsIFile* aFMDirectory, |
|
104 const nsAString& aName, |
|
105 PersistenceType aPersistenceType, |
|
106 const nsACString& aGroup, |
|
107 const nsACString& aOrigin, |
|
108 mozIStorageConnection** aConnection); |
|
109 |
|
110 protected: |
|
111 // Methods only called on the main thread |
|
112 nsresult EnsureSuccessResult(); |
|
113 nsresult StartSetVersion(); |
|
114 nsresult StartDelete(); |
|
115 virtual nsresult GetSuccessResult(JSContext* aCx, |
|
116 JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE; |
|
117 void DispatchSuccessEvent(); |
|
118 void DispatchErrorEvent(); |
|
119 virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE; |
|
120 |
|
121 // Called by CheckPermissionsHelper on the main thread before dispatch. |
|
122 void SetUnlimitedQuotaAllowed() |
|
123 { |
|
124 mTrackingQuota = false; |
|
125 } |
|
126 |
|
127 // Methods only called on the DB thread |
|
128 nsresult DoDatabaseWork(); |
|
129 |
|
130 // In-params. |
|
131 nsRefPtr<IDBOpenDBRequest> mOpenDBRequest; |
|
132 nsString mName; |
|
133 nsCString mGroup; |
|
134 nsCString mASCIIOrigin; |
|
135 uint64_t mRequestedVersion; |
|
136 PersistenceType mPersistenceType; |
|
137 bool mForDeletion; |
|
138 StoragePrivilege mPrivilege; |
|
139 nsCString mDatabaseId; |
|
140 mozilla::dom::ContentParent* mContentParent; |
|
141 |
|
142 // Out-params. |
|
143 nsTArray<nsRefPtr<ObjectStoreInfo> > mObjectStores; |
|
144 uint64_t mCurrentVersion; |
|
145 nsString mDatabaseFilePath; |
|
146 int64_t mLastObjectStoreId; |
|
147 int64_t mLastIndexId; |
|
148 nsRefPtr<IDBDatabase> mDatabase; |
|
149 |
|
150 // State variables |
|
151 enum OpenDatabaseState { |
|
152 eCreated = 0, // Not yet dispatched to the DB thread |
|
153 eOpenPending, // Waiting for open allowed/open allowed |
|
154 eDBWork, // Waiting to do/doing work on the DB thread |
|
155 eFiringEvents, // Waiting to fire/firing events on the main thread |
|
156 eSetVersionPending, // Waiting on a SetVersionHelper |
|
157 eSetVersionCompleted, // SetVersionHelper is done |
|
158 eDeletePending, // Waiting on a DeleteDatabaseHelper |
|
159 eDeleteCompleted, // DeleteDatabaseHelper is done |
|
160 }; |
|
161 OpenDatabaseState mState; |
|
162 nsresult mResultCode; |
|
163 |
|
164 nsRefPtr<FileManager> mFileManager; |
|
165 |
|
166 nsRefPtr<DatabaseInfo> mDBInfo; |
|
167 bool mLoadDBMetadata; |
|
168 bool mTrackingQuota; |
|
169 }; |
|
170 |
|
171 END_INDEXEDDB_NAMESPACE |
|
172 |
|
173 #endif // mozilla_dom_indexeddb_opendatabasehelper_h__ |