• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CHROME_BROWSER_HISTORY_ANDROID_ANDROID_CACHE_DATABASE_H_
6 #define CHROME_BROWSER_HISTORY_ANDROID_ANDROID_CACHE_DATABASE_H_
7 
8 #include "base/files/file_path.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/time/time.h"
11 #include "chrome/browser/history/android/android_history_types.h"
12 #include "sql/connection.h"
13 #include "sql/init_status.h"
14 
15 namespace history {
16 
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 {
21  public:
22   AndroidCacheDatabase();
23   virtual ~AndroidCacheDatabase();
24 
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);
29 
30   // The bookmark_cache table ------------------------------------------------
31   //
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,
35                            URLID url_id);
36 
37   // Clears all rows in the bookmark_cache table; returns true on success.
38   bool ClearAllBookmarkCache();
39 
40   // Marks the given |url_ids| as bookmarked; Returns true on success.
41   bool MarkURLsAsBookmarked(const std::vector<URLID>& url_id);
42 
43   // Set the given |url_id|'s favicon column to |favicon_id|. Returns true on
44   // success.
45   bool SetFaviconID(URLID url_id, favicon_base::FaviconID favicon_id);
46 
47   // The search_terms table -------------------------------------------------
48   //
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
51   // returned.
52   SearchTermID AddSearchTerm(const base::string16& term,
53                              const base::Time& last_visit_time);
54 
55   // Updates the |id|'s row with the given |row|; returns true on success.
56   bool UpdateSearchTerm(SearchTermID id, const SearchTermRow& row);
57 
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);
62 
63   // Delete the search terms which don't exist in keyword_search_terms table.
64   bool DeleteUnusedSearchTerms();
65 
66  protected:
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;
70 
71  private:
72   FRIEND_TEST_ALL_PREFIXES(AndroidCacheDatabaseTest, InitAndroidCacheDatabase);
73 
74   // Creates the database and make it ready for attaching; returns true on
75   // success.
76   bool CreateDatabase(const base::FilePath& db_name);
77 
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.
80   //
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.
84   //
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'.
89   //
90   // Bookmark column is used to indicate whether the url is bookmarked.
91   bool CreateBookmarkCacheTable();
92 
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
95   // definition.
96   //
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.
102   //
103   // The search term last visit time is stored in date as Android needs the time
104   // in milliseconds.
105   bool CreateSearchTermsTable();
106 
107   // Attachs to history database; returns true on success.
108   bool Attach();
109 
110   // Does the real attach. Returns true on success.
111   bool DoAttach();
112 
113   base::FilePath db_name_;
114 
115   DISALLOW_COPY_AND_ASSIGN(AndroidCacheDatabase);
116 };
117 
118 }  // namespace history
119 
120 #endif  // CHROME_BROWSER_HISTORY_ANDROID_ANDROID_CACHE_DATABASE_H_
121