1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef COMPONENTS_HISTORY_CORE_BROWSER_URL_DATABASE_H_
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define COMPONENTS_HISTORY_CORE_BROWSER_URL_DATABASE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/history/core/browser/keyword_id.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/history/core/browser/url_row.h"
110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/query_parser/query_parser.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sql/statement.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class GURL;
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace sql {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Connection;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace history {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstruct KeywordSearchTermRow;
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstruct KeywordSearchTermVisit;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class VisitDatabase;  // For friend statement.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Encapsulates an SQL database that holds URL info.  This is a subset of the
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// full history data.  We split this class' functionality out from the larger
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// HistoryDatabase class to support maintaining separate databases of URLs with
2946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// different capabilities (for example, the in-memory database).
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is refcounted to support calling InvokeLater() with some of its methods
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (necessary to maintain ordering of DB operations).
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class URLDatabase {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Must call CreateURLTable() and CreateURLIndexes() before using to make
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sure the database is initialized.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLDatabase();
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This object must be destroyed on the thread where all accesses are
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // happening to avoid thread-safety problems.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~URLDatabase();
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts a GURL to a string used in the history database. We plan to
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // do more complex operations than just getting the spec out involving
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // punycode, so this function should be used instead of url.spec() when
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // interacting with the database.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(brettw) this should be moved out of the public section and the
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // entire public HistoryDatabase interface should use GURL. This should
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // also probably return a string instead since that is what the DB uses
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // internally and we can avoid the extra conversion.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static std::string GURLToDatabaseURL(const GURL& url);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URL table functions -------------------------------------------------------
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Looks up a url given an id. Fills info with the data. Returns true on
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // success and false otherwise.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetURLRow(URLID url_id, URLRow* info);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Looks up all urls that were typed in manually. Fills info with the data.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true on success and false otherwise.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetAllTypedUrls(URLRows* urls);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Looks up the given URL and if it exists, fills the given pointers with the
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // associated info and returns the ID of that URL. If the info pointer is
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NULL, no information about the URL will be filled in, only the ID will be
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returned. Returns 0 if the URL was not found.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLID GetRowForURL(const GURL& url, URLRow* info);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Given an already-existing row in the URL table, updates that URL's stats.
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This can not change the URL.  Returns true on success.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This will NOT update the title used for full text indexing. If you are
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // setting the title, call SetPageIndexedData with the new title.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateURLRow(URLID url_id, const URLRow& info);
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds a line to the URL database with the given information and returns the
78c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // newly generated ID for the row (the |id| in |info| is ignored). A row with
79c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // the given URL must not exist. Returns 0 on error.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This does NOT add a row to the full text search database. Use
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // HistoryDatabase::SetPageIndexedData to do this.
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLID AddURL(const URLRow& info) {
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return AddURLInternal(info, false);
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
87c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Either adds a new row to the URL table with the given information (with the
88c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // the |id| as specified in |info|), or updates the pre-existing row with this
89c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // |id| if there is one already. This is also known as an "upsert" or "merge"
90c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // operation. Returns true on success.
91c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool InsertOrUpdateURLRowByID(const URLRow& info);
92c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
93c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Delete the row of the corresponding URL. Only the row in the URL table and
94c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // corresponding keyword search terms will be deleted, not any other data that
95c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // may refer to the URL row. Returns true if the row existed and was deleted.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DeleteURLRow(URLID id);
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URL mass-deleting ---------------------------------------------------------
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Begins the mass-deleting operation by creating a temporary URL table.
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The caller than adds the URLs it wants to preseve to the temporary table,
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and then deletes everything else by calling CommitTemporaryURLTable().
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true on success.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CreateTemporaryURLTable();
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds a row to the temporary URL table. This must be called between
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // CreateTemporaryURLTable() and CommitTemporaryURLTable() (see those for more
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // info). The ID of the URL will change in the temporary table, so the new ID
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is returned. Returns 0 on failure.
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLID AddTemporaryURL(const URLRow& row) {
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return AddURLInternal(row, true);
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Ends the mass-deleting by replacing the original URL table with the
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // temporary one created in CreateTemporaryURLTable. Returns true on success.
11646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool CommitTemporaryURLTable();
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Enumeration ---------------------------------------------------------------
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A basic enumerator to enumerate urls database.
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class URLEnumeratorBase {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLEnumeratorBase();
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~URLEnumeratorBase();
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    friend class URLDatabase;
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool initialized_;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    sql::Statement statement_;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(URLEnumeratorBase);
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A basic enumerator to enumerate urls
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class URLEnumerator : public URLEnumeratorBase {
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLEnumerator();
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Retreives the next url. Returns false if no more urls are available
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool GetNextURL(history::URLRow* r);
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(URLEnumerator);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes the given enumerator to enumerator all URLs in the database.
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool InitURLEnumeratorForEverything(URLEnumerator* enumerator);
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes the given enumerator to enumerator all URLs in the database
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that are historically significant: ones having been visited within 3 days,
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // having their URL manually typed more than once, or having been visited
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // more than 3 times.
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool InitURLEnumeratorForSignificant(URLEnumerator* enumerator);
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Favicons ------------------------------------------------------------------
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Autocomplete --------------------------------------------------------------
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Fills the given array with URLs matching the given prefix.  They will be
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sorted by typed count, then by visit count, then by visit date (most recent
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // first) up to the given maximum number.  If |typed_only| is true, only urls
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that have been typed once are returned.  For caller convenience, returns
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // whether any results were found.
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AutocompleteForPrefix(const std::string& prefix,
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             size_t max_results,
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             bool typed_only,
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             URLRows* results);
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the database holds some past typed navigation to a URL on
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the provided hostname.
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsTypedHost(const std::string& host);
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tries to find the shortest URL beginning with |base| that strictly
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // prefixes |url|, and has minimum visit_ and typed_counts as specified.
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If found, fills in |info| and returns true; otherwise returns false,
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // leaving |info| unchanged.
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We allow matches of exactly |base| iff |allow_base| is true.
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool FindShortestURLFromBase(const std::string& base,
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const std::string& url,
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               int min_visits,
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               int min_typed,
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               bool allow_base,
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               history::URLRow* info);
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
186eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // History search ------------------------------------------------------------
187eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
188eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Performs a brute force search over the database to find any URLs or titles
189eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // which match the |query| string.  Returns any matches in |results|.
190a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  bool GetTextMatches(const base::string16& query, URLRows* results);
191eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Keyword Search Terms ------------------------------------------------------
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the search terms for the specified url/keyword pair.
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SetKeywordSearchTermsForURL(URLID url_id,
196116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                   KeywordID keyword_id,
197a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                   const base::string16& term);
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Looks up a keyword search term given a url id. Returns all the search terms
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in |rows|. Returns true on success.
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetKeywordSearchTermRow(URLID url_id, KeywordSearchTermRow* row);
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Looks up all keyword search terms given a term, Fills the rows with data.
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true on success and false otherwise.
205a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  bool GetKeywordSearchTermRows(const base::string16& term,
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                std::vector<KeywordSearchTermRow>* rows);
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes all search terms for the specified keyword that have been added by
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // way of SetKeywordSearchTermsForURL.
210116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void DeleteAllSearchTermsForKeyword(KeywordID keyword_id);
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns up to max_count of the most recent search terms for the specified
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // keyword.
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void GetMostRecentKeywordSearchTerms(
215116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      KeywordID keyword_id,
216a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const base::string16& prefix,
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int max_count,
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::vector<KeywordSearchTermVisit>* matches);
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes all searches matching |term|.
221a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  bool DeleteKeywordSearchTerm(const base::string16& term);
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Deletes any search corresponding to |url_id|.
2240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool DeleteKeywordSearchTermForURL(URLID url_id);
2250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Migration -----------------------------------------------------------------
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Do to a bug we were setting the favicon of about:blank. This forces
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // about:blank to have no icon or title. Returns true on success, false if
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the favicon couldn't be updated.
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool MigrateFromVersion11ToVersion12();
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class VisitDatabase;
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See HISTORY_URL_ROW_FIELDS below.
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const char kURLRowFields[];
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The number of fiends in kURLRowFields. If callers need additional
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // fields, they can add their 0-based index to this value to get the index of
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // fields following kURLRowFields.
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int kNumURLRowFields;
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Drops the starred_id column from urls, returning true on success. This does
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // nothing (and returns true) if the urls doesn't contain the starred_id
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // column.
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DropStarredIDFromURLs();
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initialization functions. The indexing functions are separate from the
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // table creation functions so the in-memory database and the temporary tables
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // used when clearing history can populate the table and then create the
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // index, which is faster than the reverse.
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is_temporary is false when generating the "regular" URLs table. The expirer
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sets this to true to generate the  temporary table, which will have a
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // different name but the same schema.
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CreateURLTable(bool is_temporary);
25846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
25946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Creates the index over URLs so we can quickly look up based on URL.
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CreateMainURLIndex();
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Ensures the keyword search terms table exists.
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool InitKeywordSearchTermsTable();
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates the indices used for keyword search terms.
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CreateKeywordSearchTermsIndices();
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the keyword search terms table.
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DropKeywordSearchTermsTable();
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Inserts the given URL row into the URLs table, using the regular table
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if is_temporary is false, or the temporary URL table if is temporary is
273c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // true. The current |id| of |info| will be ignored in both cases and a new ID
274c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // will be generated, which will also constitute the return value, except in
275c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // case of an error, when the return value is 0. The temporary table may only
276c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // be used in between CreateTemporaryURLTable() and CommitTemporaryURLTable().
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLID AddURLInternal(const URLRow& info, bool is_temporary);
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Convenience to fill a history::URLRow. Must be in sync with the fields in
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // kHistoryURLRowFields.
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void FillURLRow(sql::Statement& s, URLRow* i);
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the database for the functions in this interface. The decendent of
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this class implements these functions to return its objects.
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual sql::Connection& GetDB() = 0;
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if InitKeywordSearchTermsTable() has been invoked. Not all subclasses
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // have keyword search terms.
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool has_keyword_search_terms_;
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  query_parser::QueryParser query_parser_;
293eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(URLDatabase);
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The fields and order expected by FillURLRow(). ID is guaranteed to be first
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// so that DISTINCT can be prepended to get distinct URLs.
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is available BOTH as a macro and a static string (kURLRowFields). Use
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the macro if you want to put this in the middle of an otherwise constant
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// string, it will save time doing string appends. If you have to build a SQL
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// string dynamically anyway, use the constant, it will save space.
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define HISTORY_URL_ROW_FIELDS \
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    " urls.id, urls.url, urls.title, urls.visit_count, urls.typed_count, " \
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "urls.last_visit_time, urls.hidden "
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
308116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Constants which specify, when considered altogether, 'significant'
309116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// history items. These are used to filter out insignificant items
310116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// for consideration as autocomplete candidates.
311116680a4aac90f2aa7413d9095a592090648e557Ben Murdochextern const int kLowQualityMatchTypedLimit;
312116680a4aac90f2aa7413d9095a592090648e557Ben Murdochextern const int kLowQualityMatchVisitLimit;
313116680a4aac90f2aa7413d9095a592090648e557Ben Murdochextern const int kLowQualityMatchAgeLimitInDays;
314116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
315116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Returns the date threshold for considering an history item as significant.
316116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbase::Time AutocompleteAgeThreshold();
317116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
318116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Return true if |row| qualifies as an autocomplete candidate. If |time_cache|
319116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// is_null() then this function determines a new time threshold each time it is
320116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// called. Since getting system time can be costly (such as for cases where
321116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// this function will be called in a loop over many history items), you can
322116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// provide a non-null |time_cache| by simply initializing |time_cache| with
323116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// AutocompleteAgeThreshold() (or any other desired time in the past).
324116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool RowQualifiesAsSignificant(const URLRow& row, const base::Time& threshold);
325116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
326eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}  // namespace history
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
328116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif  // COMPONENTS_HISTORY_CORE_BROWSER_URL_DATABASE_H_
329