history_backend.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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#include "chrome/browser/history/history_backend.h"
6
7#include <algorithm>
8#include <functional>
9#include <list>
10#include <map>
11#include <set>
12#include <vector>
13
14#include "base/basictypes.h"
15#include "base/bind.h"
16#include "base/compiler_specific.h"
17#include "base/file_util.h"
18#include "base/memory/scoped_ptr.h"
19#include "base/memory/scoped_vector.h"
20#include "base/message_loop.h"
21#include "base/metrics/histogram.h"
22#include "base/rand_util.h"
23#include "base/string_util.h"
24#include "base/time.h"
25#include "base/utf_string_conversions.h"
26#include "chrome/browser/autocomplete/history_url_provider.h"
27#include "chrome/browser/bookmarks/bookmark_service.h"
28#include "chrome/browser/history/download_row.h"
29#include "chrome/browser/history/history_db_task.h"
30#include "chrome/browser/history/history_notifications.h"
31#include "chrome/browser/history/history_publisher.h"
32#include "chrome/browser/history/in_memory_history_backend.h"
33#include "chrome/browser/history/page_usage_data.h"
34#include "chrome/browser/history/select_favicon_frames.h"
35#include "chrome/browser/history/top_sites.h"
36#include "chrome/browser/history/visit_filter.h"
37#include "chrome/common/chrome_constants.h"
38#include "chrome/common/chrome_notification_types.h"
39#include "chrome/common/url_constants.h"
40#include "googleurl/src/gurl.h"
41#include "grit/chromium_strings.h"
42#include "grit/generated_resources.h"
43#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
44#include "sql/error_delegate_util.h"
45
46#if defined(OS_ANDROID)
47#include "chrome/browser/history/android/android_provider_backend.h"
48#endif
49
50using base::Time;
51using base::TimeDelta;
52using base::TimeTicks;
53
54/* The HistoryBackend consists of a number of components:
55
56    HistoryDatabase (stores past 3 months of history)
57      URLDatabase (stores a list of URLs)
58      DownloadDatabase (stores a list of downloads)
59      VisitDatabase (stores a list of visits for the URLs)
60      VisitSegmentDatabase (stores groups of URLs for the most visited view).
61
62    ArchivedDatabase (stores history older than 3 months)
63      URLDatabase (stores a list of URLs)
64      DownloadDatabase (stores a list of downloads)
65      VisitDatabase (stores a list of visits for the URLs)
66
67      (this does not store visit segments as they expire after 3 mos.)
68
69    TextDatabaseManager (manages multiple text database for different times)
70      TextDatabase (represents a single month of full-text index).
71      ...more TextDatabase objects...
72
73    ExpireHistoryBackend (manages moving things from HistoryDatabase to
74                          the ArchivedDatabase and deleting)
75*/
76
77namespace history {
78
79// How long we keep segment data for in days. Currently 3 months.
80// This value needs to be greater or equal to
81// MostVisitedModel::kMostVisitedScope but we don't want to introduce a direct
82// dependency between MostVisitedModel and the history backend.
83static const int kSegmentDataRetention = 90;
84
85// How long we'll wait to do a commit, so that things are batched together.
86static const int kCommitIntervalSeconds = 10;
87
88// The amount of time before we re-fetch the favicon.
89static const int kFaviconRefetchDays = 7;
90
91// GetSessionTabs returns all open tabs, or tabs closed kSessionCloseTimeWindow
92// seconds ago.
93static const int kSessionCloseTimeWindowSecs = 10;
94
95// The maximum number of items we'll allow in the redirect list before
96// deleting some.
97static const int kMaxRedirectCount = 32;
98
99// The number of days old a history entry can be before it is considered "old"
100// and is archived.
101static const int kArchiveDaysThreshold = 90;
102
103// Converts from PageUsageData to MostVisitedURL. |redirects| is a
104// list of redirects for this URL. Empty list means no redirects.
105MostVisitedURL MakeMostVisitedURL(const PageUsageData& page_data,
106                                  const RedirectList& redirects) {
107  MostVisitedURL mv;
108  mv.url = page_data.GetURL();
109  mv.title = page_data.GetTitle();
110  if (redirects.empty()) {
111    // Redirects must contain at least the target url.
112    mv.redirects.push_back(mv.url);
113  } else {
114    mv.redirects = redirects;
115    if (mv.redirects[mv.redirects.size() - 1] != mv.url) {
116      // The last url must be the target url.
117      mv.redirects.push_back(mv.url);
118    }
119  }
120  return mv;
121}
122
123// This task is run on a timer so that commits happen at regular intervals
124// so they are batched together. The important thing about this class is that
125// it supports canceling of the task so the reference to the backend will be
126// freed. The problem is that when history is shutting down, there is likely
127// to be one of these commits still pending and holding a reference.
128//
129// The backend can call Cancel to have this task release the reference. The
130// task will still run (if we ever get to processing the event before
131// shutdown), but it will not do anything.
132//
133// Note that this is a refcounted object and is not a task in itself. It should
134// be assigned to a RunnableMethod.
135//
136// TODO(brettw): bug 1165182: This should be replaced with a
137// base::WeakPtrFactory which will handle everything automatically (like we do
138// in ExpireHistoryBackend).
139class CommitLaterTask : public base::RefCounted<CommitLaterTask> {
140 public:
141  explicit CommitLaterTask(HistoryBackend* history_backend)
142      : history_backend_(history_backend) {
143  }
144
145  // The backend will call this function if it is being destroyed so that we
146  // release our reference.
147  void Cancel() {
148    history_backend_ = NULL;
149  }
150
151  void RunCommit() {
152    if (history_backend_.get())
153      history_backend_->Commit();
154  }
155
156 private:
157  friend class base::RefCounted<CommitLaterTask>;
158
159  ~CommitLaterTask() {}
160
161  scoped_refptr<HistoryBackend> history_backend_;
162};
163
164// Handles querying first the main database, then the full text database if that
165// fails. It will optionally keep track of all URLs seen so duplicates can be
166// eliminated. This is used by the querying sub-functions.
167//
168// TODO(brettw): This class may be able to be simplified or eliminated. After
169// this was written, QueryResults can efficiently look up by URL, so the need
170// for this extra set of previously queried URLs is less important.
171class HistoryBackend::URLQuerier {
172 public:
173  URLQuerier(URLDatabase* main_db, URLDatabase* archived_db, bool track_unique)
174      : main_db_(main_db),
175        archived_db_(archived_db),
176        track_unique_(track_unique) {
177  }
178
179  // When we're tracking unique URLs, returns true if this URL has been
180  // previously queried. Only call when tracking unique URLs.
181  bool HasURL(const GURL& url) {
182    DCHECK(track_unique_);
183    return unique_urls_.find(url) != unique_urls_.end();
184  }
185
186  bool GetRowForURL(const GURL& url, URLRow* row) {
187    if (!main_db_->GetRowForURL(url, row)) {
188      if (!archived_db_ || !archived_db_->GetRowForURL(url, row)) {
189        // This row is neither in the main nor the archived DB.
190        return false;
191      }
192    }
193
194    if (track_unique_)
195      unique_urls_.insert(url);
196    return true;
197  }
198
199 private:
200  URLDatabase* main_db_;  // Guaranteed non-NULL.
201  URLDatabase* archived_db_;  // Possibly NULL.
202
203  bool track_unique_;
204
205  // When track_unique_ is set, this is updated with every URL seen so far.
206  std::set<GURL> unique_urls_;
207
208  DISALLOW_COPY_AND_ASSIGN(URLQuerier);
209};
210
211// KillHistoryDatabaseErrorDelegate -------------------------------------------
212
213class KillHistoryDatabaseErrorDelegate : public sql::ErrorDelegate {
214 public:
215  explicit KillHistoryDatabaseErrorDelegate(HistoryBackend* backend)
216      : backend_(backend),
217        scheduled_killing_database_(false) {
218  }
219
220  // sql::ErrorDelegate implementation.
221  virtual int OnError(int error,
222                      sql::Connection* connection,
223                      sql::Statement* stmt) OVERRIDE {
224    // Do not schedule killing database more than once. If the first time
225    // failed, it is unlikely that a second time will be successful.
226    if (!scheduled_killing_database_ && sql::IsErrorCatastrophic(error)) {
227      scheduled_killing_database_ = true;
228
229      // Don't just do the close/delete here, as we are being called by |db| and
230      // that seems dangerous.
231      MessageLoop::current()->PostTask(
232          FROM_HERE,
233          base::Bind(&HistoryBackend::KillHistoryDatabase, backend_));
234    }
235
236    return error;
237  }
238
239  // Returns true if the delegate has previously scheduled killing the database.
240  bool scheduled_killing_database() const {
241    return scheduled_killing_database_;
242  }
243
244 private:
245  // Do not increment the count on |HistoryBackend| as that would create a
246  // circular reference (HistoryBackend -> HistoryDatabase -> Connection ->
247  // ErrorDelegate -> HistoryBackend).
248  HistoryBackend* backend_;
249
250  // True if the backend has previously scheduled killing the history database.
251  bool scheduled_killing_database_;
252
253  DISALLOW_COPY_AND_ASSIGN(KillHistoryDatabaseErrorDelegate);
254};
255
256// HistoryBackend --------------------------------------------------------------
257
258HistoryBackend::HistoryBackend(const base::FilePath& history_dir,
259                               int id,
260                               Delegate* delegate,
261                               BookmarkService* bookmark_service)
262    : delegate_(delegate),
263      id_(id),
264      history_dir_(history_dir),
265      expirer_(this, bookmark_service),
266      recent_redirects_(kMaxRedirectCount),
267      backend_destroy_message_loop_(NULL),
268      segment_queried_(false),
269      bookmark_service_(bookmark_service) {
270}
271
272HistoryBackend::~HistoryBackend() {
273  DCHECK(!scheduled_commit_) << "Deleting without cleanup";
274  ReleaseDBTasks();
275
276#if defined(OS_ANDROID)
277  // Release AndroidProviderBackend before other objects.
278  android_provider_backend_.reset();
279#endif
280
281  // First close the databases before optionally running the "destroy" task.
282  CloseAllDatabases();
283
284  if (!backend_destroy_task_.is_null()) {
285    // Notify an interested party (typically a unit test) that we're done.
286    DCHECK(backend_destroy_message_loop_);
287    backend_destroy_message_loop_->PostTask(FROM_HERE, backend_destroy_task_);
288  }
289
290#if defined(OS_ANDROID)
291  file_util::Delete(GetAndroidCacheFileName(), false);
292#endif
293}
294
295void HistoryBackend::Init(const std::string& languages, bool force_fail) {
296  if (!force_fail)
297    InitImpl(languages);
298  delegate_->DBLoaded(id_);
299}
300
301void HistoryBackend::SetOnBackendDestroyTask(MessageLoop* message_loop,
302                                             const base::Closure& task) {
303  if (!backend_destroy_task_.is_null())
304    DLOG(WARNING) << "Setting more than one destroy task, overriding";
305  backend_destroy_message_loop_ = message_loop;
306  backend_destroy_task_ = task;
307}
308
309void HistoryBackend::Closing() {
310  // Any scheduled commit will have a reference to us, we must make it
311  // release that reference before we can be destroyed.
312  CancelScheduledCommit();
313
314  // Release our reference to the delegate, this reference will be keeping the
315  // history service alive.
316  delegate_.reset();
317}
318
319void HistoryBackend::NotifyRenderProcessHostDestruction(const void* host) {
320  tracker_.NotifyRenderProcessHostDestruction(host);
321}
322
323base::FilePath HistoryBackend::GetThumbnailFileName() const {
324  return history_dir_.Append(chrome::kThumbnailsFilename);
325}
326
327base::FilePath HistoryBackend::GetFaviconsFileName() const {
328  return history_dir_.Append(chrome::kFaviconsFilename);
329}
330
331base::FilePath HistoryBackend::GetArchivedFileName() const {
332  return history_dir_.Append(chrome::kArchivedHistoryFilename);
333}
334
335#if defined(OS_ANDROID)
336base::FilePath HistoryBackend::GetAndroidCacheFileName() const {
337  return history_dir_.Append(chrome::kAndroidCacheFilename);
338}
339#endif
340
341SegmentID HistoryBackend::GetLastSegmentID(VisitID from_visit) {
342  // Set is used to detect referrer loops.  Should not happen, but can
343  // if the database is corrupt.
344  std::set<VisitID> visit_set;
345  VisitID visit_id = from_visit;
346  while (visit_id) {
347    VisitRow row;
348    if (!db_->GetRowForVisit(visit_id, &row))
349      return 0;
350    if (row.segment_id)
351      return row.segment_id;  // Found a visit in this change with a segment.
352
353    // Check the referrer of this visit, if any.
354    visit_id = row.referring_visit;
355
356    if (visit_set.find(visit_id) != visit_set.end()) {
357      NOTREACHED() << "Loop in referer chain, giving up";
358      break;
359    }
360    visit_set.insert(visit_id);
361  }
362  return 0;
363}
364
365SegmentID HistoryBackend::UpdateSegments(
366    const GURL& url,
367    VisitID from_visit,
368    VisitID visit_id,
369    content::PageTransition transition_type,
370    const Time ts) {
371  if (!db_.get())
372    return 0;
373
374  // We only consider main frames.
375  if (!content::PageTransitionIsMainFrame(transition_type))
376    return 0;
377
378  SegmentID segment_id = 0;
379  content::PageTransition t =
380      content::PageTransitionStripQualifier(transition_type);
381
382  // Are we at the beginning of a new segment?
383  // Note that navigating to an existing entry (with back/forward) reuses the
384  // same transition type.  We are not adding it as a new segment in that case
385  // because if this was the target of a redirect, we might end up with
386  // 2 entries for the same final URL. Ex: User types google.net, gets
387  // redirected to google.com. A segment is created for google.net. On
388  // google.com users navigates through a link, then press back. That last
389  // navigation is for the entry google.com transition typed. We end up adding
390  // a segment for that one as well. So we end up with google.net and google.com
391  // in the segement table, showing as 2 entries in the NTP.
392  // Note also that we should still be updating the visit count for that segment
393  // which we are not doing now. It should be addressed when
394  // http://crbug.com/96860 is fixed.
395  if ((t == content::PAGE_TRANSITION_TYPED ||
396       t == content::PAGE_TRANSITION_AUTO_BOOKMARK) &&
397      (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) == 0) {
398    // If so, create or get the segment.
399    std::string segment_name = db_->ComputeSegmentName(url);
400    URLID url_id = db_->GetRowForURL(url, NULL);
401    if (!url_id)
402      return 0;
403
404    if (!(segment_id = db_->GetSegmentNamed(segment_name))) {
405      if (!(segment_id = db_->CreateSegment(url_id, segment_name))) {
406        NOTREACHED();
407        return 0;
408      }
409    } else {
410      // Note: if we update an existing segment, we update the url used to
411      // represent that segment in order to minimize stale most visited
412      // images.
413      db_->UpdateSegmentRepresentationURL(segment_id, url_id);
414    }
415  } else {
416    // Note: it is possible there is no segment ID set for this visit chain.
417    // This can happen if the initial navigation wasn't AUTO_BOOKMARK or
418    // TYPED. (For example GENERATED). In this case this visit doesn't count
419    // toward any segment.
420    if (!(segment_id = GetLastSegmentID(from_visit)))
421      return 0;
422  }
423
424  // Set the segment in the visit.
425  if (!db_->SetSegmentID(visit_id, segment_id)) {
426    NOTREACHED();
427    return 0;
428  }
429
430  // Finally, increase the counter for that segment / day.
431  if (!db_->IncreaseSegmentVisitCount(segment_id, ts, 1)) {
432    NOTREACHED();
433    return 0;
434  }
435  return segment_id;
436}
437
438void HistoryBackend::UpdateWithPageEndTime(const void* host,
439                                           int32 page_id,
440                                           const GURL& url,
441                                           Time end_ts) {
442  // Will be filled with the URL ID and the visit ID of the last addition.
443  VisitID visit_id = tracker_.GetLastVisit(host, page_id, url);
444  UpdateVisitDuration(visit_id, end_ts);
445}
446
447void HistoryBackend::UpdateVisitDuration(VisitID visit_id, const Time end_ts) {
448  if (!db_.get())
449    return;
450
451  // Get the starting visit_time for visit_id.
452  VisitRow visit_row;
453  if (db_->GetRowForVisit(visit_id, &visit_row)) {
454    // We should never have a negative duration time even when time is skewed.
455    visit_row.visit_duration = end_ts > visit_row.visit_time ?
456        end_ts - visit_row.visit_time : TimeDelta::FromMicroseconds(0);
457    db_->UpdateVisitRow(visit_row);
458  }
459}
460
461void HistoryBackend::AddPage(const HistoryAddPageArgs& request) {
462  if (!db_.get())
463    return;
464
465  // Will be filled with the URL ID and the visit ID of the last addition.
466  std::pair<URLID, VisitID> last_ids(0, tracker_.GetLastVisit(
467      request.id_scope, request.page_id, request.referrer));
468
469  VisitID from_visit_id = last_ids.second;
470
471  // If a redirect chain is given, we expect the last item in that chain to be
472  // the final URL.
473  DCHECK(request.redirects.empty() ||
474         request.redirects.back() == request.url);
475
476  // If the user is adding older history, we need to make sure our times
477  // are correct.
478  if (request.time < first_recorded_time_)
479    first_recorded_time_ = request.time;
480
481  content::PageTransition request_transition = request.transition;
482  content::PageTransition stripped_transition =
483    content::PageTransitionStripQualifier(request_transition);
484  bool is_keyword_generated =
485      (stripped_transition == content::PAGE_TRANSITION_KEYWORD_GENERATED);
486
487  // If the user is navigating to a not-previously-typed intranet hostname,
488  // change the transition to TYPED so that the omnibox will learn that this is
489  // a known host.
490  bool has_redirects = request.redirects.size() > 1;
491  if (content::PageTransitionIsMainFrame(request_transition) &&
492      (stripped_transition != content::PAGE_TRANSITION_TYPED) &&
493      !is_keyword_generated) {
494    const GURL& origin_url(has_redirects ?
495        request.redirects[0] : request.url);
496    if (origin_url.SchemeIs(chrome::kHttpScheme) ||
497        origin_url.SchemeIs(chrome::kHttpsScheme) ||
498        origin_url.SchemeIs(chrome::kFtpScheme)) {
499      std::string host(origin_url.host());
500      if ((net::RegistryControlledDomainService::GetRegistryLength(
501          host, false) == 0) && !db_->IsTypedHost(host)) {
502        stripped_transition = content::PAGE_TRANSITION_TYPED;
503        request_transition =
504            content::PageTransitionFromInt(
505                stripped_transition |
506                content::PageTransitionGetQualifier(request_transition));
507      }
508    }
509  }
510
511  if (!has_redirects) {
512    // The single entry is both a chain start and end.
513    content::PageTransition t = content::PageTransitionFromInt(
514        request_transition |
515        content::PAGE_TRANSITION_CHAIN_START |
516        content::PAGE_TRANSITION_CHAIN_END);
517
518    // No redirect case (one element means just the page itself).
519    last_ids = AddPageVisit(request.url, request.time,
520                            last_ids.second, t, request.visit_source);
521
522    // Update the segment for this visit. KEYWORD_GENERATED visits should not
523    // result in changing most visited, so we don't update segments (most
524    // visited db).
525    if (!is_keyword_generated) {
526      UpdateSegments(request.url, from_visit_id, last_ids.second, t,
527                     request.time);
528
529      // Update the referrer's duration.
530      UpdateVisitDuration(from_visit_id, request.time);
531    }
532  } else {
533    // Redirect case. Add the redirect chain.
534
535    content::PageTransition redirect_info =
536        content::PAGE_TRANSITION_CHAIN_START;
537
538    RedirectList redirects = request.redirects;
539    if (redirects[0].SchemeIs(chrome::kAboutScheme)) {
540      // When the redirect source + referrer is "about" we skip it. This
541      // happens when a page opens a new frame/window to about:blank and then
542      // script sets the URL to somewhere else (used to hide the referrer). It
543      // would be nice to keep all these redirects properly but we don't ever
544      // see the initial about:blank load, so we don't know where the
545      // subsequent client redirect came from.
546      //
547      // In this case, we just don't bother hooking up the source of the
548      // redirects, so we remove it.
549      redirects.erase(redirects.begin());
550    } else if (request_transition & content::PAGE_TRANSITION_CLIENT_REDIRECT) {
551      redirect_info = content::PAGE_TRANSITION_CLIENT_REDIRECT;
552      // The first entry in the redirect chain initiated a client redirect.
553      // We don't add this to the database since the referrer is already
554      // there, so we skip over it but change the transition type of the first
555      // transition to client redirect.
556      //
557      // The referrer is invalid when restoring a session that features an
558      // https tab that redirects to a different host or to http. In this
559      // case we don't need to reconnect the new redirect with the existing
560      // chain.
561      if (request.referrer.is_valid()) {
562        DCHECK(request.referrer == redirects[0]);
563        redirects.erase(redirects.begin());
564
565        // If the navigation entry for this visit has replaced that for the
566        // first visit, remove the CHAIN_END marker from the first visit. This
567        // can be called a lot, for example, the page cycler, and most of the
568        // time we won't have changed anything.
569        VisitRow visit_row;
570        if (request.did_replace_entry &&
571            db_->GetRowForVisit(last_ids.second, &visit_row) &&
572            visit_row.transition & content::PAGE_TRANSITION_CHAIN_END) {
573          visit_row.transition = content::PageTransitionFromInt(
574              visit_row.transition & ~content::PAGE_TRANSITION_CHAIN_END);
575          db_->UpdateVisitRow(visit_row);
576        }
577      }
578    }
579
580    for (size_t redirect_index = 0; redirect_index < redirects.size();
581         redirect_index++) {
582      content::PageTransition t =
583          content::PageTransitionFromInt(stripped_transition | redirect_info);
584
585      // If this is the last transition, add a CHAIN_END marker
586      if (redirect_index == (redirects.size() - 1)) {
587        t = content::PageTransitionFromInt(
588            t | content::PAGE_TRANSITION_CHAIN_END);
589      }
590
591      // Record all redirect visits with the same timestamp. We don't display
592      // them anyway, and if we ever decide to, we can reconstruct their order
593      // from the redirect chain.
594      last_ids = AddPageVisit(redirects[redirect_index],
595                              request.time, last_ids.second,
596                              t, request.visit_source);
597      if (t & content::PAGE_TRANSITION_CHAIN_START) {
598        // Update the segment for this visit.
599        UpdateSegments(redirects[redirect_index],
600                       from_visit_id, last_ids.second, t, request.time);
601
602        // Update the visit_details for this visit.
603        UpdateVisitDuration(from_visit_id, request.time);
604      }
605
606      // Subsequent transitions in the redirect list must all be server
607      // redirects.
608      redirect_info = content::PAGE_TRANSITION_SERVER_REDIRECT;
609    }
610
611    // Last, save this redirect chain for later so we can set titles & favicons
612    // on the redirected pages properly. It is indexed by the destination page.
613    recent_redirects_.Put(request.url, redirects);
614  }
615
616  // TODO(brettw) bug 1140015: Add an "add page" notification so the history
617  // views can keep in sync.
618
619  // Add the last visit to the tracker so we can get outgoing transitions.
620  // TODO(evanm): Due to http://b/1194536 we lose the referrers of a subframe
621  // navigation anyway, so last_visit_id is always zero for them.  But adding
622  // them here confuses main frame history, so we skip them for now.
623  if (stripped_transition != content::PAGE_TRANSITION_AUTO_SUBFRAME &&
624      stripped_transition != content::PAGE_TRANSITION_MANUAL_SUBFRAME &&
625      !is_keyword_generated) {
626    tracker_.AddVisit(request.id_scope, request.page_id, request.url,
627                      last_ids.second);
628  }
629
630  if (text_database_.get()) {
631    text_database_->AddPageURL(request.url, last_ids.first, last_ids.second,
632                               request.time);
633  }
634
635  ScheduleCommit();
636}
637
638void HistoryBackend::InitImpl(const std::string& languages) {
639  DCHECK(!db_.get()) << "Initializing HistoryBackend twice";
640  // In the rare case where the db fails to initialize a dialog may get shown
641  // the blocks the caller, yet allows other messages through. For this reason
642  // we only set db_ to the created database if creation is successful. That
643  // way other methods won't do anything as db_ is still NULL.
644
645  TimeTicks beginning_time = TimeTicks::Now();
646
647  // Compute the file names. Note that the index file can be removed when the
648  // text db manager is finished being hooked up.
649  base::FilePath history_name = history_dir_.Append(chrome::kHistoryFilename);
650  base::FilePath thumbnail_name = GetThumbnailFileName();
651  base::FilePath archived_name = GetArchivedFileName();
652
653  // History database.
654  db_.reset(new HistoryDatabase());
655
656  // |HistoryDatabase::Init| takes ownership of |error_delegate|.
657  KillHistoryDatabaseErrorDelegate* error_delegate =
658      new KillHistoryDatabaseErrorDelegate(this);
659
660  sql::InitStatus status = db_->Init(history_name, error_delegate);
661  switch (status) {
662    case sql::INIT_OK:
663      break;
664    case sql::INIT_FAILURE: {
665      // A NULL db_ will cause all calls on this object to notice this error
666      // and to not continue. If the error delegate scheduled killing the
667      // database, the task it posted has not executed yet. Try killing the
668      // database now before we close it.
669      bool kill_database = error_delegate->scheduled_killing_database();
670      if (kill_database)
671        KillHistoryDatabase();
672      UMA_HISTOGRAM_BOOLEAN("History.AttemptedToFixProfileError",
673                            kill_database);
674      delegate_->NotifyProfileError(id_, status);
675      db_.reset();
676      return;
677    }
678    default:
679      NOTREACHED();
680  }
681
682  // Fill the in-memory database and send it back to the history service on the
683  // main thread.
684  InMemoryHistoryBackend* mem_backend = new InMemoryHistoryBackend;
685  if (mem_backend->Init(history_name, db_.get()))
686    delegate_->SetInMemoryBackend(id_, mem_backend);  // Takes ownership of
687                                                      // pointer.
688  else
689    delete mem_backend;  // Error case, run without the in-memory DB.
690  db_->BeginExclusiveMode();  // Must be after the mem backend read the data.
691
692  // Create the history publisher which needs to be passed on to the text and
693  // thumbnail databases for publishing history.
694  history_publisher_.reset(new HistoryPublisher());
695  if (!history_publisher_->Init()) {
696    // The init may fail when there are no indexers wanting our history.
697    // Hence no need to log the failure.
698    history_publisher_.reset();
699  }
700
701  // Full-text database. This has to be first so we can pass it to the
702  // HistoryDatabase for migration.
703  text_database_.reset(new TextDatabaseManager(history_dir_,
704                                               db_.get(), db_.get()));
705  if (!text_database_->Init(history_publisher_.get())) {
706    LOG(WARNING) << "Text database initialization failed, running without it.";
707    text_database_.reset();
708  }
709  if (db_->needs_version_17_migration()) {
710    // See needs_version_17_migration() decl for more. In this case, we want
711    // to erase all the text database files. This must be done after the text
712    // database manager has been initialized, since it knows about all the
713    // files it manages.
714    text_database_->DeleteAll();
715  }
716
717  // Thumbnail database.
718  thumbnail_db_.reset(new ThumbnailDatabase());
719  if (!db_->GetNeedsThumbnailMigration()) {
720    // No convertion needed - use new filename right away.
721    thumbnail_name = GetFaviconsFileName();
722  }
723  if (thumbnail_db_->Init(thumbnail_name,
724                          history_publisher_.get(),
725                          db_.get()) != sql::INIT_OK) {
726    // Unlike the main database, we don't error out when the database is too
727    // new because this error is much less severe. Generally, this shouldn't
728    // happen since the thumbnail and main datbase versions should be in sync.
729    // We'll just continue without thumbnails & favicons in this case or any
730    // other error.
731    LOG(WARNING) << "Could not initialize the thumbnail database.";
732    thumbnail_db_.reset();
733  }
734
735  if (db_->GetNeedsThumbnailMigration()) {
736    VLOG(1) << "Starting TopSites migration";
737    delegate_->StartTopSitesMigration(id_);
738  }
739
740  // Archived database.
741  if (db_->needs_version_17_migration()) {
742    // See needs_version_17_migration() decl for more. In this case, we want
743    // to delete the archived database and need to do so before we try to
744    // open the file. We can ignore any error (maybe the file doesn't exist).
745    file_util::Delete(archived_name, false);
746  }
747  archived_db_.reset(new ArchivedDatabase());
748  if (!archived_db_->Init(archived_name)) {
749    LOG(WARNING) << "Could not initialize the archived database.";
750    archived_db_.reset();
751  }
752
753  // Generate the history and thumbnail database metrics only after performing
754  // any migration work.
755  if (base::RandInt(1, 100) == 50) {
756    // Only do this computation sometimes since it can be expensive.
757    db_->ComputeDatabaseMetrics(history_name);
758    thumbnail_db_->ComputeDatabaseMetrics();
759  }
760
761  // Tell the expiration module about all the nice databases we made. This must
762  // happen before db_->Init() is called since the callback ForceArchiveHistory
763  // may need to expire stuff.
764  //
765  // *sigh*, this can all be cleaned up when that migration code is removed.
766  // The main DB initialization should intuitively be first (not that it
767  // actually matters) and the expirer should be set last.
768  expirer_.SetDatabases(db_.get(), archived_db_.get(),
769                        thumbnail_db_.get(), text_database_.get());
770
771  // Open the long-running transaction.
772  db_->BeginTransaction();
773  if (thumbnail_db_.get())
774    thumbnail_db_->BeginTransaction();
775  if (archived_db_.get())
776    archived_db_->BeginTransaction();
777  if (text_database_.get())
778    text_database_->BeginTransaction();
779
780  // Get the first item in our database.
781  db_->GetStartDate(&first_recorded_time_);
782
783  // Start expiring old stuff.
784  expirer_.StartArchivingOldStuff(TimeDelta::FromDays(kArchiveDaysThreshold));
785
786#if defined(OS_ANDROID)
787  if (thumbnail_db_.get()) {
788    android_provider_backend_.reset(new AndroidProviderBackend(
789        GetAndroidCacheFileName(), db_.get(), thumbnail_db_.get(),
790        bookmark_service_, delegate_.get()));
791  }
792#endif
793
794  HISTOGRAM_TIMES("History.InitTime",
795                  TimeTicks::Now() - beginning_time);
796}
797
798void HistoryBackend::CloseAllDatabases() {
799  if (db_.get()) {
800    // Commit the long-running transaction.
801    db_->CommitTransaction();
802    db_.reset();
803  }
804  if (thumbnail_db_.get()) {
805    thumbnail_db_->CommitTransaction();
806    thumbnail_db_.reset();
807  }
808  if (archived_db_.get()) {
809    archived_db_->CommitTransaction();
810    archived_db_.reset();
811  }
812  if (text_database_.get()) {
813    text_database_->CommitTransaction();
814    text_database_.reset();
815  }
816}
817
818std::pair<URLID, VisitID> HistoryBackend::AddPageVisit(
819    const GURL& url,
820    Time time,
821    VisitID referring_visit,
822    content::PageTransition transition,
823    VisitSource visit_source) {
824  // Top-level frame navigations are visible, everything else is hidden
825  bool new_hidden = !content::PageTransitionIsMainFrame(transition);
826
827  // NOTE: This code must stay in sync with
828  // ExpireHistoryBackend::ExpireURLsForVisits().
829  // TODO(pkasting): http://b/1148304 We shouldn't be marking so many URLs as
830  // typed, which would eliminate the need for this code.
831  int typed_increment = 0;
832  content::PageTransition transition_type =
833      content::PageTransitionStripQualifier(transition);
834  if ((transition_type == content::PAGE_TRANSITION_TYPED &&
835      !content::PageTransitionIsRedirect(transition)) ||
836      transition_type == content::PAGE_TRANSITION_KEYWORD_GENERATED)
837    typed_increment = 1;
838
839  // See if this URL is already in the DB.
840  URLRow url_info(url);
841  URLID url_id = db_->GetRowForURL(url, &url_info);
842  if (url_id) {
843    // Update of an existing row.
844    if (content::PageTransitionStripQualifier(transition) !=
845        content::PAGE_TRANSITION_RELOAD)
846      url_info.set_visit_count(url_info.visit_count() + 1);
847    if (typed_increment)
848      url_info.set_typed_count(url_info.typed_count() + typed_increment);
849    if (url_info.last_visit() < time)
850      url_info.set_last_visit(time);
851
852    // Only allow un-hiding of pages, never hiding.
853    if (!new_hidden)
854      url_info.set_hidden(false);
855
856    db_->UpdateURLRow(url_id, url_info);
857  } else {
858    // Addition of a new row.
859    url_info.set_visit_count(1);
860    url_info.set_typed_count(typed_increment);
861    url_info.set_last_visit(time);
862    url_info.set_hidden(new_hidden);
863
864    url_id = db_->AddURL(url_info);
865    if (!url_id) {
866      NOTREACHED() << "Adding URL failed.";
867      return std::make_pair(0, 0);
868    }
869    url_info.id_ = url_id;
870
871    // We don't actually add the URL to the full text index at this point. It
872    // might be nice to do this so that even if we get no title or body, the
873    // user can search for URL components and get the page.
874    //
875    // However, in most cases, we'll get at least a title and usually contents,
876    // and this add will be redundant, slowing everything down. As a result,
877    // we ignore this edge case.
878  }
879
880  // Add the visit with the time to the database.
881  VisitRow visit_info(url_id, time, referring_visit, transition, 0);
882  VisitID visit_id = db_->AddVisit(&visit_info, visit_source);
883  NotifyVisitObservers(visit_info);
884
885  if (visit_info.visit_time < first_recorded_time_)
886    first_recorded_time_ = visit_info.visit_time;
887
888  // Broadcast a notification of the visit.
889  if (visit_id) {
890    URLVisitedDetails* details = new URLVisitedDetails;
891    details->transition = transition;
892    details->row = url_info;
893    // TODO(meelapshah) Disabled due to potential PageCycler regression.
894    // Re-enable this.
895    // GetMostRecentRedirectsTo(url, &details->redirects);
896    BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URL_VISITED, details);
897  } else {
898    VLOG(0) << "Failed to build visit insert statement:  "
899            << "url_id = " << url_id;
900  }
901
902  return std::make_pair(url_id, visit_id);
903}
904
905void HistoryBackend::AddPagesWithDetails(const URLRows& urls,
906                                         VisitSource visit_source) {
907  if (!db_.get())
908    return;
909
910  scoped_ptr<URLsModifiedDetails> modified(new URLsModifiedDetails);
911  for (URLRows::const_iterator i = urls.begin(); i != urls.end(); ++i) {
912    DCHECK(!i->last_visit().is_null());
913
914    // We will add to either the archived database or the main one depending on
915    // the date of the added visit.
916    URLDatabase* url_database;
917    VisitDatabase* visit_database;
918    if (IsExpiredVisitTime(i->last_visit())) {
919      if (!archived_db_.get())
920        return;  // No archived database to save it to, just forget this.
921      url_database = archived_db_.get();
922      visit_database = archived_db_.get();
923    } else {
924      url_database = db_.get();
925      visit_database = db_.get();
926    }
927
928    URLRow existing_url;
929    URLID url_id = url_database->GetRowForURL(i->url(), &existing_url);
930    if (!url_id) {
931      // Add the page if it doesn't exist.
932      url_id = url_database->AddURL(*i);
933      if (!url_id) {
934        NOTREACHED() << "Could not add row to DB";
935        return;
936      }
937
938      if (i->typed_count() > 0) {
939        modified->changed_urls.push_back(*i);
940        modified->changed_urls.back().set_id(url_id);  // *i likely has |id_| 0.
941      }
942    }
943
944    // Add the page to the full text index. This function is also used for
945    // importing. Even though we don't have page contents, we can at least
946    // add the title and URL to the index so they can be searched. We don't
947    // bother to delete any already-existing FTS entries for the URL, since
948    // this is normally called on import.
949    //
950    // If you ever import *after* first run (selecting import from the menu),
951    // then these additional entries will "shadow" the originals when querying
952    // for the most recent match only, and the user won't get snippets. This is
953    // a very minor issue, and fixing it will make import slower, so we don't
954    // bother.
955    bool has_indexed = false;
956    if (text_database_.get()) {
957      // We do not have to make it update the visit database, below, we will
958      // create the visit entry with the indexed flag set.
959      has_indexed = text_database_->AddPageData(i->url(), url_id, 0,
960                                                i->last_visit(),
961                                                i->title(), string16());
962    }
963
964    // Sync code manages the visits itself.
965    if (visit_source != SOURCE_SYNCED) {
966      // Make up a visit to correspond to the last visit to the page.
967      VisitRow visit_info(url_id, i->last_visit(), 0,
968                          content::PageTransitionFromInt(
969                              content::PAGE_TRANSITION_LINK |
970                              content::PAGE_TRANSITION_CHAIN_START |
971                              content::PAGE_TRANSITION_CHAIN_END), 0);
972      visit_info.is_indexed = has_indexed;
973      if (!visit_database->AddVisit(&visit_info, visit_source)) {
974        NOTREACHED() << "Adding visit failed.";
975        return;
976      }
977      NotifyVisitObservers(visit_info);
978
979      if (visit_info.visit_time < first_recorded_time_)
980        first_recorded_time_ = visit_info.visit_time;
981    }
982  }
983
984  // Broadcast a notification for typed URLs that have been modified. This
985  // will be picked up by the in-memory URL database on the main thread.
986  //
987  // TODO(brettw) bug 1140015: Add an "add page" notification so the history
988  // views can keep in sync.
989  BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URLS_MODIFIED,
990                         modified.release());
991
992  ScheduleCommit();
993}
994
995bool HistoryBackend::IsExpiredVisitTime(const base::Time& time) {
996  return time < expirer_.GetCurrentArchiveTime();
997}
998
999void HistoryBackend::SetPageTitle(const GURL& url,
1000                                  const string16& title) {
1001  if (!db_.get())
1002    return;
1003
1004  // Update the full text index.
1005  if (text_database_.get())
1006    text_database_->AddPageTitle(url, title);
1007
1008  // Search for recent redirects which should get the same title. We make a
1009  // dummy list containing the exact URL visited if there are no redirects so
1010  // the processing below can be the same.
1011  history::RedirectList dummy_list;
1012  history::RedirectList* redirects;
1013  RedirectCache::iterator iter = recent_redirects_.Get(url);
1014  if (iter != recent_redirects_.end()) {
1015    redirects = &iter->second;
1016
1017    // This redirect chain should have the destination URL as the last item.
1018    DCHECK(!redirects->empty());
1019    DCHECK(redirects->back() == url);
1020  } else {
1021    // No redirect chain stored, make up one containing the URL we want so we
1022    // can use the same logic below.
1023    dummy_list.push_back(url);
1024    redirects = &dummy_list;
1025  }
1026
1027  scoped_ptr<URLsModifiedDetails> details(new URLsModifiedDetails);
1028  for (size_t i = 0; i < redirects->size(); i++) {
1029    URLRow row;
1030    URLID row_id = db_->GetRowForURL(redirects->at(i), &row);
1031    if (row_id && row.title() != title) {
1032      row.set_title(title);
1033      db_->UpdateURLRow(row_id, row);
1034      details->changed_urls.push_back(row);
1035    }
1036  }
1037
1038  // Broadcast notifications for any URLs that have changed. This will
1039  // update the in-memory database and the InMemoryURLIndex.
1040  if (!details->changed_urls.empty()) {
1041    BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URLS_MODIFIED,
1042                           details.release());
1043    ScheduleCommit();
1044  }
1045}
1046
1047void HistoryBackend::AddPageNoVisitForBookmark(const GURL& url,
1048                                               const string16& title) {
1049  if (!db_.get())
1050    return;
1051
1052  URLRow url_info(url);
1053  URLID url_id = db_->GetRowForURL(url, &url_info);
1054  if (url_id) {
1055    // URL is already known, nothing to do.
1056    return;
1057  }
1058
1059  if (!title.empty()) {
1060    url_info.set_title(title);
1061  } else {
1062    url_info.set_title(UTF8ToUTF16(url.spec()));
1063  }
1064
1065  url_info.set_last_visit(Time::Now());
1066  // Mark the page hidden. If the user types it in, it'll unhide.
1067  url_info.set_hidden(true);
1068
1069  db_->AddURL(url_info);
1070}
1071
1072void HistoryBackend::IterateURLs(
1073    const scoped_refptr<components::VisitedLinkDelegate::URLEnumerator>&
1074    iterator) {
1075  if (db_.get()) {
1076    HistoryDatabase::URLEnumerator e;
1077    if (db_->InitURLEnumeratorForEverything(&e)) {
1078      URLRow info;
1079      while (e.GetNextURL(&info)) {
1080        iterator->OnURL(info.url());
1081      }
1082      iterator->OnComplete(true);  // Success.
1083      return;
1084    }
1085  }
1086  iterator->OnComplete(false);  // Failure.
1087}
1088
1089bool HistoryBackend::GetAllTypedURLs(URLRows* urls) {
1090  if (db_.get())
1091    return db_->GetAllTypedUrls(urls);
1092  return false;
1093}
1094
1095bool HistoryBackend::GetVisitsForURL(URLID id, VisitVector* visits) {
1096  if (db_.get())
1097    return db_->GetVisitsForURL(id, visits);
1098  return false;
1099}
1100
1101bool HistoryBackend::GetMostRecentVisitsForURL(URLID id,
1102                                               int max_visits,
1103                                               VisitVector* visits) {
1104  if (db_.get())
1105    return db_->GetMostRecentVisitsForURL(id, max_visits, visits);
1106  return false;
1107}
1108
1109bool HistoryBackend::UpdateURL(URLID id, const history::URLRow& url) {
1110  if (db_.get())
1111    return db_->UpdateURLRow(id, url);
1112  return false;
1113}
1114
1115bool HistoryBackend::AddVisits(const GURL& url,
1116                               const std::vector<VisitInfo>& visits,
1117                               VisitSource visit_source) {
1118  if (db_.get()) {
1119    for (std::vector<VisitInfo>::const_iterator visit = visits.begin();
1120         visit != visits.end(); ++visit) {
1121      if (!AddPageVisit(
1122              url, visit->first, 0, visit->second, visit_source).first) {
1123        return false;
1124      }
1125    }
1126    ScheduleCommit();
1127    return true;
1128  }
1129  return false;
1130}
1131
1132bool HistoryBackend::RemoveVisits(const VisitVector& visits) {
1133  if (!db_.get())
1134    return false;
1135
1136  expirer_.ExpireVisits(visits);
1137  ScheduleCommit();
1138  return true;
1139}
1140
1141bool HistoryBackend::GetVisitsSource(const VisitVector& visits,
1142                                     VisitSourceMap* sources) {
1143  if (!db_.get())
1144    return false;
1145
1146  db_->GetVisitsSource(visits, sources);
1147  return true;
1148}
1149
1150bool HistoryBackend::GetURL(const GURL& url, history::URLRow* url_row) {
1151  if (db_.get())
1152    return db_->GetRowForURL(url, url_row) != 0;
1153  return false;
1154}
1155
1156void HistoryBackend::QueryURL(scoped_refptr<QueryURLRequest> request,
1157                              const GURL& url,
1158                              bool want_visits) {
1159  if (request->canceled())
1160    return;
1161
1162  bool success = false;
1163  URLRow* row = &request->value.a;
1164  VisitVector* visits = &request->value.b;
1165  if (db_.get()) {
1166    if (db_->GetRowForURL(url, row)) {
1167      // Have a row.
1168      success = true;
1169
1170      // Optionally query the visits.
1171      if (want_visits)
1172        db_->GetVisitsForURL(row->id(), visits);
1173    }
1174  }
1175  request->ForwardResult(request->handle(), success, row, visits);
1176}
1177
1178// Segment usage ---------------------------------------------------------------
1179
1180void HistoryBackend::DeleteOldSegmentData() {
1181  if (db_.get())
1182    db_->DeleteSegmentData(Time::Now() -
1183                           TimeDelta::FromDays(kSegmentDataRetention));
1184}
1185
1186void HistoryBackend::QuerySegmentUsage(
1187    scoped_refptr<QuerySegmentUsageRequest> request,
1188    const Time from_time,
1189    int max_result_count) {
1190  if (request->canceled())
1191    return;
1192
1193  if (db_.get()) {
1194    db_->QuerySegmentUsage(from_time, max_result_count, &request->value.get());
1195
1196    // If this is the first time we query segments, invoke
1197    // DeleteOldSegmentData asynchronously. We do this to cleanup old
1198    // entries.
1199    if (!segment_queried_) {
1200      segment_queried_ = true;
1201      MessageLoop::current()->PostTask(
1202          FROM_HERE,
1203          base::Bind(&HistoryBackend::DeleteOldSegmentData, this));
1204    }
1205  }
1206  request->ForwardResult(request->handle(), &request->value.get());
1207}
1208
1209void HistoryBackend::IncreaseSegmentDuration(const GURL& url,
1210                                             base::Time time,
1211                                             base::TimeDelta delta) {
1212  if (!db_.get())
1213    return;
1214
1215  const std::string segment_name(VisitSegmentDatabase::ComputeSegmentName(url));
1216  SegmentID segment_id = db_->GetSegmentNamed(segment_name);
1217  if (!segment_id) {
1218    URLID url_id = db_->GetRowForURL(url, NULL);
1219    if (!url_id)
1220      return;
1221    segment_id = db_->CreateSegment(url_id, segment_name);
1222    if (!segment_id)
1223      return;
1224  }
1225  SegmentDurationID duration_id;
1226  base::TimeDelta total_delta;
1227  if (!db_->GetSegmentDuration(segment_id, time, &duration_id,
1228                               &total_delta)) {
1229    db_->CreateSegmentDuration(segment_id, time, delta);
1230    return;
1231  }
1232  total_delta += delta;
1233  db_->SetSegmentDuration(duration_id, total_delta);
1234}
1235
1236void HistoryBackend::QuerySegmentDuration(
1237    scoped_refptr<QuerySegmentUsageRequest> request,
1238    const base::Time from_time,
1239    int max_result_count) {
1240  if (request->canceled())
1241    return;
1242
1243  if (db_.get()) {
1244    db_->QuerySegmentDuration(from_time, max_result_count,
1245                              &request->value.get());
1246  }
1247  request->ForwardResult(request->handle(), &request->value.get());
1248}
1249
1250// Keyword visits --------------------------------------------------------------
1251
1252void HistoryBackend::SetKeywordSearchTermsForURL(const GURL& url,
1253                                                 TemplateURLID keyword_id,
1254                                                 const string16& term) {
1255  if (!db_.get())
1256    return;
1257
1258  // Get the ID for this URL.
1259  URLRow url_row;
1260  if (!db_->GetRowForURL(url, &url_row)) {
1261    // There is a small possibility the url was deleted before the keyword
1262    // was added. Ignore the request.
1263    return;
1264  }
1265
1266  db_->SetKeywordSearchTermsForURL(url_row.id(), keyword_id, term);
1267
1268  // details is deleted by BroadcastNotifications.
1269  KeywordSearchTermDetails* details = new KeywordSearchTermDetails;
1270  details->url = url;
1271  details->keyword_id = keyword_id;
1272  details->term = term;
1273  BroadcastNotifications(
1274      chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED, details);
1275  ScheduleCommit();
1276}
1277
1278void HistoryBackend::DeleteAllSearchTermsForKeyword(
1279    TemplateURLID keyword_id) {
1280  if (!db_.get())
1281    return;
1282
1283  db_->DeleteAllSearchTermsForKeyword(keyword_id);
1284  // TODO(sky): bug 1168470. Need to move from archive dbs too.
1285  ScheduleCommit();
1286}
1287
1288void HistoryBackend::GetMostRecentKeywordSearchTerms(
1289    scoped_refptr<GetMostRecentKeywordSearchTermsRequest> request,
1290    TemplateURLID keyword_id,
1291    const string16& prefix,
1292    int max_count) {
1293  if (request->canceled())
1294    return;
1295
1296  if (db_.get()) {
1297    db_->GetMostRecentKeywordSearchTerms(keyword_id, prefix, max_count,
1298                                         &(request->value));
1299  }
1300  request->ForwardResult(request->handle(), &request->value);
1301}
1302
1303// Downloads -------------------------------------------------------------------
1304
1305void HistoryBackend::GetNextDownloadId(int* id) {
1306  if (db_.get())
1307    *id = db_->next_download_id();
1308}
1309
1310// Get all the download entries from the database.
1311void HistoryBackend::QueryDownloads(std::vector<DownloadRow>* rows) {
1312  if (db_.get())
1313    db_->QueryDownloads(rows);
1314}
1315
1316// Update a particular download entry.
1317void HistoryBackend::UpdateDownload(const history::DownloadRow& data) {
1318  if (!db_.get())
1319    return;
1320  db_->UpdateDownload(data);
1321  ScheduleCommit();
1322}
1323
1324void HistoryBackend::CreateDownload(const history::DownloadRow& history_info,
1325                                    int64* db_handle) {
1326  if (!db_.get())
1327    return;
1328  *db_handle = db_->CreateDownload(history_info);
1329  ScheduleCommit();
1330}
1331
1332void HistoryBackend::RemoveDownloads(const std::set<int64>& handles) {
1333  if (!db_.get())
1334    return;
1335  int downloads_count_before = db_->CountDownloads();
1336  base::TimeTicks started_removing = base::TimeTicks::Now();
1337  // HistoryBackend uses a long-running Transaction that is committed
1338  // periodically, so this loop doesn't actually hit the disk too hard.
1339  for (std::set<int64>::const_iterator it = handles.begin();
1340       it != handles.end(); ++it) {
1341    db_->RemoveDownload(*it);
1342  }
1343  base::TimeTicks finished_removing = base::TimeTicks::Now();
1344  int downloads_count_after = db_->CountDownloads();
1345  int num_downloads_deleted = downloads_count_before - downloads_count_after;
1346  if (num_downloads_deleted >= 0) {
1347    UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCount",
1348                         num_downloads_deleted);
1349    base::TimeDelta micros = (1000 * (finished_removing - started_removing));
1350    UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTime", micros);
1351    if (num_downloads_deleted > 0) {
1352      UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTimePerRecord",
1353                          (1000 * micros) / num_downloads_deleted);
1354    }
1355  }
1356  int num_downloads_not_deleted = handles.size() - num_downloads_deleted;
1357  if (num_downloads_not_deleted >= 0) {
1358    UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCountNotRemoved",
1359                         num_downloads_not_deleted);
1360  }
1361  ScheduleCommit();
1362}
1363
1364void HistoryBackend::QueryHistory(scoped_refptr<QueryHistoryRequest> request,
1365                                  const string16& text_query,
1366                                  const QueryOptions& options) {
1367  if (request->canceled())
1368    return;
1369
1370  TimeTicks beginning_time = TimeTicks::Now();
1371
1372  if (db_.get()) {
1373    if (text_query.empty()) {
1374      // Basic history query for the main database.
1375      QueryHistoryBasic(db_.get(), db_.get(), options, &request->value);
1376
1377      // Now query the archived database. This is a bit tricky because we don't
1378      // want to query it if the queried time range isn't going to find anything
1379      // in it.
1380      // TODO(brettw) bug 1171036: do blimpie querying for the archived database
1381      // as well.
1382      // if (archived_db_.get() &&
1383      //     expirer_.GetCurrentArchiveTime() - TimeDelta::FromDays(7)) {
1384    } else {
1385      // Full text history query.
1386      QueryHistoryFTS(text_query, options, &request->value);
1387    }
1388  }
1389
1390  request->ForwardResult(request->handle(), &request->value);
1391
1392  UMA_HISTOGRAM_TIMES("History.QueryHistory",
1393                      TimeTicks::Now() - beginning_time);
1394}
1395
1396// Basic time-based querying of history.
1397void HistoryBackend::QueryHistoryBasic(URLDatabase* url_db,
1398                                       VisitDatabase* visit_db,
1399                                       const QueryOptions& options,
1400                                       QueryResults* result) {
1401  // First get all visits.
1402  VisitVector visits;
1403  bool has_more_results = visit_db->GetVisibleVisitsInRange(options, &visits);
1404  DCHECK(static_cast<int>(visits.size()) <= options.EffectiveMaxCount());
1405
1406  // Now add them and the URL rows to the results.
1407  URLResult url_result;
1408  for (size_t i = 0; i < visits.size(); i++) {
1409    const VisitRow visit = visits[i];
1410
1411    // Add a result row for this visit, get the URL info from the DB.
1412    if (!url_db->GetURLRow(visit.url_id, &url_result)) {
1413      VLOG(0) << "Failed to get id " << visit.url_id
1414              << " from history.urls.";
1415      continue;  // DB out of sync and URL doesn't exist, try to recover.
1416    }
1417
1418    if (!url_result.url().is_valid()) {
1419      VLOG(0) << "Got invalid URL from history.urls with id "
1420              << visit.url_id << ":  "
1421              << url_result.url().possibly_invalid_spec();
1422      continue;  // Don't report invalid URLs in case of corruption.
1423    }
1424
1425    // The archived database may be out of sync with respect to starring,
1426    // titles, last visit date, etc. Therefore, we query the main DB if the
1427    // current URL database is not the main one.
1428    if (url_db == db_.get()) {
1429      // Currently querying the archived DB, update with the main database to
1430      // catch any interesting stuff. This will update it if it exists in the
1431      // main DB, and do nothing otherwise.
1432      db_->GetRowForURL(url_result.url(), &url_result);
1433    }
1434
1435    url_result.set_visit_time(visit.visit_time);
1436
1437    // Set whether the visit was blocked for a managed user by looking at the
1438    // transition type.
1439    url_result.set_blocked_visit(
1440        (visit.transition & content::PAGE_TRANSITION_BLOCKED) != 0);
1441
1442    // We don't set any of the query-specific parts of the URLResult, since
1443    // snippets and stuff don't apply to basic querying.
1444    result->AppendURLBySwapping(&url_result);
1445  }
1446
1447  if (!has_more_results && options.begin_time <= first_recorded_time_)
1448    result->set_reached_beginning(true);
1449}
1450
1451void HistoryBackend::QueryHistoryFTS(const string16& text_query,
1452                                     const QueryOptions& options,
1453                                     QueryResults* result) {
1454  if (!text_database_.get())
1455    return;
1456
1457  // Full text query, first get all the FTS results in the time range.
1458  std::vector<TextDatabase::Match> fts_matches;
1459  Time first_time_searched;
1460  text_database_->GetTextMatches(text_query, options,
1461                                 &fts_matches, &first_time_searched);
1462
1463  URLQuerier querier(db_.get(), archived_db_.get(), true);
1464
1465  // Now get the row and visit information for each one.
1466  URLResult url_result;  // Declare outside loop to prevent re-construction.
1467  for (size_t i = 0; i < fts_matches.size(); i++) {
1468    if (options.max_count != 0 &&
1469        static_cast<int>(result->size()) >= options.max_count)
1470      break;  // Got too many items.
1471
1472    // Get the URL, querying the main and archived databases as necessary. If
1473    // this is not found, the history and full text search databases are out
1474    // of sync and we give up with this result.
1475    if (!querier.GetRowForURL(fts_matches[i].url, &url_result))
1476      continue;
1477
1478    if (!url_result.url().is_valid())
1479      continue;  // Don't report invalid URLs in case of corruption.
1480
1481    // Copy over the FTS stuff that the URLDatabase doesn't know about.
1482    // We do this with swap() to avoid copying, since we know we don't
1483    // need the original any more. Note that we override the title with the
1484    // one from FTS, since that will match the title_match_positions (the
1485    // FTS title and the history DB title may differ).
1486    url_result.set_title(fts_matches[i].title);
1487    url_result.title_match_positions_.swap(
1488        fts_matches[i].title_match_positions);
1489    url_result.snippet_.Swap(&fts_matches[i].snippet);
1490
1491    // The visit time also comes from the full text search database. Since it
1492    // has the time, we can avoid an extra query of the visits table.
1493    url_result.set_visit_time(fts_matches[i].time);
1494
1495    // Add it to the vector, this will clear our |url_row| object as a
1496    // result of the swap.
1497    result->AppendURLBySwapping(&url_result);
1498  }
1499
1500  if (first_time_searched <= first_recorded_time_)
1501    result->set_reached_beginning(true);
1502}
1503
1504// Frontend to GetMostRecentRedirectsFrom from the history thread.
1505void HistoryBackend::QueryRedirectsFrom(
1506    scoped_refptr<QueryRedirectsRequest> request,
1507    const GURL& url) {
1508  if (request->canceled())
1509    return;
1510  bool success = GetMostRecentRedirectsFrom(url, &request->value);
1511  request->ForwardResult(request->handle(), url, success, &request->value);
1512}
1513
1514void HistoryBackend::QueryRedirectsTo(
1515    scoped_refptr<QueryRedirectsRequest> request,
1516    const GURL& url) {
1517  if (request->canceled())
1518    return;
1519  bool success = GetMostRecentRedirectsTo(url, &request->value);
1520  request->ForwardResult(request->handle(), url, success, &request->value);
1521}
1522
1523void HistoryBackend::GetVisibleVisitCountToHost(
1524    scoped_refptr<GetVisibleVisitCountToHostRequest> request,
1525    const GURL& url) {
1526  if (request->canceled())
1527    return;
1528  int count = 0;
1529  Time first_visit;
1530  const bool success = db_.get() &&
1531      db_->GetVisibleVisitCountToHost(url, &count, &first_visit);
1532  request->ForwardResult(request->handle(), success, count, first_visit);
1533}
1534
1535void HistoryBackend::QueryTopURLsAndRedirects(
1536    scoped_refptr<QueryTopURLsAndRedirectsRequest> request,
1537    int result_count) {
1538  if (request->canceled())
1539    return;
1540
1541  if (!db_.get()) {
1542    request->ForwardResult(request->handle(), false, NULL, NULL);
1543    return;
1544  }
1545
1546  std::vector<GURL>* top_urls = &request->value.a;
1547  history::RedirectMap* redirects = &request->value.b;
1548
1549  ScopedVector<PageUsageData> data;
1550  db_->QuerySegmentUsage(base::Time::Now() - base::TimeDelta::FromDays(90),
1551      result_count, &data.get());
1552
1553  for (size_t i = 0; i < data.size(); ++i) {
1554    top_urls->push_back(data[i]->GetURL());
1555    RefCountedVector<GURL>* list = new RefCountedVector<GURL>;
1556    GetMostRecentRedirectsFrom(top_urls->back(), &list->data);
1557    (*redirects)[top_urls->back()] = list;
1558  }
1559
1560  request->ForwardResult(request->handle(), true, top_urls, redirects);
1561}
1562
1563// Will replace QueryTopURLsAndRedirectsRequest.
1564void HistoryBackend::QueryMostVisitedURLs(
1565    scoped_refptr<QueryMostVisitedURLsRequest> request,
1566    int result_count,
1567    int days_back) {
1568  if (request->canceled())
1569    return;
1570
1571  if (!db_.get()) {
1572    // No History Database - return an empty list.
1573    request->ForwardResult(request->handle(), MostVisitedURLList());
1574    return;
1575  }
1576
1577  MostVisitedURLList* result = &request->value;
1578  QueryMostVisitedURLsImpl(result_count, days_back, result);
1579  request->ForwardResult(request->handle(), *result);
1580}
1581
1582void HistoryBackend::QueryFilteredURLs(
1583      scoped_refptr<QueryFilteredURLsRequest> request,
1584      int result_count,
1585      const history::VisitFilter& filter,
1586      bool extended_info)  {
1587  if (request->canceled())
1588    return;
1589
1590  base::Time request_start = base::Time::Now();
1591
1592  if (!db_.get()) {
1593    // No History Database - return an empty list.
1594    request->ForwardResult(request->handle(), FilteredURLList());
1595    return;
1596  }
1597
1598  VisitVector visits;
1599  db_->GetDirectVisitsDuringTimes(filter, 0, &visits);
1600
1601  std::map<URLID, double> score_map;
1602  for (size_t i = 0; i < visits.size(); ++i) {
1603    score_map[visits[i].url_id] += filter.GetVisitScore(visits[i]);
1604  }
1605
1606  // TODO(georgey): experiment with visit_segment database granularity (it is
1607  // currently 24 hours) to use it directly instead of using visits database,
1608  // which is considerably slower.
1609  ScopedVector<PageUsageData> data;
1610  data.reserve(score_map.size());
1611  for (std::map<URLID, double>::iterator it = score_map.begin();
1612       it != score_map.end(); ++it) {
1613    PageUsageData* pud = new PageUsageData(it->first);
1614    pud->SetScore(it->second);
1615    data.push_back(pud);
1616  }
1617
1618  // Limit to the top |result_count| results.
1619  std::sort(data.begin(), data.end(), PageUsageData::Predicate);
1620  if (result_count && implicit_cast<int>(data.size()) > result_count)
1621    data.resize(result_count);
1622
1623  for (size_t i = 0; i < data.size(); ++i) {
1624    URLRow info;
1625    if (db_->GetURLRow(data[i]->GetID(), &info)) {
1626      data[i]->SetURL(info.url());
1627      data[i]->SetTitle(info.title());
1628    }
1629  }
1630
1631  FilteredURLList& result = request->value;
1632  for (size_t i = 0; i < data.size(); ++i) {
1633    PageUsageData* current_data = data[i];
1634    FilteredURL url(*current_data);
1635
1636    if (extended_info) {
1637      VisitVector visits;
1638      db_->GetVisitsForURL(current_data->GetID(), &visits);
1639      if (visits.size() > 0) {
1640        url.extended_info.total_visits = visits.size();
1641        for (size_t i = 0; i < visits.size(); ++i) {
1642          url.extended_info.duration_opened +=
1643              visits[i].visit_duration.InSeconds();
1644          if (visits[i].visit_time > url.extended_info.last_visit_time) {
1645            url.extended_info.last_visit_time = visits[i].visit_time;
1646          }
1647        }
1648        // TODO(macourteau): implement the url.extended_info.visits stat.
1649      }
1650    }
1651    result.push_back(url);
1652  }
1653
1654  int delta_time = std::max(1, std::min(999,
1655      static_cast<int>((base::Time::Now() - request_start).InMilliseconds())));
1656  STATIC_HISTOGRAM_POINTER_BLOCK(
1657      "NewTabPage.SuggestedSitesLoadTime",
1658      Add(delta_time),
1659      base::LinearHistogram::FactoryGet("NewTabPage.SuggestedSitesLoadTime",
1660          1, 1000, 100, base::Histogram::kUmaTargetedHistogramFlag));
1661
1662  request->ForwardResult(request->handle(), result);
1663}
1664
1665void HistoryBackend::QueryMostVisitedURLsImpl(int result_count,
1666                                              int days_back,
1667                                              MostVisitedURLList* result) {
1668  if (!db_.get())
1669    return;
1670
1671  ScopedVector<PageUsageData> data;
1672  db_->QuerySegmentUsage(base::Time::Now() -
1673                         base::TimeDelta::FromDays(days_back),
1674                         result_count, &data.get());
1675
1676  for (size_t i = 0; i < data.size(); ++i) {
1677    PageUsageData* current_data = data[i];
1678    RedirectList redirects;
1679    GetMostRecentRedirectsFrom(current_data->GetURL(), &redirects);
1680    MostVisitedURL url = MakeMostVisitedURL(*current_data, redirects);
1681    result->push_back(url);
1682  }
1683}
1684
1685void HistoryBackend::GetRedirectsFromSpecificVisit(
1686    VisitID cur_visit, history::RedirectList* redirects) {
1687  // Follow any redirects from the given visit and add them to the list.
1688  // It *should* be impossible to get a circular chain here, but we check
1689  // just in case to avoid infinite loops.
1690  GURL cur_url;
1691  std::set<VisitID> visit_set;
1692  visit_set.insert(cur_visit);
1693  while (db_->GetRedirectFromVisit(cur_visit, &cur_visit, &cur_url)) {
1694    if (visit_set.find(cur_visit) != visit_set.end()) {
1695      NOTREACHED() << "Loop in visit chain, giving up";
1696      return;
1697    }
1698    visit_set.insert(cur_visit);
1699    redirects->push_back(cur_url);
1700  }
1701}
1702
1703void HistoryBackend::GetRedirectsToSpecificVisit(
1704    VisitID cur_visit,
1705    history::RedirectList* redirects) {
1706  // Follow redirects going to cur_visit. These are added to |redirects| in
1707  // the order they are found. If a redirect chain looks like A -> B -> C and
1708  // |cur_visit| = C, redirects will be {B, A} in that order.
1709  if (!db_.get())
1710    return;
1711
1712  GURL cur_url;
1713  std::set<VisitID> visit_set;
1714  visit_set.insert(cur_visit);
1715  while (db_->GetRedirectToVisit(cur_visit, &cur_visit, &cur_url)) {
1716    if (visit_set.find(cur_visit) != visit_set.end()) {
1717      NOTREACHED() << "Loop in visit chain, giving up";
1718      return;
1719    }
1720    visit_set.insert(cur_visit);
1721    redirects->push_back(cur_url);
1722  }
1723}
1724
1725bool HistoryBackend::GetMostRecentRedirectsFrom(
1726    const GURL& from_url,
1727    history::RedirectList* redirects) {
1728  redirects->clear();
1729  if (!db_.get())
1730    return false;
1731
1732  URLID from_url_id = db_->GetRowForURL(from_url, NULL);
1733  VisitID cur_visit = db_->GetMostRecentVisitForURL(from_url_id, NULL);
1734  if (!cur_visit)
1735    return false;  // No visits for URL.
1736
1737  GetRedirectsFromSpecificVisit(cur_visit, redirects);
1738  return true;
1739}
1740
1741bool HistoryBackend::GetMostRecentRedirectsTo(
1742    const GURL& to_url,
1743    history::RedirectList* redirects) {
1744  redirects->clear();
1745  if (!db_.get())
1746    return false;
1747
1748  URLID to_url_id = db_->GetRowForURL(to_url, NULL);
1749  VisitID cur_visit = db_->GetMostRecentVisitForURL(to_url_id, NULL);
1750  if (!cur_visit)
1751    return false;  // No visits for URL.
1752
1753  GetRedirectsToSpecificVisit(cur_visit, redirects);
1754  return true;
1755}
1756
1757void HistoryBackend::ScheduleAutocomplete(HistoryURLProvider* provider,
1758                                          HistoryURLProviderParams* params) {
1759  // ExecuteWithDB should handle the NULL database case.
1760  provider->ExecuteWithDB(this, db_.get(), params);
1761}
1762
1763void HistoryBackend::SetPageContents(const GURL& url,
1764                                     const string16& contents) {
1765  // This is histogrammed in the text database manager.
1766  if (!text_database_.get())
1767    return;
1768  text_database_->AddPageContents(url, contents);
1769}
1770
1771void HistoryBackend::SetPageThumbnail(
1772    const GURL& url,
1773    const gfx::Image* thumbnail,
1774    const ThumbnailScore& score) {
1775  if (!db_.get() || !thumbnail_db_.get())
1776    return;
1777
1778  URLRow url_row;
1779  URLID url_id = db_->GetRowForURL(url, &url_row);
1780  if (url_id) {
1781    thumbnail_db_->SetPageThumbnail(url, url_id, thumbnail, score,
1782                                    url_row.last_visit());
1783  }
1784
1785  ScheduleCommit();
1786}
1787
1788void HistoryBackend::GetPageThumbnail(
1789    scoped_refptr<GetPageThumbnailRequest> request,
1790    const GURL& page_url) {
1791  if (request->canceled())
1792    return;
1793
1794  scoped_refptr<base::RefCountedBytes> data;
1795  GetPageThumbnailDirectly(page_url, &data);
1796
1797  request->ForwardResult(request->handle(), data);
1798}
1799
1800void HistoryBackend::GetPageThumbnailDirectly(
1801    const GURL& page_url,
1802    scoped_refptr<base::RefCountedBytes>* data) {
1803  if (thumbnail_db_.get()) {
1804    *data = new base::RefCountedBytes;
1805
1806    // Time the result.
1807    TimeTicks beginning_time = TimeTicks::Now();
1808
1809    history::RedirectList redirects;
1810    URLID url_id;
1811    bool success = false;
1812
1813    // If there are some redirects, try to get a thumbnail from the last
1814    // redirect destination.
1815    if (GetMostRecentRedirectsFrom(page_url, &redirects) &&
1816        !redirects.empty()) {
1817      if ((url_id = db_->GetRowForURL(redirects.back(), NULL)))
1818        success = thumbnail_db_->GetPageThumbnail(url_id, &(*data)->data());
1819    }
1820
1821    // If we don't have a thumbnail from redirects, try the URL directly.
1822    if (!success) {
1823      if ((url_id = db_->GetRowForURL(page_url, NULL)))
1824        success = thumbnail_db_->GetPageThumbnail(url_id, &(*data)->data());
1825    }
1826
1827    // In this rare case, we start to mine the older redirect sessions
1828    // from the visit table to try to find a thumbnail.
1829    if (!success) {
1830      success = GetThumbnailFromOlderRedirect(page_url, &(*data)->data());
1831    }
1832
1833    if (!success)
1834      *data = NULL;  // This will tell the callback there was an error.
1835
1836    UMA_HISTOGRAM_TIMES("History.GetPageThumbnail",
1837                        TimeTicks::Now() - beginning_time);
1838  }
1839}
1840
1841void HistoryBackend::MigrateThumbnailsDatabase() {
1842  // If there is no History DB, we can't record that the migration was done.
1843  // It will be recorded on the next run.
1844  if (db_.get()) {
1845    // If there is no thumbnail DB, we can still record a successful migration.
1846    if (thumbnail_db_.get()) {
1847      thumbnail_db_->RenameAndDropThumbnails(GetThumbnailFileName(),
1848                                             GetFaviconsFileName());
1849    }
1850    db_->ThumbnailMigrationDone();
1851  }
1852}
1853
1854bool HistoryBackend::GetThumbnailFromOlderRedirect(
1855    const GURL& page_url,
1856    std::vector<unsigned char>* data) {
1857  // Look at a few previous visit sessions.
1858  VisitVector older_sessions;
1859  URLID page_url_id = db_->GetRowForURL(page_url, NULL);
1860  static const int kVisitsToSearchForThumbnail = 4;
1861  db_->GetMostRecentVisitsForURL(
1862      page_url_id, kVisitsToSearchForThumbnail, &older_sessions);
1863
1864  // Iterate across all those previous visits, and see if any of the
1865  // final destinations of those redirect chains have a good thumbnail
1866  // for us.
1867  bool success = false;
1868  for (VisitVector::const_iterator it = older_sessions.begin();
1869       !success && it != older_sessions.end(); ++it) {
1870    history::RedirectList redirects;
1871    if (it->visit_id) {
1872      GetRedirectsFromSpecificVisit(it->visit_id, &redirects);
1873
1874      if (!redirects.empty()) {
1875        URLID url_id;
1876        if ((url_id = db_->GetRowForURL(redirects.back(), NULL)))
1877          success = thumbnail_db_->GetPageThumbnail(url_id, data);
1878      }
1879    }
1880  }
1881
1882  return success;
1883}
1884
1885void HistoryBackend::GetFavicons(
1886    const std::vector<GURL>& icon_urls,
1887    int icon_types,
1888    int desired_size_in_dip,
1889    const std::vector<ui::ScaleFactor>& desired_scale_factors,
1890    std::vector<FaviconBitmapResult>* bitmap_results) {
1891  UpdateFaviconMappingsAndFetchImpl(NULL, icon_urls, icon_types,
1892                                    desired_size_in_dip, desired_scale_factors,
1893                                    bitmap_results);
1894}
1895
1896void HistoryBackend::GetFaviconsForURL(
1897    const GURL& page_url,
1898    int icon_types,
1899    int desired_size_in_dip,
1900    const std::vector<ui::ScaleFactor>& desired_scale_factors,
1901    std::vector<FaviconBitmapResult>* bitmap_results) {
1902  DCHECK(bitmap_results);
1903  GetFaviconsFromDB(page_url, icon_types, desired_size_in_dip,
1904                    desired_scale_factors, bitmap_results);
1905}
1906
1907void HistoryBackend::GetFaviconForID(
1908    FaviconID favicon_id,
1909    int desired_size_in_dip,
1910    ui::ScaleFactor desired_scale_factor,
1911    std::vector<FaviconBitmapResult>* bitmap_results) {
1912  std::vector<FaviconID> favicon_ids;
1913  favicon_ids.push_back(favicon_id);
1914  std::vector<ui::ScaleFactor> desired_scale_factors;
1915  desired_scale_factors.push_back(desired_scale_factor);
1916
1917  // Get results from DB.
1918  GetFaviconBitmapResultsForBestMatch(favicon_ids,
1919                                      desired_size_in_dip,
1920                                      desired_scale_factors,
1921                                      bitmap_results);
1922}
1923
1924void HistoryBackend::UpdateFaviconMappingsAndFetch(
1925    const GURL& page_url,
1926    const std::vector<GURL>& icon_urls,
1927    int icon_types,
1928    int desired_size_in_dip,
1929    const std::vector<ui::ScaleFactor>& desired_scale_factors,
1930    std::vector<FaviconBitmapResult>* bitmap_results) {
1931  UpdateFaviconMappingsAndFetchImpl(&page_url, icon_urls, icon_types,
1932                                    desired_size_in_dip, desired_scale_factors,
1933                                    bitmap_results);
1934}
1935
1936void HistoryBackend::MergeFavicon(
1937    const GURL& page_url,
1938    const GURL& icon_url,
1939    history::IconType icon_type,
1940    scoped_refptr<base::RefCountedMemory> bitmap_data,
1941    const gfx::Size& pixel_size) {
1942  if (!thumbnail_db_.get() || !db_.get())
1943    return;
1944
1945  FaviconID favicon_id = thumbnail_db_->GetFaviconIDForFaviconURL(icon_url,
1946      icon_type, NULL);
1947
1948  if (!favicon_id) {
1949    // There is no favicon at |icon_url|, create it.
1950    favicon_id = thumbnail_db_->AddFavicon(icon_url, icon_type,
1951                                           GetDefaultFaviconSizes());
1952  }
1953
1954  std::vector<FaviconBitmapIDSize> bitmap_id_sizes;
1955  thumbnail_db_->GetFaviconBitmapIDSizes(favicon_id, &bitmap_id_sizes);
1956
1957  // If there is already a favicon bitmap of |pixel_size| at |icon_url|,
1958  // replace it.
1959  bool bitmap_identical = false;
1960  bool replaced_bitmap = false;
1961  for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) {
1962    if (bitmap_id_sizes[i].pixel_size == pixel_size) {
1963      if (IsFaviconBitmapDataEqual(bitmap_id_sizes[i].bitmap_id, bitmap_data)) {
1964        thumbnail_db_->SetFaviconBitmapLastUpdateTime(
1965            bitmap_id_sizes[i].bitmap_id, base::Time::Now());
1966        bitmap_identical = true;
1967      } else {
1968        thumbnail_db_->SetFaviconBitmap(bitmap_id_sizes[i].bitmap_id,
1969            bitmap_data, base::Time::Now());
1970        replaced_bitmap = true;
1971      }
1972      break;
1973    }
1974  }
1975
1976  // Create a vector of the pixel sizes of the favicon bitmaps currently at
1977  // |icon_url|.
1978  std::vector<gfx::Size> favicon_sizes;
1979  for (size_t i = 0; i < bitmap_id_sizes.size(); ++i)
1980    favicon_sizes.push_back(bitmap_id_sizes[i].pixel_size);
1981
1982  if (!replaced_bitmap && !bitmap_identical) {
1983    // Set the preexisting favicon bitmaps as expired as the preexisting favicon
1984    // bitmaps are not consistent with the merged in data.
1985    thumbnail_db_->SetFaviconOutOfDate(favicon_id);
1986
1987    // Delete an arbitrary favicon bitmap to avoid going over the limit of
1988    // |kMaxFaviconBitmapsPerIconURL|.
1989    if (bitmap_id_sizes.size() >= kMaxFaviconBitmapsPerIconURL) {
1990      thumbnail_db_->DeleteFaviconBitmap(bitmap_id_sizes[0].bitmap_id);
1991      favicon_sizes.erase(favicon_sizes.begin());
1992    }
1993    thumbnail_db_->AddFaviconBitmap(favicon_id, bitmap_data, base::Time::Now(),
1994                                    pixel_size);
1995    favicon_sizes.push_back(pixel_size);
1996  }
1997
1998  // A site may have changed the favicons that it uses for |page_url|.
1999  // Example Scenario:
2000  //   page_url = news.google.com
2001  //   Intial State: www.google.com/favicon.ico 16x16, 32x32
2002  //   MergeFavicon(news.google.com, news.google.com/news_specific.ico, ...,
2003  //                ..., 16x16)
2004  //
2005  // Difficulties:
2006  // 1. Sync requires that a call to GetFaviconsForURL() returns the
2007  //    |bitmap_data| passed into MergeFavicon().
2008  //    - It is invalid for the 16x16 bitmap for www.google.com/favicon.ico to
2009  //      stay mapped to news.google.com because it would be unclear which 16x16
2010  //      bitmap should be returned via GetFaviconsForURL().
2011  //
2012  // 2. www.google.com/favicon.ico may be mapped to more than just
2013  //    news.google.com (eg www.google.com).
2014  //    - The 16x16 bitmap cannot be deleted from www.google.com/favicon.ico
2015  //
2016  // To resolve these problems, we copy all of the favicon bitmaps previously
2017  // mapped to news.google.com (|page_url|) and add them to the favicon at
2018  // news.google.com/news_specific.ico (|icon_url|). The favicon sizes for
2019  // |icon_url| are set to default to indicate that |icon_url| has incomplete
2020  // / incorrect data.
2021  // Difficlty 1: All but news.google.com/news_specific.ico are unmapped from
2022  //              news.google.com
2023  // Difficulty 2: The favicon bitmaps for www.google.com/favicon.ico are not
2024  //               modified.
2025
2026  std::vector<IconMapping> icon_mappings;
2027  thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_type, &icon_mappings);
2028
2029  // Copy the favicon bitmaps mapped to |page_url| to the favicon at |icon_url|
2030  // till the limit of |kMaxFaviconBitmapsPerIconURL| is reached.
2031  for (size_t i = 0; i < icon_mappings.size(); ++i) {
2032    if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL)
2033      break;
2034
2035    if (icon_mappings[i].icon_url == icon_url)
2036      continue;
2037
2038    std::vector<FaviconBitmap> bitmaps_to_copy;
2039    thumbnail_db_->GetFaviconBitmaps(icon_mappings[i].icon_id,
2040                                     &bitmaps_to_copy);
2041    for (size_t j = 0; j < bitmaps_to_copy.size(); ++j) {
2042      // Do not add a favicon bitmap at a pixel size for which there is already
2043      // a favicon bitmap mapped to |icon_url|. The one there is more correct
2044      // and having multiple equally sized favicon bitmaps for |page_url| is
2045      // ambiguous in terms of GetFaviconsForURL().
2046      std::vector<gfx::Size>::iterator it = std::find(favicon_sizes.begin(),
2047          favicon_sizes.end(), bitmaps_to_copy[j].pixel_size);
2048      if (it != favicon_sizes.end())
2049        continue;
2050
2051      // Add the favicon bitmap as expired as it is not consistent with the
2052      // merged in data.
2053      thumbnail_db_->AddFaviconBitmap(favicon_id,
2054          bitmaps_to_copy[j].bitmap_data, base::Time(),
2055          bitmaps_to_copy[j].pixel_size);
2056      favicon_sizes.push_back(bitmaps_to_copy[j].pixel_size);
2057
2058      if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL)
2059        break;
2060    }
2061  }
2062
2063  // Update the favicon mappings such that only |icon_url| is mapped to
2064  // |page_url|.
2065  bool mapping_changed = false;
2066  if (icon_mappings.size() != 1 || icon_mappings[0].icon_url != icon_url) {
2067    std::vector<FaviconID> favicon_ids;
2068    favicon_ids.push_back(favicon_id);
2069    SetFaviconMappingsForPageAndRedirects(page_url, icon_type, favicon_ids);
2070    mapping_changed = true;
2071  }
2072
2073  if (mapping_changed || !bitmap_identical)
2074    SendFaviconChangedNotificationForPageAndRedirects(page_url);
2075  ScheduleCommit();
2076}
2077
2078void HistoryBackend::SetFavicons(
2079    const GURL& page_url,
2080    IconType icon_type,
2081    const std::vector<FaviconBitmapData>& favicon_bitmap_data) {
2082  if (!thumbnail_db_.get() || !db_.get())
2083    return;
2084
2085  DCHECK(ValidateSetFaviconsParams(favicon_bitmap_data));
2086
2087  // Build map of FaviconBitmapData for each icon url.
2088  typedef std::map<GURL, std::vector<FaviconBitmapData> >
2089      BitmapDataByIconURL;
2090  BitmapDataByIconURL grouped_by_icon_url;
2091  for (size_t i = 0; i < favicon_bitmap_data.size(); ++i) {
2092    const GURL& icon_url = favicon_bitmap_data[i].icon_url;
2093    grouped_by_icon_url[icon_url].push_back(favicon_bitmap_data[i]);
2094  }
2095
2096  // Track whether the method modifies or creates any favicon bitmaps, favicons
2097  // or icon mappings.
2098  bool data_modified = false;
2099
2100  std::vector<FaviconID> icon_ids;
2101  for (BitmapDataByIconURL::const_iterator it = grouped_by_icon_url.begin();
2102       it != grouped_by_icon_url.end(); ++it) {
2103    const GURL& icon_url = it->first;
2104    FaviconID icon_id =
2105        thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type, NULL);
2106
2107    if (!icon_id) {
2108      // TODO(pkotwicz): Remove the favicon sizes attribute from
2109      // ThumbnailDatabase::AddFavicon().
2110      icon_id = thumbnail_db_->AddFavicon(icon_url, icon_type,
2111                                          GetDefaultFaviconSizes());
2112      data_modified = true;
2113    }
2114    icon_ids.push_back(icon_id);
2115
2116    if (!data_modified)
2117      SetFaviconBitmaps(icon_id, it->second, &data_modified);
2118    else
2119      SetFaviconBitmaps(icon_id, it->second, NULL);
2120  }
2121
2122  data_modified |=
2123    SetFaviconMappingsForPageAndRedirects(page_url, icon_type, icon_ids);
2124
2125  if (data_modified) {
2126    // Send notification to the UI as an icon mapping, favicon, or favicon
2127    // bitmap was changed by this function.
2128    SendFaviconChangedNotificationForPageAndRedirects(page_url);
2129  }
2130  ScheduleCommit();
2131}
2132
2133void HistoryBackend::SetFaviconsOutOfDateForPage(const GURL& page_url) {
2134  std::vector<IconMapping> icon_mappings;
2135
2136  if (!thumbnail_db_.get() ||
2137      !thumbnail_db_->GetIconMappingsForPageURL(page_url,
2138                                                &icon_mappings))
2139    return;
2140
2141  for (std::vector<IconMapping>::iterator m = icon_mappings.begin();
2142       m != icon_mappings.end(); ++m) {
2143    thumbnail_db_->SetFaviconOutOfDate(m->icon_id);
2144  }
2145  ScheduleCommit();
2146}
2147
2148void HistoryBackend::CloneFavicons(const GURL& old_page_url,
2149                                   const GURL& new_page_url) {
2150  if (!thumbnail_db_.get())
2151    return;
2152
2153  // Prevent cross-domain cloning.
2154  if (old_page_url.GetOrigin() != new_page_url.GetOrigin())
2155    return;
2156
2157  thumbnail_db_->CloneIconMappings(old_page_url, new_page_url);
2158  ScheduleCommit();
2159}
2160
2161void HistoryBackend::SetImportedFavicons(
2162    const std::vector<ImportedFaviconUsage>& favicon_usage) {
2163  if (!db_.get() || !thumbnail_db_.get())
2164    return;
2165
2166  Time now = Time::Now();
2167
2168  // Track all URLs that had their favicons set or updated.
2169  std::set<GURL> favicons_changed;
2170
2171  for (size_t i = 0; i < favicon_usage.size(); i++) {
2172    FaviconID favicon_id = thumbnail_db_->GetFaviconIDForFaviconURL(
2173        favicon_usage[i].favicon_url, history::FAVICON, NULL);
2174    if (!favicon_id) {
2175      // This favicon doesn't exist yet, so we create it using the given data.
2176      // TODO(pkotwicz): Pass in real pixel size.
2177      favicon_id = thumbnail_db_->AddFavicon(
2178          favicon_usage[i].favicon_url,
2179          history::FAVICON,
2180          GetDefaultFaviconSizes(),
2181          new base::RefCountedBytes(favicon_usage[i].png_data),
2182          now,
2183          gfx::Size());
2184    }
2185
2186    // Save the mapping from all the URLs to the favicon.
2187    BookmarkService* bookmark_service = GetBookmarkService();
2188    for (std::set<GURL>::const_iterator url = favicon_usage[i].urls.begin();
2189         url != favicon_usage[i].urls.end(); ++url) {
2190      URLRow url_row;
2191      if (!db_->GetRowForURL(*url, &url_row)) {
2192        // If the URL is present as a bookmark, add the url in history to
2193        // save the favicon mapping. This will match with what history db does
2194        // for regular bookmarked URLs with favicons - when history db is
2195        // cleaned, we keep an entry in the db with 0 visits as long as that
2196        // url is bookmarked.
2197        if (bookmark_service && bookmark_service_->IsBookmarked(*url)) {
2198          URLRow url_info(*url);
2199          url_info.set_visit_count(0);
2200          url_info.set_typed_count(0);
2201          url_info.set_last_visit(base::Time());
2202          url_info.set_hidden(false);
2203          db_->AddURL(url_info);
2204          thumbnail_db_->AddIconMapping(*url, favicon_id);
2205          favicons_changed.insert(*url);
2206        }
2207      } else {
2208        if (!thumbnail_db_->GetIconMappingsForPageURL(*url, FAVICON, NULL)) {
2209          // URL is present in history, update the favicon *only* if it is not
2210          // set already.
2211          thumbnail_db_->AddIconMapping(*url, favicon_id);
2212          favicons_changed.insert(*url);
2213        }
2214      }
2215    }
2216  }
2217
2218  if (!favicons_changed.empty()) {
2219    // Send the notification about the changed favicon URLs.
2220    FaviconChangeDetails* changed_details = new FaviconChangeDetails;
2221    changed_details->urls.swap(favicons_changed);
2222    BroadcastNotifications(chrome::NOTIFICATION_FAVICON_CHANGED,
2223                           changed_details);
2224  }
2225}
2226
2227void HistoryBackend::UpdateFaviconMappingsAndFetchImpl(
2228    const GURL* page_url,
2229    const std::vector<GURL>& icon_urls,
2230    int icon_types,
2231    int desired_size_in_dip,
2232    const std::vector<ui::ScaleFactor>& desired_scale_factors,
2233    std::vector<FaviconBitmapResult>* bitmap_results) {
2234  // If |page_url| is specified, |icon_types| must be either a single icon
2235  // type or icon types which are equivalent.
2236  DCHECK(!page_url ||
2237         icon_types == FAVICON ||
2238         icon_types == TOUCH_ICON ||
2239         icon_types == TOUCH_PRECOMPOSED_ICON ||
2240         icon_types == (TOUCH_ICON | TOUCH_PRECOMPOSED_ICON));
2241  bitmap_results->clear();
2242
2243  if (!thumbnail_db_.get()) {
2244    return;
2245  }
2246
2247  std::vector<FaviconID> favicon_ids;
2248
2249  // The icon type for which the mappings will the updated and data will be
2250  // returned.
2251  IconType selected_icon_type = INVALID_ICON;
2252
2253  for (size_t i = 0; i < icon_urls.size(); ++i) {
2254    const GURL& icon_url = icon_urls[i];
2255    IconType icon_type_out;
2256    const FaviconID favicon_id = thumbnail_db_->GetFaviconIDForFaviconURL(
2257        icon_url, icon_types, &icon_type_out);
2258
2259    if (favicon_id) {
2260      // Return and update icon mappings only for the largest icon type. As
2261      // |icon_urls| is not sorted in terms of icon type, clear |favicon_ids|
2262      // if an |icon_url| with a larger icon type is found.
2263      if (icon_type_out > selected_icon_type) {
2264        selected_icon_type = icon_type_out;
2265        favicon_ids.clear();
2266      }
2267      if (icon_type_out == selected_icon_type)
2268        favicon_ids.push_back(favicon_id);
2269    }
2270  }
2271
2272  if (page_url && !favicon_ids.empty()) {
2273    bool mappings_updated =
2274        SetFaviconMappingsForPageAndRedirects(*page_url, selected_icon_type,
2275                                              favicon_ids);
2276    if (mappings_updated) {
2277      SendFaviconChangedNotificationForPageAndRedirects(*page_url);
2278      ScheduleCommit();
2279    }
2280  }
2281
2282  GetFaviconBitmapResultsForBestMatch(favicon_ids, desired_size_in_dip,
2283      desired_scale_factors, bitmap_results);
2284}
2285
2286void HistoryBackend::SetFaviconBitmaps(
2287    FaviconID icon_id,
2288    const std::vector<FaviconBitmapData>& favicon_bitmap_data,
2289    bool* favicon_bitmaps_changed) {
2290  if (favicon_bitmaps_changed)
2291    *favicon_bitmaps_changed = false;
2292
2293  std::vector<FaviconBitmapIDSize> bitmap_id_sizes;
2294  thumbnail_db_->GetFaviconBitmapIDSizes(icon_id, &bitmap_id_sizes);
2295
2296  std::vector<FaviconBitmapData> to_add = favicon_bitmap_data;
2297
2298  for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) {
2299    const gfx::Size& pixel_size = bitmap_id_sizes[i].pixel_size;
2300    std::vector<FaviconBitmapData>::iterator match_it = to_add.end();
2301    for (std::vector<FaviconBitmapData>::iterator it = to_add.begin();
2302         it != to_add.end(); ++it) {
2303      if (it->pixel_size == pixel_size) {
2304        match_it = it;
2305        break;
2306      }
2307    }
2308
2309    FaviconBitmapID bitmap_id = bitmap_id_sizes[i].bitmap_id;
2310    if (match_it == to_add.end()) {
2311      thumbnail_db_->DeleteFaviconBitmap(bitmap_id);
2312
2313      if (favicon_bitmaps_changed)
2314        *favicon_bitmaps_changed = true;
2315    } else {
2316      if (favicon_bitmaps_changed &&
2317          !*favicon_bitmaps_changed &&
2318          IsFaviconBitmapDataEqual(bitmap_id, match_it->bitmap_data)) {
2319        thumbnail_db_->SetFaviconBitmapLastUpdateTime(
2320            bitmap_id, base::Time::Now());
2321      } else {
2322        thumbnail_db_->SetFaviconBitmap(bitmap_id, match_it->bitmap_data,
2323            base::Time::Now());
2324
2325        if (favicon_bitmaps_changed)
2326          *favicon_bitmaps_changed = true;
2327      }
2328      to_add.erase(match_it);
2329    }
2330  }
2331
2332  for (size_t i = 0; i < to_add.size(); ++i) {
2333    thumbnail_db_->AddFaviconBitmap(icon_id, to_add[i].bitmap_data,
2334        base::Time::Now(), to_add[i].pixel_size);
2335
2336    if (favicon_bitmaps_changed)
2337      *favicon_bitmaps_changed = true;
2338  }
2339}
2340
2341bool HistoryBackend::ValidateSetFaviconsParams(
2342    const std::vector<FaviconBitmapData>& favicon_bitmap_data) const {
2343  typedef std::map<GURL, size_t> BitmapsPerIconURL;
2344  BitmapsPerIconURL num_bitmaps_per_icon_url;
2345  for (size_t i = 0; i < favicon_bitmap_data.size(); ++i) {
2346    if (!favicon_bitmap_data[i].bitmap_data.get())
2347      return false;
2348
2349    const GURL& icon_url = favicon_bitmap_data[i].icon_url;
2350    if (!num_bitmaps_per_icon_url.count(icon_url))
2351      num_bitmaps_per_icon_url[icon_url] = 1u;
2352    else
2353      ++num_bitmaps_per_icon_url[icon_url];
2354  }
2355
2356  if (num_bitmaps_per_icon_url.size() > kMaxFaviconsPerPage)
2357    return false;
2358
2359  for (BitmapsPerIconURL::const_iterator it = num_bitmaps_per_icon_url.begin();
2360       it != num_bitmaps_per_icon_url.end(); ++it) {
2361    if (it->second > kMaxFaviconBitmapsPerIconURL)
2362      return false;
2363  }
2364  return true;
2365}
2366
2367bool HistoryBackend::IsFaviconBitmapDataEqual(
2368    FaviconBitmapID bitmap_id,
2369    const scoped_refptr<base::RefCountedMemory>& new_bitmap_data) {
2370  if (!new_bitmap_data.get())
2371    return false;
2372
2373  scoped_refptr<base::RefCountedMemory> original_bitmap_data;
2374  thumbnail_db_->GetFaviconBitmap(bitmap_id,
2375                                  NULL,
2376                                  &original_bitmap_data,
2377                                  NULL);
2378  return new_bitmap_data->Equals(original_bitmap_data);
2379}
2380
2381bool HistoryBackend::GetFaviconsFromDB(
2382    const GURL& page_url,
2383    int icon_types,
2384    int desired_size_in_dip,
2385    const std::vector<ui::ScaleFactor>& desired_scale_factors,
2386    std::vector<FaviconBitmapResult>* favicon_bitmap_results) {
2387  DCHECK(favicon_bitmap_results);
2388  favicon_bitmap_results->clear();
2389
2390  if (!db_.get() || !thumbnail_db_.get())
2391    return false;
2392
2393  // Time the query.
2394  TimeTicks beginning_time = TimeTicks::Now();
2395
2396  // Get FaviconIDs for |page_url| and one of |icon_types|.
2397  std::vector<IconMapping> icon_mappings;
2398  thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_types,
2399                                           &icon_mappings);
2400  std::vector<FaviconID> favicon_ids;
2401  for (size_t i = 0; i < icon_mappings.size(); ++i)
2402    favicon_ids.push_back(icon_mappings[i].icon_id);
2403
2404  // Populate |favicon_bitmap_results| and |icon_url_sizes|.
2405  bool success = GetFaviconBitmapResultsForBestMatch(favicon_ids,
2406      desired_size_in_dip, desired_scale_factors, favicon_bitmap_results);
2407  UMA_HISTOGRAM_TIMES("History.GetFavIconFromDB",  // historical name
2408                      TimeTicks::Now() - beginning_time);
2409  return success && !favicon_bitmap_results->empty();
2410}
2411
2412bool HistoryBackend::GetFaviconBitmapResultsForBestMatch(
2413    const std::vector<FaviconID>& candidate_favicon_ids,
2414    int desired_size_in_dip,
2415    const std::vector<ui::ScaleFactor>& desired_scale_factors,
2416    std::vector<FaviconBitmapResult>* favicon_bitmap_results) {
2417  favicon_bitmap_results->clear();
2418
2419  if (candidate_favicon_ids.empty())
2420    return true;
2421
2422  // Find the FaviconID and the FaviconBitmapIDs which best match
2423  // |desired_size_in_dip| and |desired_scale_factors|.
2424  // TODO(pkotwicz): Select bitmap results from multiple favicons once
2425  // content::FaviconStatus supports multiple icon URLs.
2426  FaviconID best_favicon_id = 0;
2427  std::vector<FaviconBitmapID> best_bitmap_ids;
2428  float highest_score = kSelectFaviconFramesInvalidScore;
2429  for (size_t i = 0; i < candidate_favicon_ids.size(); ++i) {
2430    std::vector<FaviconBitmapIDSize> bitmap_id_sizes;
2431    thumbnail_db_->GetFaviconBitmapIDSizes(candidate_favicon_ids[i],
2432                                           &bitmap_id_sizes);
2433
2434    // Build vector of gfx::Size from |bitmap_id_sizes|.
2435    std::vector<gfx::Size> sizes;
2436    for (size_t j = 0; j < bitmap_id_sizes.size(); ++j)
2437      sizes.push_back(bitmap_id_sizes[j].pixel_size);
2438
2439    std::vector<size_t> candidate_bitmap_indices;
2440    float score = 0;
2441    SelectFaviconFrameIndices(sizes,
2442                              desired_scale_factors,
2443                              desired_size_in_dip,
2444                              &candidate_bitmap_indices,
2445                              &score);
2446    if (score > highest_score) {
2447      highest_score = score;
2448      best_favicon_id = candidate_favicon_ids[i],
2449      best_bitmap_ids.clear();
2450      for (size_t j = 0; j < candidate_bitmap_indices.size(); ++j) {
2451        size_t candidate_index = candidate_bitmap_indices[j];
2452        best_bitmap_ids.push_back(
2453            bitmap_id_sizes[candidate_index].bitmap_id);
2454      }
2455    }
2456  }
2457
2458  // Construct FaviconBitmapResults from |best_favicon_id| and
2459  // |best_bitmap_ids|.
2460  GURL icon_url;
2461  IconType icon_type;
2462  if (!thumbnail_db_->GetFaviconHeader(best_favicon_id, &icon_url,
2463                                       &icon_type, NULL)) {
2464    return false;
2465  }
2466
2467  for (size_t i = 0; i < best_bitmap_ids.size(); ++i) {
2468    base::Time last_updated;
2469    FaviconBitmapResult bitmap_result;
2470    bitmap_result.icon_url = icon_url;
2471    bitmap_result.icon_type = icon_type;
2472    if (!thumbnail_db_->GetFaviconBitmap(best_bitmap_ids[i],
2473                                         &last_updated,
2474                                         &bitmap_result.bitmap_data,
2475                                         &bitmap_result.pixel_size)) {
2476      return false;
2477    }
2478
2479    bitmap_result.expired = (Time::Now() - last_updated) >
2480        TimeDelta::FromDays(kFaviconRefetchDays);
2481    if (bitmap_result.is_valid())
2482      favicon_bitmap_results->push_back(bitmap_result);
2483  }
2484  return true;
2485}
2486
2487bool HistoryBackend::SetFaviconMappingsForPageAndRedirects(
2488    const GURL& page_url,
2489    IconType icon_type,
2490    const std::vector<FaviconID>& icon_ids) {
2491  if (!thumbnail_db_.get())
2492    return false;
2493
2494  // Find all the pages whose favicons we should set, we want to set it for
2495  // all the pages in the redirect chain if it redirected.
2496  history::RedirectList redirects;
2497  GetCachedRecentRedirects(page_url, &redirects);
2498
2499  bool mappings_changed = false;
2500
2501  // Save page <-> favicon associations.
2502  for (history::RedirectList::const_iterator i(redirects.begin());
2503       i != redirects.end(); ++i) {
2504    mappings_changed |= SetFaviconMappingsForPage(*i, icon_type, icon_ids);
2505  }
2506  return mappings_changed;
2507}
2508
2509bool HistoryBackend::SetFaviconMappingsForPage(
2510    const GURL& page_url,
2511    IconType icon_type,
2512    const std::vector<FaviconID>& icon_ids) {
2513  DCHECK_LE(icon_ids.size(), kMaxFaviconsPerPage);
2514  bool mappings_changed = false;
2515
2516  // Two icon types are considered 'equivalent' if one of the icon types is
2517  // TOUCH_ICON and the other is TOUCH_PRECOMPOSED_ICON.
2518  //
2519  // Sets the icon mappings from |page_url| for |icon_type| to the favicons
2520  // with |icon_ids|. Mappings for |page_url| to favicons of type |icon_type|
2521  // whose FaviconID is not in |icon_ids| are removed. All icon mappings for
2522  // |page_url| to favicons of a type equivalent to |icon_type| are removed.
2523  // Remove any favicons which are orphaned as a result of the removal of the
2524  // icon mappings.
2525
2526  std::vector<FaviconID> unmapped_icon_ids = icon_ids;
2527
2528  std::vector<IconMapping> icon_mappings;
2529  thumbnail_db_->GetIconMappingsForPageURL(page_url, &icon_mappings);
2530
2531  for (std::vector<IconMapping>::iterator m = icon_mappings.begin();
2532       m != icon_mappings.end(); ++m) {
2533    std::vector<FaviconID>::iterator icon_id_it = std::find(
2534        unmapped_icon_ids.begin(), unmapped_icon_ids.end(), m->icon_id);
2535
2536    // If the icon mapping already exists, avoid removing it and adding it back.
2537    if (icon_id_it != unmapped_icon_ids.end()) {
2538      unmapped_icon_ids.erase(icon_id_it);
2539      continue;
2540    }
2541
2542    if ((icon_type == TOUCH_ICON && m->icon_type == TOUCH_PRECOMPOSED_ICON) ||
2543        (icon_type == TOUCH_PRECOMPOSED_ICON && m->icon_type == TOUCH_ICON) ||
2544        (icon_type == m->icon_type)) {
2545      thumbnail_db_->DeleteIconMapping(m->mapping_id);
2546
2547      // Removing the icon mapping may have orphaned the associated favicon so
2548      // we must recheck it. This is not super fast, but this case will get
2549      // triggered rarely, since normally a page will always map to the same
2550      // favicon IDs. It will mostly happen for favicons we import.
2551      if (!thumbnail_db_->HasMappingFor(m->icon_id))
2552        thumbnail_db_->DeleteFavicon(m->icon_id);
2553      mappings_changed = true;
2554    }
2555  }
2556
2557  for (size_t i = 0; i < unmapped_icon_ids.size(); ++i) {
2558    thumbnail_db_->AddIconMapping(page_url, unmapped_icon_ids[i]);
2559    mappings_changed = true;
2560  }
2561  return mappings_changed;
2562}
2563
2564void HistoryBackend::GetCachedRecentRedirects(
2565    const GURL& page_url,
2566    history::RedirectList* redirect_list) {
2567  RedirectCache::iterator iter = recent_redirects_.Get(page_url);
2568  if (iter != recent_redirects_.end()) {
2569    *redirect_list = iter->second;
2570
2571    // The redirect chain should have the destination URL as the last item.
2572    DCHECK(!redirect_list->empty());
2573    DCHECK(redirect_list->back() == page_url);
2574  } else {
2575    // No known redirects, construct mock redirect chain containing |page_url|.
2576    redirect_list->push_back(page_url);
2577  }
2578}
2579
2580void HistoryBackend::SendFaviconChangedNotificationForPageAndRedirects(
2581    const GURL& page_url) {
2582  history::RedirectList redirect_list;
2583  GetCachedRecentRedirects(page_url, &redirect_list);
2584
2585  FaviconChangeDetails* changed_details = new FaviconChangeDetails;
2586  for (size_t i = 0; i < redirect_list.size(); ++i)
2587    changed_details->urls.insert(redirect_list[i]);
2588
2589  BroadcastNotifications(chrome::NOTIFICATION_FAVICON_CHANGED,
2590                         changed_details);
2591}
2592
2593void HistoryBackend::Commit() {
2594  if (!db_.get())
2595    return;
2596
2597  // Note that a commit may not actually have been scheduled if a caller
2598  // explicitly calls this instead of using ScheduleCommit. Likewise, we
2599  // may reset the flag written by a pending commit. But this is OK! It
2600  // will merely cause extra commits (which is kind of the idea). We
2601  // could optimize more for this case (we may get two extra commits in
2602  // some cases) but it hasn't been important yet.
2603  CancelScheduledCommit();
2604
2605  db_->CommitTransaction();
2606  DCHECK(db_->transaction_nesting() == 0) << "Somebody left a transaction open";
2607  db_->BeginTransaction();
2608
2609  if (thumbnail_db_.get()) {
2610    thumbnail_db_->CommitTransaction();
2611    DCHECK(thumbnail_db_->transaction_nesting() == 0) <<
2612        "Somebody left a transaction open";
2613    thumbnail_db_->BeginTransaction();
2614  }
2615
2616  if (archived_db_.get()) {
2617    archived_db_->CommitTransaction();
2618    archived_db_->BeginTransaction();
2619  }
2620
2621  if (text_database_.get()) {
2622    text_database_->CommitTransaction();
2623    text_database_->BeginTransaction();
2624  }
2625}
2626
2627void HistoryBackend::ScheduleCommit() {
2628  if (scheduled_commit_.get())
2629    return;
2630  scheduled_commit_ = new CommitLaterTask(this);
2631  MessageLoop::current()->PostDelayedTask(
2632      FROM_HERE,
2633      base::Bind(&CommitLaterTask::RunCommit, scheduled_commit_.get()),
2634      base::TimeDelta::FromSeconds(kCommitIntervalSeconds));
2635}
2636
2637void HistoryBackend::CancelScheduledCommit() {
2638  if (scheduled_commit_) {
2639    scheduled_commit_->Cancel();
2640    scheduled_commit_ = NULL;
2641  }
2642}
2643
2644void HistoryBackend::ProcessDBTaskImpl() {
2645  if (!db_.get()) {
2646    // db went away, release all the refs.
2647    ReleaseDBTasks();
2648    return;
2649  }
2650
2651  // Remove any canceled tasks.
2652  while (!db_task_requests_.empty() && db_task_requests_.front()->canceled()) {
2653    db_task_requests_.front()->Release();
2654    db_task_requests_.pop_front();
2655  }
2656  if (db_task_requests_.empty())
2657    return;
2658
2659  // Run the first task.
2660  HistoryDBTaskRequest* request = db_task_requests_.front();
2661  db_task_requests_.pop_front();
2662  if (request->value->RunOnDBThread(this, db_.get())) {
2663    // The task is done. Notify the callback.
2664    request->ForwardResult();
2665    // We AddRef'd the request before adding, need to release it now.
2666    request->Release();
2667  } else {
2668    // Tasks wants to run some more. Schedule it at the end of current tasks.
2669    db_task_requests_.push_back(request);
2670    // And process it after an invoke later.
2671    MessageLoop::current()->PostTask(
2672        FROM_HERE, base::Bind(&HistoryBackend::ProcessDBTaskImpl, this));
2673  }
2674}
2675
2676void HistoryBackend::ReleaseDBTasks() {
2677  for (std::list<HistoryDBTaskRequest*>::iterator i =
2678       db_task_requests_.begin(); i != db_task_requests_.end(); ++i) {
2679    (*i)->Release();
2680  }
2681  db_task_requests_.clear();
2682}
2683
2684////////////////////////////////////////////////////////////////////////////////
2685//
2686// Generic operations
2687//
2688////////////////////////////////////////////////////////////////////////////////
2689
2690void HistoryBackend::DeleteURLs(const std::vector<GURL>& urls) {
2691  expirer_.DeleteURLs(urls);
2692
2693  db_->GetStartDate(&first_recorded_time_);
2694  // Force a commit, if the user is deleting something for privacy reasons, we
2695  // want to get it on disk ASAP.
2696  Commit();
2697}
2698
2699void HistoryBackend::DeleteURL(const GURL& url) {
2700  expirer_.DeleteURL(url);
2701
2702  db_->GetStartDate(&first_recorded_time_);
2703  // Force a commit, if the user is deleting something for privacy reasons, we
2704  // want to get it on disk ASAP.
2705  Commit();
2706}
2707
2708void HistoryBackend::ExpireHistoryBetween(
2709    const std::set<GURL>& restrict_urls,
2710    Time begin_time,
2711    Time end_time) {
2712  if (db_.get()) {
2713    if (begin_time.is_null() && (end_time.is_null() || end_time.is_max()) &&
2714        restrict_urls.empty()) {
2715      // Special case deleting all history so it can be faster and to reduce the
2716      // possibility of an information leak.
2717      DeleteAllHistory();
2718    } else {
2719      // Clearing parts of history, have the expirer do the depend
2720      expirer_.ExpireHistoryBetween(restrict_urls, begin_time, end_time);
2721
2722      // Force a commit, if the user is deleting something for privacy reasons,
2723      // we want to get it on disk ASAP.
2724      Commit();
2725    }
2726  }
2727
2728  if (begin_time <= first_recorded_time_)
2729    db_->GetStartDate(&first_recorded_time_);
2730}
2731
2732void HistoryBackend::ExpireHistoryForTimes(
2733    const std::set<base::Time>& times,
2734    base::Time begin_time, base::Time end_time) {
2735  if (times.empty() || !db_.get())
2736    return;
2737
2738  DCHECK(*times.begin() >= begin_time)
2739      << "Min time is before begin time: "
2740      << times.begin()->ToJsTime() << " v.s. " << begin_time.ToJsTime();
2741  DCHECK(*times.rbegin() < end_time)
2742      << "Max time is after end time: "
2743      << times.rbegin()->ToJsTime() << " v.s. " << end_time.ToJsTime();
2744
2745  history::QueryOptions options;
2746  options.begin_time = begin_time;
2747  options.end_time = end_time;
2748  options.duplicate_policy = QueryOptions::KEEP_ALL_DUPLICATES;
2749  QueryResults results;
2750  QueryHistoryBasic(db_.get(), db_.get(), options, &results);
2751
2752  // 1st pass: find URLs that are visited at one of |times|.
2753  std::set<GURL> urls;
2754  for (size_t i = 0; i < results.size(); ++i) {
2755    if (times.count(results[i].visit_time()) > 0)
2756      urls.insert(results[i].url());
2757  }
2758  if (urls.empty())
2759    return;
2760
2761  // 2nd pass: collect all visit times of those URLs.
2762  std::vector<base::Time> times_to_expire;
2763  for (size_t i = 0; i < results.size(); ++i) {
2764    if (urls.count(results[i].url()))
2765      times_to_expire.push_back(results[i].visit_time());
2766  }
2767
2768  // Put the times in reverse chronological order and remove
2769  // duplicates (for expirer_.ExpireHistoryForTimes()).
2770  std::sort(times_to_expire.begin(), times_to_expire.end(),
2771            std::greater<base::Time>());
2772  times_to_expire.erase(
2773      std::unique(times_to_expire.begin(), times_to_expire.end()),
2774      times_to_expire.end());
2775
2776  // Expires by times and commit.
2777  DCHECK(!times_to_expire.empty());
2778  expirer_.ExpireHistoryForTimes(times_to_expire);
2779  Commit();
2780
2781  DCHECK(times_to_expire.back() >= first_recorded_time_);
2782  // Update |first_recorded_time_| if we expired it.
2783  if (times_to_expire.back() == first_recorded_time_)
2784    db_->GetStartDate(&first_recorded_time_);
2785}
2786
2787void HistoryBackend::ExpireHistory(
2788    const std::vector<history::ExpireHistoryArgs>& expire_list) {
2789  if (db_.get()) {
2790    bool update_first_recorded_time = false;
2791
2792    for (std::vector<history::ExpireHistoryArgs>::const_iterator it =
2793         expire_list.begin(); it != expire_list.end(); ++it) {
2794      expirer_.ExpireHistoryBetween(it->urls, it->begin_time, it->end_time);
2795
2796      if (it->begin_time < first_recorded_time_)
2797        update_first_recorded_time = true;
2798    }
2799    Commit();
2800
2801    // Update |first_recorded_time_| if any deletion might have affected it.
2802    if (update_first_recorded_time)
2803      db_->GetStartDate(&first_recorded_time_);
2804  }
2805}
2806
2807void HistoryBackend::URLsNoLongerBookmarked(const std::set<GURL>& urls) {
2808  if (!db_.get())
2809    return;
2810
2811  for (std::set<GURL>::const_iterator i = urls.begin(); i != urls.end(); ++i) {
2812    URLRow url_row;
2813    if (!db_->GetRowForURL(*i, &url_row))
2814      continue;  // The URL isn't in the db; nothing to do.
2815
2816    VisitVector visits;
2817    db_->GetVisitsForURL(url_row.id(), &visits);
2818
2819    if (visits.empty())
2820      expirer_.DeleteURL(*i);  // There are no more visits; nuke the URL.
2821  }
2822}
2823
2824void HistoryBackend::KillHistoryDatabase() {
2825  if (!db_.get())
2826    return;
2827
2828  // Rollback transaction because Raze() cannot be called from within a
2829  // transaction.
2830  db_->RollbackTransaction();
2831  bool success = db_->Raze();
2832  UMA_HISTOGRAM_BOOLEAN("History.KillHistoryDatabaseResult", success);
2833
2834#if defined(OS_ANDROID)
2835  // Release AndroidProviderBackend before other objects.
2836  android_provider_backend_.reset();
2837#endif
2838
2839  // The expirer keeps tabs on the active databases. Tell it about the
2840  // databases which will be closed.
2841  expirer_.SetDatabases(NULL, NULL, NULL, NULL);
2842
2843  // Reopen a new transaction for |db_| for the sake of CloseAllDatabases().
2844  db_->BeginTransaction();
2845  CloseAllDatabases();
2846}
2847
2848void HistoryBackend::ProcessDBTask(
2849    scoped_refptr<HistoryDBTaskRequest> request) {
2850  DCHECK(request.get());
2851  if (request->canceled())
2852    return;
2853
2854  bool task_scheduled = !db_task_requests_.empty();
2855  // Make sure we up the refcount of the request. ProcessDBTaskImpl will
2856  // release when done with the task.
2857  request->AddRef();
2858  db_task_requests_.push_back(request.get());
2859  if (!task_scheduled) {
2860    // No other tasks are scheduled. Process request now.
2861    ProcessDBTaskImpl();
2862  }
2863}
2864
2865void HistoryBackend::BroadcastNotifications(
2866    int type,
2867    HistoryDetails* details_deleted) {
2868  // |delegate_| may be NULL if |this| is in the process of closing (closed by
2869  // HistoryService -> HistroyBackend::Closing().
2870  if (delegate_.get())
2871    delegate_->BroadcastNotifications(type, details_deleted);
2872  else
2873    delete details_deleted;
2874}
2875
2876// Deleting --------------------------------------------------------------------
2877
2878void HistoryBackend::DeleteAllHistory() {
2879  // Our approach to deleting all history is:
2880  //  1. Copy the bookmarks and their dependencies to new tables with temporary
2881  //     names.
2882  //  2. Delete the original tables. Since tables can not share pages, we know
2883  //     that any data we don't want to keep is now in an unused page.
2884  //  3. Renaming the temporary tables to match the original.
2885  //  4. Vacuuming the database to delete the unused pages.
2886  //
2887  // Since we are likely to have very few bookmarks and their dependencies
2888  // compared to all history, this is also much faster than just deleting from
2889  // the original tables directly.
2890
2891  // Get the bookmarked URLs.
2892  std::vector<BookmarkService::URLAndTitle> starred_urls;
2893  BookmarkService* bookmark_service = GetBookmarkService();
2894  if (bookmark_service)
2895    bookmark_service_->GetBookmarks(&starred_urls);
2896
2897  URLRows kept_urls;
2898  for (size_t i = 0; i < starred_urls.size(); i++) {
2899    URLRow row;
2900    if (!db_->GetRowForURL(starred_urls[i].url, &row))
2901      continue;
2902
2903    // Clear the last visit time so when we write these rows they are "clean."
2904    row.set_last_visit(Time());
2905    row.set_visit_count(0);
2906    row.set_typed_count(0);
2907    kept_urls.push_back(row);
2908  }
2909
2910  // Clear thumbnail and favicon history. The favicons for the given URLs will
2911  // be kept.
2912  if (!ClearAllThumbnailHistory(&kept_urls)) {
2913    LOG(ERROR) << "Thumbnail history could not be cleared";
2914    // We continue in this error case. If the user wants to delete their
2915    // history, we should delete as much as we can.
2916  }
2917
2918  // ClearAllMainHistory will change the IDs of the URLs in kept_urls. Therfore,
2919  // we clear the list afterwards to make sure nobody uses this invalid data.
2920  if (!ClearAllMainHistory(kept_urls))
2921    LOG(ERROR) << "Main history could not be cleared";
2922  kept_urls.clear();
2923
2924  // Delete FTS files & archived history.
2925  if (text_database_.get()) {
2926    // We assume that the text database has one transaction on them that we need
2927    // to close & restart (the long-running history transaction).
2928    text_database_->CommitTransaction();
2929    text_database_->DeleteAll();
2930    text_database_->BeginTransaction();
2931  }
2932
2933  if (archived_db_.get()) {
2934    // Close the database and delete the file.
2935    archived_db_.reset();
2936    base::FilePath archived_file_name = GetArchivedFileName();
2937    file_util::Delete(archived_file_name, false);
2938
2939    // Now re-initialize the database (which may fail).
2940    archived_db_.reset(new ArchivedDatabase());
2941    if (!archived_db_->Init(archived_file_name)) {
2942      LOG(WARNING) << "Could not initialize the archived database.";
2943      archived_db_.reset();
2944    } else {
2945      // Open our long-running transaction on this database.
2946      archived_db_->BeginTransaction();
2947    }
2948  }
2949
2950  db_->GetStartDate(&first_recorded_time_);
2951
2952  // Send out the notfication that history is cleared. The in-memory datdabase
2953  // will pick this up and clear itself.
2954  URLsDeletedDetails* details = new URLsDeletedDetails;
2955  details->all_history = true;
2956  BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URLS_DELETED, details);
2957}
2958
2959bool HistoryBackend::ClearAllThumbnailHistory(URLRows* kept_urls) {
2960  if (!thumbnail_db_.get()) {
2961    // When we have no reference to the thumbnail database, maybe there was an
2962    // error opening it. In this case, we just try to blow it away to try to
2963    // fix the error if it exists. This may fail, in which case either the
2964    // file doesn't exist or there's no more we can do.
2965    file_util::Delete(GetThumbnailFileName(), false);
2966    return true;
2967  }
2968
2969  // Create duplicate icon_mapping, favicon, and favicon_bitmaps tables, this
2970  // is where the favicons we want to keep will be stored.
2971  if (!thumbnail_db_->InitTemporaryTables())
2972    return false;
2973
2974  // This maps existing favicon IDs to the ones in the temporary table.
2975  typedef std::map<FaviconID, FaviconID> FaviconMap;
2976  FaviconMap copied_favicons;
2977
2978  // Copy all unique favicons to the temporary table, and update all the
2979  // URLs to have the new IDs.
2980  for (URLRows::iterator i = kept_urls->begin(); i != kept_urls->end(); ++i) {
2981    std::vector<IconMapping> icon_mappings;
2982    if (!thumbnail_db_->GetIconMappingsForPageURL(i->url(), &icon_mappings))
2983      continue;
2984
2985    for (std::vector<IconMapping>::iterator m = icon_mappings.begin();
2986         m != icon_mappings.end(); ++m) {
2987      FaviconID old_id = m->icon_id;
2988      FaviconID new_id;
2989      FaviconMap::const_iterator found = copied_favicons.find(old_id);
2990      if (found == copied_favicons.end()) {
2991        new_id = thumbnail_db_->CopyFaviconAndFaviconBitmapsToTemporaryTables(
2992            old_id);
2993        copied_favicons[old_id] = new_id;
2994      } else {
2995        // We already encountered a URL that used this favicon, use the ID we
2996        // previously got.
2997        new_id = found->second;
2998      }
2999      // Add Icon mapping, and we don't care wheteher it suceeded or not.
3000      thumbnail_db_->AddToTemporaryIconMappingTable(i->url(), new_id);
3001    }
3002  }
3003#if defined(OS_ANDROID)
3004  // TODO (michaelbai): Add the unit test once AndroidProviderBackend is
3005  // avaliable in HistoryBackend.
3006  db_->ClearAndroidURLRows();
3007#endif
3008
3009  // Drop original favicon_bitmaps, favicons, and icon mapping tables and
3010  // replace them with the duplicate tables. Recreate the other tables. This
3011  // will make the database consistent again.
3012  thumbnail_db_->CommitTemporaryTables();
3013
3014  thumbnail_db_->RecreateThumbnailTable();
3015
3016  // Vacuum to remove all the pages associated with the dropped tables. There
3017  // must be no transaction open on the table when we do this. We assume that
3018  // our long-running transaction is open, so we complete it and start it again.
3019  DCHECK(thumbnail_db_->transaction_nesting() == 1);
3020  thumbnail_db_->CommitTransaction();
3021  thumbnail_db_->Vacuum();
3022  thumbnail_db_->BeginTransaction();
3023  return true;
3024}
3025
3026bool HistoryBackend::ClearAllMainHistory(const URLRows& kept_urls) {
3027  // Create the duplicate URL table. We will copy the kept URLs into this.
3028  if (!db_->CreateTemporaryURLTable())
3029    return false;
3030
3031  // Insert the URLs into the temporary table, we need to keep a map of changed
3032  // IDs since the ID will be different in the new table.
3033  typedef std::map<URLID, URLID> URLIDMap;
3034  URLIDMap old_to_new;  // Maps original ID to new one.
3035  for (URLRows::const_iterator i = kept_urls.begin(); i != kept_urls.end();
3036       ++i) {
3037    URLID new_id = db_->AddTemporaryURL(*i);
3038    old_to_new[i->id()] = new_id;
3039  }
3040
3041  // Replace the original URL table with the temporary one.
3042  if (!db_->CommitTemporaryURLTable())
3043    return false;
3044
3045  // Delete the old tables and recreate them empty.
3046  db_->RecreateAllTablesButURL();
3047
3048  // Vacuum to reclaim the space from the dropped tables. This must be done
3049  // when there is no transaction open, and we assume that our long-running
3050  // transaction is currently open.
3051  db_->CommitTransaction();
3052  db_->Vacuum();
3053  db_->BeginTransaction();
3054  db_->GetStartDate(&first_recorded_time_);
3055
3056  return true;
3057}
3058
3059BookmarkService* HistoryBackend::GetBookmarkService() {
3060  if (bookmark_service_)
3061    bookmark_service_->BlockTillLoaded();
3062  return bookmark_service_;
3063}
3064
3065void HistoryBackend::NotifyVisitObservers(const VisitRow& visit) {
3066  BriefVisitInfo info;
3067  info.url_id = visit.url_id;
3068  info.time = visit.visit_time;
3069  info.transition = visit.transition;
3070  // If we don't have a delegate yet during setup or shutdown, we will drop
3071  // these notifications.
3072  if (delegate_.get())
3073    delegate_->NotifyVisitDBObserversOnAddVisit(info);
3074}
3075
3076}  // namespace history
3077