web_database_backend.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2013 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_WEBDATA_COMMON_WEB_DATABASE_BACKEND_H_
  5. #define COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_BACKEND_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/callback_forward.h"
  9. #include "base/compiler_specific.h"
  10. #include "base/files/file_path.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/memory/ref_counted_delete_on_sequence.h"
  13. #include "base/task/single_thread_task_runner.h"
  14. #include "components/webdata/common/web_data_request_manager.h"
  15. #include "components/webdata/common/web_database_service.h"
  16. #include "components/webdata/common/webdata_export.h"
  17. class WebDatabase;
  18. class WebDatabaseTable;
  19. class WebDataRequest;
  20. class WebDataRequestManager;
  21. // WebDatabaseBackend handles all database tasks posted by
  22. // WebDatabaseService. It is refcounted to allow asynchronous destruction on the
  23. // DB thread.
  24. class WEBDATA_EXPORT WebDatabaseBackend
  25. : public base::RefCountedDeleteOnSequence<WebDatabaseBackend> {
  26. public:
  27. class Delegate {
  28. public:
  29. virtual ~Delegate() {}
  30. // Invoked when the backend has finished loading the db.
  31. // |status| is the result of initializing the db.
  32. // |diagnostics| contains diagnostic information about the db, and it will
  33. // only be populated when an error occurs.
  34. virtual void DBLoaded(sql::InitStatus status,
  35. const std::string& diagnostics) = 0;
  36. };
  37. WebDatabaseBackend(
  38. const base::FilePath& path,
  39. std::unique_ptr<Delegate> delegate,
  40. const scoped_refptr<base::SingleThreadTaskRunner>& db_thread);
  41. WebDatabaseBackend(const WebDatabaseBackend&) = delete;
  42. WebDatabaseBackend& operator=(const WebDatabaseBackend&) = delete;
  43. // Must call only before InitDatabaseWithCallback.
  44. void AddTable(std::unique_ptr<WebDatabaseTable> table);
  45. // Initializes the database and notifies caller via callback when complete.
  46. // Callback is called synchronously.
  47. void InitDatabase();
  48. // Shuts down the database.
  49. void ShutdownDatabase();
  50. // Task wrappers to update requests and and notify |request_manager_|. These
  51. // are used in cases where the request is being made from the UI thread and an
  52. // asyncronous callback is required to notify the client of |request|'s
  53. // completion.
  54. void DBWriteTaskWrapper(WebDatabaseService::WriteTask task,
  55. std::unique_ptr<WebDataRequest> request);
  56. void DBReadTaskWrapper(WebDatabaseService::ReadTask task,
  57. std::unique_ptr<WebDataRequest> request);
  58. // Task runners to run database tasks.
  59. void ExecuteWriteTask(WebDatabaseService::WriteTask task);
  60. std::unique_ptr<WDTypedResult> ExecuteReadTask(
  61. WebDatabaseService::ReadTask task);
  62. const scoped_refptr<WebDataRequestManager>& request_manager() {
  63. return request_manager_;
  64. }
  65. WebDatabase* database() { return db_.get(); }
  66. protected:
  67. friend class base::RefCountedDeleteOnSequence<WebDatabaseBackend>;
  68. friend class base::DeleteHelper<WebDatabaseBackend>;
  69. virtual ~WebDatabaseBackend();
  70. private:
  71. // Opens the database file from the profile path if an init has not yet been
  72. // attempted. Separated from the constructor to ease construction/destruction
  73. // of this object on one thread but database access on the DB thread.
  74. void LoadDatabaseIfNecessary();
  75. // Invoked on a db error.
  76. void DatabaseErrorCallback(int error, sql::Statement* statement);
  77. // Commit the current transaction.
  78. void Commit();
  79. // Path to database file.
  80. base::FilePath db_path_;
  81. // The tables that participate in managing the database. These are
  82. // owned here but other than that this class does nothing with
  83. // them. Their initialization is in whatever factory creates
  84. // WebDatabaseService, and lookup by type is provided by the
  85. // WebDatabase class. The tables need to be owned by this refcounted
  86. // object, or they themselves would need to be refcounted. Owning
  87. // them here rather than having WebDatabase own them makes for
  88. // easier unit testing of WebDatabase.
  89. std::vector<std::unique_ptr<WebDatabaseTable>> tables_;
  90. std::unique_ptr<WebDatabase> db_;
  91. // Keeps track of all pending requests made to the db.
  92. scoped_refptr<WebDataRequestManager> request_manager_ =
  93. base::MakeRefCounted<WebDataRequestManager>();
  94. // State of database initialization. Used to prevent the executing of tasks
  95. // before the db is ready.
  96. sql::InitStatus init_status_ = sql::INIT_FAILURE;
  97. // True if an attempt has been made to load the database (even if the attempt
  98. // fails), used to avoid continually trying to reinit if the db init fails.
  99. bool init_complete_ = false;
  100. // True if a catastrophic database error occurs and further error callbacks
  101. // from the database should be ignored.
  102. bool catastrophic_error_occurred_ = false;
  103. // If a catastrophic database error has occurred, this contains any available
  104. // diagnostic information.
  105. std::string diagnostics_;
  106. // Delegate. See the class definition above for more information.
  107. std::unique_ptr<Delegate> delegate_;
  108. };
  109. #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_BACKEND_H_