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_VISITSEGMENT_DATABASE_H_
6#define CHROME_BROWSER_HISTORY_VISITSEGMENT_DATABASE_H_
7
8#include "base/basictypes.h"
9#include "components/history/core/browser/history_types.h"
10
11class PageUsageData;
12
13namespace sql {
14class Connection;
15}
16
17namespace history {
18
19// Tracks pages used for the most visited view.
20class VisitSegmentDatabase {
21 public:
22  // Must call InitSegmentTables before using any other part of this class.
23  VisitSegmentDatabase();
24  virtual ~VisitSegmentDatabase();
25
26  // Compute a segment name given a URL. The segment name is currently the
27  // source url spec less some information such as query strings.
28  static std::string ComputeSegmentName(const GURL& url);
29
30  // Returns the ID of the segment with the corresponding name, or 0 if there
31  // is no segment with that name.
32  SegmentID GetSegmentNamed(const std::string& segment_name);
33
34  // Update the segment identified by |out_segment_id| with the provided URL ID.
35  // The URL identifies the page that will now represent the segment. If url_id
36  // is non zero, it is assumed to be the row id of |url|.
37  bool UpdateSegmentRepresentationURL(SegmentID segment_id,
38                                      URLID url_id);
39
40  // Return the ID of the URL currently used to represent this segment or 0 if
41  // an error occured.
42  URLID GetSegmentRepresentationURL(SegmentID segment_id);
43
44  // Create a segment for the provided URL ID with the given name. Returns the
45  // ID of the newly created segment, or 0 on failure.
46  SegmentID CreateSegment(URLID url_id, const std::string& segment_name);
47
48  // Increase the segment visit count by the provided amount. Return true on
49  // success.
50  bool IncreaseSegmentVisitCount(SegmentID segment_id, base::Time ts,
51                                 int amount);
52
53  // Compute the segment usage since |from_time| using the provided aggregator.
54  // A PageUsageData is added in |result| for the highest-scored segments up to
55  // |max_result_count|.
56  void QuerySegmentUsage(base::Time from_time,
57                         int max_result_count,
58                         std::vector<PageUsageData*>* result);
59
60  // Delete all the segment usage data which is older than the provided time
61  // stamp.
62  bool DeleteSegmentData(base::Time older_than);
63
64  // Change the presentation id for the segment identified by |segment_id|
65  bool SetSegmentPresentationIndex(SegmentID segment_id, int index);
66
67  // Delete the segment currently using the provided url for representation.
68  // This will also delete any associated segment usage data.
69  bool DeleteSegmentForURL(URLID url_id);
70
71 protected:
72  // Returns the database for the functions in this interface.
73  virtual sql::Connection& GetDB() = 0;
74
75  // Creates the tables used by this class if necessary. Returns true on
76  // success.
77  bool InitSegmentTables();
78
79  // Deletes all the segment tables, returning true on success.
80  bool DropSegmentTables();
81
82  // Removes the 'pres_index' column from the segments table and the
83  // presentation table is removed entirely.
84  bool MigratePresentationIndex();
85
86 private:
87  DISALLOW_COPY_AND_ASSIGN(VisitSegmentDatabase);
88};
89
90}  // namespace history
91
92#endif  // CHROME_BROWSER_HISTORY_VISITSEGMENT_DATABASE_H_
93