summaryrefslogtreecommitdiff
path: root/chromium/components/ntp_snippets/offline_pages/recent_tab_suggestions_provider.cc
blob: 43be2a4daa6fc31d291f82b8c6c3692a164520ae (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2016 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/offline_pages/recent_tab_suggestions_provider.h"

#include <algorithm>
#include <utility>

#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/ntp_snippets/features.h"
#include "components/ntp_snippets/pref_names.h"
#include "components/ntp_snippets/pref_util.h"
#include "components/offline_pages/core/client_policy_controller.h"
#include "components/offline_pages/core/recent_tabs/recent_tabs_ui_adapter_delegate.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "components/variations/variations_associated_data.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image.h"

using offline_pages::ClientId;
using offline_pages::DownloadUIAdapter;
using offline_pages::DownloadUIItem;

namespace ntp_snippets {

namespace {

const int kDefaultMaxSuggestionsCount = 5;

const char* kMaxSuggestionsCountParamName = "recent_tabs_max_count";

int GetMaxSuggestionsCount() {
  return variations::GetVariationParamByFeatureAsInt(
      kRecentOfflineTabSuggestionsFeature, kMaxSuggestionsCountParamName,
      kDefaultMaxSuggestionsCount);
}

struct OrderUIItemsByMostRecentlyCreatedFirst {
  bool operator()(const DownloadUIItem* left,
                  const DownloadUIItem* right) const {
    return left->start_time > right->start_time;
  }
};

struct OrderUIItemsByUrlAndThenMostRecentlyCreatedFirst {
  bool operator()(const DownloadUIItem* left,
                  const DownloadUIItem* right) const {
    if (left->url != right->url) {
      return left->url < right->url;
    }
    return left->start_time > right->start_time;
  }
};

}  // namespace

RecentTabSuggestionsProvider::RecentTabSuggestionsProvider(
    ContentSuggestionsProvider::Observer* observer,
    offline_pages::DownloadUIAdapter* ui_adapter,
    PrefService* pref_service)
    : ContentSuggestionsProvider(observer),
      category_status_(CategoryStatus::AVAILABLE_LOADING),
      provided_category_(
          Category::FromKnownCategory(KnownCategories::RECENT_TABS)),
      recent_tabs_ui_adapter_(ui_adapter),
      pref_service_(pref_service),
      weak_ptr_factory_(this) {
  observer->OnCategoryStatusChanged(this, provided_category_, category_status_);
  recent_tabs_ui_adapter_->AddObserver(this);
}

RecentTabSuggestionsProvider::~RecentTabSuggestionsProvider() {
  recent_tabs_ui_adapter_->RemoveObserver(this);
}

CategoryStatus RecentTabSuggestionsProvider::GetCategoryStatus(
    Category category) {
  if (category == provided_category_) {
    return category_status_;
  }
  NOTREACHED() << "Unknown category " << category.id();
  return CategoryStatus::NOT_PROVIDED;
}

CategoryInfo RecentTabSuggestionsProvider::GetCategoryInfo(Category category) {
  DCHECK_EQ(provided_category_, category);
  return CategoryInfo(
      l10n_util::GetStringUTF16(IDS_NTP_RECENT_TAB_SUGGESTIONS_SECTION_HEADER),
      ContentSuggestionsCardLayout::MINIMAL_CARD,
      ContentSuggestionsAdditionalAction::NONE,
      /*show_if_empty=*/false,
      l10n_util::GetStringUTF16(IDS_NTP_RECENT_TAB_SUGGESTIONS_SECTION_EMPTY));
}

void RecentTabSuggestionsProvider::DismissSuggestion(
    const ContentSuggestion::ID& suggestion_id) {
  DCHECK_EQ(provided_category_, suggestion_id.category());
  std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs();
  dismissed_ids.insert(suggestion_id.id_within_category());
  StoreDismissedIDsToPrefs(dismissed_ids);
}

void RecentTabSuggestionsProvider::FetchSuggestionImage(
    const ContentSuggestion::ID& suggestion_id,
    const ImageFetchedCallback& callback) {
  // TODO(vitaliii): Fetch proper thumbnail from OfflinePageModel once it's
  // available there.
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::Bind(callback, gfx::Image()));
}

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

void RecentTabSuggestionsProvider::ClearHistory(
    base::Time begin,
    base::Time end,
    const base::Callback<bool(const GURL& url)>& filter) {
  ClearDismissedSuggestionsForDebugging(provided_category_);
  FetchRecentTabs();
}

void RecentTabSuggestionsProvider::ClearCachedSuggestions(Category category) {
  // Ignored.
}

void RecentTabSuggestionsProvider::GetDismissedSuggestionsForDebugging(
    Category category,
    const DismissedSuggestionsCallback& callback) {
  DCHECK_EQ(provided_category_, category);

  std::vector<const DownloadUIItem*> items =
      recent_tabs_ui_adapter_->GetAllItems();

  std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs();
  std::vector<ContentSuggestion> suggestions;
  for (const DownloadUIItem* item : items) {
    int64_t offline_page_id =
        recent_tabs_ui_adapter_->GetOfflineIdByGuid(item->guid);
    if (!dismissed_ids.count(base::IntToString(offline_page_id))) {
      continue;
    }

    suggestions.push_back(ConvertUIItem(*item));
  }
  callback.Run(std::move(suggestions));
}

void RecentTabSuggestionsProvider::ClearDismissedSuggestionsForDebugging(
    Category category) {
  DCHECK_EQ(provided_category_, category);
  StoreDismissedIDsToPrefs(std::set<std::string>());
  FetchRecentTabs();
}

// static
void RecentTabSuggestionsProvider::RegisterProfilePrefs(
    PrefRegistrySimple* registry) {
  registry->RegisterListPref(prefs::kDismissedRecentOfflineTabSuggestions);
}

////////////////////////////////////////////////////////////////////////////////
// Private methods

void RecentTabSuggestionsProvider::ItemsLoaded() {
  FetchRecentTabs();
}

void RecentTabSuggestionsProvider::ItemAdded(const DownloadUIItem& ui_item) {
  FetchRecentTabs();
}

void RecentTabSuggestionsProvider::ItemUpdated(const DownloadUIItem& ui_item) {
  FetchRecentTabs();
}

void RecentTabSuggestionsProvider::ItemDeleted(
    const std::string& ui_item_guid) {
  // Because we never switch to NOT_PROVIDED dynamically, there can be no open
  // UI containing an invalidated suggestion unless the status is something
  // other than NOT_PROVIDED, so only notify invalidation in that case.
  if (category_status_ != CategoryStatus::NOT_PROVIDED) {
    InvalidateSuggestion(ui_item_guid);
  }
}

void RecentTabSuggestionsProvider::FetchRecentTabs() {
  std::vector<const DownloadUIItem*> ui_items =
      recent_tabs_ui_adapter_->GetAllItems();
  NotifyStatusChanged(CategoryStatus::AVAILABLE);
  std::set<std::string> old_dismissed_ids = ReadDismissedIDsFromPrefs();
  std::set<std::string> new_dismissed_ids;
  std::vector<const DownloadUIItem*> non_dismissed_items;

  for (const DownloadUIItem* item : ui_items) {
    std::string offline_page_id = base::IntToString(
        recent_tabs_ui_adapter_->GetOfflineIdByGuid(item->guid));
    if (old_dismissed_ids.count(offline_page_id)) {
      new_dismissed_ids.insert(offline_page_id);
    } else {
      non_dismissed_items.push_back(item);
    }
  }

  observer()->OnNewSuggestions(
      this, provided_category_,
      GetMostRecentlyCreatedWithoutDuplicates(std::move(non_dismissed_items)));
  if (new_dismissed_ids.size() != old_dismissed_ids.size()) {
    StoreDismissedIDsToPrefs(new_dismissed_ids);
  }
}

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

ContentSuggestion RecentTabSuggestionsProvider::ConvertUIItem(
    const DownloadUIItem& ui_item) const {
  // UI items have the Tab ID embedded in the GUID and the offline ID is
  // available by querying.
  int64_t offline_page_id =
      recent_tabs_ui_adapter_->GetOfflineIdByGuid(ui_item.guid);
  ContentSuggestion suggestion(provided_category_,
                               base::IntToString(offline_page_id), ui_item.url);
  suggestion.set_title(ui_item.title);
  suggestion.set_publish_date(ui_item.start_time);
  suggestion.set_publisher_name(base::UTF8ToUTF16(ui_item.url.host()));
  auto extra = base::MakeUnique<RecentTabSuggestionExtra>();
  int tab_id;
  bool success = base::StringToInt(ui_item.guid, &tab_id);
  DCHECK(success);
  extra->tab_id = tab_id;
  extra->offline_page_id = offline_page_id;
  suggestion.set_recent_tab_suggestion_extra(std::move(extra));

  return suggestion;
}

std::vector<ContentSuggestion>
RecentTabSuggestionsProvider::GetMostRecentlyCreatedWithoutDuplicates(
    std::vector<const DownloadUIItem*> ui_items) const {
  // |std::unique| only removes duplicates that immediately follow each other.
  // Thus, first, we have to sort by URL and creation time and only then remove
  // duplicates and sort the remaining items by creation time.
  std::sort(ui_items.begin(), ui_items.end(),
            OrderUIItemsByUrlAndThenMostRecentlyCreatedFirst());
  std::vector<const DownloadUIItem*>::iterator new_end =
      std::unique(ui_items.begin(), ui_items.end(),
                  [](const DownloadUIItem* left, const DownloadUIItem* right) {
                    return left->url == right->url;
                  });
  ui_items.erase(new_end, ui_items.end());
  std::sort(ui_items.begin(), ui_items.end(),
            OrderUIItemsByMostRecentlyCreatedFirst());
  std::vector<ContentSuggestion> suggestions;
  for (const DownloadUIItem* ui_item : ui_items) {
    suggestions.push_back(ConvertUIItem(*ui_item));
    if (static_cast<int>(suggestions.size()) == GetMaxSuggestionsCount()) {
      break;
    }
  }
  return suggestions;
}

void RecentTabSuggestionsProvider::InvalidateSuggestion(
    const std::string& ui_item_guid) {
  std::string offline_page_id = base::IntToString(
      recent_tabs_ui_adapter_->GetOfflineIdByGuid(ui_item_guid));
  observer()->OnSuggestionInvalidated(
      this, ContentSuggestion::ID(provided_category_, offline_page_id));

  std::set<std::string> dismissed_ids = ReadDismissedIDsFromPrefs();
  auto it = dismissed_ids.find(offline_page_id);
  if (it != dismissed_ids.end()) {
    dismissed_ids.erase(it);
    StoreDismissedIDsToPrefs(dismissed_ids);
  }
}

std::set<std::string> RecentTabSuggestionsProvider::ReadDismissedIDsFromPrefs()
    const {
  return prefs::ReadDismissedIDsFromPrefs(
      *pref_service_, prefs::kDismissedRecentOfflineTabSuggestions);
}

void RecentTabSuggestionsProvider::StoreDismissedIDsToPrefs(
    const std::set<std::string>& dismissed_ids) {
  prefs::StoreDismissedIDsToPrefs(pref_service_,
                                  prefs::kDismissedRecentOfflineTabSuggestions,
                                  dismissed_ids);
}

}  // namespace ntp_snippets