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.
5 #ifndef CHROME_BROWSER_HISTORY_ANDROID_ANDROID_CACHE_DATABASE_H_
6 #define CHROME_BROWSER_HISTORY_ANDROID_ANDROID_CACHE_DATABASE_H_
8 #include "base/files/file_path.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/time/time.h"
11 #include "components/history/core/android/android_history_types.h"
12 #include "sql/connection.h"
13 #include "sql/init_status.h"
17 // This database is used to support Android ContentProvider APIs.
18 // It will be created only when it used, and deleted by HistoryBackend when
19 // history system shutdown.
20 class AndroidCacheDatabase {
22 AndroidCacheDatabase();
23 virtual ~AndroidCacheDatabase();
25 // Creates the database, deletes existing one if any; also attach it to the
26 // database returned by GetDB(). Returns sql::INIT_OK on success, otherwise
27 // sql::INIT_FAILURE returned.
28 sql::InitStatus InitAndroidCacheDatabase(const base::FilePath& db_name);
30 // The bookmark_cache table ------------------------------------------------
32 // Adds a row to the bookmark_cache table. Returns true on success.
33 bool AddBookmarkCacheRow(const base::Time& created_time,
34 const base::Time& last_visit_time,
37 // Clears all rows in the bookmark_cache table; returns true on success.
38 bool ClearAllBookmarkCache();
40 // Marks the given |url_ids| as bookmarked; Returns true on success.
41 bool MarkURLsAsBookmarked(const std::vector<URLID>& url_id);
43 // Set the given |url_id|'s favicon column to |favicon_id|. Returns true on
45 bool SetFaviconID(URLID url_id, favicon_base::FaviconID favicon_id);
47 // The search_terms table -------------------------------------------------
49 // Add a row in the search_term table with the given |term| and
50 // |last_visit_time|. Return the new row's id on success, otherwise 0 is
52 SearchTermID AddSearchTerm(const base::string16& term,
53 const base::Time& last_visit_time);
55 // Updates the |id|'s row with the given |row|; returns true on success.
56 bool UpdateSearchTerm(SearchTermID id, const SearchTermRow& row);
58 // Get SearchTermRow of the given |term|; return the row id on success.
59 // otherwise 0 is returned.
60 // The found row is return in |row| if it is not NULL.
61 SearchTermID GetSearchTerm(const base::string16& term, SearchTermRow* row);
63 // Delete the search terms which don't exist in keyword_search_terms table.
64 bool DeleteUnusedSearchTerms();
67 // Returns the database for the functions in this interface. The decendent of
68 // this class implements these functions to return its objects.
69 virtual sql::Connection& GetDB() = 0;
72 FRIEND_TEST_ALL_PREFIXES(AndroidCacheDatabaseTest, InitAndroidCacheDatabase);
74 // Creates the database and make it ready for attaching; returns true on
76 bool CreateDatabase(const base::FilePath& db_name);
78 // Creates the bookmark_cache table in attached DB; returns true on success.
79 // The created_time, last_visit_time, favicon_id and bookmark are stored.
81 // The created_time and last_visit_time are cached because Android use the
82 // millisecond for the time unit, and we don't want to convert it in the
83 // runtime for it requires to parsing the SQL.
85 // The favicon_id is also cached because it is in thumbnail database. Its
86 // default value is set to null as the type of favicon column in Android APIs
87 // is blob. To use default value null, we can support client query by
88 // 'WHERE favicon IS NULL'.
90 // Bookmark column is used to indicate whether the url is bookmarked.
91 bool CreateBookmarkCacheTable();
93 // Creates the search_terms table in attached DB; returns true on success.
94 // This table has _id, search, and date fields which match the Android's
97 // When Android Client require update the search term, the search term can't
98 // be updated as it always associated a URL. We simulate the update by
99 // deleting the old search term then inserting a new one, but the ID given
100 // to client can not be changed, so it appears to client as update. This
101 // table is used to mapping the ID given to client to the search term.
103 // The search term last visit time is stored in date as Android needs the time
105 bool CreateSearchTermsTable();
107 // Attachs to history database; returns true on success.
110 // Does the real attach. Returns true on success.
113 base::FilePath db_name_;
115 DISALLOW_COPY_AND_ASSIGN(AndroidCacheDatabase);
118 } // namespace history
120 #endif // CHROME_BROWSER_HISTORY_ANDROID_ANDROID_CACHE_DATABASE_H_