DBAction.cpp 6.7 KB

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