[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_HISTORY_VISIT_DATABASE_H_ |
| 6 | #define CHROME_BROWSER_HISTORY_VISIT_DATABASE_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame^] | 7 | #pragma once |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
| 9 | #include "chrome/browser/history/history_types.h" |
| 10 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 11 | namespace sql { |
| 12 | class Connection; |
| 13 | class Statement; |
| 14 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | |
| 16 | namespace history { |
| 17 | |
| 18 | // A visit database is one which stores visits for URLs, that is, times and |
| 19 | // linking information. A visit database must also be a URLDatabase, as this |
| 20 | // modifies tables used by URLs directly and could be thought of as inheriting |
| 21 | // from URLDatabase. However, this inheritance is not explicit as things would |
| 22 | // get too complicated and have multiple inheritance. |
| 23 | class VisitDatabase { |
| 24 | public: |
| 25 | // Must call InitVisitTable() before using to make sure the database is |
| 26 | // initialized. |
| 27 | VisitDatabase(); |
| 28 | virtual ~VisitDatabase(); |
| 29 | |
| 30 | // Deletes the visit table. Used for rapidly clearing all visits. In this |
| 31 | // case, InitVisitTable would be called immediately afterward to re-create it. |
| 32 | // Returns true on success. |
| 33 | bool DropVisitTable(); |
| 34 | |
| 35 | // Adds a line to the visit database with the given information, returning |
| 36 | // the added row ID on success, 0 on failure. The given visit is updated with |
| 37 | // the new row ID on success. |
| 38 | VisitID AddVisit(VisitRow* visit); |
| 39 | |
| 40 | // Deletes the given visit from the database. If a visit with the given ID |
| 41 | // doesn't exist, it will not do anything. |
| 42 | void DeleteVisit(const VisitRow& visit); |
| 43 | |
| 44 | // Query a VisitInfo giving an visit id, filling the given VisitRow. |
| 45 | // Returns true on success. |
| 46 | bool GetRowForVisit(VisitID visit_id, VisitRow* out_visit); |
| 47 | |
| 48 | // Updates an existing row. The new information is set on the row, using the |
| 49 | // VisitID as the key. The visit must exist. Returns true on success. |
| 50 | bool UpdateVisitRow(const VisitRow& visit); |
| 51 | |
| 52 | // Fills in the given vector with all of the visits for the given page ID, |
| 53 | // sorted in ascending order of date. Returns true on success (although there |
| 54 | // may still be no matches). |
| 55 | bool GetVisitsForURL(URLID url_id, VisitVector* visits); |
| 56 | |
| 57 | // Fills all visits in the time range [begin, end) to the given vector. Either |
| 58 | // time can be is_null(), in which case the times in that direction are |
| 59 | // unbounded. |
| 60 | // |
| 61 | // If |max_results| is non-zero, up to that many results will be returned. If |
| 62 | // there are more results than that, the oldest ones will be returned. (This |
| 63 | // is used for history expiration.) |
| 64 | // |
| 65 | // The results will be in increasing order of date. |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 66 | void GetAllVisitsInRange(base::Time begin_time, base::Time end_time, |
| 67 | int max_results, VisitVector* visits); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 69 | // Fills all visits with specified transition in the time range [begin, end) |
| 70 | // to the given vector. Either time can be is_null(), in which case the times |
| 71 | // in that direction are unbounded. |
| 72 | // |
| 73 | // If |max_results| is non-zero, up to that many results will be returned. If |
| 74 | // there are more results than that, the oldest ones will be returned. (This |
| 75 | // is used for history expiration.) |
| 76 | // |
| 77 | // The results will be in increasing order of date. |
| 78 | void GetVisitsInRangeForTransition(base::Time begin_time, |
| 79 | base::Time end_time, |
| 80 | int max_results, |
| 81 | PageTransition::Type transition, |
| 82 | VisitVector* visits); |
| 83 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | // Fills all visits in the given time range into the given vector that should |
| 85 | // be user-visible, which excludes things like redirects and subframes. The |
| 86 | // begin time is inclusive, the end time is exclusive. Either time can be |
| 87 | // is_null(), in which case the times in that direction are unbounded. |
| 88 | // |
| 89 | // Up to |max_count| visits will be returned. If there are more visits than |
| 90 | // that, the most recent |max_count| will be returned. If 0, all visits in the |
| 91 | // range will be computed. |
| 92 | // |
[email protected] | 755bbc43 | 2009-11-03 23:12:17 | [diff] [blame] | 93 | // Only one visit for each URL will be returned, and it will be the most |
| 94 | // recent one in the time range. |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 95 | void GetVisibleVisitsInRange(base::Time begin_time, base::Time end_time, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | int max_count, |
| 97 | VisitVector* visits); |
| 98 | |
| 99 | // Returns the visit ID for the most recent visit of the given URL ID, or 0 |
| 100 | // if there is no visit for the URL. |
| 101 | // |
| 102 | // If non-NULL, the given visit row will be filled with the information of |
| 103 | // the found visit. When no visit is found, the row will be unchanged. |
| 104 | VisitID GetMostRecentVisitForURL(URLID url_id, |
| 105 | VisitRow* visit_row); |
| 106 | |
| 107 | // Returns the |max_results| most recent visit sessions for |url_id|. |
| 108 | // |
| 109 | // Returns false if there's a failure preparing the statement. True |
| 110 | // otherwise. (No results are indicated with an empty |visits| |
| 111 | // vector.) |
| 112 | bool GetMostRecentVisitsForURL(URLID url_id, |
| 113 | int max_results, |
| 114 | VisitVector* visits); |
| 115 | |
| 116 | // Finds a redirect coming from the given |from_visit|. If a redirect is |
| 117 | // found, it fills the visit ID and URL into the out variables and returns |
| 118 | // true. If there is no redirect from the given visit, returns false. |
| 119 | // |
| 120 | // If there is more than one redirect, this will compute a random one. But |
| 121 | // duplicates should be very rare, and we don't actually care which one we |
| 122 | // get in most cases. These will occur when the user goes back and gets |
| 123 | // redirected again. |
| 124 | // |
| 125 | // to_visit and to_url can be NULL in which case they are ignored. |
| 126 | bool GetRedirectFromVisit(VisitID from_visit, |
| 127 | VisitID* to_visit, |
| 128 | GURL* to_url); |
| 129 | |
[email protected] | 0f8c112 | 2009-06-30 22:13:13 | [diff] [blame] | 130 | // Similar to the above function except finds a redirect going to a given |
| 131 | // |to_visit|. |
| 132 | bool GetRedirectToVisit(VisitID to_visit, |
| 133 | VisitID* from_visit, |
| 134 | GURL* from_url); |
| 135 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | // Returns the number of visits to all urls on the scheme/host/post |
| 137 | // identified by url. This is only valid for http and https urls (all other |
| 138 | // schemes are ignored and false is returned). |
| 139 | // count is set to the number of visits, first_visit is set to the first time |
| 140 | // the host was visited. Returns true on success. |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 141 | bool GetVisitCountToHost(const GURL& url, int* count, |
| 142 | base::Time* first_visit); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | |
[email protected] | 153c698 | 2009-03-11 01:24:08 | [diff] [blame] | 144 | // Get the time of the first item in our database. |
| 145 | bool GetStartDate(base::Time* first_visit); |
| 146 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | protected: |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 148 | // Returns the database for the functions in this interface. |
| 149 | virtual sql::Connection& GetDB() = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | |
| 151 | // Called by the derived classes on initialization to make sure the tables |
| 152 | // and indices are properly set up. Must be called before anything else. |
| 153 | bool InitVisitTable(); |
| 154 | |
| 155 | // Convenience to fill a VisitRow. Assumes the visit values are bound starting |
| 156 | // at index 0. |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 157 | static void FillVisitRow(sql::Statement& statement, VisitRow* visit); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | |
| 159 | // Convenience to fill a VisitVector. Assumes that statement.step() |
| 160 | // hasn't happened yet. |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 161 | static void FillVisitVector(sql::Statement& statement, VisitVector* visits); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | |
| 163 | private: |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 164 | DISALLOW_COPY_AND_ASSIGN(VisitDatabase); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | } // history |
| 168 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 169 | #endif // CHROME_BROWSER_HISTORY_VISIT_DATABASE_H_ |