1 // Copyright (c) 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 
5 #ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_H_
6 #define COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_H_
7 
8 #include <map>
9 #include <string>
10 
11 #include "base/files/file_path.h"
12 #include "base/macros.h"
13 #include "components/webdata/common/web_database_table.h"
14 #include "components/webdata/common/webdata_export.h"
15 #include "sql/database.h"
16 #include "sql/init_status.h"
17 #include "sql/meta_table.h"
18 
19 // This class manages a SQLite database that stores various web page meta data.
20 class WEBDATA_EXPORT WebDatabase {
21  public:
22   enum State {
23     COMMIT_NOT_NEEDED,
24     COMMIT_NEEDED
25   };
26   // Exposed publicly so the keyword table can access it.
27   static const int kCurrentVersionNumber;
28   // The newest version of the database Chrome will NOT try to migrate.
29   static const int kDeprecatedVersionNumber;
30   // Use this as a path to create an in-memory database.
31   static const base::FilePath::CharType kInMemoryPath[];
32 
33   WebDatabase();
34   virtual ~WebDatabase();
35 
36   // Adds a database table. Ownership remains with the caller, which
37   // must ensure that the lifetime of |table| exceeds this object's
38   // lifetime. Must only be called before Init.
39   void AddTable(WebDatabaseTable* table);
40 
41   // Retrieves a table based on its |key|.
42   WebDatabaseTable* GetTable(WebDatabaseTable::TypeKey key);
43 
44   // Call before Init() to set the error callback to be used for the
45   // underlying database connection.
set_error_callback(const sql::Database::ErrorCallback & error_callback)46   void set_error_callback(const sql::Database::ErrorCallback& error_callback) {
47     db_.set_error_callback(error_callback);
48   }
49 
50   // Initialize the database given a name. The name defines where the SQLite
51   // file is. If this returns an error code, no other method should be called.
52   //
53   // Before calling this method, you must call AddTable for any
54   // WebDatabaseTable objects that are supposed to participate in
55   // managing the database.
56   sql::InitStatus Init(const base::FilePath& db_name);
57 
58   // Transactions management
59   void BeginTransaction();
60   void CommitTransaction();
61 
62   std::string GetDiagnosticInfo(int extended_error, sql::Statement* statement);
63 
64   // Exposed for testing only.
65   sql::Database* GetSQLConnection();
66 
67  private:
68   // Used by |Init()| to migration database schema from older versions to
69   // current version.
70   sql::InitStatus MigrateOldVersionsAsNeeded();
71 
72   // Migrates this database to |version|. Returns false if there was
73   // migration work to do and it failed, true otherwise.
74   //
75   // Implementations may set |*update_compatible_version| to true if
76   // the compatible version should be changed to |version|.
77   // Implementations should otherwise not modify this parameter.
78   bool MigrateToVersion(int version,
79                         bool* update_compatible_version);
80 
81   bool MigrateToVersion58DropWebAppsAndIntents();
82   bool MigrateToVersion79DropLoginsTable();
83 
84   sql::Database db_;
85   sql::MetaTable meta_table_;
86 
87   // Map of all the different tables that have been added to this
88   // object. Non-owning.
89   typedef std::map<WebDatabaseTable::TypeKey, WebDatabaseTable*> TableMap;
90   TableMap tables_;
91 
92   DISALLOW_COPY_AND_ASSIGN(WebDatabase);
93 };
94 
95 #endif  // COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_H_
96