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