1 // Copyright 2014 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.
5 #ifndef COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_TYPES_H_
6 #define COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_TYPES_H_
14 #include "base/basictypes.h"
15 #include "base/containers/stack_container.h"
16 #include "base/memory/ref_counted_memory.h"
17 #include "base/memory/scoped_vector.h"
18 #include "base/strings/string16.h"
19 #include "base/time/time.h"
20 #include "components/favicon_base/favicon_types.h"
21 #include "components/history/core/browser/url_row.h"
22 #include "components/history/core/common/thumbnail_score.h"
23 #include "ui/base/page_transition_types.h"
24 #include "ui/gfx/size.h"
29 // TODO(sdefresne): remove, http://crbug.com/371816
36 // Forward declaration for friend statements.
40 // Container for a list of URLs.
41 typedef std::vector<GURL> RedirectList;
43 typedef int64 FaviconBitmapID; // Identifier for a bitmap in a favicon.
44 typedef int64 SegmentID; // URL segments for the most visited view.
45 typedef int64 IconMappingID; // For page url and icon mapping.
47 // Identifier for a context to scope page ids. (ContextIDs are used in
48 // comparisons only and are never dereferenced.)
49 // NB: The use of WebContents here is temporary; when the dependency on content
50 // is broken, some other type will take its place.
51 typedef content::WebContents* ContextID;
53 // The enumeration of all possible sources of visits is listed below.
54 // The source will be propagated along with a URL or a visit item
55 // and eventually be stored in the history database,
56 // visit_source table specifically.
57 // Different from page transition types, they describe the origins of visits.
58 // (Warning): Please don't change any existing values while it is ok to add
59 // new values when needed.
61 SOURCE_SYNCED = 0, // Synchronized from somewhere else.
62 SOURCE_BROWSED = 1, // User browsed.
63 SOURCE_EXTENSION = 2, // Added by an extension.
64 SOURCE_FIREFOX_IMPORTED = 3,
65 SOURCE_IE_IMPORTED = 4,
66 SOURCE_SAFARI_IMPORTED = 5,
69 typedef int64 VisitID;
70 // Structure to hold the mapping between each visit's id and its source.
71 typedef std::map<VisitID, VisitSource> VisitSourceMap;
73 // VisitRow -------------------------------------------------------------------
75 // Holds all information associated with a specific visit. A visit holds time
76 // and referrer information for one time a URL is visited.
80 VisitRow(URLID arg_url_id,
81 base::Time arg_visit_time,
82 VisitID arg_referring_visit,
83 ui::PageTransition arg_transition,
84 SegmentID arg_segment_id);
87 // ID of this row (visit ID, used a a referrer for other visits).
90 // Row ID into the URL table of the URL that this page is.
93 base::Time visit_time;
95 // Indicates another visit that was the referring page for this one.
96 // 0 indicates no referrer.
97 VisitID referring_visit;
99 // A combination of bits from PageTransition.
100 ui::PageTransition transition;
102 // The segment id (see visitsegment_database.*).
103 // If 0, the segment id is null in the table.
104 SegmentID segment_id;
106 // Record how much time a user has this visit starting from the user
107 // opened this visit to the user closed or ended this visit.
108 // This includes both active and inactive time as long as
109 // the visit was present.
110 base::TimeDelta visit_duration;
112 // Compares two visits based on dates, for sorting.
113 bool operator<(const VisitRow& other) {
114 return visit_time < other.visit_time;
117 // We allow the implicit copy constuctor and operator=.
120 // We pass around vectors of visits a lot
121 typedef std::vector<VisitRow> VisitVector;
123 // The basic information associated with a visit (timestamp, type of visit),
124 // used by HistoryBackend::AddVisits() to create new visits for a URL.
125 typedef std::pair<base::Time, ui::PageTransition> VisitInfo;
127 // PageVisit ------------------------------------------------------------------
129 // Represents a simplified version of a visit for external users. Normally,
130 // views are only interested in the time, and not the other information
131 // associated with a VisitRow.
134 base::Time visit_time;
137 // QueryResults ----------------------------------------------------------------
139 // Encapsulates the results of a history query. It supports an ordered list of
140 // URLResult objects, plus an efficient way of looking up the index of each time
141 // a given URL appears in those results.
144 typedef std::vector<URLResult*> URLResultVector;
149 // Indicates the first time that the query includes results for (queries are
150 // clipped at the beginning, so it will always include to the end of the time
153 // If the number of results was clipped as a result of the max count, this
154 // will be the time of the first query returned. If there were fewer results
155 // than we were allowed to return, this represents the first date considered
156 // in the query (this will be before the first result if there was time
157 // queried with no results).
159 // TODO(brettw): bug 1203054: This field is not currently set properly! Do
160 // not use until the bug is fixed.
161 base::Time first_time_searched() const { return first_time_searched_; }
162 void set_first_time_searched(base::Time t) { first_time_searched_ = t; }
163 // Note: If you need end_time_searched, it can be added.
165 void set_reached_beginning(bool reached) { reached_beginning_ = reached; }
166 bool reached_beginning() { return reached_beginning_; }
168 size_t size() const { return results_.size(); }
169 bool empty() const { return results_.empty(); }
171 URLResult& back() { return *results_.back(); }
172 const URLResult& back() const { return *results_.back(); }
174 URLResult& operator[](size_t i) { return *results_[i]; }
175 const URLResult& operator[](size_t i) const { return *results_[i]; }
177 URLResultVector::const_iterator begin() const { return results_.begin(); }
178 URLResultVector::const_iterator end() const { return results_.end(); }
179 URLResultVector::const_reverse_iterator rbegin() const {
180 return results_.rbegin();
182 URLResultVector::const_reverse_iterator rend() const {
183 return results_.rend();
186 // Returns a pointer to the beginning of an array of all matching indices
187 // for entries with the given URL. The array will be |*num_matches| long.
188 // |num_matches| can be NULL if the caller is not interested in the number of
189 // results (commonly it will only be interested in the first one and can test
190 // the pointer for NULL).
192 // When there is no match, it will return NULL and |*num_matches| will be 0.
193 const size_t* MatchesForURL(const GURL& url, size_t* num_matches) const;
195 // Swaps the current result with another. This allows ownership to be
196 // efficiently transferred without copying.
197 void Swap(QueryResults* other);
199 // Adds the given result to the map, using swap() on the members to avoid
200 // copying (there are a lot of strings and vectors). This means the parameter
201 // object will be cleared after this call.
202 void AppendURLBySwapping(URLResult* result);
204 // Removes all instances of the given URL from the result set.
205 void DeleteURL(const GURL& url);
207 // Deletes the given range of items in the result set.
208 void DeleteRange(size_t begin, size_t end);
211 // Maps the given URL to a list of indices into results_ which identify each
212 // time an entry with that URL appears. Normally, each URL will have one or
213 // very few indices after it, so we optimize this to use statically allocated
214 // memory when possible.
215 typedef std::map<GURL, base::StackVector<size_t, 4> > URLToResultIndices;
217 // Inserts an entry into the |url_to_results_| map saying that the given URL
218 // is at the given index in the results_.
219 void AddURLUsageAtIndex(const GURL& url, size_t index);
221 // Adds |delta| to each index in url_to_results_ in the range [begin,end]
222 // (this is inclusive). This is used when inserting or deleting.
223 void AdjustResultMap(size_t begin, size_t end, ptrdiff_t delta);
225 base::Time first_time_searched_;
227 // Whether the query reaches the beginning of the database.
228 bool reached_beginning_;
230 // The ordered list of results. The pointers inside this are owned by this
231 // QueryResults object.
232 ScopedVector<URLResult> results_;
234 // Maps URLs to entries in results_.
235 URLToResultIndices url_to_results_;
237 DISALLOW_COPY_AND_ASSIGN(QueryResults);
240 // QueryOptions ----------------------------------------------------------------
242 struct QueryOptions {
245 // The time range to search for matches in. The beginning is inclusive and
246 // the ending is exclusive. Either one (or both) may be null.
248 // This will match only the one recent visit of a URL. For text search
249 // queries, if the URL was visited in the given time period, but has also
250 // been visited more recently than that, it will not be returned. When the
251 // text query is empty, this will return the most recent visit within the
253 base::Time begin_time;
256 // Sets the query time to the last |days_ago| days to the present time.
257 void SetRecentDayRange(int days_ago);
259 // The maximum number of results to return. The results will be sorted with
260 // the most recent first, so older results may not be returned if there is not
261 // enough room. When 0, this will return everything (the default).
264 enum DuplicateHandling {
265 // Omit visits for which there is a more recent visit to the same URL.
266 // Each URL in the results will appear only once.
267 REMOVE_ALL_DUPLICATES,
269 // Omit visits for which there is a more recent visit to the same URL on
270 // the same day. Each URL will appear no more than once per day, where the
271 // day is defined by the local timezone.
272 REMOVE_DUPLICATES_PER_DAY,
274 // Return all visits without deduping.
278 // Allows the caller to specify how duplicate URLs in the result set should
279 // be handled. The default is REMOVE_DUPLICATES.
280 DuplicateHandling duplicate_policy;
282 // Helpers to get the effective parameters values, since a value of 0 means
284 int EffectiveMaxCount() const;
285 int64 EffectiveBeginTime() const;
286 int64 EffectiveEndTime() const;
289 // QueryURLResult -------------------------------------------------------------
291 // QueryURLResult encapsulates the result of a call to HistoryBackend::QueryURL.
292 struct QueryURLResult {
296 // Indicates whether the call to HistoryBackend::QueryURL was successfull
297 // or not. If false, then both |row| and |visits| fields are undefined.
303 // VisibleVisitCountToHostResult ----------------------------------------------
305 // VisibleVisitCountToHostResult encapsulates the result of a call to
306 // HistoryBackend::GetVisibleVisitCountToHost.
307 struct VisibleVisitCountToHostResult {
308 // Indicates whether the call to HistoryBackend::GetVisibleVisitCountToHost
309 // was successfull or not. If false, then both |count| and |first_visit| are
313 base::Time first_visit;
316 // MostVisitedURL --------------------------------------------------------------
318 // Holds the per-URL information of the most visited query.
319 struct MostVisitedURL {
321 MostVisitedURL(const GURL& url, const base::string16& title);
322 MostVisitedURL(const GURL& url,
323 const base::string16& title,
324 const base::Time& last_forced_time);
328 base::string16 title;
330 // If this is a URL for which we want to force a thumbnail, records the last
331 // time it was forced so we can evict it when more recent URLs are requested.
332 // If it's not a forced thumbnail, keep a time of 0.
333 base::Time last_forced_time;
335 RedirectList redirects;
337 bool operator==(const MostVisitedURL& other) {
338 return url == other.url;
342 // FilteredURL -----------------------------------------------------------------
344 // Holds the per-URL information of the filterd url query.
346 struct ExtendedInfo {
348 // The absolute number of visits.
349 unsigned int total_visits;
350 // The number of visits, as seen by the Most Visited NTP pane.
352 // The total number of seconds that the page was open.
353 int64 duration_opened;
354 // The time when the page was last visited.
355 base::Time last_visit_time;
359 explicit FilteredURL(const PageUsageData& data);
363 base::string16 title;
365 ExtendedInfo extended_info;
368 // Navigation -----------------------------------------------------------------
370 // Marshalling structure for AddPage.
371 struct HistoryAddPageArgs {
372 // The default constructor is equivalent to:
374 // HistoryAddPageArgs(
375 // GURL(), base::Time(), NULL, 0, GURL(),
376 // history::RedirectList(), ui::PAGE_TRANSITION_LINK,
377 // SOURCE_BROWSED, false)
378 HistoryAddPageArgs();
379 HistoryAddPageArgs(const GURL& url,
381 ContextID context_id,
383 const GURL& referrer,
384 const history::RedirectList& redirects,
385 ui::PageTransition transition,
387 bool did_replace_entry);
388 ~HistoryAddPageArgs();
393 ContextID context_id;
397 history::RedirectList redirects;
398 ui::PageTransition transition;
399 VisitSource visit_source;
400 bool did_replace_entry;
403 // TopSites -------------------------------------------------------------------
405 typedef std::vector<MostVisitedURL> MostVisitedURLList;
406 typedef std::vector<FilteredURL> FilteredURLList;
408 // Used by TopSites to store the thumbnails.
413 scoped_refptr<base::RefCountedMemory> thumbnail;
414 ThumbnailScore thumbnail_score;
416 // TODO(brettw): this will eventually store the favicon.
417 // scoped_refptr<base::RefCountedBytes> favicon;
420 struct MostVisitedURLWithRank {
425 typedef std::vector<MostVisitedURLWithRank> MostVisitedURLWithRankList;
427 struct TopSitesDelta {
431 MostVisitedURLList deleted;
432 MostVisitedURLWithRankList added;
433 MostVisitedURLWithRankList moved;
436 typedef std::map<GURL, scoped_refptr<base::RefCountedBytes> > URLToThumbnailMap;
438 // Used when migrating most visited thumbnails out of history and into topsites.
439 struct ThumbnailMigration {
440 ThumbnailMigration();
441 ~ThumbnailMigration();
443 MostVisitedURLList most_visited;
444 URLToThumbnailMap url_to_thumbnail_map;
447 typedef std::map<GURL, Images> URLToImagesMap;
449 class MostVisitedThumbnails
450 : public base::RefCountedThreadSafe<MostVisitedThumbnails> {
452 MostVisitedThumbnails();
454 MostVisitedURLList most_visited;
455 URLToImagesMap url_to_images_map;
458 friend class base::RefCountedThreadSafe<MostVisitedThumbnails>;
459 virtual ~MostVisitedThumbnails();
461 DISALLOW_COPY_AND_ASSIGN(MostVisitedThumbnails);
464 // Favicons -------------------------------------------------------------------
466 // Used for the mapping between the page and icon.
471 // The unique id of the mapping.
472 IconMappingID mapping_id;
474 // The url of a web page.
477 // The unique id of the icon.
478 favicon_base::FaviconID icon_id;
480 // The url of the icon.
484 favicon_base::IconType icon_type;
487 // Defines a favicon bitmap and its associated pixel size.
488 struct FaviconBitmapIDSize {
489 FaviconBitmapIDSize();
490 ~FaviconBitmapIDSize();
492 // The unique id of the favicon bitmap.
493 FaviconBitmapID bitmap_id;
495 // The pixel dimensions of the associated bitmap.
496 gfx::Size pixel_size;
499 // Defines a favicon bitmap stored in the history backend.
500 struct FaviconBitmap {
504 // The unique id of the bitmap.
505 FaviconBitmapID bitmap_id;
507 // The id of the favicon to which the bitmap belongs to.
508 favicon_base::FaviconID icon_id;
510 // Time at which |bitmap_data| was last updated.
511 base::Time last_updated;
513 // The bits of the bitmap.
514 scoped_refptr<base::RefCountedMemory> bitmap_data;
516 // The pixel dimensions of bitmap_data.
517 gfx::Size pixel_size;
520 // Abbreviated information about a visit.
521 struct BriefVisitInfo {
524 ui::PageTransition transition;
527 // An observer of VisitDatabase.
528 class VisitDatabaseObserver {
530 virtual ~VisitDatabaseObserver();
531 virtual void OnAddVisit(const BriefVisitInfo& info) = 0;
534 struct ExpireHistoryArgs {
536 ~ExpireHistoryArgs();
538 // Sets |begin_time| and |end_time| to the beginning and end of the day (in
539 // local time) on which |time| occurs.
540 void SetTimeRangeForOneDay(base::Time time);
543 base::Time begin_time;
547 } // namespace history
549 #endif // COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_TYPES_H_