web_database.h 3.3 KB

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