| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ntp_snippets/offline_pages/offline_page_suggestions_provide
r.h" | 5 #include "components/ntp_snippets/offline_pages/offline_page_suggestions_provide
r.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/guid.h" | 10 #include "base/guid.h" |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 | 209 |
| 210 void OfflinePageSuggestionsProvider::OfflinePageModelChanged( | 210 void OfflinePageSuggestionsProvider::OfflinePageModelChanged( |
| 211 OfflinePageModel* model) { | 211 OfflinePageModel* model) { |
| 212 DCHECK_EQ(offline_page_model_, model); | 212 DCHECK_EQ(offline_page_model_, model); |
| 213 FetchOfflinePages(); | 213 FetchOfflinePages(); |
| 214 } | 214 } |
| 215 | 215 |
| 216 void OfflinePageSuggestionsProvider::OfflinePageDeleted( | 216 void OfflinePageSuggestionsProvider::OfflinePageDeleted( |
| 217 int64_t offline_id, | 217 int64_t offline_id, |
| 218 const offline_pages::ClientId& client_id) { | 218 const offline_pages::ClientId& client_id) { |
| 219 // TODO(pke): Implement, suggestion has to be removed from UI immediately. | 219 std::string offline_page_id = base::IntToString(offline_id); |
| 220 if (recent_tabs_status_ != CategoryStatus::NOT_PROVIDED && |
| 221 client_id.name_space == offline_pages::kLastNNamespace) { |
| 222 auto it = std::find(dismissed_recent_tab_ids_.begin(), |
| 223 dismissed_recent_tab_ids_.end(), offline_page_id); |
| 224 if (it == dismissed_recent_tab_ids_.end()) { |
| 225 observer()->OnSuggestionInvalidated( |
| 226 this, recent_tabs_category_, |
| 227 MakeUniqueID(recent_tabs_category_, offline_page_id)); |
| 228 } else { |
| 229 dismissed_recent_tab_ids_.erase(it); |
| 230 StoreDismissedIDsToPrefs(prefs::kDismissedRecentOfflineTabSuggestions, |
| 231 dismissed_recent_tab_ids_); |
| 232 } |
| 233 } else if (downloads_status_ != CategoryStatus::NOT_PROVIDED && |
| 234 client_id.name_space == offline_pages::kAsyncNamespace && |
| 235 base::IsValidGUID(client_id.id)) { |
| 236 auto it = std::find(dismissed_download_ids_.begin(), |
| 237 dismissed_download_ids_.end(), offline_page_id); |
| 238 if (it == dismissed_download_ids_.end()) { |
| 239 observer()->OnSuggestionInvalidated( |
| 240 this, downloads_category_, |
| 241 MakeUniqueID(downloads_category_, offline_page_id)); |
| 242 } else { |
| 243 dismissed_download_ids_.erase(it); |
| 244 StoreDismissedIDsToPrefs(prefs::kDismissedDownloadSuggestions, |
| 245 dismissed_download_ids_); |
| 246 } |
| 247 } |
| 220 } | 248 } |
| 221 | 249 |
| 222 void OfflinePageSuggestionsProvider::FetchOfflinePages() { | 250 void OfflinePageSuggestionsProvider::FetchOfflinePages() { |
| 223 offline_page_model_->GetAllPages( | 251 offline_page_model_->GetAllPages( |
| 224 base::Bind(&OfflinePageSuggestionsProvider::OnOfflinePagesLoaded, | 252 base::Bind(&OfflinePageSuggestionsProvider::OnOfflinePagesLoaded, |
| 225 base::Unretained(this))); | 253 base::Unretained(this))); |
| 226 } | 254 } |
| 227 | 255 |
| 228 void OfflinePageSuggestionsProvider::OnOfflinePagesLoaded( | 256 void OfflinePageSuggestionsProvider::OnOfflinePagesLoaded( |
| 229 const MultipleOfflinePageItemResult& result) { | 257 const MultipleOfflinePageItemResult& result) { |
| 230 bool need_recent_tabs = recent_tabs_status_ != CategoryStatus::NOT_PROVIDED; | 258 bool need_recent_tabs = recent_tabs_status_ != CategoryStatus::NOT_PROVIDED; |
| 231 bool need_downloads = downloads_status_ != CategoryStatus::NOT_PROVIDED; | 259 bool need_downloads = downloads_status_ != CategoryStatus::NOT_PROVIDED; |
| 232 if (need_recent_tabs) | 260 if (need_recent_tabs) |
| 233 NotifyStatusChanged(recent_tabs_category_, CategoryStatus::AVAILABLE); | 261 NotifyStatusChanged(recent_tabs_category_, CategoryStatus::AVAILABLE); |
| 234 if (need_downloads) | 262 if (need_downloads) |
| 235 NotifyStatusChanged(downloads_category_, CategoryStatus::AVAILABLE); | 263 NotifyStatusChanged(downloads_category_, CategoryStatus::AVAILABLE); |
| 236 | 264 |
| 237 std::vector<const OfflinePageItem*> recent_tab_items; | 265 std::vector<const OfflinePageItem*> recent_tab_items; |
| 238 std::vector<const OfflinePageItem*> download_items; | 266 std::vector<const OfflinePageItem*> download_items; |
| 239 for (const OfflinePageItem& item : result) { | 267 for (const OfflinePageItem& item : result) { |
| 240 if (need_recent_tabs && | 268 if (need_recent_tabs && |
| 241 item.client_id.name_space == offline_pages::kLastNNamespace && | 269 item.client_id.name_space == offline_pages::kLastNNamespace && |
| 242 !dismissed_recent_tab_ids_.count(base::IntToString(item.offline_id))) { | 270 !dismissed_recent_tab_ids_.count(base::IntToString(item.offline_id))) { |
| 243 recent_tab_items.push_back(&item); | 271 recent_tab_items.push_back(&item); |
| 244 } | 272 } |
| 245 | 273 |
| 246 // TODO(pke): Use kDownloadNamespace once the OfflinePageModel uses that. | 274 // TODO(pke): Use kDownloadNamespace once the OfflinePageModel uses that. |
| 247 // The current logic is taken from DownloadUIAdapter::IsVisibleInUI. | 275 // The current logic is taken from DownloadUIAdapter::IsVisibleInUI. |
| 276 // Note: This is also copied in OfflinePageDeleted above. |
| 248 if (need_downloads && | 277 if (need_downloads && |
| 249 item.client_id.name_space == offline_pages::kAsyncNamespace && | 278 item.client_id.name_space == offline_pages::kAsyncNamespace && |
| 250 base::IsValidGUID(item.client_id.id) && | 279 base::IsValidGUID(item.client_id.id) && |
| 251 !dismissed_download_ids_.count(base::IntToString(item.offline_id))) { | 280 !dismissed_download_ids_.count(base::IntToString(item.offline_id))) { |
| 252 download_items.push_back(&item); | 281 download_items.push_back(&item); |
| 253 } | 282 } |
| 254 } | 283 } |
| 255 | 284 |
| 256 // TODO(pke): Once we have our OfflinePageModel getter and that doesn't do it | 285 // TODO(pke): Once we have our OfflinePageModel getter and that doesn't do it |
| 257 // already, filter out duplicate URLs for recent tabs here. Duplicates for | 286 // already, filter out duplicate URLs for recent tabs here. Duplicates for |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 void OfflinePageSuggestionsProvider::StoreDismissedIDsToPrefs( | 372 void OfflinePageSuggestionsProvider::StoreDismissedIDsToPrefs( |
| 344 const std::string& pref_name, | 373 const std::string& pref_name, |
| 345 const std::set<std::string>& dismissed_ids) { | 374 const std::set<std::string>& dismissed_ids) { |
| 346 base::ListValue list; | 375 base::ListValue list; |
| 347 for (const std::string& dismissed_id : dismissed_ids) | 376 for (const std::string& dismissed_id : dismissed_ids) |
| 348 list.AppendString(dismissed_id); | 377 list.AppendString(dismissed_id); |
| 349 pref_service_->Set(pref_name, list); | 378 pref_service_->Set(pref_name, list); |
| 350 } | 379 } |
| 351 | 380 |
| 352 } // namespace ntp_snippets | 381 } // namespace ntp_snippets |
| OLD | NEW |