Home | History | Annotate | Download | only in common
      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 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "components/webdata/common/web_database_table.h"
     12 #include "components/webdata/common/webdata_export.h"
     13 #include "sql/connection.h"
     14 #include "sql/init_status.h"
     15 #include "sql/meta_table.h"
     16 
     17 namespace base {
     18 class FilePath;
     19 }
     20 
     21 // This class manages a SQLite database that stores various web page meta data.
     22 class WEBDATA_EXPORT WebDatabase {
     23  public:
     24   enum State {
     25     COMMIT_NOT_NEEDED,
     26     COMMIT_NEEDED
     27   };
     28   // Exposed publicly so the keyword table can access it.
     29   static const int kCurrentVersionNumber;
     30 
     31   WebDatabase();
     32   virtual ~WebDatabase();
     33 
     34   // Adds a database table. Ownership remains with the caller, which
     35   // must ensure that the lifetime of |table| exceeds this object's
     36   // lifetime. Must only be called before Init.
     37   void AddTable(WebDatabaseTable* table);
     38 
     39   // Retrieves a table based on its |key|.
     40   WebDatabaseTable* GetTable(WebDatabaseTable::TypeKey key);
     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 
     50   // Transactions management
     51   void BeginTransaction();
     52   void CommitTransaction();
     53 
     54   // Exposed for testing only.
     55   sql::Connection* GetSQLConnection();
     56 
     57  private:
     58   // Used by |Init()| to migration database schema from older versions to
     59   // current version.
     60   sql::InitStatus MigrateOldVersionsAsNeeded();
     61 
     62   // Migrates this database to |version|. Returns false if there was
     63   // migration work to do and it failed, true otherwise.
     64   //
     65   // Implementations may set |*update_compatible_version| to true if
     66   // the compatible version should be changed to |version|.
     67   // Implementations should otherwise not modify this parameter.
     68   bool MigrateToVersion(int version,
     69                         bool* update_compatible_version);
     70 
     71   // Migration method for version 58.
     72   bool MigrateToVersion58DropWebAppsAndIntents();
     73 
     74   sql::Connection db_;
     75   sql::MetaTable meta_table_;
     76 
     77   // Map of all the different tables that have been added to this
     78   // object. Non-owning.
     79   typedef std::map<WebDatabaseTable::TypeKey, WebDatabaseTable*> TableMap;
     80   TableMap tables_;
     81 
     82   DISALLOW_COPY_AND_ASSIGN(WebDatabase);
     83 };
     84 
     85 #endif  // COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_H_
     86