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