web_database_service.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // Chromium settings and storage represent user-selected preferences and
  5. // information and MUST not be extracted, overwritten or modified except
  6. // through Chromium defined APIs.
  7. #ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_SERVICE_H_
  8. #define COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_SERVICE_H_
  9. #include <memory>
  10. #include "base/callback_forward.h"
  11. #include "base/compiler_specific.h"
  12. #include "base/files/file_path.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/memory/ref_counted_delete_on_sequence.h"
  15. #include "base/memory/weak_ptr.h"
  16. #include "base/task/single_thread_task_runner.h"
  17. #include "components/webdata/common/web_data_service_base.h"
  18. #include "components/webdata/common/web_database.h"
  19. #include "components/webdata/common/webdata_export.h"
  20. class WebDatabaseBackend;
  21. namespace base {
  22. class Location;
  23. }
  24. class WDTypedResult;
  25. class WebDataServiceConsumer;
  26. ////////////////////////////////////////////////////////////////////////////////
  27. //
  28. // WebDatabaseService defines the interface to a generic data repository
  29. // responsible for controlling access to the web database (metadata associated
  30. // with web pages).
  31. //
  32. ////////////////////////////////////////////////////////////////////////////////
  33. class WEBDATA_EXPORT WebDatabaseService
  34. : public base::RefCountedDeleteOnSequence<WebDatabaseService> {
  35. public:
  36. using ReadTask =
  37. base::OnceCallback<std::unique_ptr<WDTypedResult>(WebDatabase*)>;
  38. using WriteTask = base::OnceCallback<WebDatabase::State(WebDatabase*)>;
  39. // Types for managing DB loading callbacks.
  40. using DBLoadErrorCallback =
  41. base::OnceCallback<void(sql::InitStatus, const std::string&)>;
  42. // WebDatabaseService lives on the UI sequence and posts tasks to the DB
  43. // sequence. |path| points to the WebDatabase file.
  44. WebDatabaseService(
  45. const base::FilePath& path,
  46. scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
  47. scoped_refptr<base::SingleThreadTaskRunner> db_task_runner);
  48. WebDatabaseService(const WebDatabaseService&) = delete;
  49. WebDatabaseService& operator=(const WebDatabaseService&) = delete;
  50. // Adds |table| as a WebDatabaseTable that will participate in
  51. // managing the database, transferring ownership. All calls to this
  52. // method must be made before |LoadDatabase| is called.
  53. virtual void AddTable(std::unique_ptr<WebDatabaseTable> table);
  54. // Initializes the web database service.
  55. virtual void LoadDatabase();
  56. // Unloads the database and shuts down the service.
  57. virtual void ShutdownDatabase();
  58. // Gets a pointer to the WebDatabase (owned by WebDatabaseService).
  59. // TODO(caitkp): remove this method once SyncServices no longer depend on it.
  60. virtual WebDatabase* GetDatabaseOnDB() const;
  61. // Returns a pointer to the WebDatabaseBackend.
  62. scoped_refptr<WebDatabaseBackend> GetBackend() const;
  63. // Schedule an update/write task on the DB sequence.
  64. virtual void ScheduleDBTask(const base::Location& from_here, WriteTask task);
  65. // Schedule a read task on the DB sequence.
  66. // Retrieves a WeakPtr to the |consumer| so that |consumer| does not have to
  67. // outlive the WebDatabaseService.
  68. virtual WebDataServiceBase::Handle ScheduleDBTaskWithResult(
  69. const base::Location& from_here,
  70. ReadTask task,
  71. WebDataServiceConsumer* consumer);
  72. // Cancel an existing request for a task on the DB sequence.
  73. // TODO(caitkp): Think about moving the definition of the Handle type to
  74. // somewhere else.
  75. virtual void CancelRequest(WebDataServiceBase::Handle h);
  76. // Register a callback to be notified that the database has failed to load.
  77. // Multiple callbacks may be registered, and each will be called at most once
  78. // (following a database load failure), then cleared.
  79. // Note: if the database load is already complete, then the callback will NOT
  80. // be stored or called.
  81. void RegisterDBErrorCallback(DBLoadErrorCallback callback);
  82. private:
  83. class BackendDelegate;
  84. friend class BackendDelegate;
  85. friend class base::RefCountedDeleteOnSequence<WebDatabaseService>;
  86. friend class base::DeleteHelper<WebDatabaseService>;
  87. using ErrorCallbacks = std::vector<DBLoadErrorCallback>;
  88. virtual ~WebDatabaseService();
  89. void OnDatabaseLoadDone(sql::InitStatus status,
  90. const std::string& diagnostics);
  91. base::FilePath path_;
  92. // The primary owner is |WebDatabaseService| but is refcounted because
  93. // PostTask on DB sequence may outlive us.
  94. scoped_refptr<WebDatabaseBackend> web_db_backend_;
  95. // Callbacks to be called if the DB has failed to load.
  96. ErrorCallbacks error_callbacks_;
  97. scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_;
  98. // All vended weak pointers are invalidated in ShutdownDatabase().
  99. base::WeakPtrFactory<WebDatabaseService> weak_ptr_factory_{this};
  100. };
  101. #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_SERVICE_H_