Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / history / url_database.h
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_URL_DATABASE_H_
6 #define CHROME_BROWSER_HISTORY_URL_DATABASE_H_
7
8 #include "base/basictypes.h"
9 #include "chrome/browser/history/history_types.h"
10 #include "chrome/browser/search_engines/template_url_id.h"
11 #include "components/query_parser/query_parser.h"
12 #include "sql/statement.h"
13
14 class GURL;
15
16 namespace sql {
17 class Connection;
18 }
19
20 namespace history {
21
22 class VisitDatabase;  // For friend statement.
23
24 // Encapsulates an SQL database that holds URL info.  This is a subset of the
25 // full history data.  We split this class' functionality out from the larger
26 // HistoryDatabase class to support maintaining separate databases of URLs with
27 // different capabilities (for example, in-memory, or archived).
28 //
29 // This is refcounted to support calling InvokeLater() with some of its methods
30 // (necessary to maintain ordering of DB operations).
31 class URLDatabase {
32  public:
33   // Must call CreateURLTable() and CreateURLIndexes() before using to make
34   // sure the database is initialized.
35   URLDatabase();
36
37   // This object must be destroyed on the thread where all accesses are
38   // happening to avoid thread-safety problems.
39   virtual ~URLDatabase();
40
41   // Converts a GURL to a string used in the history database. We plan to
42   // do more complex operations than just getting the spec out involving
43   // punycode, so this function should be used instead of url.spec() when
44   // interacting with the database.
45   //
46   // TODO(brettw) this should be moved out of the public section and the
47   // entire public HistoryDatabase interface should use GURL. This should
48   // also probably return a string instead since that is what the DB uses
49   // internally and we can avoid the extra conversion.
50   static std::string GURLToDatabaseURL(const GURL& url);
51
52   // URL table functions -------------------------------------------------------
53
54   // Looks up a url given an id. Fills info with the data. Returns true on
55   // success and false otherwise.
56   bool GetURLRow(URLID url_id, URLRow* info);
57
58   // Looks up all urls that were typed in manually. Fills info with the data.
59   // Returns true on success and false otherwise.
60   bool GetAllTypedUrls(URLRows* urls);
61
62   // Looks up the given URL and if it exists, fills the given pointers with the
63   // associated info and returns the ID of that URL. If the info pointer is
64   // NULL, no information about the URL will be filled in, only the ID will be
65   // returned. Returns 0 if the URL was not found.
66   URLID GetRowForURL(const GURL& url, URLRow* info);
67
68   // Given an already-existing row in the URL table, updates that URL's stats.
69   // This can not change the URL.  Returns true on success.
70   //
71   // This will NOT update the title used for full text indexing. If you are
72   // setting the title, call SetPageIndexedData with the new title.
73   bool UpdateURLRow(URLID url_id, const URLRow& info);
74
75   // Adds a line to the URL database with the given information and returns the
76   // newly generated ID for the row (the |id| in |info| is ignored). A row with
77   // the given URL must not exist. Returns 0 on error.
78   //
79   // This does NOT add a row to the full text search database. Use
80   // HistoryDatabase::SetPageIndexedData to do this.
81   URLID AddURL(const URLRow& info) {
82     return AddURLInternal(info, false);
83   }
84
85   // Either adds a new row to the URL table with the given information (with the
86   // the |id| as specified in |info|), or updates the pre-existing row with this
87   // |id| if there is one already. This is also known as an "upsert" or "merge"
88   // operation. Returns true on success.
89   bool InsertOrUpdateURLRowByID(const URLRow& info);
90
91   // Delete the row of the corresponding URL. Only the row in the URL table and
92   // corresponding keyword search terms will be deleted, not any other data that
93   // may refer to the URL row. Returns true if the row existed and was deleted.
94   bool DeleteURLRow(URLID id);
95
96   // URL mass-deleting ---------------------------------------------------------
97
98   // Begins the mass-deleting operation by creating a temporary URL table.
99   // The caller than adds the URLs it wants to preseve to the temporary table,
100   // and then deletes everything else by calling CommitTemporaryURLTable().
101   // Returns true on success.
102   bool CreateTemporaryURLTable();
103
104   // Adds a row to the temporary URL table. This must be called between
105   // CreateTemporaryURLTable() and CommitTemporaryURLTable() (see those for more
106   // info). The ID of the URL will change in the temporary table, so the new ID
107   // is returned. Returns 0 on failure.
108   URLID AddTemporaryURL(const URLRow& row) {
109     return AddURLInternal(row, true);
110   }
111
112   // Ends the mass-deleting by replacing the original URL table with the
113   // temporary one created in CreateTemporaryURLTable. Returns true on success.
114   //
115   // This function does not create the supplimentary indices. It is virtual so
116   // that the main history database can provide this additional behavior.
117   virtual bool CommitTemporaryURLTable();
118
119   // Enumeration ---------------------------------------------------------------
120
121   // A basic enumerator to enumerate urls database.
122   class URLEnumeratorBase {
123    public:
124     URLEnumeratorBase();
125     virtual ~URLEnumeratorBase();
126
127    private:
128     friend class URLDatabase;
129
130     bool initialized_;
131     sql::Statement statement_;
132
133     DISALLOW_COPY_AND_ASSIGN(URLEnumeratorBase);
134   };
135
136   // A basic enumerator to enumerate urls
137   class URLEnumerator : public URLEnumeratorBase {
138    public:
139     URLEnumerator();
140
141     // Retreives the next url. Returns false if no more urls are available
142     bool GetNextURL(history::URLRow* r);
143
144    private:
145     DISALLOW_COPY_AND_ASSIGN(URLEnumerator);
146   };
147
148   // Initializes the given enumerator to enumerator all URLs in the database.
149   bool InitURLEnumeratorForEverything(URLEnumerator* enumerator);
150
151   // Initializes the given enumerator to enumerator all URLs in the database
152   // that are historically significant: ones having been visited within 3 days,
153   // having their URL manually typed more than once, or having been visited
154   // more than 3 times.
155   bool InitURLEnumeratorForSignificant(URLEnumerator* enumerator);
156
157   // Favicons ------------------------------------------------------------------
158
159   // Autocomplete --------------------------------------------------------------
160
161   // Fills the given array with URLs matching the given prefix.  They will be
162   // sorted by typed count, then by visit count, then by visit date (most recent
163   // first) up to the given maximum number.  If |typed_only| is true, only urls
164   // that have been typed once are returned.  For caller convenience, returns
165   // whether any results were found.
166   bool AutocompleteForPrefix(const std::string& prefix,
167                              size_t max_results,
168                              bool typed_only,
169                              URLRows* results);
170
171   // Returns true if the database holds some past typed navigation to a URL on
172   // the provided hostname.
173   bool IsTypedHost(const std::string& host);
174
175   // Tries to find the shortest URL beginning with |base| that strictly
176   // prefixes |url|, and has minimum visit_ and typed_counts as specified.
177   // If found, fills in |info| and returns true; otherwise returns false,
178   // leaving |info| unchanged.
179   // We allow matches of exactly |base| iff |allow_base| is true.
180   bool FindShortestURLFromBase(const std::string& base,
181                                const std::string& url,
182                                int min_visits,
183                                int min_typed,
184                                bool allow_base,
185                                history::URLRow* info);
186
187   // History search ------------------------------------------------------------
188
189   // Performs a brute force search over the database to find any URLs or titles
190   // which match the |query| string.  Returns any matches in |results|.
191   bool GetTextMatches(const base::string16& query, URLRows* results);
192
193   // Keyword Search Terms ------------------------------------------------------
194
195   // Sets the search terms for the specified url/keyword pair.
196   bool SetKeywordSearchTermsForURL(URLID url_id,
197                                    TemplateURLID keyword_id,
198                                    const base::string16& term);
199
200   // Looks up a keyword search term given a url id. Returns all the search terms
201   // in |rows|. Returns true on success.
202   bool GetKeywordSearchTermRow(URLID url_id, KeywordSearchTermRow* row);
203
204   // Looks up all keyword search terms given a term, Fills the rows with data.
205   // Returns true on success and false otherwise.
206   bool GetKeywordSearchTermRows(const base::string16& term,
207                                 std::vector<KeywordSearchTermRow>* rows);
208
209   // Deletes all search terms for the specified keyword that have been added by
210   // way of SetKeywordSearchTermsForURL.
211   void DeleteAllSearchTermsForKeyword(TemplateURLID keyword_id);
212
213   // Returns up to max_count of the most recent search terms for the specified
214   // keyword.
215   void GetMostRecentKeywordSearchTerms(
216       TemplateURLID keyword_id,
217       const base::string16& prefix,
218       int max_count,
219       std::vector<KeywordSearchTermVisit>* matches);
220
221   // Deletes all searches matching |term|.
222   bool DeleteKeywordSearchTerm(const base::string16& term);
223
224   // Deletes any search corresponding to |url_id|.
225   bool DeleteKeywordSearchTermForURL(URLID url_id);
226
227   // Migration -----------------------------------------------------------------
228
229   // Do to a bug we were setting the favicon of about:blank. This forces
230   // about:blank to have no icon or title. Returns true on success, false if
231   // the favicon couldn't be updated.
232   bool MigrateFromVersion11ToVersion12();
233
234  protected:
235   friend class VisitDatabase;
236
237   // See HISTORY_URL_ROW_FIELDS below.
238   static const char kURLRowFields[];
239
240   // The number of fiends in kURLRowFields. If callers need additional
241   // fields, they can add their 0-based index to this value to get the index of
242   // fields following kURLRowFields.
243   static const int kNumURLRowFields;
244
245   // Drops the starred_id column from urls, returning true on success. This does
246   // nothing (and returns true) if the urls doesn't contain the starred_id
247   // column.
248   bool DropStarredIDFromURLs();
249
250   // Initialization functions. The indexing functions are separate from the
251   // table creation functions so the in-memory database and the temporary tables
252   // used when clearing history can populate the table and then create the
253   // index, which is faster than the reverse.
254   //
255   // is_temporary is false when generating the "regular" URLs table. The expirer
256   // sets this to true to generate the  temporary table, which will have a
257   // different name but the same schema.
258   bool CreateURLTable(bool is_temporary);
259   // We have two tiers of indices for the URL table. The main tier is used by
260   // all URL databases, and is an index over the URL itself.
261   bool CreateMainURLIndex();
262
263   // Ensures the keyword search terms table exists.
264   bool InitKeywordSearchTermsTable();
265
266   // Creates the indices used for keyword search terms.
267   bool CreateKeywordSearchTermsIndices();
268
269   // Deletes the keyword search terms table.
270   bool DropKeywordSearchTermsTable();
271
272   // Inserts the given URL row into the URLs table, using the regular table
273   // if is_temporary is false, or the temporary URL table if is temporary is
274   // true. The current |id| of |info| will be ignored in both cases and a new ID
275   // will be generated, which will also constitute the return value, except in
276   // case of an error, when the return value is 0. The temporary table may only
277   // be used in between CreateTemporaryURLTable() and CommitTemporaryURLTable().
278   URLID AddURLInternal(const URLRow& info, bool is_temporary);
279
280   // Convenience to fill a history::URLRow. Must be in sync with the fields in
281   // kHistoryURLRowFields.
282   static void FillURLRow(sql::Statement& s, URLRow* i);
283
284   // Returns the database for the functions in this interface. The decendent of
285   // this class implements these functions to return its objects.
286   virtual sql::Connection& GetDB() = 0;
287
288  private:
289   // True if InitKeywordSearchTermsTable() has been invoked. Not all subclasses
290   // have keyword search terms.
291   bool has_keyword_search_terms_;
292
293   query_parser::QueryParser query_parser_;
294
295   DISALLOW_COPY_AND_ASSIGN(URLDatabase);
296 };
297
298 // The fields and order expected by FillURLRow(). ID is guaranteed to be first
299 // so that DISTINCT can be prepended to get distinct URLs.
300 //
301 // This is available BOTH as a macro and a static string (kURLRowFields). Use
302 // the macro if you want to put this in the middle of an otherwise constant
303 // string, it will save time doing string appends. If you have to build a SQL
304 // string dynamically anyway, use the constant, it will save space.
305 #define HISTORY_URL_ROW_FIELDS \
306     " urls.id, urls.url, urls.title, urls.visit_count, urls.typed_count, " \
307     "urls.last_visit_time, urls.hidden "
308
309 }  // namespace history
310
311 #endif  // CHROME_BROWSER_HISTORY_URL_DATABASE_H_