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   sql::Connection db_;
     63   sql::MetaTable meta_table_;
     64 
     65   // Map of all the different tables that have been added to this
     66   // object. Non-owning.
     67   typedef std::map<WebDatabaseTable::TypeKey, WebDatabaseTable*> TableMap;
     68   TableMap tables_;
     69 
     70   DISALLOW_COPY_AND_ASSIGN(WebDatabase);
     71 };
     72 
     73 #endif  // COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_H_
     74