Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(942)

Unified Diff: components/offline_pages/downloads/download_ui_adapter.cc

Issue 2221293002: Implement deleteItem and openItem on OfflinePageDownloadBridge. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: cleanup Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/offline_pages/downloads/download_ui_adapter.cc
diff --git a/components/offline_pages/downloads/download_ui_adapter.cc b/components/offline_pages/downloads/download_ui_adapter.cc
index 776fd8cd79b691e167cd48adfc895c7e9d622056..32055e65fd1aeabccc85d6a665c8fd4d8272ec00 100644
--- a/components/offline_pages/downloads/download_ui_adapter.cc
+++ b/components/offline_pages/downloads/download_ui_adapter.cc
@@ -19,6 +19,14 @@ namespace {
const char kDownloadUIAdapterKey[] = "download-ui-adapter";
}
+DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page)
+ : ui_item(base::MakeUnique<DownloadUIItem>(page)),
+ offline_id(page.offline_id),
+ offline_url(page.GetOfflineURL()) {
+}
+
+DownloadUIAdapter::ItemInfo::~ItemInfo() {}
+
DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model)
: model_(model),
is_loaded_(false),
@@ -75,7 +83,7 @@ void DownloadUIAdapter::OfflinePageModelChanged(OfflinePageModel* model) {
void DownloadUIAdapter::OfflinePageDeleted(
int64_t offline_id, const ClientId& client_id) {
for(const auto& item : items_) {
- if (item.second->guid == client_id.id) {
+ if (item.second->ui_item->guid == client_id.id) {
fgorski 2016/08/12 15:43:52 Since items_ are keyed with guid: items_.find(clie
Dmitry Titov 2016/08/12 19:43:37 Done.
items_.erase(item.first);
FOR_EACH_OBSERVER(Observer, observers_, ItemDeleted(client_id.id));
return;
@@ -83,16 +91,45 @@ void DownloadUIAdapter::OfflinePageDeleted(
}
}
-const DownloadUIItemsMap& DownloadUIAdapter::GetAllItems() const {
- return items_;
+std::vector<const DownloadUIItem*> DownloadUIAdapter::GetAllItems() const {
+ std::vector<const DownloadUIItem*> result;
+ for(const auto& item : items_)
+ result.push_back(item.second->ui_item.get());
+ return result;
}
const DownloadUIItem*
DownloadUIAdapter::GetItem(const std::string& guid) const {
fgorski 2016/08/12 15:43:52 nit: it that indentation what git cl format delive
Dmitry Titov 2016/08/12 19:43:37 I've run it again.
fgorski 2016/08/12 20:01:14 Acknowledged.
- DownloadUIItemsMap::const_iterator it = items_.find(guid);
+ DownloadUIItems::const_iterator it = items_.find(guid);
if (it == items_.end())
return nullptr;
- return (*it).second.get();
+ return it->second->ui_item.get();
+}
+
+void DownloadUIAdapter::DeleteItem(const std::string& guid) {
+ // TODO(dimich): Also remove pending request from RequestQueue.
+ DownloadUIItems::const_iterator it = items_.find(guid);
+ if (it == items_.end())
+ return;
+
+ std::vector<int64_t> page_ids;
+ page_ids.push_back(it->second->offline_id);
+ // TODO(dimich): This should be ExpirePages(...Now()..) when Expire is
+ // firing Observer method. The resulting Observer notification will update
+ // local cache.
+ model_->DeletePagesByOfflineId(page_ids,
+ base::Bind(&DownloadUIAdapter::OnDeletePagesDone,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
+std::string DownloadUIAdapter::GetOfflineUrlByGuid(
+ const std::string& guid) const {
+ // TODO(dimich): when requests are also in the cache, filter them out.
+ // Requests do not yet have offline URL.
+ DownloadUIItems::const_iterator it = items_.find(guid);
+ if (it != items_.end())
+ return it->second->offline_url.spec();
fgorski 2016/08/12 15:43:52 why do you prefer string over GURL? If this is bec
Dmitry Titov 2016/08/12 19:43:37 Done.
+ return std::string();
}
void DownloadUIAdapter::LoadCache() {
@@ -114,10 +151,9 @@ void DownloadUIAdapter::OnOfflinePagesLoaded(
const MultipleOfflinePageItemResult& pages) {
for (const auto& page : pages) {
if (IsVisibleInUI(page)) {
- std::unique_ptr<DownloadUIItem> new_item(new DownloadUIItem(page));
- const std::string& guid = new_item->guid;
+ std::string guid = page.client_id.id;
DCHECK(items_.find(guid) == items_.end());
- items_[guid] = std::move(new_item);
+ items_[guid] = base::MakeUnique<ItemInfo>(page);
}
}
is_loaded_ = true;
@@ -141,21 +177,24 @@ void DownloadUIAdapter::OnOfflinePagesChanged(
const std::string& guid = page.client_id.id;
if (items_.find(guid) != items_.end()) // Item already exists.
continue;
- std::unique_ptr<DownloadUIItem> item(new DownloadUIItem(page));
- items_[guid] = std::move(item);
+ items_[guid] = base::MakeUnique<ItemInfo>(page);
added_guids.push_back(guid);
}
// Only one added page
CHECK(added_guids.size() <= 1);
for (auto& guid : added_guids) {
- const DownloadUIItem& item = *(items_.find(guid)->second.get());
+ const DownloadUIItem& item = *(items_.find(guid)->second->ui_item.get());
FOR_EACH_OBSERVER(Observer, observers_, ItemAdded(item));
}
}
+void DownloadUIAdapter::OnDeletePagesDone(DeletePageResult result) {
+ // TODO(dimich): Consider adding UMA to record user actions.
+}
+
bool DownloadUIAdapter::IsVisibleInUI(const OfflinePageItem& page) {
- // TODO(dimich): set up the right filter here.
- return page.client_id.name_space == kAsyncNamespace &&
+ std::string name_space = page.client_id.name_space;
+ return (name_space == kAsyncNamespace || name_space == kDownloadNamespace) &&
base::IsValidGUID(page.client_id.id);
}

Powered by Google App Engine
This is Rietveld 408576698