chromium/components/history/core/browser/history_service.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// The history system runs on a background sequence so that potentially slow
// database operations don't delay the browser. This backend processing is
// represented by HistoryBackend. The HistoryService's job is to dispatch to
// that sequence.
//
// Main thread                       backend_task_runner_
// -----------                       --------------
// HistoryService <----------------> HistoryBackend
//                                   -> HistoryDatabase
//                                      -> SQLite connection to History
//                                   -> FaviconDatabase
//                                      -> SQLite connection to favicons

#include "components/history/core/browser/history_service.h"

#include <functional>
#include <vector>

#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/observer_list.h"
#include "base/process/process.h"
#include "base/sequence_checker.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "components/history/core/browser/download_row.h"
#include "components/history/core/browser/features.h"
#include "components/history/core/browser/history_backend.h"
#include "components/history/core/browser/history_backend_client.h"
#include "components/history/core/browser/history_client.h"
#include "components/history/core/browser/history_database_params.h"
#include "components/history/core/browser/history_db_task.h"
#include "components/history/core/browser/history_service_observer.h"
#include "components/history/core/browser/history_types.h"
#include "components/history/core/browser/in_memory_database.h"
#include "components/history/core/browser/in_memory_history_backend.h"
#include "components/history/core/browser/keyword_search_term.h"
#include "components/history/core/browser/sync/delete_directive_handler.h"
#include "components/history/core/browser/visit_database.h"
#include "components/history/core/browser/visit_delegate.h"
#include "components/history/core/browser/web_history_service.h"
#include "components/sync/model/proxy_data_type_controller_delegate.h"
#include "components/visitedlink/core/visited_link.h"
#include "net/base/schemeful_site.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/page_transition_types.h"
#include "url/gurl.h"
#include "url/origin.h"

#if BUILDFLAG(IS_IOS)
#include "base/critical_closure.h"
#endif

Time;

namespace history {

// These values are logged to UMA. Entries should not be renumbered and
// numeric values should never be reused. Please keep in sync with
// "PageTransitionForVisitedLinks" in tools/metrics/histograms/enums.xml.
enum class PageTransitionForVisitedLinks {};

// Sends messages from the backend to us on the main thread. This must be a
// separate class from the history service so that it can hold a reference to
// the history service (otherwise we would have to manually AddRef and
// Release when the Backend has a reference to us).
class HistoryService::BackendDelegate : public HistoryBackend::Delegate {};

HistoryService::HistoryService() :{}

HistoryService::HistoryService(std::unique_ptr<HistoryClient> history_client,
                               std::unique_ptr<VisitDelegate> visit_delegate)
    :{}

HistoryService::~HistoryService() {}

bool HistoryService::BackendLoaded() {}

#if BUILDFLAG(IS_IOS)
void HistoryService::HandleBackgrounding() {
  TRACE_EVENT0("browser", "HistoryService::HandleBackgrounding");

  if (!backend_task_runner_ || !history_backend_.get())
    return;

  ScheduleTask(
      PRIORITY_NORMAL,
      base::MakeCriticalClosure(
          "HistoryService::HandleBackgrounding",
          base::BindOnce(&HistoryBackend::PersistState, history_backend_.get()),
          /*is_immediate=*/true));
}
#endif

void HistoryService::ClearCachedDataForContextID(ContextID context_id) {}

void HistoryService::ClearAllOnDemandFavicons() {}

URLDatabase* HistoryService::InMemoryDatabase() {}

void HistoryService::Shutdown() {}

void HistoryService::SetKeywordSearchTermsForURL(const GURL& url,
                                                 KeywordID keyword_id,
                                                 const std::u16string& term) {}

void HistoryService::DeleteAllSearchTermsForKeyword(KeywordID keyword_id) {}

void HistoryService::DeleteKeywordSearchTermForURL(const GURL& url) {}

void HistoryService::DeleteMatchingURLsForKeyword(KeywordID keyword_id,
                                                  const std::u16string& term) {}

void HistoryService::URLsNoLongerBookmarked(const std::set<GURL>& urls) {}

void HistoryService::SetOnCloseContextAnnotationsForVisit(
    VisitID visit_id,
    const VisitContextAnnotations& visit_context_annotations) {}

base::CancelableTaskTracker::TaskId HistoryService::GetAnnotatedVisits(
    const QueryOptions& options,
    bool compute_redirect_chain_start_properties,
    bool get_unclustered_visits_only,
    GetAnnotatedVisitsCallback callback,
    base::CancelableTaskTracker* tracker) const {}

base::CancelableTaskTracker::TaskId HistoryService::ToAnnotatedVisits(
    const VisitVector& visit_rows,
    bool compute_redirect_chain_start_properties,
    ToAnnotatedVisitsCallback callback,
    base::CancelableTaskTracker* tracker) const {}

base::CancelableTaskTracker::TaskId HistoryService::ReplaceClusters(
    const std::vector<int64_t>& ids_to_delete,
    const std::vector<Cluster>& clusters_to_add,
    base::OnceClosure callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId
HistoryService::ReserveNextClusterIdWithVisit(
    const ClusterVisit& cluster_visit,
    ClusterIdCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::AddVisitsToCluster(
    int64_t cluster_id,
    const std::vector<ClusterVisit>& visits,
    base::OnceClosure callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::UpdateClusterTriggerability(
    const std::vector<history::Cluster>& clusters,
    base::OnceClosure callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::HideVisits(
    const std::vector<VisitID>& visit_ids,
    base::OnceClosure callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::UpdateClusterVisit(
    const history::ClusterVisit& cluster_visit,
    base::OnceClosure callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId
HistoryService::UpdateVisitsInteractionState(
    const std::vector<VisitID>& visit_ids,
    const ClusterVisit::InteractionState interaction_state,
    base::OnceClosure callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::GetMostRecentClusters(
    base::Time inclusive_min_time,
    base::Time exclusive_max_time,
    size_t max_clusters,
    size_t max_visits_soft_cap,
    base::OnceCallback<void(std::vector<Cluster>)> callback,
    bool include_keywords_and_duplicates,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::AddObserver(HistoryServiceObserver* observer) {}

void HistoryService::RemoveObserver(HistoryServiceObserver* observer) {}

void HistoryService::SetDeviceInfoServices(
    syncer::DeviceInfoTracker* device_info_tracker,
    syncer::LocalDeviceInfoProvider* local_device_info_provider) {}

void HistoryService::SetCanAddForeignVisitsToSegmentsOnBackend(
    bool add_foreign_visits) {}

void HistoryService::OnDeviceInfoChange() {}

// TODO(crbug.com/40250371): `OnDeviceInfoShutdown()` was created as a
// workaround because PrivacySandboxSettingsFactory incorrectly declares its
// KeyedServices dependencies. Once this is fixed, `OnDeviceInfoShutdown()`
// should be deprecated.
void HistoryService::OnDeviceInfoShutdown() {}

void HistoryService::SendLocalDeviceOriginatorCacheGuidToBackend() {}

base::CancelableTaskTracker::TaskId HistoryService::ScheduleDBTask(
    const base::Location& from_here,
    std::unique_ptr<HistoryDBTask> task,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::ScheduleDBTaskForUI(
    base::OnceCallback<void(HistoryBackend*, URLDatabase*)> callback) {}

void HistoryService::FlushForTest(base::OnceClosure flushed) {}

void HistoryService::SetOnBackendDestroyTask(base::OnceClosure task) {}

void HistoryService::GetCountsAndLastVisitForOriginsForTesting(
    const std::set<GURL>& origins,
    GetCountsAndLastVisitForOriginsCallback callback) const {}

void HistoryService::AddPage(const GURL& url,
                             Time time,
                             ContextID context_id,
                             int nav_entry_id,
                             const GURL& referrer,
                             const RedirectList& redirects,
                             ui::PageTransition transition,
                             VisitSource visit_source,
                             bool did_replace_entry) {}

void HistoryService::AddPage(const GURL& url,
                             base::Time time,
                             VisitSource visit_source) {}

void HistoryService::AddPage(HistoryAddPageArgs add_page_args) {}

void HistoryService::AddPartitionedVisitedLinks(
    const HistoryAddPageArgs& args) {}

void HistoryService::AddPageNoVisitForBookmark(const GURL& url,
                                               const std::u16string& title) {}

void HistoryService::SetPageTitle(const GURL& url,
                                  const std::u16string& title) {}

void HistoryService::UpdateWithPageEndTime(ContextID context_id,
                                           int nav_entry_id,
                                           const GURL& url,
                                           Time end_ts) {}

void HistoryService::SetBrowsingTopicsAllowed(ContextID context_id,
                                              int nav_entry_id,
                                              const GURL& url) {}

void HistoryService::SetPageLanguageForVisit(ContextID context_id,
                                             int nav_entry_id,
                                             const GURL& url,
                                             const std::string& page_language) {}

void HistoryService::SetPasswordStateForVisit(
    ContextID context_id,
    int nav_entry_id,
    const GURL& url,
    VisitContentAnnotations::PasswordState password_state) {}

void HistoryService::AddContentModelAnnotationsForVisit(
    const VisitContentModelAnnotations& model_annotations,
    VisitID visit_id) {}

void HistoryService::AddRelatedSearchesForVisit(
    const std::vector<std::string>& related_searches,
    VisitID visit_id) {}

std::optional<uint64_t> HistoryService::GetOrAddOriginSalt(
    const url::Origin& origin) {}

void HistoryService::AddSearchMetadataForVisit(
    const GURL& search_normalized_url,
    const std::u16string& search_terms,
    VisitID visit_id) {}

void HistoryService::AddPageMetadataForVisit(
    const std::string& alternative_title,
    VisitID visit_id) {}

void HistoryService::SetHasUrlKeyedImageForVisit(bool has_url_keyed_image,
                                                 VisitID visit_id) {}

void HistoryService::AddPageWithDetails(const GURL& url,
                                        const std::u16string& title,
                                        int visit_count,
                                        int typed_count,
                                        Time last_visit,
                                        bool hidden,
                                        VisitSource visit_source) {}

void HistoryService::AddPagesWithDetails(const URLRows& info,
                                         VisitSource visit_source) {}

base::CancelableTaskTracker::TaskId HistoryService::GetFavicon(
    const GURL& icon_url,
    favicon_base::IconType icon_type,
    const std::vector<int>& desired_sizes,
    favicon_base::FaviconResultsCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::GetFaviconsForURL(
    const GURL& page_url,
    const favicon_base::IconTypeSet& icon_types,
    const std::vector<int>& desired_sizes,
    bool fallback_to_host,
    favicon_base::FaviconResultsCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::GetLargestFaviconForURL(
    const GURL& page_url,
    const std::vector<favicon_base::IconTypeSet>& icon_types,
    int minimum_size_in_pixels,
    favicon_base::FaviconRawBitmapCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::GetFaviconForID(
    favicon_base::FaviconID favicon_id,
    int desired_size,
    favicon_base::FaviconResultsCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId
HistoryService::UpdateFaviconMappingsAndFetch(
    const base::flat_set<GURL>& page_urls,
    const GURL& icon_url,
    favicon_base::IconType icon_type,
    const std::vector<int>& desired_sizes,
    favicon_base::FaviconResultsCallback callback,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::DeleteFaviconMappings(
    const base::flat_set<GURL>& page_urls,
    favicon_base::IconType icon_type) {}

void HistoryService::MergeFavicon(
    const GURL& page_url,
    const GURL& icon_url,
    favicon_base::IconType icon_type,
    scoped_refptr<base::RefCountedMemory> bitmap_data,
    const gfx::Size& pixel_size) {}

void HistoryService::SetFavicons(const base::flat_set<GURL>& page_urls,
                                 favicon_base::IconType icon_type,
                                 const GURL& icon_url,
                                 const std::vector<SkBitmap>& bitmaps) {}

void HistoryService::CloneFaviconMappingsForPages(
    const GURL& page_url_to_read,
    const favicon_base::IconTypeSet& icon_types,
    const base::flat_set<GURL>& page_urls_to_write) {}

void HistoryService::CanSetOnDemandFavicons(
    const GURL& page_url,
    favicon_base::IconType icon_type,
    base::OnceCallback<void(bool)> callback) {}

void HistoryService::SetOnDemandFavicons(
    const GURL& page_url,
    favicon_base::IconType icon_type,
    const GURL& icon_url,
    const std::vector<SkBitmap>& bitmaps,
    base::OnceCallback<void(bool)> callback) {}

void HistoryService::SetFaviconsOutOfDateForPage(const GURL& page_url) {}

void HistoryService::SetFaviconsOutOfDateBetween(
    base::Time begin,
    base::Time end,
    base::OnceClosure callback,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::TouchOnDemandFavicon(const GURL& icon_url) {}

void HistoryService::SetImportedFavicons(
    const favicon_base::FaviconUsageDataList& favicon_usage) {}

// Querying --------------------------------------------------------------------

base::CancelableTaskTracker::TaskId
HistoryService::GetMostRecentVisitForEachURL(
    const std::vector<GURL>& urls,
    base::OnceCallback<void(std::map<GURL, VisitRow>)> callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::QueryURL(
    const GURL& url,
    bool want_visits,
    QueryURLCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::QueryURLs(
    const std::vector<GURL>& urls,
    bool want_visits,
    QueryURLsCallback callback,
    base::CancelableTaskTracker* tracker) {}

// Statistics ------------------------------------------------------------------

base::CancelableTaskTracker::TaskId HistoryService::GetHistoryCount(
    const Time& begin_time,
    const Time& end_time,
    GetHistoryCountCallback callback,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::CountUniqueHostsVisitedLastMonth(
    GetHistoryCountCallback callback,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::GetDomainDiversity(
    base::Time report_time,
    int number_of_days_to_report,
    DomainMetricBitmaskType metric_type_bitmask,
    DomainDiversityCallback callback,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::GetUniqueDomainsVisited(
    const base::Time begin_time,
    const base::Time end_time,
    GetUniqueDomainsVisitedCallback callback,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::GetAllAppIds(GetAllAppIdsCallback callback,
                                  base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::GetLastVisitToHost(
    const std::string& host,
    base::Time begin_time,
    base::Time end_time,
    GetLastVisitCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::GetLastVisitToOrigin(
    const url::Origin& origin,
    base::Time begin_time,
    base::Time end_time,
    GetLastVisitCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::GetLastVisitToURL(
    const GURL& url,
    base::Time end_time,
    GetLastVisitCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::GetDailyVisitsToOrigin(
    const url::Origin& origin,
    base::Time begin_time,
    base::Time end_time,
    GetDailyVisitsToOriginCallback callback,
    base::CancelableTaskTracker* tracker) {}

// Generic operations ----------------------------------------------------------

base::CancelableTaskTracker::TaskId HistoryService::GetMostRecentVisitsForGurl(
    GURL url,
    int max_visits,
    QueryURLCallback callback,
    base::CancelableTaskTracker* tracker) {}

// Downloads -------------------------------------------------------------------

// Handle creation of a download by creating an entry in the history service's
// 'downloads' table.
void HistoryService::CreateDownload(const DownloadRow& create_info,
                                    DownloadCreateCallback callback) {}

void HistoryService::GetNextDownloadId(DownloadIdCallback callback) {}

// Handle queries for a list of all downloads in the history database's
// 'downloads' table.
void HistoryService::QueryDownloads(DownloadQueryCallback callback) {}

// Handle updates for a particular download. This is a 'fire and forget'
// operation, so we don't need to be called back.
void HistoryService::UpdateDownload(const DownloadRow& data,
                                    bool should_commit_immediately) {}

void HistoryService::RemoveDownloads(const std::set<uint32_t>& ids) {}

base::CancelableTaskTracker::TaskId HistoryService::QueryHistory(
    const std::u16string& text_query,
    const QueryOptions& options,
    QueryHistoryCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::QueryRedirectsFrom(
    const GURL& from_url,
    QueryRedirectsCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::QueryRedirectsTo(
    const GURL& to_url,
    QueryRedirectsCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::GetVisibleVisitCountToHost(
    const GURL& url,
    GetVisibleVisitCountToHostCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId HistoryService::QueryMostVisitedURLs(
    int result_count,
    QueryMostVisitedURLsCallback callback,
    base::CancelableTaskTracker* tracker) {}

base::CancelableTaskTracker::TaskId
HistoryService::QueryMostRepeatedQueriesForKeyword(
    KeywordID keyword_id,
    size_t result_count,
    base::OnceCallback<void(KeywordSearchTermVisitList)> callback,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::Cleanup() {}

bool HistoryService::Init(
    bool no_db,
    const HistoryDatabaseParams& history_database_params) {}

void HistoryService::ScheduleTask(SchedulePriority priority,
                                  base::OnceClosure task) {}

base::WeakPtr<HistoryService> HistoryService::AsWeakPtr() {}

base::SafeRef<HistoryService> HistoryService::AsSafeRef() {}

base::WeakPtr<syncer::SyncableService>
HistoryService::GetDeleteDirectivesSyncableService() {}

std::unique_ptr<syncer::DataTypeControllerDelegate>
HistoryService::GetHistorySyncControllerDelegate() {}

void HistoryService::SetSyncTransportState(
    syncer::SyncService::TransportState state) {}

void HistoryService::ProcessLocalDeleteDirective(
    const sync_pb::HistoryDeleteDirectiveSpecifics& delete_directive) {}

void HistoryService::SetInMemoryBackend(
    std::unique_ptr<InMemoryHistoryBackend> mem_backend) {}

void HistoryService::NotifyProfileError(sql::InitStatus init_status,
                                        const std::string& diagnostics) {}

void HistoryService::DeleteURLs(const std::vector<GURL>& urls) {}

void HistoryService::ExpireHistoryBetween(
    const std::set<GURL>& restrict_urls,
    std::optional<std::string> restrict_app_id,
    Time begin_time,
    Time end_time,
    bool user_initiated,
    base::OnceClosure callback,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::ExpireHistory(
    const std::vector<ExpireHistoryArgs>& expire_list,
    base::OnceClosure callback,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::ExpireHistoryBeforeForTesting(
    base::Time end_time,
    base::OnceClosure callback,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::DeleteLocalAndRemoteHistoryBetween(
    WebHistoryService* web_history,
    Time begin_time,
    Time end_time,
    std::optional<std::string> app_id,
    base::OnceClosure callback,
    base::CancelableTaskTracker* tracker) {}

void HistoryService::DeleteLocalAndRemoteUrl(WebHistoryService* web_history,
                                             const GURL& url) {}

void HistoryService::OnDBLoaded() {}

void HistoryService::NotifyURLVisited(
    const URLRow& url_row,
    const VisitRow& new_visit,
    std::optional<int64_t> local_navigation_id) {}

void HistoryService::NotifyURLsModified(const URLRows& changed_urls) {}

void HistoryService::NotifyDeletions(const DeletionInfo& deletion_info) {}

void HistoryService::NotifyVisitedLinksDeleted(
    const std::vector<DeletedVisitedLink>& links) {}

void HistoryService::NotifyHistoryServiceLoaded() {}

void HistoryService::NotifyHistoryServiceBeingDeleted() {}

void HistoryService::NotifyKeywordSearchTermUpdated(
    const URLRow& row,
    KeywordID keyword_id,
    const std::u16string& term) {}

void HistoryService::NotifyKeywordSearchTermDeleted(URLID url_id) {}

base::CallbackListSubscription HistoryService::AddFaviconsChangedCallback(
    const HistoryService::FaviconsChangedCallback& callback) {}

void HistoryService::NotifyFaviconsChanged(const std::set<GURL>& page_urls,
                                           const GURL& icon_url) {}

bool HistoryService::CanAddURL(const GURL& url) {}

void HistoryService::LogTransitionMetricsForVisit(
    ui::PageTransition transition) {}

}  // namespace history