File: reading_list_suggestions_provider.cc

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (261 lines) | stat: -rw-r--r-- 8,856 bytes parent folder | download | duplicates (2)
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
// 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/reading_list/reading_list_suggestions_provider.h"

#include <algorithm>
#include <memory>
#include <vector>

#include "base/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "components/ntp_snippets/category.h"
#include "components/reading_list/core/reading_list_entry.h"
#include "components/reading_list/core/reading_list_model.h"
#include "components/strings/grit/components_strings.h"
#include "components/url_formatter/url_formatter.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image.h"

namespace ntp_snippets {

namespace {
// Max number of entries to return.
const int kMaxEntries = 3;

bool CompareEntries(const ReadingListEntry* lhs, const ReadingListEntry* rhs) {
  return lhs->UpdateTime() > rhs->UpdateTime();
}
}  // namespace

ReadingListSuggestionsProvider::ReadingListSuggestionsProvider(
    ContentSuggestionsProvider::Observer* observer,
    ReadingListModel* reading_list_model)
    : ContentSuggestionsProvider(observer),
      category_status_(CategoryStatus::AVAILABLE_LOADING),
      provided_category_(
          Category::FromKnownCategory(KnownCategories::READING_LIST)),
      reading_list_model_(reading_list_model),
      scoped_observer_(this) {
  observer->OnCategoryStatusChanged(this, provided_category_, category_status_);

  // If the ReadingListModel is loaded, this will trigger a call to
  // ReadingListModelLoaded. Keep it as last instruction.
  scoped_observer_.Add(reading_list_model_);
}

ReadingListSuggestionsProvider::~ReadingListSuggestionsProvider(){};

CategoryStatus ReadingListSuggestionsProvider::GetCategoryStatus(
    Category category) {
  DCHECK_EQ(category, provided_category_);
  return category_status_;
}

CategoryInfo ReadingListSuggestionsProvider::GetCategoryInfo(
    Category category) {
  DCHECK_EQ(category, provided_category_);

  return CategoryInfo(l10n_util::GetStringUTF16(
                          IDS_NTP_READING_LIST_SUGGESTIONS_SECTION_HEADER),
                      ContentSuggestionsCardLayout::FULL_CARD,
                      ContentSuggestionsAdditionalAction::VIEW_ALL,
                      /*show_if_empty=*/false,
                      l10n_util::GetStringUTF16(
                          IDS_NTP_READING_LIST_SUGGESTIONS_SECTION_EMPTY));
}

void ReadingListSuggestionsProvider::DismissSuggestion(
    const ContentSuggestion::ID& suggestion_id) {
  if (!reading_list_model_) {
    return;
  }

  DCHECK(reading_list_model_->loaded());
  GURL url(suggestion_id.id_within_category());
  SetDismissedState(url, true);
}

void ReadingListSuggestionsProvider::FetchSuggestionImage(
    const ContentSuggestion::ID& suggestion_id,
    ImageFetchedCallback callback) {
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback), gfx::Image()));
}

void ReadingListSuggestionsProvider::FetchSuggestionImageData(
    const ContentSuggestion::ID& suggestion_id,
    ImageDataFetchedCallback callback) {
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback), std::string()));
}

void ReadingListSuggestionsProvider::Fetch(
    const Category& category,
    const std::set<std::string>& known_suggestion_ids,
    FetchDoneCallback callback) {
  LOG(DFATAL) << "ReadingListSuggestionsProvider has no |Fetch| functionality!";
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(callback),
                     Status(StatusCode::PERMANENT_ERROR,
                            "ReadingListSuggestionsProvider has no |Fetch| "
                            "functionality!"),
                     std::vector<ContentSuggestion>()));
}

void ReadingListSuggestionsProvider::ClearHistory(
    base::Time begin,
    base::Time end,
    const base::Callback<bool(const GURL& url)>& filter) {
  // Ignored, Reading List does not depend on history.
}

void ReadingListSuggestionsProvider::ClearCachedSuggestions() {
  // Ignored.
}

void ReadingListSuggestionsProvider::GetDismissedSuggestionsForDebugging(
    Category category,
    DismissedSuggestionsCallback callback) {
  if (!reading_list_model_ || reading_list_model_->IsPerformingBatchUpdates()) {
    std::move(callback).Run(std::vector<ContentSuggestion>());
    return;
  }

  DCHECK(reading_list_model_->loaded());
  std::vector<const ReadingListEntry*> entries;
  for (const GURL& url : reading_list_model_->Keys()) {
    const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url);
    if (entry->ContentSuggestionsExtra()->dismissed) {
      entries.emplace_back(entry);
    }
  }

  std::sort(entries.begin(), entries.end(), CompareEntries);

  std::vector<ContentSuggestion> suggestions;
  for (const ReadingListEntry* entry : entries) {
    suggestions.emplace_back(ConvertEntry(entry));
  }

  std::move(callback).Run(std::move(suggestions));
}

void ReadingListSuggestionsProvider::ClearDismissedSuggestionsForDebugging(
    Category category) {
  for (const auto& url : reading_list_model_->Keys()) {
    SetDismissedState(url, false);
  }
}

void ReadingListSuggestionsProvider::ReadingListModelLoaded(
    const ReadingListModel* model) {
  DCHECK(model == reading_list_model_);
  FetchReadingListInternal();
}

void ReadingListSuggestionsProvider::ReadingListModelBeingDeleted(
    const ReadingListModel* model) {
  DCHECK(model == reading_list_model_);
  scoped_observer_.Remove(reading_list_model_);
  reading_list_model_ = nullptr;
}

void ReadingListSuggestionsProvider::ReadingListDidApplyChanges(
    ReadingListModel* model) {
  DCHECK(model == reading_list_model_);
  if (model->IsPerformingBatchUpdates())
    return;

  FetchReadingListInternal();
}

void ReadingListSuggestionsProvider::ReadingListModelCompletedBatchUpdates(
    const ReadingListModel* model) {
  DCHECK(model == reading_list_model_);

  FetchReadingListInternal();
}

void ReadingListSuggestionsProvider::FetchReadingListInternal() {
  if (!reading_list_model_ || reading_list_model_->IsPerformingBatchUpdates()) {
    return;
  }

  DCHECK(reading_list_model_->loaded());
  std::vector<const ReadingListEntry*> entries;
  for (const GURL& url : reading_list_model_->Keys()) {
    const ReadingListEntry* entry = reading_list_model_->GetEntryByURL(url);
    if (!entry->IsRead() && !entry->ContentSuggestionsExtra()->dismissed) {
      entries.emplace_back(entry);
    }
  }

  if (entries.size() > kMaxEntries) {
    // Get the |kMaxEntries| most recent entries.
    std::partial_sort(entries.begin(), entries.begin() + kMaxEntries,
                      entries.end(), CompareEntries);
    entries.resize(kMaxEntries);
  } else {
    std::sort(entries.begin(), entries.end(), CompareEntries);
  }

  std::vector<ContentSuggestion> suggestions;
  for (const ReadingListEntry* entry : entries) {
    suggestions.emplace_back(ConvertEntry(entry));
  }

  NotifyStatusChanged(CategoryStatus::AVAILABLE);
  observer()->OnNewSuggestions(this, provided_category_,
                               std::move(suggestions));
}

ContentSuggestion ReadingListSuggestionsProvider::ConvertEntry(
    const ReadingListEntry* entry) {
  ContentSuggestion suggestion(provided_category_, entry->URL().spec(),
                               entry->URL());

  if (!entry->Title().empty()) {
    suggestion.set_title(base::UTF8ToUTF16(entry->Title()));
  } else {
    suggestion.set_title(url_formatter::FormatUrl(entry->URL()));
  }
  suggestion.set_publisher_name(
      url_formatter::FormatUrl(entry->URL().GetOrigin()));
  int64_t entry_time = entry->DistillationTime();
  if (entry_time == 0) {
    entry_time = entry->CreationTime();
  }

  suggestion.set_publish_date(
      base::Time::FromDoubleT(entry_time / base::Time::kMicrosecondsPerSecond));

  auto extra = std::make_unique<ReadingListSuggestionExtra>();
  extra->favicon_page_url =
      entry->DistilledURL().is_valid() ? entry->DistilledURL() : entry->URL();
  suggestion.set_reading_list_suggestion_extra(std::move(extra));

  return suggestion;
}

void ReadingListSuggestionsProvider::NotifyStatusChanged(
    CategoryStatus new_status) {
  if (category_status_ == new_status) {
    return;
  }
  category_status_ = new_status;
  observer()->OnCategoryStatusChanged(this, provided_category_, new_status);
}

void ReadingListSuggestionsProvider::SetDismissedState(const GURL& url,
                                                       bool dismissed) {
  reading_list::ContentSuggestionsExtra extra;
  extra.dismissed = dismissed;
  reading_list_model_->SetContentSuggestionsExtra(url, extra);
}

}  // namespace ntp_snippets