summaryrefslogtreecommitdiff
path: root/chromium/components/feed/content/feed_offline_host.cc
blob: b47ebda16068e819d376c7495991480b5967307a (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2018 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/feed/content/feed_offline_host.h"

#include <utility>

#include "base/bind.h"
#include "base/hash.h"
#include "base/metrics/histogram_macros.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/feed/core/feed_scheduler_host.h"
#include "components/offline_pages/core/client_namespace_constants.h"
#include "components/offline_pages/core/prefetch/prefetch_service.h"
#include "url/gurl.h"

namespace feed {

using offline_pages::OfflinePageItem;
using offline_pages::OfflinePageModel;
using offline_pages::PrefetchService;
using offline_pages::PrefetchSuggestion;
using offline_pages::SuggestionsProvider;

namespace {

// |url| is always set. Sometimes |original_url| is set. If |original_url| is
// set it is returned by this method, otherwise fall back to |url|.
const GURL& PreferOriginal(const OfflinePageItem& item) {
  return item.original_url.is_empty() ? item.url : item.original_url;
}

// Aggregates multiple callbacks from OfflinePageModel, storing the offline url.
// When all callbacks have been invoked, tracked by ref counting, then
// |on_completeion_| is finally invoked, sending all results together.
class CallbackAggregator : public base::RefCounted<CallbackAggregator> {
 public:
  using ReportStatusCallback =
      base::OnceCallback<void(std::vector<std::string>)>;
  using CacheIdCallback =
      base::RepeatingCallback<void(const std::string&, int64_t)>;

  CallbackAggregator(ReportStatusCallback on_completion,
                     CacheIdCallback on_each_result)
      : on_completion_(std::move(on_completion)),
        on_each_result_(std::move(on_each_result)),
        start_time_(base::Time::Now()) {}

  // We curry |feed_url|, which is the URL the Feed requested with. This is used
  // instead of the URLs present in the |pages| because offline pages has
  // non-exact URL matching, and we must communicate with the Feed with exact
  // matches.
  void OnGetPages(std::string feed_url,
                  const std::vector<OfflinePageItem>& pages) {
    if (!pages.empty()) {
      OfflinePageItem best =
          *std::max_element(pages.begin(), pages.end(), [](auto lhs, auto rhs) {
            // Prefer prefetched articles over any other. They are typically of
            // higher quality.
            bool leftIsPrefetch = lhs.client_id.name_space ==
                                  offline_pages::kSuggestedArticlesNamespace;
            bool rightIsPrefetch = rhs.client_id.name_space ==
                                   offline_pages::kSuggestedArticlesNamespace;
            if (leftIsPrefetch != rightIsPrefetch) {
              // Only one is prefetch, if that is |rhs|, then they're in the
              // correct order.
              return rightIsPrefetch;
            } else {
              // Newer articles are also better, but not as important as being
              // prefetched.
              return lhs.creation_time < rhs.creation_time;
            }
          });
      urls_.push_back(feed_url);
      on_each_result_.Run(feed_url, best.offline_id);
    }
  }

 private:
  friend class base::RefCounted<CallbackAggregator>;

  ~CallbackAggregator() {
    base::TimeDelta duration = base::Time::Now() - start_time_;
    UMA_HISTOGRAM_TIMES("ContentSuggestions.Feed.Offline.GetStatusDuration",
                        duration);
    std::move(on_completion_).Run(std::move(urls_));
  }

  // To be called once all callbacks are run or destroyed.
  ReportStatusCallback on_completion_;

  // The urls of the offlined pages seen so far. Ultimately will be given to
  // |on_completeion_|.
  std::vector<std::string> urls_;

  // Is not run if there are no results for a given url.
  CacheIdCallback on_each_result_;

  // The time the aggregator was created, before any requests were sent to the
  // OfflinePageModel.
  base::Time start_time_;
};

// Consumes |metadataVector|, moving as many of the fields as possible.
std::vector<PrefetchSuggestion> ConvertMetadataToSuggestions(
    std::vector<ContentMetadata> metadataVector) {
  std::vector<PrefetchSuggestion> suggestionsVector;
  for (ContentMetadata& metadata : metadataVector) {
    // TODO(skym): Copy over published time when PrefetchSuggestion adds
    // support.
    PrefetchSuggestion suggestion;
    suggestion.article_url = GURL(metadata.url);
    suggestion.article_title = std::move(metadata.title);
    suggestion.article_attribution = std::move(metadata.publisher);
    suggestion.article_snippet = std::move(metadata.snippet);
    suggestion.thumbnail_url = GURL(metadata.image_url);
    suggestion.favicon_url = GURL(metadata.favicon_url);
    suggestionsVector.push_back(std::move(suggestion));
  }
  return suggestionsVector;
}

void RunSuggestionCallbackWithConversion(
    SuggestionsProvider::SuggestionCallback suggestions_callback,
    std::vector<ContentMetadata> metadataVector) {
  std::move(suggestions_callback)
      .Run(ConvertMetadataToSuggestions(std::move(metadataVector)));
}

}  //  namespace

FeedOfflineHost::FeedOfflineHost(OfflinePageModel* offline_page_model,
                                 PrefetchService* prefetch_service,
                                 base::RepeatingClosure on_suggestion_consumed,
                                 base::RepeatingClosure on_suggestions_shown)
    : offline_page_model_(offline_page_model),
      prefetch_service_(prefetch_service),
      on_suggestion_consumed_(on_suggestion_consumed),
      on_suggestions_shown_(on_suggestions_shown),
      weak_factory_(this) {
  DCHECK(offline_page_model_);
  DCHECK(prefetch_service_);
  DCHECK(!on_suggestion_consumed_.is_null());
  DCHECK(!on_suggestions_shown_.is_null());
}

FeedOfflineHost::~FeedOfflineHost() {
  // Safe to call RemoveObserver() even if AddObserver() has not been called.
  offline_page_model_->RemoveObserver(this);
}

void FeedOfflineHost::Initialize(
    const base::RepeatingClosure& trigger_get_known_content,
    const NotifyStatusChangeCallback& notify_status_change) {
  DCHECK(trigger_get_known_content_.is_null());
  DCHECK(!trigger_get_known_content.is_null());
  DCHECK(notify_status_change_.is_null());
  DCHECK(!notify_status_change.is_null());
  trigger_get_known_content_ = trigger_get_known_content;
  notify_status_change_ = notify_status_change;
  offline_page_model_->AddObserver(this);
  // The host guarantees that the two callbacks passed into this method will not
  // be invoked until Initialize() has exited. To guarantee this, the host
  // cannot call SetSuggestionProvider() in task, because that would give
  // Prefetch the ability to run |trigger_get_known_content_| immediately.
  // PostTask is used to delay when SetSuggestionProvider() is called.
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::BindOnce(&FeedOfflineHost::SetSuggestionProvider,
                                weak_factory_.GetWeakPtr()));
}

void FeedOfflineHost::SetSuggestionProvider() {
  prefetch_service_->SetSuggestionProvider(this);
}

base::Optional<int64_t> FeedOfflineHost::GetOfflineId(const std::string& url) {
  auto iter = url_hash_to_id_.find(base::Hash(url));
  return iter == url_hash_to_id_.end() ? base::Optional<int64_t>()
                                       : base::Optional<int64_t>(iter->second);
}

void FeedOfflineHost::GetOfflineStatus(
    std::vector<std::string> urls,
    base::OnceCallback<void(std::vector<std::string>)> callback) {
  UMA_HISTOGRAM_EXACT_LINEAR("ContentSuggestions.Feed.Offline.GetStatusCount",
                             urls.size(), 50);

  scoped_refptr<CallbackAggregator> aggregator =
      base::MakeRefCounted<CallbackAggregator>(
          std::move(callback),
          base::BindRepeating(&FeedOfflineHost::CacheOfflinePageUrlAndId,
                              weak_factory_.GetWeakPtr()));

  for (std::string url : urls) {
    GURL gurl(url);
    offline_page_model_->GetPagesByURL(
        gurl, base::BindOnce(&CallbackAggregator::OnGetPages, aggregator,
                             std::move(url)));
  }
}

void FeedOfflineHost::OnContentRemoved(std::vector<std::string> urls) {
  for (const std::string& url : urls) {
    prefetch_service_->RemoveSuggestion(GURL(url));
  }
}

void FeedOfflineHost::OnNewContentReceived() {
  prefetch_service_->NewSuggestionsAvailable();
}

void FeedOfflineHost::OnNoListeners() {
  url_hash_to_id_.clear();
}

void FeedOfflineHost::OnGetKnownContentDone(
    std::vector<ContentMetadata> suggestions) {
  // While |suggestions| are movable, there might be multiple callbacks in
  // |pending_known_content_callbacks_|. All but one callback will need to
  // receive a full copy of |suggestions|, and the last one will received a
  // moved copy.
  for (size_t i = 0; i < pending_known_content_callbacks_.size(); i++) {
    bool can_move = (i + 1 == pending_known_content_callbacks_.size());
    std::move(pending_known_content_callbacks_[i])
        .Run(can_move ? std::move(suggestions) : suggestions);
  }
  pending_known_content_callbacks_.clear();
}

void FeedOfflineHost::GetCurrentArticleSuggestions(
    SuggestionsProvider::SuggestionCallback suggestions_callback) {
  DCHECK(!trigger_get_known_content_.is_null());
  pending_known_content_callbacks_.push_back(base::BindOnce(
      &RunSuggestionCallbackWithConversion, std::move(suggestions_callback)));
  // Trigger after push_back() in case triggering results in a synchronous
  // response via OnGetKnownContentDone().
  if (pending_known_content_callbacks_.size() <= 1) {
    trigger_get_known_content_.Run();
  }
}

void FeedOfflineHost::ReportArticleListViewed() {
  on_suggestion_consumed_.Run();
}

void FeedOfflineHost::ReportArticleViewed(GURL article_url) {
  on_suggestions_shown_.Run();
}

void FeedOfflineHost::OfflinePageModelLoaded(OfflinePageModel* model) {
  // Ignored.
}

void FeedOfflineHost::OfflinePageAdded(OfflinePageModel* model,
                                       const OfflinePageItem& added_page) {
  DCHECK(!notify_status_change_.is_null());
  const std::string& url = PreferOriginal(added_page).spec();
  CacheOfflinePageUrlAndId(url, added_page.offline_id);
  notify_status_change_.Run(url, true);
}

void FeedOfflineHost::OfflinePageDeleted(
    const OfflinePageModel::DeletedPageInfo& page_info) {
  DCHECK(!notify_status_change_.is_null());
  const std::string& url = page_info.url.spec();
  EvictOfflinePageUrl(url);
  notify_status_change_.Run(url, false);
}

void FeedOfflineHost::CacheOfflinePageUrlAndId(const std::string& url,
                                               int64_t id) {
  url_hash_to_id_[base::Hash(url)] = id;
}

void FeedOfflineHost::EvictOfflinePageUrl(const std::string& url) {
  url_hash_to_id_.erase(base::Hash(url));
}

}  // namespace feed