/src/mozilla-central/dom/cache/DBAction.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "mozilla/dom/cache/DBAction.h" |
8 | | |
9 | | #include "mozilla/dom/cache/Connection.h" |
10 | | #include "mozilla/dom/cache/DBSchema.h" |
11 | | #include "mozilla/dom/cache/FileUtils.h" |
12 | | #include "mozilla/dom/cache/QuotaClient.h" |
13 | | #include "mozilla/dom/quota/PersistenceType.h" |
14 | | #include "mozilla/net/nsFileProtocolHandler.h" |
15 | | #include "mozIStorageConnection.h" |
16 | | #include "mozIStorageService.h" |
17 | | #include "mozStorageCID.h" |
18 | | #include "nsIFile.h" |
19 | | #include "nsIURI.h" |
20 | | #include "nsIURIMutator.h" |
21 | | #include "nsIFileURL.h" |
22 | | #include "nsThreadUtils.h" |
23 | | |
24 | | namespace mozilla { |
25 | | namespace dom { |
26 | | namespace cache { |
27 | | |
28 | | using mozilla::dom::quota::AssertIsOnIOThread; |
29 | | using mozilla::dom::quota::PERSISTENCE_TYPE_DEFAULT; |
30 | | using mozilla::dom::quota::PersistenceType; |
31 | | |
32 | | namespace { |
33 | | |
34 | | nsresult |
35 | | WipeDatabase(const QuotaInfo& aQuotaInfo, nsIFile* aDBFile, |
36 | | nsIFile* aDBDir) |
37 | 0 | { |
38 | 0 | MOZ_DIAGNOSTIC_ASSERT(aDBFile); |
39 | 0 | MOZ_DIAGNOSTIC_ASSERT(aDBDir); |
40 | 0 |
|
41 | 0 | nsresult rv = RemoveNsIFile(aQuotaInfo, aDBFile); |
42 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
43 | 0 | |
44 | 0 | // Note, the -wal journal file will be automatically deleted by sqlite when |
45 | 0 | // the new database is created. No need to explicitly delete it here. |
46 | 0 | |
47 | 0 | // Delete the morgue as well. |
48 | 0 | rv = BodyDeleteDir(aQuotaInfo, aDBDir); |
49 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
50 | 0 | |
51 | 0 | rv = WipePaddingFile(aQuotaInfo, aDBDir); |
52 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
53 | 0 | |
54 | 0 | return rv; |
55 | 0 | } |
56 | | |
57 | | } |
58 | | |
59 | | DBAction::DBAction(Mode aMode) |
60 | | : mMode(aMode) |
61 | 0 | { |
62 | 0 | } |
63 | | |
64 | | DBAction::~DBAction() |
65 | 0 | { |
66 | 0 | } |
67 | | |
68 | | void |
69 | | DBAction::RunOnTarget(Resolver* aResolver, const QuotaInfo& aQuotaInfo, |
70 | | Data* aOptionalData) |
71 | 0 | { |
72 | 0 | MOZ_ASSERT(!NS_IsMainThread()); |
73 | 0 | MOZ_DIAGNOSTIC_ASSERT(aResolver); |
74 | 0 | MOZ_DIAGNOSTIC_ASSERT(aQuotaInfo.mDir); |
75 | 0 |
|
76 | 0 | if (IsCanceled()) { |
77 | 0 | aResolver->Resolve(NS_ERROR_ABORT); |
78 | 0 | return; |
79 | 0 | } |
80 | 0 | |
81 | 0 | nsCOMPtr<nsIFile> dbDir; |
82 | 0 | nsresult rv = aQuotaInfo.mDir->Clone(getter_AddRefs(dbDir)); |
83 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
84 | 0 | aResolver->Resolve(rv); |
85 | 0 | return; |
86 | 0 | } |
87 | 0 | |
88 | 0 | rv = dbDir->Append(NS_LITERAL_STRING("cache")); |
89 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
90 | 0 | aResolver->Resolve(rv); |
91 | 0 | return; |
92 | 0 | } |
93 | 0 | |
94 | 0 | nsCOMPtr<mozIStorageConnection> conn; |
95 | 0 |
|
96 | 0 | // Attempt to reuse the connection opened by a previous Action. |
97 | 0 | if (aOptionalData) { |
98 | 0 | conn = aOptionalData->GetConnection(); |
99 | 0 | } |
100 | 0 |
|
101 | 0 | // If there is no previous Action, then we must open one. |
102 | 0 | if (!conn) { |
103 | 0 | rv = OpenConnection(aQuotaInfo, dbDir, getter_AddRefs(conn)); |
104 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
105 | 0 | aResolver->Resolve(rv); |
106 | 0 | return; |
107 | 0 | } |
108 | 0 | MOZ_DIAGNOSTIC_ASSERT(conn); |
109 | 0 |
|
110 | 0 | // Save this connection in the shared Data object so later Actions can |
111 | 0 | // use it. This avoids opening a new connection for every Action. |
112 | 0 | if (aOptionalData) { |
113 | 0 | // Since we know this connection will be around for as long as the |
114 | 0 | // Cache is open, use our special wrapped connection class. This |
115 | 0 | // will let us perform certain operations once the Cache origin |
116 | 0 | // is closed. |
117 | 0 | nsCOMPtr<mozIStorageConnection> wrapped = new Connection(conn); |
118 | 0 | aOptionalData->SetConnection(wrapped); |
119 | 0 | } |
120 | 0 | } |
121 | 0 |
|
122 | 0 | RunWithDBOnTarget(aResolver, aQuotaInfo, dbDir, conn); |
123 | 0 | } |
124 | | |
125 | | nsresult |
126 | | DBAction::OpenConnection(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir, |
127 | | mozIStorageConnection** aConnOut) |
128 | 0 | { |
129 | 0 | MOZ_ASSERT(!NS_IsMainThread()); |
130 | 0 | MOZ_DIAGNOSTIC_ASSERT(aDBDir); |
131 | 0 | MOZ_DIAGNOSTIC_ASSERT(aConnOut); |
132 | 0 |
|
133 | 0 | bool exists; |
134 | 0 | nsresult rv = aDBDir->Exists(&exists); |
135 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
136 | 0 | |
137 | 0 | if (!exists) { |
138 | 0 | if (NS_WARN_IF(mMode != Create)) { return NS_ERROR_FILE_NOT_FOUND; } |
139 | 0 | rv = aDBDir->Create(nsIFile::DIRECTORY_TYPE, 0755); |
140 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
141 | 0 | } |
142 | 0 | |
143 | 0 | rv = OpenDBConnection(aQuotaInfo, aDBDir, aConnOut); |
144 | 0 |
|
145 | 0 | return rv; |
146 | 0 | } |
147 | | |
148 | | SyncDBAction::SyncDBAction(Mode aMode) |
149 | | : DBAction(aMode) |
150 | 0 | { |
151 | 0 | } |
152 | | |
153 | | SyncDBAction::~SyncDBAction() |
154 | | { |
155 | | } |
156 | | |
157 | | void |
158 | | SyncDBAction::RunWithDBOnTarget(Resolver* aResolver, |
159 | | const QuotaInfo& aQuotaInfo, nsIFile* aDBDir, |
160 | | mozIStorageConnection* aConn) |
161 | 0 | { |
162 | 0 | MOZ_ASSERT(!NS_IsMainThread()); |
163 | 0 | MOZ_DIAGNOSTIC_ASSERT(aResolver); |
164 | 0 | MOZ_DIAGNOSTIC_ASSERT(aDBDir); |
165 | 0 | MOZ_DIAGNOSTIC_ASSERT(aConn); |
166 | 0 |
|
167 | 0 | nsresult rv = RunSyncWithDBOnTarget(aQuotaInfo, aDBDir, aConn); |
168 | 0 | aResolver->Resolve(rv); |
169 | 0 | } |
170 | | |
171 | | // static |
172 | | nsresult |
173 | | OpenDBConnection(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir, |
174 | | mozIStorageConnection** aConnOut) |
175 | 0 | { |
176 | 0 | MOZ_ASSERT(!NS_IsMainThread()); |
177 | 0 | MOZ_DIAGNOSTIC_ASSERT(aDBDir); |
178 | 0 | MOZ_DIAGNOSTIC_ASSERT(aConnOut); |
179 | 0 |
|
180 | 0 | nsCOMPtr<mozIStorageConnection> conn; |
181 | 0 |
|
182 | 0 | nsCOMPtr<nsIFile> dbFile; |
183 | 0 | nsresult rv = aDBDir->Clone(getter_AddRefs(dbFile)); |
184 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
185 | 0 | |
186 | 0 | rv = dbFile->Append(NS_LITERAL_STRING("caches.sqlite")); |
187 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
188 | 0 | |
189 | 0 | bool exists = false; |
190 | 0 | rv = dbFile->Exists(&exists); |
191 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
192 | 0 | |
193 | 0 | // Use our default file:// protocol handler directly to construct the database |
194 | 0 | // URL. This avoids any problems if a plugin registers a custom file:// |
195 | 0 | // handler. If such a custom handler used javascript, then we would have a |
196 | 0 | // bad time running off the main thread here. |
197 | 0 | RefPtr<nsFileProtocolHandler> handler = new nsFileProtocolHandler(); |
198 | 0 | rv = handler->Init(); |
199 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
200 | 0 | |
201 | 0 | nsCOMPtr<nsIURIMutator> mutator; |
202 | 0 | rv = handler->NewFileURIMutator(dbFile, getter_AddRefs(mutator)); |
203 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
204 | 0 | |
205 | 0 | nsCOMPtr<nsIFileURL> dbFileUrl; |
206 | 0 |
|
207 | 0 | nsAutoCString type; |
208 | 0 | PersistenceTypeToText(PERSISTENCE_TYPE_DEFAULT, type); |
209 | 0 |
|
210 | 0 | rv = NS_MutateURI(mutator) |
211 | 0 | .SetQuery( |
212 | 0 | NS_LITERAL_CSTRING("persistenceType=") + type + |
213 | 0 | NS_LITERAL_CSTRING("&group=") + aQuotaInfo.mGroup + |
214 | 0 | NS_LITERAL_CSTRING("&origin=") + aQuotaInfo.mOrigin + |
215 | 0 | NS_LITERAL_CSTRING("&cache=private")) |
216 | 0 | .Finalize(dbFileUrl); |
217 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
218 | 0 | |
219 | 0 | nsCOMPtr<mozIStorageService> ss = |
220 | 0 | do_GetService(MOZ_STORAGE_SERVICE_CONTRACTID); |
221 | 0 | if (NS_WARN_IF(!ss)) { return NS_ERROR_UNEXPECTED; } |
222 | 0 | |
223 | 0 | rv = ss->OpenDatabaseWithFileURL(dbFileUrl, getter_AddRefs(conn)); |
224 | 0 | if (rv == NS_ERROR_FILE_CORRUPTED) { |
225 | 0 | NS_WARNING("Cache database corrupted. Recreating empty database."); |
226 | 0 |
|
227 | 0 | conn = nullptr; |
228 | 0 |
|
229 | 0 | // There is nothing else we can do to recover. Also, this data can |
230 | 0 | // be deleted by QuotaManager at any time anyways. |
231 | 0 | rv = WipeDatabase(aQuotaInfo, dbFile, aDBDir); |
232 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
233 | 0 | |
234 | 0 | rv = ss->OpenDatabaseWithFileURL(dbFileUrl, getter_AddRefs(conn)); |
235 | 0 | } |
236 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
237 | 0 | |
238 | 0 | // Check the schema to make sure it is not too old. |
239 | 0 | int32_t schemaVersion = 0; |
240 | 0 | rv = conn->GetSchemaVersion(&schemaVersion); |
241 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
242 | 0 | if (schemaVersion > 0 && schemaVersion < db::kFirstShippedSchemaVersion) { |
243 | 0 | conn = nullptr; |
244 | 0 | rv = WipeDatabase(aQuotaInfo, dbFile, aDBDir); |
245 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
246 | 0 | |
247 | 0 | rv = ss->OpenDatabaseWithFileURL(dbFileUrl, getter_AddRefs(conn)); |
248 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
249 | 0 | } |
250 | 0 | |
251 | 0 | rv = db::InitializeConnection(conn); |
252 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
253 | 0 | |
254 | 0 | conn.forget(aConnOut); |
255 | 0 |
|
256 | 0 | return rv; |
257 | 0 | } |
258 | | |
259 | | } // namespace cache |
260 | | } // namespace dom |
261 | | } // namespace mozilla |