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