Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ntp_snippets/download_suggestions_provider.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/feature_list.h" | |
| 11 #include "base/guid.h" | |
| 12 #include "base/stl_util.h" | |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/strings/string_util.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 #include "base/time/time.h" | |
| 18 #include "chrome/grit/generated_resources.h" | |
| 19 #include "components/ntp_snippets/pref_names.h" | |
| 20 #include "components/ntp_snippets/pref_util.h" | |
| 21 #include "components/offline_pages/client_namespace_constants.h" | |
| 22 #include "components/prefs/pref_registry_simple.h" | |
| 23 #include "components/prefs/pref_service.h" | |
| 24 #include "net/base/filename_util.h" | |
| 25 #include "ui/base/l10n/l10n_util.h" | |
| 26 #include "ui/gfx/image/image.h" | |
| 27 | |
| 28 using content::DownloadItem; | |
| 29 using content::DownloadManager; | |
| 30 using ntp_snippets::Category; | |
| 31 using ntp_snippets::CategoryInfo; | |
| 32 using ntp_snippets::CategoryStatus; | |
| 33 using ntp_snippets::ContentSuggestion; | |
| 34 using ntp_snippets::prefs::kDismissedAssetDownloadSuggestions; | |
| 35 using ntp_snippets::prefs::kDismissedOfflinePageDownloadSuggestions; | |
| 36 using offline_pages::OfflinePageItem; | |
| 37 | |
| 38 namespace { | |
| 39 | |
| 40 // TODO(vitaliii): Make this configurable via a variation param. See | |
| 41 // crbug.com/654800. | |
| 42 const int kMaxSuggestionsCount = 5; | |
| 43 const char kAssetDownloadsPrefix = 'D'; | |
| 44 const char kOfflinePageDownloadsPrefix = 'O'; | |
| 45 | |
| 46 std::string GetOfflinePagePerCategoryID(int64_t raw_offline_page_id) { | |
| 47 // Raw ID is prefixed in order to avoid conflicts with asset downloads. | |
| 48 return std::string(1, kOfflinePageDownloadsPrefix) + | |
| 49 base::IntToString(raw_offline_page_id); | |
| 50 } | |
| 51 | |
| 52 std::string GetAssetDownloadPerCategoryID(uint32_t raw_download_id) { | |
| 53 // Raw ID is prefixed in order to avoid conflicts with offline page downloads. | |
| 54 return std::string(1, kAssetDownloadsPrefix) + | |
| 55 base::UintToString(raw_download_id); | |
| 56 } | |
| 57 | |
| 58 // Determines whether |suggestion_id| corresponds to offline page suggestion or | |
| 59 // asset download based on |id_within_category| prefix. | |
| 60 bool CorrespondsToOfflinePage(const ContentSuggestion::ID& suggestion_id) { | |
| 61 const std::string& id_within_category = suggestion_id.id_within_category(); | |
| 62 if (!id_within_category.empty()) { | |
| 63 if (id_within_category[0] == kOfflinePageDownloadsPrefix) | |
| 64 return true; | |
| 65 if (id_within_category[0] == kAssetDownloadsPrefix) | |
| 66 return false; | |
| 67 } | |
| 68 NOTREACHED() << "Unknown id_within_category " << id_within_category; | |
| 69 return false; | |
| 70 } | |
| 71 | |
| 72 bool IsOfflinePageDownload(const offline_pages::ClientId& client_id) { | |
| 73 return (client_id.name_space == offline_pages::kAsyncNamespace || | |
| 74 client_id.name_space == offline_pages::kDownloadNamespace || | |
| 75 client_id.name_space == offline_pages::kNTPSuggestionsNamespace); | |
|
Marc Treib
2016/10/26 08:54:09
nitty nit: parens not required
vitaliii
2016/10/27 15:49:19
Done.
| |
| 76 } | |
| 77 | |
| 78 bool IsDownloadCompleted(const DownloadItem& item) { | |
| 79 return item.GetState() == DownloadItem::DownloadState::COMPLETE && | |
| 80 !item.GetFileExternallyRemoved(); | |
| 81 } | |
| 82 | |
| 83 struct OrderDownloadsMostRecentlyDownloadedFirst { | |
| 84 bool operator()(const DownloadItem* left, const DownloadItem* right) const { | |
| 85 return left->GetEndTime() > right->GetEndTime(); | |
| 86 } | |
| 87 }; | |
| 88 | |
| 89 // TODO(vitaliii): Move this function to FilePath. | |
| 90 base::string16 StringTypeToUTF16( | |
| 91 const base::FilePath::StringType& string_type) { | |
| 92 #if defined(OS_POSIX) | |
| 93 DCHECK(base::IsStringUTF8(string_type)); | |
|
Bernhard Bauer
2016/10/26 10:19:32
Note that this assumption is definitely not true i
vitaliii
2016/10/27 15:49:20
Thanks, I missed LossyDisplayName (though the name
| |
| 94 return base::UTF8ToUTF16(string_type); | |
| 95 #elif defined(OS_WIN) | |
| 96 return base::WideToUTF16(string_type); | |
| 97 #endif // OS_WIN | |
| 98 } | |
| 99 | |
| 100 } // namespace | |
| 101 | |
| 102 DownloadSuggestionsProvider::DownloadSuggestionsProvider( | |
| 103 ContentSuggestionsProvider::Observer* observer, | |
| 104 ntp_snippets::CategoryFactory* category_factory, | |
| 105 const scoped_refptr<ntp_snippets::OfflinePageProxy>& offline_page_proxy, | |
|
Bernhard Bauer
2016/10/26 10:19:32
No const-ref here either.
vitaliii
2016/10/27 15:49:20
Done.
| |
| 106 content::DownloadManager* download_manager, | |
| 107 PrefService* pref_service, | |
| 108 bool download_manager_ui_enabled) | |
| 109 : ContentSuggestionsProvider(observer, category_factory), | |
| 110 category_status_(CategoryStatus::AVAILABLE_LOADING), | |
| 111 provided_category_(category_factory->FromKnownCategory( | |
| 112 ntp_snippets::KnownCategories::DOWNLOADS)), | |
| 113 offline_page_proxy_(offline_page_proxy), | |
| 114 download_manager_(download_manager), | |
| 115 pref_service_(pref_service), | |
| 116 download_manager_ui_enabled_(download_manager_ui_enabled), | |
| 117 weak_ptr_factory_(this) { | |
| 118 observer->OnCategoryStatusChanged(this, provided_category_, category_status_); | |
| 119 offline_page_proxy_->AddObserver(this); | |
| 120 if (download_manager_) | |
| 121 download_manager_->AddObserver(this); | |
| 122 // No need to explicitly fetch the asset downloads, since for each of them | |
| 123 // |OnDownloadCreated| is fired. | |
| 124 AsynchronouslyFetchOfflinePagesDownloads(/*notify=*/true); | |
| 125 } | |
| 126 | |
| 127 DownloadSuggestionsProvider::~DownloadSuggestionsProvider() { | |
| 128 offline_page_proxy_->RemoveObserver(this); | |
| 129 if (download_manager_) { | |
| 130 download_manager_->RemoveObserver(this); | |
| 131 UnregisterDownloadItemObservers(); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 CategoryStatus DownloadSuggestionsProvider::GetCategoryStatus( | |
| 136 Category category) { | |
| 137 DCHECK_EQ(provided_category_, category); | |
| 138 return category_status_; | |
| 139 } | |
| 140 | |
| 141 CategoryInfo DownloadSuggestionsProvider::GetCategoryInfo(Category category) { | |
| 142 DCHECK_EQ(provided_category_, category); | |
| 143 return CategoryInfo( | |
| 144 l10n_util::GetStringUTF16(IDS_NTP_DOWNLOAD_SUGGESTIONS_SECTION_HEADER), | |
| 145 ntp_snippets::ContentSuggestionsCardLayout::MINIMAL_CARD, | |
| 146 /*has_more_button=*/download_manager_ui_enabled_, | |
| 147 /*show_if_empty=*/false); | |
| 148 } | |
| 149 | |
| 150 void DownloadSuggestionsProvider::DismissSuggestion( | |
| 151 const ContentSuggestion::ID& suggestion_id) { | |
| 152 DCHECK_EQ(provided_category_, suggestion_id.category()); | |
| 153 std::set<std::string> dismissed_ids = | |
| 154 ReadDismissedIDsFromPrefs(CorrespondsToOfflinePage(suggestion_id)); | |
| 155 dismissed_ids.insert(suggestion_id.id_within_category()); | |
| 156 StoreDismissedIDsToPrefs(CorrespondsToOfflinePage(suggestion_id), | |
| 157 dismissed_ids); | |
| 158 | |
| 159 RemoveSuggestionFromCacheAndRetrieveMoreIfNeeded(suggestion_id); | |
| 160 } | |
| 161 | |
| 162 void DownloadSuggestionsProvider::FetchSuggestionImage( | |
| 163 const ContentSuggestion::ID& suggestion_id, | |
| 164 const ImageFetchedCallback& callback) { | |
| 165 // TODO(vitaliii): Fetch proper thumbnail from OfflinePageModel once it is | |
| 166 // available there. | |
| 167 // TODO(vitaliii): Provide site's favicon for assets downloads. See | |
| 168 // crbug.com/631447. | |
| 169 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 170 FROM_HERE, base::Bind(callback, gfx::Image())); | |
| 171 } | |
| 172 | |
| 173 void DownloadSuggestionsProvider::ClearHistory( | |
| 174 base::Time begin, | |
| 175 base::Time end, | |
| 176 const base::Callback<bool(const GURL& url)>& filter) { | |
| 177 cached_offline_page_downloads_.clear(); | |
| 178 cached_asset_downloads_.clear(); | |
| 179 // This will trigger an asynchronous re-fetch. | |
| 180 ClearDismissedSuggestionsForDebugging(provided_category_); | |
| 181 } | |
| 182 | |
| 183 void DownloadSuggestionsProvider::ClearCachedSuggestions(Category category) { | |
| 184 DCHECK_EQ(provided_category_, category); | |
| 185 // Ignored. The internal caches are not stored on disk and they are just | |
| 186 // partial copies of the data stored at OfflinePage model and DownloadManager. | |
| 187 // If it is cleared there, it will be cleared in these caches as well. | |
| 188 } | |
| 189 | |
| 190 void DownloadSuggestionsProvider::GetDismissedSuggestionsForDebugging( | |
| 191 Category category, | |
| 192 const DismissedSuggestionsCallback& callback) { | |
| 193 DCHECK_EQ(provided_category_, category); | |
| 194 | |
| 195 offline_page_proxy_->GetAllPages( | |
| 196 base::Bind(&DownloadSuggestionsProvider:: | |
| 197 GetAllPagesCallbackForGetDismissedSuggestions, | |
| 198 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 199 } | |
| 200 | |
| 201 void DownloadSuggestionsProvider::ClearDismissedSuggestionsForDebugging( | |
| 202 Category category) { | |
| 203 DCHECK_EQ(provided_category_, category); | |
| 204 StoreAssetDismissedIDsToPrefs(std::set<std::string>()); | |
| 205 StoreOfflinePageDismissedIDsToPrefs(std::set<std::string>()); | |
| 206 AsynchronouslyFetchAllDownloadsAndSubmitSuggestions(); | |
| 207 } | |
| 208 | |
| 209 // static | |
| 210 void DownloadSuggestionsProvider::RegisterProfilePrefs( | |
| 211 PrefRegistrySimple* registry) { | |
| 212 registry->RegisterListPref(kDismissedAssetDownloadSuggestions); | |
| 213 registry->RegisterListPref(kDismissedOfflinePageDownloadSuggestions); | |
| 214 } | |
| 215 | |
| 216 //////////////////////////////////////////////////////////////////////////////// | |
| 217 // Private methods | |
| 218 | |
| 219 void DownloadSuggestionsProvider::GetAllPagesCallbackForGetDismissedSuggestions( | |
| 220 const DismissedSuggestionsCallback& callback, | |
| 221 const std::vector<OfflinePageItem>& offline_pages) const { | |
| 222 std::set<std::string> dismissed_ids = ReadOfflinePageDismissedIDsFromPrefs(); | |
| 223 std::vector<ContentSuggestion> suggestions; | |
| 224 for (const OfflinePageItem& item : offline_pages) { | |
| 225 if (dismissed_ids.count(GetOfflinePagePerCategoryID(item.offline_id))) | |
| 226 suggestions.push_back(ConvertOfflinePage(item)); | |
| 227 } | |
| 228 | |
| 229 if (download_manager_) { | |
| 230 std::vector<DownloadItem*> all_downloads; | |
| 231 download_manager_->GetAllDownloads(&all_downloads); | |
| 232 | |
| 233 dismissed_ids = ReadAssetDismissedIDsFromPrefs(); | |
| 234 | |
| 235 for (const DownloadItem* item : all_downloads) { | |
| 236 if (dismissed_ids.count(GetAssetDownloadPerCategoryID(item->GetId()))) | |
| 237 suggestions.push_back(ConvertDownloadItem(*item)); | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 callback.Run(std::move(suggestions)); | |
| 242 } | |
| 243 | |
| 244 void DownloadSuggestionsProvider::OfflinePageModelChanged( | |
| 245 const std::vector<offline_pages::OfflinePageItem>& offline_pages) { | |
| 246 UpdateOfflinePagesCache(/*notify=*/true, offline_pages); | |
| 247 } | |
| 248 | |
| 249 void DownloadSuggestionsProvider::OfflinePageDeleted( | |
| 250 int64_t offline_id, | |
| 251 const offline_pages::ClientId& client_id) { | |
| 252 if (IsOfflinePageDownload(client_id)) { | |
| 253 InvalidateSuggestion(GetOfflinePagePerCategoryID(offline_id)); | |
| 254 } | |
| 255 } | |
| 256 | |
| 257 void DownloadSuggestionsProvider::OnDownloadCreated(DownloadManager* manager, | |
| 258 DownloadItem* item) { | |
| 259 DCHECK_EQ(download_manager_, manager); | |
| 260 // This is called when new downloads are started and on startup for existing | |
| 261 // ones. We listen to each item to know when it is destroyed. | |
| 262 item->AddObserver(this); | |
| 263 if (CacheAssetDownloadIfNeeded(item)) | |
| 264 SubmitContentSuggestions(); | |
| 265 } | |
| 266 | |
| 267 void DownloadSuggestionsProvider::ManagerGoingDown(DownloadManager* manager) { | |
| 268 DCHECK_EQ(download_manager_, manager); | |
| 269 UnregisterDownloadItemObservers(); | |
| 270 download_manager_ = nullptr; | |
| 271 } | |
| 272 | |
| 273 void DownloadSuggestionsProvider::OnDownloadUpdated(DownloadItem* item) { | |
| 274 if (base::ContainsValue(cached_asset_downloads_, item)) { | |
| 275 if (item->GetFileExternallyRemoved()) { | |
| 276 InvalidateSuggestion(GetAssetDownloadPerCategoryID(item->GetId())); | |
| 277 } else { | |
| 278 // The download may have changed. | |
| 279 SubmitContentSuggestions(); | |
| 280 } | |
| 281 } else { | |
| 282 // Unfinished downloads may become completed. | |
| 283 if (CacheAssetDownloadIfNeeded(item)) | |
| 284 SubmitContentSuggestions(); | |
| 285 } | |
| 286 } | |
| 287 | |
| 288 void DownloadSuggestionsProvider::OnDownloadOpened(DownloadItem* item) { | |
| 289 // Ignored. | |
| 290 } | |
| 291 | |
| 292 void DownloadSuggestionsProvider::OnDownloadRemoved(DownloadItem* item) { | |
| 293 // Ignored. We listen to |OnDownloadDestroyed| instead. The reason is that | |
| 294 // we may need to retrieve all downloads, but |OnDownloadRemoved| is called | |
| 295 // before the download is removed from the list. | |
| 296 } | |
| 297 | |
| 298 void DownloadSuggestionsProvider::OnDownloadDestroyed( | |
| 299 content::DownloadItem* item) { | |
| 300 item->RemoveObserver(this); | |
| 301 | |
| 302 if (!IsDownloadCompleted(*item)) | |
| 303 return; | |
| 304 // TODO(vitaliii): Implement a better way to clean up dismissed IDs (in case | |
| 305 // some calls are missed). | |
| 306 InvalidateSuggestion(GetAssetDownloadPerCategoryID(item->GetId())); | |
| 307 } | |
| 308 | |
| 309 void DownloadSuggestionsProvider::NotifyStatusChanged( | |
| 310 CategoryStatus new_status) { | |
| 311 DCHECK_NE(CategoryStatus::NOT_PROVIDED, category_status_); | |
| 312 DCHECK_NE(CategoryStatus::NOT_PROVIDED, new_status); | |
| 313 if (category_status_ == new_status) | |
| 314 return; | |
| 315 category_status_ = new_status; | |
| 316 observer()->OnCategoryStatusChanged(this, provided_category_, | |
| 317 category_status_); | |
| 318 } | |
| 319 | |
| 320 void DownloadSuggestionsProvider::AsynchronouslyFetchOfflinePagesDownloads( | |
| 321 bool notify) { | |
| 322 offline_page_proxy_->GetAllPages( | |
| 323 base::Bind(&DownloadSuggestionsProvider::UpdateOfflinePagesCache, | |
| 324 weak_ptr_factory_.GetWeakPtr(), notify)); | |
| 325 } | |
| 326 | |
| 327 void DownloadSuggestionsProvider::FetchAssetsDownloads() { | |
| 328 if (!download_manager_) { | |
| 329 // The manager has gone down. | |
| 330 return; | |
| 331 } | |
| 332 | |
| 333 std::vector<DownloadItem*> all_downloads; | |
| 334 download_manager_->GetAllDownloads(&all_downloads); | |
| 335 std::set<std::string> old_dismissed_ids = ReadAssetDismissedIDsFromPrefs(); | |
| 336 std::set<std::string> retained_dismissed_ids; | |
| 337 cached_asset_downloads_.clear(); | |
| 338 for (const DownloadItem* item : all_downloads) { | |
| 339 std::string within_category_id = | |
| 340 GetAssetDownloadPerCategoryID(item->GetId()); | |
| 341 if (!old_dismissed_ids.count(within_category_id)) { | |
| 342 if (IsDownloadCompleted(*item)) | |
| 343 cached_asset_downloads_.push_back(item); | |
| 344 } else { | |
| 345 retained_dismissed_ids.insert(within_category_id); | |
| 346 } | |
| 347 } | |
| 348 | |
| 349 if (old_dismissed_ids.size() != retained_dismissed_ids.size()) { | |
| 350 StoreAssetDismissedIDsToPrefs(retained_dismissed_ids); | |
| 351 } | |
| 352 | |
| 353 if (static_cast<int>(cached_asset_downloads_.size()) > kMaxSuggestionsCount) { | |
| 354 // Partially sorts |downloads| such that: | |
| 355 // 1) The element at the index |kMaxSuggestionsCount| is changed to the | |
| 356 // element which would occur on this position if |downloads| was sorted; | |
| 357 // 2) All of the elements before index |kMaxSuggestionsCount| are less than | |
| 358 // or equal to the elements after it. | |
| 359 std::nth_element(cached_asset_downloads_.begin(), | |
| 360 cached_asset_downloads_.begin() + kMaxSuggestionsCount, | |
| 361 cached_asset_downloads_.end(), | |
| 362 OrderDownloadsMostRecentlyDownloadedFirst()); | |
| 363 cached_asset_downloads_.resize(kMaxSuggestionsCount); | |
| 364 } | |
| 365 } | |
| 366 | |
| 367 void DownloadSuggestionsProvider:: | |
| 368 AsynchronouslyFetchAllDownloadsAndSubmitSuggestions() { | |
| 369 FetchAssetsDownloads(); | |
| 370 AsynchronouslyFetchOfflinePagesDownloads(/*notify=*/true); | |
| 371 } | |
| 372 | |
| 373 void DownloadSuggestionsProvider::SubmitContentSuggestions() { | |
| 374 NotifyStatusChanged(CategoryStatus::AVAILABLE); | |
| 375 | |
| 376 std::vector<ContentSuggestion> suggestions; | |
| 377 for (const OfflinePageItem& item : cached_offline_page_downloads_) | |
| 378 suggestions.push_back(ConvertOfflinePage(item)); | |
| 379 | |
| 380 for (const DownloadItem* item : cached_asset_downloads_) | |
| 381 suggestions.push_back(ConvertDownloadItem(*item)); | |
| 382 | |
| 383 std::sort(suggestions.begin(), suggestions.end(), | |
| 384 [](const ContentSuggestion& left, const ContentSuggestion& right) { | |
| 385 return left.publish_date() > right.publish_date(); | |
| 386 }); | |
| 387 | |
| 388 // |resize()| cannot be used here (does not compile), because | |
| 389 // |ContentSuggestion|'s move constructor is not marked |noexcept|. |resize()| | |
| 390 // provides a strong guarantee that it either succeeds or the state of the | |
| 391 // vector is left unchanged, therefore, the only option left is to copy, but | |
| 392 // |ContentSuggestion| is not copyable either. | |
|
Bernhard Bauer
2016/10/26 10:19:32
It just *might* be worth pushing for getting the e
vitaliii
2016/10/27 15:49:20
I posted there, though I do not expect to get any
Bernhard Bauer
2016/10/27 15:57:09
Yeah, that's fine; maybe add a TODO to use resize(
vitaliii
2016/11/01 03:09:54
The style guide change has already been done. Howe
| |
| 393 // On the contrary, |pop_back()| does not reallocate any memory. | |
| 394 while (suggestions.size() > kMaxSuggestionsCount) | |
| 395 suggestions.pop_back(); | |
| 396 | |
| 397 observer()->OnNewSuggestions(this, provided_category_, | |
| 398 std::move(suggestions)); | |
| 399 } | |
| 400 | |
| 401 ContentSuggestion DownloadSuggestionsProvider::ConvertOfflinePage( | |
| 402 const OfflinePageItem& offline_page) const { | |
| 403 // TODO(vitaliii): Make sure the URL is actually opened as an offline URL even | |
| 404 // when the user is online. See crbug.com/641568. | |
| 405 ContentSuggestion suggestion( | |
| 406 ContentSuggestion::ID(provided_category_, GetOfflinePagePerCategoryID( | |
| 407 offline_page.offline_id)), | |
| 408 offline_page.url); | |
| 409 | |
| 410 if (offline_page.title.empty()) { | |
| 411 // TODO(vitaliii): Remove this fallback once the OfflinePageModel provides | |
| 412 // titles for all (relevant) OfflinePageItems. | |
| 413 suggestion.set_title(base::UTF8ToUTF16(offline_page.url.spec())); | |
| 414 } else { | |
| 415 suggestion.set_title(offline_page.title); | |
| 416 } | |
| 417 suggestion.set_publish_date(offline_page.creation_time); | |
| 418 suggestion.set_publisher_name(base::UTF8ToUTF16(offline_page.url.host())); | |
| 419 return suggestion; | |
| 420 } | |
| 421 | |
| 422 ContentSuggestion DownloadSuggestionsProvider::ConvertDownloadItem( | |
| 423 const DownloadItem& download_item) const { | |
| 424 // TODO(vitaliii): Ensure that files are opened in browser, but not downloaded | |
| 425 // again. See crbug.com/641568. | |
| 426 ContentSuggestion suggestion( | |
| 427 ContentSuggestion::ID(provided_category_, GetAssetDownloadPerCategoryID( | |
| 428 download_item.GetId())), | |
| 429 net::FilePathToFileURL(download_item.GetTargetFilePath())); | |
| 430 // TODO(vitaliii): Set proper title. | |
| 431 suggestion.set_title( | |
| 432 StringTypeToUTF16(download_item.GetTargetFilePath().BaseName().value())); | |
| 433 suggestion.set_publish_date(download_item.GetEndTime()); | |
| 434 suggestion.set_publisher_name( | |
| 435 base::UTF8ToUTF16(download_item.GetURL().host())); | |
| 436 // TODO(vitaliii): Set suggestion icon. | |
| 437 return suggestion; | |
| 438 } | |
| 439 | |
| 440 bool DownloadSuggestionsProvider::CacheAssetDownloadIfNeeded( | |
| 441 const content::DownloadItem* item) { | |
| 442 if (!IsDownloadCompleted(*item)) | |
| 443 return false; | |
| 444 | |
| 445 if (base::ContainsValue(cached_asset_downloads_, item)) | |
| 446 return false; | |
| 447 | |
| 448 std::set<std::string> dismissed_ids = ReadAssetDismissedIDsFromPrefs(); | |
| 449 if (dismissed_ids.count(GetAssetDownloadPerCategoryID(item->GetId()))) | |
| 450 return false; | |
| 451 | |
| 452 DCHECK_LE(static_cast<int>(cached_asset_downloads_.size()), | |
| 453 kMaxSuggestionsCount); | |
| 454 if (cached_asset_downloads_.size() == kMaxSuggestionsCount) { | |
| 455 auto oldest = std::max_element(cached_asset_downloads_.begin(), | |
| 456 cached_asset_downloads_.end(), | |
| 457 OrderDownloadsMostRecentlyDownloadedFirst()); | |
| 458 if (item->GetEndTime() <= (*oldest)->GetEndTime()) | |
| 459 return false; | |
| 460 | |
| 461 *oldest = item; | |
| 462 } else { | |
| 463 cached_asset_downloads_.push_back(item); | |
| 464 } | |
| 465 | |
| 466 return true; | |
| 467 } | |
| 468 | |
| 469 bool DownloadSuggestionsProvider::RemoveSuggestionFromCacheIfPresent( | |
| 470 const ContentSuggestion::ID& suggestion_id) { | |
| 471 DCHECK_EQ(provided_category_, suggestion_id.category()); | |
| 472 if (CorrespondsToOfflinePage(suggestion_id)) { | |
| 473 auto matching = | |
| 474 std::find_if(cached_offline_page_downloads_.begin(), | |
| 475 cached_offline_page_downloads_.end(), | |
| 476 [&suggestion_id](const OfflinePageItem& item) { | |
| 477 return GetOfflinePagePerCategoryID(item.offline_id) == | |
| 478 suggestion_id.id_within_category(); | |
| 479 }); | |
| 480 if (matching != cached_offline_page_downloads_.end()) { | |
| 481 cached_offline_page_downloads_.erase(matching); | |
| 482 return true; | |
| 483 } | |
| 484 return false; | |
| 485 } | |
| 486 | |
| 487 auto matching = std::find_if( | |
| 488 cached_asset_downloads_.begin(), cached_asset_downloads_.end(), | |
| 489 [&suggestion_id](const DownloadItem* item) { | |
| 490 return GetAssetDownloadPerCategoryID(item->GetId()) == | |
| 491 suggestion_id.id_within_category(); | |
| 492 }); | |
| 493 if (matching != cached_asset_downloads_.end()) { | |
| 494 cached_asset_downloads_.erase(matching); | |
| 495 return true; | |
| 496 } | |
| 497 return false; | |
| 498 } | |
| 499 | |
| 500 void DownloadSuggestionsProvider:: | |
| 501 RemoveSuggestionFromCacheAndRetrieveMoreIfNeeded( | |
| 502 const ContentSuggestion::ID& suggestion_id) { | |
| 503 DCHECK_EQ(provided_category_, suggestion_id.category()); | |
| 504 if (!RemoveSuggestionFromCacheIfPresent(suggestion_id)) | |
| 505 return; | |
| 506 | |
| 507 if (CorrespondsToOfflinePage(suggestion_id)) { | |
| 508 if (cached_offline_page_downloads_.size() == kMaxSuggestionsCount - 1) { | |
| 509 // Previously there were |kMaxSuggestionsCount| cached suggestion, | |
| 510 // therefore, overall there may be more than |kMaxSuggestionsCount| | |
| 511 // suggestions in the model and now one of them may be cached instead of | |
| 512 // the removed one. Even though, the suggestions are not immediately | |
| 513 // used the cache has to be kept up to date, because it may be used when | |
| 514 // other data source is updated. | |
| 515 AsynchronouslyFetchOfflinePagesDownloads(/*notify=*/false); | |
| 516 } | |
| 517 } else { | |
| 518 if (cached_asset_downloads_.size() == kMaxSuggestionsCount - 1) { | |
| 519 // The same as the case above. | |
| 520 FetchAssetsDownloads(); | |
| 521 } | |
| 522 } | |
| 523 } | |
| 524 | |
| 525 void DownloadSuggestionsProvider::UpdateOfflinePagesCache( | |
| 526 bool notify, | |
| 527 const std::vector<offline_pages::OfflinePageItem>& all_offline_pages) { | |
| 528 std::set<std::string> old_dismissed_ids = | |
| 529 ReadOfflinePageDismissedIDsFromPrefs(); | |
| 530 std::set<std::string> retained_dismissed_ids; | |
| 531 std::vector<const OfflinePageItem*> items; | |
| 532 // Filtering out dismissed items and pruning dismissed IDs. | |
| 533 for (const OfflinePageItem& item : all_offline_pages) { | |
| 534 if (!IsOfflinePageDownload(item.client_id)) | |
| 535 continue; | |
| 536 | |
| 537 std::string id_within_category = | |
| 538 GetOfflinePagePerCategoryID(item.offline_id); | |
| 539 if (!old_dismissed_ids.count(id_within_category)) | |
| 540 items.push_back(&item); | |
| 541 else | |
| 542 retained_dismissed_ids.insert(id_within_category); | |
| 543 } | |
| 544 | |
| 545 if (static_cast<int>(items.size()) > kMaxSuggestionsCount) { | |
| 546 // Partially sorts |items| such that: | |
| 547 // 1) The element at the index |kMaxSuggestionsCount| is changed to the | |
| 548 // element which would occur on this position if |items| was sorted; | |
| 549 // 2) All of the elements before index |kMaxSuggestionsCount| are less than | |
| 550 // or equal to the elements after it. | |
| 551 std::nth_element( | |
| 552 items.begin(), items.begin() + kMaxSuggestionsCount, items.end(), | |
| 553 [](const OfflinePageItem* left, const OfflinePageItem* right) { | |
| 554 return left->creation_time > right->creation_time; | |
| 555 }); | |
| 556 items.resize(kMaxSuggestionsCount); | |
| 557 } | |
| 558 | |
| 559 cached_offline_page_downloads_.clear(); | |
| 560 for (const OfflinePageItem* item : items) { | |
| 561 cached_offline_page_downloads_.push_back(*item); | |
| 562 } | |
| 563 | |
| 564 if (old_dismissed_ids.size() != retained_dismissed_ids.size()) { | |
|
Bernhard Bauer
2016/10/26 10:19:32
Be consistent whether you use braces for single-li
vitaliii
2016/10/27 15:49:19
Done.
| |
| 565 StoreOfflinePageDismissedIDsToPrefs(retained_dismissed_ids); | |
| 566 } | |
| 567 | |
| 568 if (notify) | |
| 569 SubmitContentSuggestions(); | |
| 570 } | |
| 571 | |
| 572 void DownloadSuggestionsProvider::InvalidateSuggestion( | |
| 573 const std::string& id_within_category) { | |
| 574 ContentSuggestion::ID suggestion_id(provided_category_, id_within_category); | |
| 575 observer()->OnSuggestionInvalidated(this, suggestion_id); | |
| 576 | |
| 577 std::set<std::string> dismissed_ids = | |
| 578 ReadDismissedIDsFromPrefs(CorrespondsToOfflinePage(suggestion_id)); | |
| 579 auto it = dismissed_ids.find(id_within_category); | |
| 580 if (it != dismissed_ids.end()) { | |
| 581 dismissed_ids.erase(it); | |
| 582 StoreDismissedIDsToPrefs(CorrespondsToOfflinePage(suggestion_id), | |
| 583 dismissed_ids); | |
| 584 } | |
| 585 | |
| 586 RemoveSuggestionFromCacheAndRetrieveMoreIfNeeded(suggestion_id); | |
| 587 } | |
| 588 | |
| 589 std::set<std::string> | |
| 590 DownloadSuggestionsProvider::ReadAssetDismissedIDsFromPrefs() const { | |
| 591 return ntp_snippets::prefs::ReadDismissedIDsFromPrefs( | |
| 592 *pref_service_, kDismissedAssetDownloadSuggestions); | |
| 593 } | |
| 594 | |
| 595 void DownloadSuggestionsProvider::StoreAssetDismissedIDsToPrefs( | |
| 596 const std::set<std::string>& dismissed_ids) { | |
| 597 DCHECK(std::all_of( | |
| 598 dismissed_ids.begin(), dismissed_ids.end(), | |
| 599 [](const std::string& id) { return id[0] == kAssetDownloadsPrefix; })); | |
| 600 ntp_snippets::prefs::StoreDismissedIDsToPrefs( | |
| 601 pref_service_, kDismissedAssetDownloadSuggestions, dismissed_ids); | |
| 602 } | |
| 603 | |
| 604 std::set<std::string> | |
| 605 DownloadSuggestionsProvider::ReadOfflinePageDismissedIDsFromPrefs() const { | |
| 606 return ntp_snippets::prefs::ReadDismissedIDsFromPrefs( | |
| 607 *pref_service_, kDismissedOfflinePageDownloadSuggestions); | |
| 608 } | |
| 609 | |
| 610 void DownloadSuggestionsProvider::StoreOfflinePageDismissedIDsToPrefs( | |
| 611 const std::set<std::string>& dismissed_ids) { | |
| 612 DCHECK(std::all_of(dismissed_ids.begin(), dismissed_ids.end(), | |
| 613 [](const std::string& id) { | |
| 614 return id[0] == kOfflinePageDownloadsPrefix; | |
| 615 })); | |
| 616 ntp_snippets::prefs::StoreDismissedIDsToPrefs( | |
| 617 pref_service_, kDismissedOfflinePageDownloadSuggestions, dismissed_ids); | |
| 618 } | |
| 619 | |
| 620 std::set<std::string> DownloadSuggestionsProvider::ReadDismissedIDsFromPrefs( | |
| 621 bool for_offline_page_downloads) const { | |
| 622 if (for_offline_page_downloads) { | |
| 623 return ReadOfflinePageDismissedIDsFromPrefs(); | |
| 624 } | |
| 625 return ReadAssetDismissedIDsFromPrefs(); | |
| 626 } | |
| 627 | |
| 628 // TODO(vitaliii): Store one set instead of two. See crbug.com/656024. | |
| 629 void DownloadSuggestionsProvider::StoreDismissedIDsToPrefs( | |
| 630 bool for_offline_page_downloads, | |
| 631 const std::set<std::string>& dismissed_ids) { | |
| 632 if (for_offline_page_downloads) | |
| 633 StoreOfflinePageDismissedIDsToPrefs(dismissed_ids); | |
| 634 else | |
| 635 StoreAssetDismissedIDsToPrefs(dismissed_ids); | |
| 636 } | |
| 637 | |
| 638 void DownloadSuggestionsProvider::UnregisterDownloadItemObservers() { | |
| 639 if (download_manager_) { | |
|
Marc Treib
2016/10/26 08:54:09
optional: "if (!download_manager_) return;" and sa
vitaliii
2016/10/27 15:49:20
Now we don't, but what if we will?
Bernhard Bauer
2016/10/27 15:57:09
Assuming that you remove the check, you'll get a c
vitaliii
2016/11/01 03:09:54
Good point, added the DCHECK.
| |
| 640 std::vector<DownloadItem*> all_downloads; | |
| 641 download_manager_->GetAllDownloads(&all_downloads); | |
| 642 | |
| 643 for (DownloadItem* item : all_downloads) { | |
| 644 item->RemoveObserver(this); | |
| 645 } | |
| 646 } | |
| 647 } | |
| OLD | NEW |