database.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBDATABASE_DATABASE_H_
  26. #define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBDATABASE_DATABASE_H_
  27. #include <atomic>
  28. #include "base/single_thread_task_runner.h"
  29. #include "third_party/blink/renderer/bindings/modules/v8/v8_database_callback.h"
  30. #include "third_party/blink/renderer/modules/webdatabase/database_authorizer.h"
  31. #include "third_party/blink/renderer/modules/webdatabase/database_basic_types.h"
  32. #include "third_party/blink/renderer/modules/webdatabase/database_error.h"
  33. #include "third_party/blink/renderer/modules/webdatabase/sql_transaction.h"
  34. #include "third_party/blink/renderer/modules/webdatabase/sql_transaction_backend.h"
  35. #include "third_party/blink/renderer/modules/webdatabase/sqlite/sqlite_database.h"
  36. #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
  37. #include "third_party/blink/renderer/platform/scheduler/public/frame_or_worker_scheduler.h"
  38. #include "third_party/blink/renderer/platform/weborigin/security_origin.h"
  39. #include "third_party/blink/renderer/platform/wtf/deque.h"
  40. #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
  41. namespace blink {
  42. class ChangeVersionData;
  43. class DatabaseAuthorizer;
  44. class DatabaseContext;
  45. class ExecutionContext;
  46. class SQLTransactionClient;
  47. class SQLTransactionCoordinator;
  48. class Database final : public ScriptWrappable {
  49. DEFINE_WRAPPERTYPEINFO();
  50. public:
  51. Database(DatabaseContext*,
  52. const String& name,
  53. const String& expected_version,
  54. const String& display_name,
  55. uint32_t estimated_size);
  56. ~Database() override;
  57. void Trace(Visitor*) const override;
  58. bool OpenAndVerifyVersion(bool set_version_in_new_database,
  59. DatabaseError&,
  60. String& error_message,
  61. V8DatabaseCallback* creation_callback);
  62. void Close();
  63. SQLTransactionBackend* RunTransaction(SQLTransaction*,
  64. bool read_only,
  65. const ChangeVersionData*);
  66. void ScheduleTransactionStep(SQLTransactionBackend*);
  67. void InProgressTransactionCompleted();
  68. SQLTransactionClient* TransactionClient() const;
  69. SQLTransactionCoordinator* TransactionCoordinator() const;
  70. // Direct support for the DOM API
  71. String version() const;
  72. void changeVersion(const String& old_version,
  73. const String& new_version,
  74. V8SQLTransactionCallback*,
  75. V8SQLTransactionErrorCallback*,
  76. V8VoidCallback* success_callback);
  77. void transaction(V8SQLTransactionCallback*,
  78. V8SQLTransactionErrorCallback*,
  79. V8VoidCallback* success_callback);
  80. void readTransaction(V8SQLTransactionCallback*,
  81. V8SQLTransactionErrorCallback*,
  82. V8VoidCallback* success_callback);
  83. void PerformTransaction(SQLTransaction::OnProcessCallback*,
  84. SQLTransaction::OnErrorCallback*,
  85. SQLTransaction::OnSuccessCallback*);
  86. bool Opened() { return opened_.load(std::memory_order_acquire); }
  87. bool IsNew() const { return new_; }
  88. const SecurityOrigin* GetSecurityOrigin() const;
  89. String StringIdentifier() const;
  90. String DisplayName() const;
  91. uint32_t EstimatedSize() const;
  92. String FileName() const;
  93. SQLiteDatabase& SqliteDatabase() { return sqlite_database_; }
  94. uint64_t MaximumSize() const;
  95. void IncrementalVacuumIfNeeded();
  96. void DisableAuthorizer();
  97. void EnableAuthorizer();
  98. void SetAuthorizerPermissions(int);
  99. bool LastActionChangedDatabase();
  100. bool LastActionWasInsert();
  101. void ResetDeletes();
  102. bool HadDeletes();
  103. void ResetAuthorizer();
  104. Vector<String> TableNames();
  105. void ScheduleTransactionCallback(SQLTransaction*);
  106. void CloseImmediately();
  107. void CloseDatabase();
  108. DatabaseContext* GetDatabaseContext() const {
  109. return database_context_.Get();
  110. }
  111. ExecutionContext* GetExecutionContext() const;
  112. base::SingleThreadTaskRunner* GetDatabaseTaskRunner() const;
  113. private:
  114. class DatabaseOpenTask;
  115. class DatabaseCloseTask;
  116. class DatabaseTransactionTask;
  117. class DatabaseTableNamesTask;
  118. bool PerformOpenAndVerify(bool set_version_in_new_database,
  119. DatabaseError&,
  120. String& error_message);
  121. void RunCreationCallback(V8DatabaseCallback* creation_callback,
  122. std::unique_ptr<probe::AsyncTaskId> task_id);
  123. void ScheduleTransaction();
  124. bool GetVersionFromDatabase(String& version,
  125. bool should_cache_version = true);
  126. bool SetVersionInDatabase(const String& version,
  127. bool should_cache_version = true);
  128. void SetExpectedVersion(const String&);
  129. const String& ExpectedVersion() const { return expected_version_; }
  130. String GetCachedVersion() const;
  131. void SetCachedVersion(const String&);
  132. bool GetActualVersionForTransaction(String& version);
  133. void RunTransaction(SQLTransaction::OnProcessCallback*,
  134. SQLTransaction::OnErrorCallback*,
  135. SQLTransaction::OnSuccessCallback*,
  136. bool read_only,
  137. const ChangeVersionData* = nullptr);
  138. Vector<String> PerformGetTableNames();
  139. void ReportSqliteError(int sqlite_error_code);
  140. void LogErrorMessage(const String&);
  141. static const char* DatabaseInfoTableName();
  142. String DatabaseDebugName() const {
  143. return context_thread_security_origin_->ToString() + "::" + name_;
  144. }
  145. scoped_refptr<const SecurityOrigin> context_thread_security_origin_;
  146. scoped_refptr<const SecurityOrigin> database_thread_security_origin_;
  147. Member<DatabaseContext>
  148. database_context_; // Associated with m_executionContext.
  149. // ExecutionContext::GetTaskRunner() is not thread-safe, so we save
  150. // SingleThreadTaskRunner for TaskType::DatabaseAccess for later use as the
  151. // constructor runs in the main thread.
  152. scoped_refptr<base::SingleThreadTaskRunner> database_task_runner_;
  153. String name_;
  154. String expected_version_;
  155. String display_name_;
  156. uint32_t estimated_size_;
  157. String filename_;
  158. DatabaseGuid guid_;
  159. // Atomically written from the database thread only, but read from multiple
  160. // threads.
  161. std::atomic_bool opened_;
  162. bool new_;
  163. DatabaseAuthorizer database_authorizer_;
  164. SQLiteDatabase sqlite_database_;
  165. Deque<CrossThreadPersistent<SQLTransactionBackend>> transaction_queue_;
  166. Mutex transaction_in_progress_mutex_;
  167. bool transaction_in_progress_;
  168. bool is_transaction_queue_enabled_;
  169. // Disable BackForwardCache when using WebDatabase feature, because we do not
  170. // handle the state inside the portal after putting the page in cache.
  171. FrameOrWorkerScheduler::SchedulingAffectingFeatureHandle
  172. feature_handle_for_scheduler_;
  173. friend class ChangeVersionWrapper;
  174. friend class DatabaseManager;
  175. friend class SQLStatementBackend;
  176. friend class SQLTransaction;
  177. friend class SQLTransactionBackend;
  178. };
  179. } // namespace blink
  180. #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBDATABASE_DATABASE_H_