Home | History | Annotate | Download | only in history
      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 "chrome/browser/history/history_types.h"
     10 
     11 class PageUsageData;
     12 
     13 namespace sql {
     14 class Connection;
     15 class Statement;
     16 }
     17 
     18 namespace history {
     19 
     20 // Tracks pages used for the most visited view.
     21 class VisitSegmentDatabase {
     22  public:
     23   // Must call InitSegmentTables before using any other part of this class.
     24   VisitSegmentDatabase();
     25   virtual ~VisitSegmentDatabase();
     26 
     27   // Compute a segment name given a URL. The segment name is currently the
     28   // source url spec less some information such as query strings.
     29   static std::string ComputeSegmentName(const GURL& url);
     30 
     31   // The segment tables use the time as a key for visit count and duration. This
     32   // returns the appropriate time.
     33   static base::Time SegmentTime(base::Time time);
     34 
     35   // Returns the ID of the segment with the corresponding name, or 0 if there
     36   // is no segment with that name.
     37   SegmentID GetSegmentNamed(const std::string& segment_name);
     38 
     39   // Update the segment identified by |out_segment_id| with the provided URL ID.
     40   // The URL identifies the page that will now represent the segment. If url_id
     41   // is non zero, it is assumed to be the row id of |url|.
     42   bool UpdateSegmentRepresentationURL(SegmentID segment_id,
     43                                       URLID url_id);
     44 
     45   // Return the ID of the URL currently used to represent this segment or 0 if
     46   // an error occured.
     47   URLID GetSegmentRepresentationURL(SegmentID segment_id);
     48 
     49   // Create a segment for the provided URL ID with the given name. Returns the
     50   // ID of the newly created segment, or 0 on failure.
     51   SegmentID CreateSegment(URLID url_id, const std::string& segment_name);
     52 
     53   // Increase the segment visit count by the provided amount. Return true on
     54   // success.
     55   bool IncreaseSegmentVisitCount(SegmentID segment_id, base::Time ts,
     56                                  int amount);
     57 
     58   // Compute the segment usage since |from_time| using the provided aggregator.
     59   // A PageUsageData is added in |result| for the highest-scored segments up to
     60   // |max_result_count|.
     61   void QuerySegmentUsage(base::Time from_time,
     62                          int max_result_count,
     63                          std::vector<PageUsageData*>* result);
     64 
     65   // Delete all the segment usage data which is older than the provided time
     66   // stamp.
     67   bool DeleteSegmentData(base::Time older_than);
     68 
     69   // Change the presentation id for the segment identified by |segment_id|
     70   bool SetSegmentPresentationIndex(SegmentID segment_id, int index);
     71 
     72   // Delete the segment currently using the provided url for representation.
     73   // This will also delete any associated segment usage data.
     74   bool DeleteSegmentForURL(URLID url_id);
     75 
     76   // Creates a new SegmentDurationID for the SegmentID and Time pair. The
     77   // duration is set to |delta|.
     78   SegmentDurationID CreateSegmentDuration(SegmentID segment_id,
     79                                           base::Time time,
     80                                           base::TimeDelta delta);
     81 
     82   // Sets the duration of the |duration_id| to |time_delta|.
     83   bool SetSegmentDuration(SegmentDurationID duration_id,
     84                           base::TimeDelta time_delta);
     85 
     86   // Gets the SegmentDurationID of the |segment_id| and |time| pair. Returns
     87   // true on success and sets |duration_id| and |time_delta| appropriately.
     88   bool GetSegmentDuration(SegmentID segment_id,
     89                           base::Time time,
     90                           SegmentDurationID* duration_id,
     91                           base::TimeDelta* time_delta);
     92 
     93   // Queries segments by duration.
     94   void QuerySegmentDuration(base::Time from_time,
     95                             int max_result_count,
     96                             std::vector<PageUsageData*>* result);
     97 
     98  protected:
     99   // Returns the database for the functions in this interface.
    100   virtual sql::Connection& GetDB() = 0;
    101 
    102   // Creates the tables used by this class if necessary. Returns true on
    103   // success.
    104   bool InitSegmentTables();
    105 
    106   // Deletes all the segment tables, returning true on success.
    107   bool DropSegmentTables();
    108 
    109   // Removes the 'pres_index' column from the segments table and the
    110   // presentation table is removed entirely.
    111   bool MigratePresentationIndex();
    112 
    113  private:
    114   enum QueryType {
    115     QUERY_VISIT_COUNT,
    116     QUERY_DURATION,
    117   };
    118 
    119   // Used by both QuerySegment fucntions.
    120   void QuerySegmentsCommon(sql::Statement* statement,
    121                            base::Time from_time,
    122                            int max_result_count,
    123                            QueryType query_type,
    124                            std::vector<PageUsageData*>* result);
    125 
    126   // Was the |segment_duration| table created?
    127   const bool has_duration_table_;
    128 
    129   DISALLOW_COPY_AND_ASSIGN(VisitSegmentDatabase);
    130 };
    131 
    132 }  // namespace history
    133 
    134 #endif  // CHROME_BROWSER_HISTORY_VISITSEGMENT_DATABASE_H_
    135