1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/ntp_snippets/contextual/contextual_content_suggestions_service.h"
#include <iterator>
#include <memory>
#include <set>
#include <utility>
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "components/ntp_snippets/contextual/contextual_content_suggestions_service_proxy.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_result.h"
#include "components/ntp_snippets/remote/cached_image_fetcher.h"
#include "components/ntp_snippets/remote/remote_suggestions_database.h"
#include "components/ntp_snippets/remote/remote_suggestions_provider_impl.h"
#include "ui/gfx/image/image.h"
namespace contextual_suggestions {
using ntp_snippets::ContentSuggestion;
using ntp_snippets::ImageDataFetchedCallback;
using ntp_snippets::ImageFetchedCallback;
using ntp_snippets::CachedImageFetcher;
using ntp_snippets::RemoteSuggestionsDatabase;
namespace {
bool IsEligibleURL(const GURL& url) {
return url.is_valid() && url.SchemeIsHTTPOrHTTPS() && !url.HostIsIPAddress();
}
static constexpr float kMinimumConfidence = 0.75;
} // namespace
ContextualContentSuggestionsService::ContextualContentSuggestionsService(
std::unique_ptr<ContextualSuggestionsFetcher>
contextual_suggestions_fetcher,
std::unique_ptr<CachedImageFetcher> image_fetcher,
std::unique_ptr<RemoteSuggestionsDatabase> contextual_suggestions_database,
std::unique_ptr<ContextualSuggestionsReporterProvider> reporter_provider)
: contextual_suggestions_database_(
std::move(contextual_suggestions_database)),
fetch_cache_(kFetchCacheCapacity),
contextual_suggestions_fetcher_(
std::move(contextual_suggestions_fetcher)),
image_fetcher_(std::move(image_fetcher)),
reporter_provider_(std::move(reporter_provider)) {}
ContextualContentSuggestionsService::~ContextualContentSuggestionsService() =
default;
void ContextualContentSuggestionsService::FetchContextualSuggestionClusters(
const GURL& url,
FetchClustersCallback callback,
ReportFetchMetricsCallback metrics_callback) {
// TODO(pnoland): Also check that the url is safe.
ContextualSuggestionsResult result;
if (IsEligibleURL(url) && !fetch_cache_.GetSuggestionsResult(url, &result)) {
FetchClustersCallback internal_callback = base::BindOnce(
&ContextualContentSuggestionsService::FetchDone, base::Unretained(this),
url, std::move(callback), metrics_callback);
contextual_suggestions_fetcher_->FetchContextualSuggestionsClusters(
url, std::move(internal_callback), metrics_callback);
} else if (result.peek_conditions.confidence < kMinimumConfidence) {
BelowConfidenceThresholdFetchDone(std::move(callback), metrics_callback);
} else {
std::move(callback).Run(result);
}
}
void ContextualContentSuggestionsService::FetchContextualSuggestionImage(
const ContentSuggestion::ID& suggestion_id,
const GURL& image_url,
ImageFetchedCallback callback) {
image_fetcher_->FetchSuggestionImage(suggestion_id, image_url,
ImageDataFetchedCallback(),
std::move(callback));
}
void ContextualContentSuggestionsService::FetchDone(
const GURL& url,
FetchClustersCallback callback,
ReportFetchMetricsCallback metrics_callback,
ContextualSuggestionsResult result) {
// We still want to cache low confidence results so that we avoid doing
// unnecessary fetches.
if (result.clusters.size() > 0) {
fetch_cache_.AddSuggestionsResult(url, result);
}
if (result.peek_conditions.confidence < kMinimumConfidence) {
BelowConfidenceThresholdFetchDone(std::move(callback), metrics_callback);
return;
}
std::move(callback).Run(result);
}
ContextualSuggestionsDebuggingReporter*
ContextualContentSuggestionsService::GetDebuggingReporter() {
return reporter_provider_->GetDebuggingReporter();
}
base::flat_map<GURL, ContextualSuggestionsResult>
ContextualContentSuggestionsService::GetAllCachedResultsForDebugging() {
return fetch_cache_.GetAllCachedResultsForDebugging();
}
void ContextualContentSuggestionsService::ClearCachedResultsForDebugging() {
fetch_cache_.Clear();
}
std::unique_ptr<
contextual_suggestions::ContextualContentSuggestionsServiceProxy>
ContextualContentSuggestionsService::CreateProxy() {
return std::make_unique<
contextual_suggestions::ContextualContentSuggestionsServiceProxy>(
this, reporter_provider_->CreateReporter());
}
void ContextualContentSuggestionsService::BelowConfidenceThresholdFetchDone(
FetchClustersCallback callback,
ReportFetchMetricsCallback metrics_callback) {
metrics_callback.Run(contextual_suggestions::FETCH_BELOW_THRESHOLD);
std::move(callback).Run(ContextualSuggestionsResult("", {}, PeekConditions(),
ServerExperimentInfos()));
}
} // namespace contextual_suggestions
|