Bug 1454293 [wpt PR 10484] - null is not the correct origin for createDocument()...
[gecko.git] / storage / VacuumManager.cpp
blob939c204e55196e08498c9ef54951594363155d03
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
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/. */
7 #include "mozilla/DebugOnly.h"
9 #include "VacuumManager.h"
11 #include "mozilla/Services.h"
12 #include "mozilla/Preferences.h"
13 #include "nsIObserverService.h"
14 #include "nsIFile.h"
15 #include "nsThreadUtils.h"
16 #include "mozilla/Logging.h"
17 #include "prtime.h"
19 #include "mozStorageConnection.h"
20 #include "mozIStorageStatement.h"
21 #include "mozIStorageAsyncStatement.h"
22 #include "mozIStoragePendingStatement.h"
23 #include "mozIStorageError.h"
24 #include "mozStorageHelper.h"
25 #include "nsXULAppAPI.h"
27 #define OBSERVER_TOPIC_IDLE_DAILY "idle-daily"
28 #define OBSERVER_TOPIC_XPCOM_SHUTDOWN "xpcom-shutdown"
30 // Used to notify begin and end of a heavy IO task.
31 #define OBSERVER_TOPIC_HEAVY_IO "heavy-io-task"
32 #define OBSERVER_DATA_VACUUM_BEGIN u"vacuum-begin"
33 #define OBSERVER_DATA_VACUUM_END u"vacuum-end"
35 // This preferences root will contain last vacuum timestamps (in seconds) for
36 // each database. The database filename is used as a key.
37 #define PREF_VACUUM_BRANCH "storage.vacuum.last."
39 // Time between subsequent vacuum calls for a certain database.
40 #define VACUUM_INTERVAL_SECONDS 30 * 86400 // 30 days.
42 extern mozilla::LazyLogModule gStorageLog;
44 namespace mozilla {
45 namespace storage {
47 namespace {
49 ////////////////////////////////////////////////////////////////////////////////
50 //// BaseCallback
52 class BaseCallback : public mozIStorageStatementCallback
54 public:
55 NS_DECL_ISUPPORTS
56 NS_DECL_MOZISTORAGESTATEMENTCALLBACK
57 BaseCallback() {}
58 protected:
59 virtual ~BaseCallback() {}
62 NS_IMETHODIMP
63 BaseCallback::HandleError(mozIStorageError *aError)
65 #ifdef DEBUG
66 int32_t result;
67 nsresult rv = aError->GetResult(&result);
68 NS_ENSURE_SUCCESS(rv, rv);
69 nsAutoCString message;
70 rv = aError->GetMessage(message);
71 NS_ENSURE_SUCCESS(rv, rv);
73 nsAutoCString warnMsg;
74 warnMsg.AppendLiteral("An error occured during async execution: ");
75 warnMsg.AppendInt(result);
76 warnMsg.Append(' ');
77 warnMsg.Append(message);
78 NS_WARNING(warnMsg.get());
79 #endif
80 return NS_OK;
83 NS_IMETHODIMP
84 BaseCallback::HandleResult(mozIStorageResultSet *aResultSet)
86 // We could get results from PRAGMA statements, but we don't mind them.
87 return NS_OK;
90 NS_IMETHODIMP
91 BaseCallback::HandleCompletion(uint16_t aReason)
93 // By default BaseCallback will just be silent on completion.
94 return NS_OK;
97 NS_IMPL_ISUPPORTS(
98 BaseCallback
99 , mozIStorageStatementCallback
102 ////////////////////////////////////////////////////////////////////////////////
103 //// Vacuumer declaration.
105 class Vacuumer : public BaseCallback
107 public:
108 NS_DECL_MOZISTORAGESTATEMENTCALLBACK
110 explicit Vacuumer(mozIStorageVacuumParticipant *aParticipant);
112 bool execute();
113 nsresult notifyCompletion(bool aSucceeded);
115 private:
116 nsCOMPtr<mozIStorageVacuumParticipant> mParticipant;
117 nsCString mDBFilename;
118 nsCOMPtr<mozIStorageConnection> mDBConn;
121 ////////////////////////////////////////////////////////////////////////////////
122 //// Vacuumer implementation.
124 Vacuumer::Vacuumer(mozIStorageVacuumParticipant *aParticipant)
125 : mParticipant(aParticipant)
129 bool
130 Vacuumer::execute()
132 MOZ_ASSERT(NS_IsMainThread(), "Must be running on the main thread!");
134 // Get the connection and check its validity.
135 nsresult rv = mParticipant->GetDatabaseConnection(getter_AddRefs(mDBConn));
136 NS_ENSURE_SUCCESS(rv, false);
137 bool ready = false;
138 if (!mDBConn || NS_FAILED(mDBConn->GetConnectionReady(&ready)) || !ready) {
139 NS_WARNING("Unable to get a connection to vacuum database");
140 return false;
143 // Ask for the expected page size. Vacuum can change the page size, unless
144 // the database is using WAL journaling.
145 // TODO Bug 634374: figure out a strategy to fix page size with WAL.
146 int32_t expectedPageSize = 0;
147 rv = mParticipant->GetExpectedDatabasePageSize(&expectedPageSize);
148 if (NS_FAILED(rv) || !Service::pageSizeIsValid(expectedPageSize)) {
149 NS_WARNING("Invalid page size requested for database, will use default ");
150 NS_WARNING(mDBFilename.get());
151 expectedPageSize = Service::getDefaultPageSize();
154 // Get the database filename. Last vacuum time is stored under this name
155 // in PREF_VACUUM_BRANCH.
156 nsCOMPtr<nsIFile> databaseFile;
157 mDBConn->GetDatabaseFile(getter_AddRefs(databaseFile));
158 if (!databaseFile) {
159 NS_WARNING("Trying to vacuum a in-memory database!");
160 return false;
162 nsAutoString databaseFilename;
163 rv = databaseFile->GetLeafName(databaseFilename);
164 NS_ENSURE_SUCCESS(rv, false);
165 mDBFilename = NS_ConvertUTF16toUTF8(databaseFilename);
166 MOZ_ASSERT(!mDBFilename.IsEmpty(), "Database filename cannot be empty");
168 // Check interval from last vacuum.
169 int32_t now = static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC);
170 int32_t lastVacuum;
171 nsAutoCString prefName(PREF_VACUUM_BRANCH);
172 prefName += mDBFilename;
173 rv = Preferences::GetInt(prefName.get(), &lastVacuum);
174 if (NS_SUCCEEDED(rv) && (now - lastVacuum) < VACUUM_INTERVAL_SECONDS) {
175 // This database was vacuumed recently, skip it.
176 return false;
179 // Notify that we are about to start vacuuming. The participant can opt-out
180 // if it cannot handle a vacuum at this time, and then we'll move to the next
181 // one.
182 bool vacuumGranted = false;
183 rv = mParticipant->OnBeginVacuum(&vacuumGranted);
184 NS_ENSURE_SUCCESS(rv, false);
185 if (!vacuumGranted) {
186 return false;
189 // Notify a heavy IO task is about to start.
190 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
191 if (os) {
192 rv =
193 os->NotifyObservers(nullptr, OBSERVER_TOPIC_HEAVY_IO,
194 OBSERVER_DATA_VACUUM_BEGIN);
195 MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to notify");
198 // Execute the statements separately, since the pragma may conflict with the
199 // vacuum, if they are executed in the same transaction.
200 nsCOMPtr<mozIStorageAsyncStatement> pageSizeStmt;
201 nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
202 "PRAGMA page_size = ");
203 pageSizeQuery.AppendInt(expectedPageSize);
204 rv = mDBConn->CreateAsyncStatement(pageSizeQuery,
205 getter_AddRefs(pageSizeStmt));
206 NS_ENSURE_SUCCESS(rv, false);
207 RefPtr<BaseCallback> callback = new BaseCallback();
208 nsCOMPtr<mozIStoragePendingStatement> ps;
209 rv = pageSizeStmt->ExecuteAsync(callback, getter_AddRefs(ps));
210 NS_ENSURE_SUCCESS(rv, false);
212 nsCOMPtr<mozIStorageAsyncStatement> stmt;
213 rv = mDBConn->CreateAsyncStatement(NS_LITERAL_CSTRING(
214 "VACUUM"
215 ), getter_AddRefs(stmt));
216 NS_ENSURE_SUCCESS(rv, false);
217 rv = stmt->ExecuteAsync(this, getter_AddRefs(ps));
218 NS_ENSURE_SUCCESS(rv, false);
220 return true;
223 ////////////////////////////////////////////////////////////////////////////////
224 //// mozIStorageStatementCallback
226 NS_IMETHODIMP
227 Vacuumer::HandleError(mozIStorageError *aError)
229 int32_t result;
230 nsresult rv;
231 nsAutoCString message;
233 #ifdef DEBUG
234 rv = aError->GetResult(&result);
235 NS_ENSURE_SUCCESS(rv, rv);
236 rv = aError->GetMessage(message);
237 NS_ENSURE_SUCCESS(rv, rv);
239 nsAutoCString warnMsg;
240 warnMsg.AppendLiteral("Unable to vacuum database: ");
241 warnMsg.Append(mDBFilename);
242 warnMsg.AppendLiteral(" - ");
243 warnMsg.AppendInt(result);
244 warnMsg.Append(' ');
245 warnMsg.Append(message);
246 NS_WARNING(warnMsg.get());
247 #endif
249 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Error)) {
250 rv = aError->GetResult(&result);
251 NS_ENSURE_SUCCESS(rv, rv);
252 rv = aError->GetMessage(message);
253 NS_ENSURE_SUCCESS(rv, rv);
254 MOZ_LOG(gStorageLog, LogLevel::Error,
255 ("Vacuum failed with error: %d '%s'. Database was: '%s'",
256 result, message.get(), mDBFilename.get()));
258 return NS_OK;
261 NS_IMETHODIMP
262 Vacuumer::HandleResult(mozIStorageResultSet *aResultSet)
264 NS_NOTREACHED("Got a resultset from a vacuum?");
265 return NS_OK;
268 NS_IMETHODIMP
269 Vacuumer::HandleCompletion(uint16_t aReason)
271 if (aReason == REASON_FINISHED) {
272 // Update last vacuum time.
273 int32_t now = static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC);
274 MOZ_ASSERT(!mDBFilename.IsEmpty(), "Database filename cannot be empty");
275 nsAutoCString prefName(PREF_VACUUM_BRANCH);
276 prefName += mDBFilename;
277 DebugOnly<nsresult> rv = Preferences::SetInt(prefName.get(), now);
278 MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to set a preference");
281 notifyCompletion(aReason == REASON_FINISHED);
283 return NS_OK;
286 nsresult
287 Vacuumer::notifyCompletion(bool aSucceeded)
289 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
290 if (os) {
291 os->NotifyObservers(nullptr, OBSERVER_TOPIC_HEAVY_IO,
292 OBSERVER_DATA_VACUUM_END);
295 nsresult rv = mParticipant->OnEndVacuum(aSucceeded);
296 NS_ENSURE_SUCCESS(rv, rv);
298 return NS_OK;
301 } // namespace
303 ////////////////////////////////////////////////////////////////////////////////
304 //// VacuumManager
306 NS_IMPL_ISUPPORTS(
307 VacuumManager
308 , nsIObserver
311 VacuumManager *
312 VacuumManager::gVacuumManager = nullptr;
314 already_AddRefed<VacuumManager>
315 VacuumManager::getSingleton()
317 //Don't allocate it in the child Process.
318 if (!XRE_IsParentProcess()) {
319 return nullptr;
322 if (!gVacuumManager) {
323 auto manager = MakeRefPtr<VacuumManager>();
324 MOZ_ASSERT(gVacuumManager == manager.get());
325 return manager.forget();
327 return do_AddRef(gVacuumManager);
330 VacuumManager::VacuumManager()
331 : mParticipants("vacuum-participant")
333 MOZ_ASSERT(!gVacuumManager,
334 "Attempting to create two instances of the service!");
335 gVacuumManager = this;
338 VacuumManager::~VacuumManager()
340 // Remove the static reference to the service. Check to make sure its us
341 // in case somebody creates an extra instance of the service.
342 MOZ_ASSERT(gVacuumManager == this,
343 "Deleting a non-singleton instance of the service");
344 if (gVacuumManager == this) {
345 gVacuumManager = nullptr;
349 ////////////////////////////////////////////////////////////////////////////////
350 //// nsIObserver
352 NS_IMETHODIMP
353 VacuumManager::Observe(nsISupports *aSubject,
354 const char *aTopic,
355 const char16_t *aData)
357 if (strcmp(aTopic, OBSERVER_TOPIC_IDLE_DAILY) == 0) {
358 // Try to run vacuum on all registered entries. Will stop at the first
359 // successful one.
360 nsCOMArray<mozIStorageVacuumParticipant> entries;
361 mParticipants.GetEntries(entries);
362 // If there are more entries than what a month can contain, we could end up
363 // skipping some, since we run daily. So we use a starting index.
364 static const char* kPrefName = PREF_VACUUM_BRANCH "index";
365 int32_t startIndex = Preferences::GetInt(kPrefName, 0);
366 if (startIndex >= entries.Count()) {
367 startIndex = 0;
369 int32_t index;
370 for (index = startIndex; index < entries.Count(); ++index) {
371 RefPtr<Vacuumer> vacuum = new Vacuumer(entries[index]);
372 // Only vacuum one database per day.
373 if (vacuum->execute()) {
374 break;
377 DebugOnly<nsresult> rv = Preferences::SetInt(kPrefName, index);
378 MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to set a preference");
381 return NS_OK;
384 } // namespace storage
385 } // namespace mozilla