Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "mozilla/DebugOnly.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "VacuumManager.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "mozilla/Services.h" |
michael@0 | 12 | #include "mozilla/Preferences.h" |
michael@0 | 13 | #include "nsIObserverService.h" |
michael@0 | 14 | #include "nsIFile.h" |
michael@0 | 15 | #include "nsThreadUtils.h" |
michael@0 | 16 | #include "prlog.h" |
michael@0 | 17 | #include "prtime.h" |
michael@0 | 18 | |
michael@0 | 19 | #include "mozStorageConnection.h" |
michael@0 | 20 | #include "mozIStorageStatement.h" |
michael@0 | 21 | #include "mozIStorageAsyncStatement.h" |
michael@0 | 22 | #include "mozIStoragePendingStatement.h" |
michael@0 | 23 | #include "mozIStorageError.h" |
michael@0 | 24 | #include "mozStorageHelper.h" |
michael@0 | 25 | #include "nsXULAppAPI.h" |
michael@0 | 26 | |
michael@0 | 27 | #define OBSERVER_TOPIC_IDLE_DAILY "idle-daily" |
michael@0 | 28 | #define OBSERVER_TOPIC_XPCOM_SHUTDOWN "xpcom-shutdown" |
michael@0 | 29 | |
michael@0 | 30 | // Used to notify begin and end of a heavy IO task. |
michael@0 | 31 | #define OBSERVER_TOPIC_HEAVY_IO "heavy-io-task" |
michael@0 | 32 | #define OBSERVER_DATA_VACUUM_BEGIN NS_LITERAL_STRING("vacuum-begin") |
michael@0 | 33 | #define OBSERVER_DATA_VACUUM_END NS_LITERAL_STRING("vacuum-end") |
michael@0 | 34 | |
michael@0 | 35 | // This preferences root will contain last vacuum timestamps (in seconds) for |
michael@0 | 36 | // each database. The database filename is used as a key. |
michael@0 | 37 | #define PREF_VACUUM_BRANCH "storage.vacuum.last." |
michael@0 | 38 | |
michael@0 | 39 | // Time between subsequent vacuum calls for a certain database. |
michael@0 | 40 | #define VACUUM_INTERVAL_SECONDS 30 * 86400 // 30 days. |
michael@0 | 41 | |
michael@0 | 42 | #ifdef PR_LOGGING |
michael@0 | 43 | extern PRLogModuleInfo *gStorageLog; |
michael@0 | 44 | #endif |
michael@0 | 45 | |
michael@0 | 46 | namespace mozilla { |
michael@0 | 47 | namespace storage { |
michael@0 | 48 | |
michael@0 | 49 | namespace { |
michael@0 | 50 | |
michael@0 | 51 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 52 | //// BaseCallback |
michael@0 | 53 | |
michael@0 | 54 | class BaseCallback : public mozIStorageStatementCallback |
michael@0 | 55 | { |
michael@0 | 56 | public: |
michael@0 | 57 | NS_DECL_ISUPPORTS |
michael@0 | 58 | NS_DECL_MOZISTORAGESTATEMENTCALLBACK |
michael@0 | 59 | BaseCallback() {} |
michael@0 | 60 | protected: |
michael@0 | 61 | virtual ~BaseCallback() {} |
michael@0 | 62 | }; |
michael@0 | 63 | |
michael@0 | 64 | NS_IMETHODIMP |
michael@0 | 65 | BaseCallback::HandleError(mozIStorageError *aError) |
michael@0 | 66 | { |
michael@0 | 67 | #ifdef DEBUG |
michael@0 | 68 | int32_t result; |
michael@0 | 69 | nsresult rv = aError->GetResult(&result); |
michael@0 | 70 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 71 | nsAutoCString message; |
michael@0 | 72 | rv = aError->GetMessage(message); |
michael@0 | 73 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 74 | |
michael@0 | 75 | nsAutoCString warnMsg; |
michael@0 | 76 | warnMsg.AppendLiteral("An error occured during async execution: "); |
michael@0 | 77 | warnMsg.AppendInt(result); |
michael@0 | 78 | warnMsg.AppendLiteral(" "); |
michael@0 | 79 | warnMsg.Append(message); |
michael@0 | 80 | NS_WARNING(warnMsg.get()); |
michael@0 | 81 | #endif |
michael@0 | 82 | return NS_OK; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | NS_IMETHODIMP |
michael@0 | 86 | BaseCallback::HandleResult(mozIStorageResultSet *aResultSet) |
michael@0 | 87 | { |
michael@0 | 88 | // We could get results from PRAGMA statements, but we don't mind them. |
michael@0 | 89 | return NS_OK; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | NS_IMETHODIMP |
michael@0 | 93 | BaseCallback::HandleCompletion(uint16_t aReason) |
michael@0 | 94 | { |
michael@0 | 95 | // By default BaseCallback will just be silent on completion. |
michael@0 | 96 | return NS_OK; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | NS_IMPL_ISUPPORTS( |
michael@0 | 100 | BaseCallback |
michael@0 | 101 | , mozIStorageStatementCallback |
michael@0 | 102 | ) |
michael@0 | 103 | |
michael@0 | 104 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 105 | //// Vacuumer declaration. |
michael@0 | 106 | |
michael@0 | 107 | class Vacuumer : public BaseCallback |
michael@0 | 108 | { |
michael@0 | 109 | public: |
michael@0 | 110 | NS_DECL_MOZISTORAGESTATEMENTCALLBACK |
michael@0 | 111 | |
michael@0 | 112 | Vacuumer(mozIStorageVacuumParticipant *aParticipant); |
michael@0 | 113 | |
michael@0 | 114 | bool execute(); |
michael@0 | 115 | nsresult notifyCompletion(bool aSucceeded); |
michael@0 | 116 | |
michael@0 | 117 | private: |
michael@0 | 118 | nsCOMPtr<mozIStorageVacuumParticipant> mParticipant; |
michael@0 | 119 | nsCString mDBFilename; |
michael@0 | 120 | nsCOMPtr<mozIStorageConnection> mDBConn; |
michael@0 | 121 | }; |
michael@0 | 122 | |
michael@0 | 123 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 124 | //// Vacuumer implementation. |
michael@0 | 125 | |
michael@0 | 126 | Vacuumer::Vacuumer(mozIStorageVacuumParticipant *aParticipant) |
michael@0 | 127 | : mParticipant(aParticipant) |
michael@0 | 128 | { |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | bool |
michael@0 | 132 | Vacuumer::execute() |
michael@0 | 133 | { |
michael@0 | 134 | MOZ_ASSERT(NS_IsMainThread(), "Must be running on the main thread!"); |
michael@0 | 135 | |
michael@0 | 136 | // Get the connection and check its validity. |
michael@0 | 137 | nsresult rv = mParticipant->GetDatabaseConnection(getter_AddRefs(mDBConn)); |
michael@0 | 138 | NS_ENSURE_SUCCESS(rv, false); |
michael@0 | 139 | bool ready = false; |
michael@0 | 140 | if (!mDBConn || NS_FAILED(mDBConn->GetConnectionReady(&ready)) || !ready) { |
michael@0 | 141 | NS_WARNING("Unable to get a connection to vacuum database"); |
michael@0 | 142 | return false; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | // Ask for the expected page size. Vacuum can change the page size, unless |
michael@0 | 146 | // the database is using WAL journaling. |
michael@0 | 147 | // TODO Bug 634374: figure out a strategy to fix page size with WAL. |
michael@0 | 148 | int32_t expectedPageSize = 0; |
michael@0 | 149 | rv = mParticipant->GetExpectedDatabasePageSize(&expectedPageSize); |
michael@0 | 150 | if (NS_FAILED(rv) || !Service::pageSizeIsValid(expectedPageSize)) { |
michael@0 | 151 | NS_WARNING("Invalid page size requested for database, will use default "); |
michael@0 | 152 | NS_WARNING(mDBFilename.get()); |
michael@0 | 153 | expectedPageSize = Service::getDefaultPageSize(); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | // Get the database filename. Last vacuum time is stored under this name |
michael@0 | 157 | // in PREF_VACUUM_BRANCH. |
michael@0 | 158 | nsCOMPtr<nsIFile> databaseFile; |
michael@0 | 159 | mDBConn->GetDatabaseFile(getter_AddRefs(databaseFile)); |
michael@0 | 160 | if (!databaseFile) { |
michael@0 | 161 | NS_WARNING("Trying to vacuum a in-memory database!"); |
michael@0 | 162 | return false; |
michael@0 | 163 | } |
michael@0 | 164 | nsAutoString databaseFilename; |
michael@0 | 165 | rv = databaseFile->GetLeafName(databaseFilename); |
michael@0 | 166 | NS_ENSURE_SUCCESS(rv, false); |
michael@0 | 167 | mDBFilename = NS_ConvertUTF16toUTF8(databaseFilename); |
michael@0 | 168 | MOZ_ASSERT(!mDBFilename.IsEmpty(), "Database filename cannot be empty"); |
michael@0 | 169 | |
michael@0 | 170 | // Check interval from last vacuum. |
michael@0 | 171 | int32_t now = static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC); |
michael@0 | 172 | int32_t lastVacuum; |
michael@0 | 173 | nsAutoCString prefName(PREF_VACUUM_BRANCH); |
michael@0 | 174 | prefName += mDBFilename; |
michael@0 | 175 | rv = Preferences::GetInt(prefName.get(), &lastVacuum); |
michael@0 | 176 | if (NS_SUCCEEDED(rv) && (now - lastVacuum) < VACUUM_INTERVAL_SECONDS) { |
michael@0 | 177 | // This database was vacuumed recently, skip it. |
michael@0 | 178 | return false; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | // Notify that we are about to start vacuuming. The participant can opt-out |
michael@0 | 182 | // if it cannot handle a vacuum at this time, and then we'll move to the next |
michael@0 | 183 | // one. |
michael@0 | 184 | bool vacuumGranted = false; |
michael@0 | 185 | rv = mParticipant->OnBeginVacuum(&vacuumGranted); |
michael@0 | 186 | NS_ENSURE_SUCCESS(rv, false); |
michael@0 | 187 | if (!vacuumGranted) { |
michael@0 | 188 | return false; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | // Notify a heavy IO task is about to start. |
michael@0 | 192 | nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService(); |
michael@0 | 193 | if (os) { |
michael@0 | 194 | DebugOnly<nsresult> rv = |
michael@0 | 195 | os->NotifyObservers(nullptr, OBSERVER_TOPIC_HEAVY_IO, |
michael@0 | 196 | OBSERVER_DATA_VACUUM_BEGIN.get()); |
michael@0 | 197 | MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to notify"); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | // Execute the statements separately, since the pragma may conflict with the |
michael@0 | 201 | // vacuum, if they are executed in the same transaction. |
michael@0 | 202 | nsCOMPtr<mozIStorageAsyncStatement> pageSizeStmt; |
michael@0 | 203 | nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR |
michael@0 | 204 | "PRAGMA page_size = "); |
michael@0 | 205 | pageSizeQuery.AppendInt(expectedPageSize); |
michael@0 | 206 | rv = mDBConn->CreateAsyncStatement(pageSizeQuery, |
michael@0 | 207 | getter_AddRefs(pageSizeStmt)); |
michael@0 | 208 | NS_ENSURE_SUCCESS(rv, false); |
michael@0 | 209 | nsRefPtr<BaseCallback> callback = new BaseCallback(); |
michael@0 | 210 | nsCOMPtr<mozIStoragePendingStatement> ps; |
michael@0 | 211 | rv = pageSizeStmt->ExecuteAsync(callback, getter_AddRefs(ps)); |
michael@0 | 212 | NS_ENSURE_SUCCESS(rv, false); |
michael@0 | 213 | |
michael@0 | 214 | nsCOMPtr<mozIStorageAsyncStatement> stmt; |
michael@0 | 215 | rv = mDBConn->CreateAsyncStatement(NS_LITERAL_CSTRING( |
michael@0 | 216 | "VACUUM" |
michael@0 | 217 | ), getter_AddRefs(stmt)); |
michael@0 | 218 | NS_ENSURE_SUCCESS(rv, false); |
michael@0 | 219 | rv = stmt->ExecuteAsync(this, getter_AddRefs(ps)); |
michael@0 | 220 | NS_ENSURE_SUCCESS(rv, false); |
michael@0 | 221 | |
michael@0 | 222 | return true; |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 226 | //// mozIStorageStatementCallback |
michael@0 | 227 | |
michael@0 | 228 | NS_IMETHODIMP |
michael@0 | 229 | Vacuumer::HandleError(mozIStorageError *aError) |
michael@0 | 230 | { |
michael@0 | 231 | #ifdef DEBUG |
michael@0 | 232 | int32_t result; |
michael@0 | 233 | nsresult rv = aError->GetResult(&result); |
michael@0 | 234 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 235 | nsAutoCString message; |
michael@0 | 236 | rv = aError->GetMessage(message); |
michael@0 | 237 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 238 | |
michael@0 | 239 | nsAutoCString warnMsg; |
michael@0 | 240 | warnMsg.AppendLiteral("Unable to vacuum database: "); |
michael@0 | 241 | warnMsg.Append(mDBFilename); |
michael@0 | 242 | warnMsg.AppendLiteral(" - "); |
michael@0 | 243 | warnMsg.AppendInt(result); |
michael@0 | 244 | warnMsg.AppendLiteral(" "); |
michael@0 | 245 | warnMsg.Append(message); |
michael@0 | 246 | NS_WARNING(warnMsg.get()); |
michael@0 | 247 | #endif |
michael@0 | 248 | |
michael@0 | 249 | #ifdef PR_LOGGING |
michael@0 | 250 | { |
michael@0 | 251 | int32_t result; |
michael@0 | 252 | nsresult rv = aError->GetResult(&result); |
michael@0 | 253 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 254 | nsAutoCString message; |
michael@0 | 255 | rv = aError->GetMessage(message); |
michael@0 | 256 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 257 | PR_LOG(gStorageLog, PR_LOG_ERROR, |
michael@0 | 258 | ("Vacuum failed with error: %d '%s'. Database was: '%s'", |
michael@0 | 259 | result, message.get(), mDBFilename.get())); |
michael@0 | 260 | } |
michael@0 | 261 | #endif |
michael@0 | 262 | return NS_OK; |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | NS_IMETHODIMP |
michael@0 | 266 | Vacuumer::HandleResult(mozIStorageResultSet *aResultSet) |
michael@0 | 267 | { |
michael@0 | 268 | NS_NOTREACHED("Got a resultset from a vacuum?"); |
michael@0 | 269 | return NS_OK; |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | NS_IMETHODIMP |
michael@0 | 273 | Vacuumer::HandleCompletion(uint16_t aReason) |
michael@0 | 274 | { |
michael@0 | 275 | if (aReason == REASON_FINISHED) { |
michael@0 | 276 | // Update last vacuum time. |
michael@0 | 277 | int32_t now = static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC); |
michael@0 | 278 | MOZ_ASSERT(!mDBFilename.IsEmpty(), "Database filename cannot be empty"); |
michael@0 | 279 | nsAutoCString prefName(PREF_VACUUM_BRANCH); |
michael@0 | 280 | prefName += mDBFilename; |
michael@0 | 281 | DebugOnly<nsresult> rv = Preferences::SetInt(prefName.get(), now); |
michael@0 | 282 | MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to set a preference"); |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | notifyCompletion(aReason == REASON_FINISHED); |
michael@0 | 286 | |
michael@0 | 287 | return NS_OK; |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | nsresult |
michael@0 | 291 | Vacuumer::notifyCompletion(bool aSucceeded) |
michael@0 | 292 | { |
michael@0 | 293 | nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService(); |
michael@0 | 294 | if (os) { |
michael@0 | 295 | os->NotifyObservers(nullptr, OBSERVER_TOPIC_HEAVY_IO, |
michael@0 | 296 | OBSERVER_DATA_VACUUM_END.get()); |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | nsresult rv = mParticipant->OnEndVacuum(aSucceeded); |
michael@0 | 300 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 301 | |
michael@0 | 302 | return NS_OK; |
michael@0 | 303 | } |
michael@0 | 304 | |
michael@0 | 305 | } // Anonymous namespace. |
michael@0 | 306 | |
michael@0 | 307 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 308 | //// VacuumManager |
michael@0 | 309 | |
michael@0 | 310 | NS_IMPL_ISUPPORTS( |
michael@0 | 311 | VacuumManager |
michael@0 | 312 | , nsIObserver |
michael@0 | 313 | ) |
michael@0 | 314 | |
michael@0 | 315 | VacuumManager * |
michael@0 | 316 | VacuumManager::gVacuumManager = nullptr; |
michael@0 | 317 | |
michael@0 | 318 | VacuumManager * |
michael@0 | 319 | VacuumManager::getSingleton() |
michael@0 | 320 | { |
michael@0 | 321 | //Don't allocate it in the child Process. |
michael@0 | 322 | if (XRE_GetProcessType() != GeckoProcessType_Default) { |
michael@0 | 323 | return nullptr; |
michael@0 | 324 | } |
michael@0 | 325 | |
michael@0 | 326 | if (gVacuumManager) { |
michael@0 | 327 | NS_ADDREF(gVacuumManager); |
michael@0 | 328 | return gVacuumManager; |
michael@0 | 329 | } |
michael@0 | 330 | gVacuumManager = new VacuumManager(); |
michael@0 | 331 | if (gVacuumManager) { |
michael@0 | 332 | NS_ADDREF(gVacuumManager); |
michael@0 | 333 | } |
michael@0 | 334 | return gVacuumManager; |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | VacuumManager::VacuumManager() |
michael@0 | 338 | : mParticipants("vacuum-participant") |
michael@0 | 339 | { |
michael@0 | 340 | MOZ_ASSERT(!gVacuumManager, |
michael@0 | 341 | "Attempting to create two instances of the service!"); |
michael@0 | 342 | gVacuumManager = this; |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | VacuumManager::~VacuumManager() |
michael@0 | 346 | { |
michael@0 | 347 | // Remove the static reference to the service. Check to make sure its us |
michael@0 | 348 | // in case somebody creates an extra instance of the service. |
michael@0 | 349 | MOZ_ASSERT(gVacuumManager == this, |
michael@0 | 350 | "Deleting a non-singleton instance of the service"); |
michael@0 | 351 | if (gVacuumManager == this) { |
michael@0 | 352 | gVacuumManager = nullptr; |
michael@0 | 353 | } |
michael@0 | 354 | } |
michael@0 | 355 | |
michael@0 | 356 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 357 | //// nsIObserver |
michael@0 | 358 | |
michael@0 | 359 | NS_IMETHODIMP |
michael@0 | 360 | VacuumManager::Observe(nsISupports *aSubject, |
michael@0 | 361 | const char *aTopic, |
michael@0 | 362 | const char16_t *aData) |
michael@0 | 363 | { |
michael@0 | 364 | if (strcmp(aTopic, OBSERVER_TOPIC_IDLE_DAILY) == 0) { |
michael@0 | 365 | // Try to run vacuum on all registered entries. Will stop at the first |
michael@0 | 366 | // successful one. |
michael@0 | 367 | nsCOMArray<mozIStorageVacuumParticipant> entries; |
michael@0 | 368 | mParticipants.GetEntries(entries); |
michael@0 | 369 | // If there are more entries than what a month can contain, we could end up |
michael@0 | 370 | // skipping some, since we run daily. So we use a starting index. |
michael@0 | 371 | static const char* kPrefName = PREF_VACUUM_BRANCH "index"; |
michael@0 | 372 | int32_t startIndex = Preferences::GetInt(kPrefName, 0); |
michael@0 | 373 | if (startIndex >= entries.Count()) { |
michael@0 | 374 | startIndex = 0; |
michael@0 | 375 | } |
michael@0 | 376 | int32_t index; |
michael@0 | 377 | for (index = startIndex; index < entries.Count(); ++index) { |
michael@0 | 378 | nsRefPtr<Vacuumer> vacuum = new Vacuumer(entries[index]); |
michael@0 | 379 | // Only vacuum one database per day. |
michael@0 | 380 | if (vacuum->execute()) { |
michael@0 | 381 | break; |
michael@0 | 382 | } |
michael@0 | 383 | } |
michael@0 | 384 | DebugOnly<nsresult> rv = Preferences::SetInt(kPrefName, index); |
michael@0 | 385 | MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to set a preference"); |
michael@0 | 386 | } |
michael@0 | 387 | |
michael@0 | 388 | return NS_OK; |
michael@0 | 389 | } |
michael@0 | 390 | |
michael@0 | 391 | } // namespace storage |
michael@0 | 392 | } // namespace mozilla |