1513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be 3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file. 4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#ifndef CHROME_BROWSER_HISTORY_HISTORY_DATABASE_H_ 6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#define CHROME_BROWSER_HISTORY_HISTORY_DATABASE_H_ 73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once 8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "app/sql/connection.h" 10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "app/sql/init_status.h" 11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "app/sql/meta_table.h" 12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "build/build_config.h" 13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/history/download_database.h" 14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/history/history_types.h" 15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/history/starred_url_database.h" 16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/history/url_database.h" 17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/history/visit_database.h" 18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/history/visitsegment_database.h" 19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass FilePath; 21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochnamespace history { 23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Forward declaration for the temporary migration code in Init(). 25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass TextDatabaseManager; 26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Encapsulates the SQL connection for the history database. This class holds 28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// the database connection and has methods the history system (including full 29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// text search) uses for writing and retrieving information. 30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// We try to keep most logic out of the history database; this should be seen 32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// as the storage interface. Logic for manipulating this storage layer should 33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// be in HistoryBackend.cc. 34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass HistoryDatabase : public DownloadDatabase, 35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // TODO(sky): See if we can nuke StarredURLDatabase and just create on the 36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // stack for migration. Then HistoryDatabase would directly descend from 37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // URLDatabase. 38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public StarredURLDatabase, 39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public VisitDatabase, 40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public VisitSegmentDatabase { 41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public: 42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // A simple class for scoping a history database transaction. This does not 43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // support rollback since the history database doesn't, either. 44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch class TransactionScoper { 45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public: 46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch explicit TransactionScoper(HistoryDatabase* db) : db_(db) { 47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch db_->BeginTransaction(); 48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ~TransactionScoper() { 50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch db_->CommitTransaction(); 51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private: 53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch HistoryDatabase* db_; 54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch }; 55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Must call Init() to complete construction. Although it can be created on 57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // any thread, it must be destructed on the history thread for proper 58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // database cleanup. 59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch HistoryDatabase(); 60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual ~HistoryDatabase(); 62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Must call this function to complete initialization. Will return true on 64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // success. On false, no other function should be called. You may want to call 65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // BeginExclusiveMode after this when you are ready. 66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch sql::InitStatus Init(const FilePath& history_name, 67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const FilePath& tmp_bookmarks_path); 68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Call to set the mode on the database to exclusive. The default locking mode 70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // is "normal" but we want to run in exclusive mode for slightly better 71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // performance since we know nobody else is using the database. This is 72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // separate from Init() since the in-memory database attaches to slurp the 73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // data out, and this can't happen in exclusive mode. 74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch void BeginExclusiveMode(); 75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Returns the current version that we will generate history databases with. 77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static int GetCurrentVersion(); 78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Transactions on the history database. Use the Transaction object above 80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // for most work instead of these directly. We support nested transactions 81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // and only commit when the outermost transaction is committed. This means 82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // that it is impossible to rollback a specific transaction. We could roll 83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // back the outermost transaction if any inner one is rolled back, but it 84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // turns out we don't really need this type of integrity for the history 85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // database, so we just don't support it. 86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch void BeginTransaction(); 87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch void CommitTransaction(); 88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch int transaction_nesting() const { // for debugging and assertion purposes 89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return db_.transaction_nesting(); 90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Drops all tables except the URL, and download tables, and recreates them 93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // from scratch. This is done to rapidly clean up stuff when deleting all 94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // history. It is faster and less likely to have problems that deleting all 95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // rows in the tables. 96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // 97c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // We don't delete the downloads table, since there may be in progress 98c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // downloads. We handle the download history clean up separately in: 99c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // DownloadManager::RemoveDownloadsFromHistoryBetween. 100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // 101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Returns true on success. On failure, the caller should assume that the 102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // database is invalid. There could have been an error recreating a table. 103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // This should be treated the same as an init failure, and the database 104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // should not be used any more. 105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // 106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // This will also recreate the supplementary URL indices, since these 107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // indices won't be created automatically when using the temporary URL 108c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // table (what the caller does right before calling this). 109c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch bool RecreateAllTablesButURL(); 110c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 111c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Vacuums the database. This will cause sqlite to defragment and collect 112c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // unused space in the file. It can be VERY SLOW. 113c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch void Vacuum(); 114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Returns true if the history backend should erase the full text search 116c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // and archived history files as part of version 16 -> 17 migration. The 117c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // time format changed in this revision, and these files would be much slower 118c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // to migrate. Since the data is less important, they should be deleted. 119c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // 120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // This flag will be valid after Init() is called. It will always be false 121c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // when running on Windows. 122c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch bool needs_version_17_migration() const { 123c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return needs_version_17_migration_; 124c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 125c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 126513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch // Marks the database as no longer needing migration. 127513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch void ThumbnailMigrationDone(); 128513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch 129513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch // Returns true if thumbnails needs to be migrated. 130513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch bool GetNeedsThumbnailMigration(); 131c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 132c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Visit table functions ---------------------------------------------------- 133c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 134c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Update the segment id of a visit. Return true on success. 135c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch bool SetSegmentID(VisitID visit_id, SegmentID segment_id); 136c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 137c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Query the segment ID for the provided visit. Return 0 on failure or if the 138c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // visit id wasn't found. 139c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch SegmentID GetSegmentID(VisitID visit_id); 140c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 141c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Retrieves/Updates early expiration threshold, which specifies the earliest 142c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // known point in history that may possibly to contain visits suitable for 143c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // early expiration (AUTO_SUBFRAMES). 144c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual base::Time GetEarlyExpirationThreshold(); 145c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual void UpdateEarlyExpirationThreshold(base::Time threshold); 146c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 147c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private: 148ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen FRIEND_TEST_ALL_PREFIXES(IconMappingMigrationTest, TestIconMappingMigration); 149c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Implemented for URLDatabase. 150c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual sql::Connection& GetDB(); 151c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 152c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Migration ----------------------------------------------------------------- 153c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 154c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Makes sure the version is up-to-date, updating if necessary. If the 155c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // database is too old to migrate, the user will be notified. In this case, or 156c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // for other errors, false will be returned. True means it is up-to-date and 157c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // ready for use. 158c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // 159c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // This assumes it is called from the init function inside a transaction. It 160c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // may commit the transaction and start a new one if migration requires it. 161c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch sql::InitStatus EnsureCurrentVersion(const FilePath& tmp_bookmarks_path); 162c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 163c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#if !defined(OS_WIN) 164c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Converts the time epoch in the database from being 1970-based to being 165c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // 1601-based which corresponds to the change in Time.internal_value_. 166c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch void MigrateTimeEpoch(); 167c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif 168c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 169c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // --------------------------------------------------------------------------- 170c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 171c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch sql::Connection db_; 172c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch sql::MetaTable meta_table_; 173c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 174c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch base::Time cached_early_expiration_threshold_; 175c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 176c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // See the getters above. 177c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch bool needs_version_17_migration_; 178c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 179c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch DISALLOW_COPY_AND_ASSIGN(HistoryDatabase); 180c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 181c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 182c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} // history 183c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 184c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif // CHROME_BROWSER_HISTORY_HISTORY_DATABASE_H_ 185