table_manager.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_SQLITE_PROTO_TABLE_MANAGER_H_
  5. #define COMPONENTS_SQLITE_PROTO_TABLE_MANAGER_H_
  6. #include "base/callback.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/synchronization/atomic_flag.h"
  10. namespace base {
  11. class Location;
  12. class SequencedTaskRunner;
  13. } // namespace base
  14. namespace sql {
  15. class Database;
  16. }
  17. namespace sqlite_proto {
  18. // Base class encapsulating database operation scheduling and management, scoped
  19. // to a collection of tables (possibly, but not necessarily, all of the
  20. // database's tables).
  21. //
  22. // Refcounted as it is created and destroyed in the main thread (e.g., the UI
  23. // thread in the browser process) but all database related functions need to
  24. // happen in the database sequence. The task runner for this sequence is
  25. // provided by the client to the constructor of this class.
  26. class TableManager : public base::RefCountedThreadSafe<TableManager> {
  27. public:
  28. // Returns a SequencedTaskRunner that is used to run tasks on the DB sequence.
  29. base::SequencedTaskRunner* GetTaskRunner();
  30. TableManager(const TableManager&) = delete;
  31. TableManager& operator=(const TableManager&) = delete;
  32. virtual void ScheduleDBTask(const base::Location& from_here,
  33. base::OnceCallback<void(sql::Database*)> task);
  34. virtual void ExecuteDBTaskOnDBSequence(
  35. base::OnceCallback<void(sql::Database*)> task);
  36. protected:
  37. explicit TableManager(
  38. scoped_refptr<base::SequencedTaskRunner> db_task_runner);
  39. virtual ~TableManager();
  40. // DB sequence functions.
  41. //
  42. // Creates tables if nonexistent, first clearing them if necessary, for
  43. // instance if this database has a versioned schema and the version has
  44. // changed since the tables were last written.
  45. virtual void CreateOrClearTablesIfNecessary() = 0;
  46. virtual void LogDatabaseStats() = 0;
  47. void Initialize(sql::Database* db);
  48. void SetCancelled();
  49. bool IsCancelled();
  50. sql::Database* DB();
  51. void ResetDB();
  52. bool CantAccessDatabase();
  53. private:
  54. base::AtomicFlag cancelled_;
  55. friend class base::RefCountedThreadSafe<TableManager>;
  56. scoped_refptr<base::SequencedTaskRunner> db_task_runner_;
  57. raw_ptr<sql::Database> db_;
  58. };
  59. } // namespace sqlite_proto
  60. #endif // COMPONENTS_SQLITE_PROTO_TABLE_MANAGER_H_