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

Side by Side Diff: components/offline_pages/core/downloads/download_ui_adapter.cc

Issue 2560573003: [Offline Pages] Convert Download UI Adapter to using OfflinePageAdded (Closed)
Patch Set: rebase Created 4 years 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 unified diff | Download patch
OLDNEW
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/offline_pages/core/downloads/download_ui_adapter.h" 5 #include "components/offline_pages/core/downloads/download_ui_adapter.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/guid.h" 8 #include "base/guid.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 ClearCache(); 75 ClearCache();
76 } 76 }
77 77
78 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { 78 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) {
79 // This signal is not used here. 79 // This signal is not used here.
80 } 80 }
81 81
82 void DownloadUIAdapter::OfflinePageAdded(OfflinePageModel* model, 82 void DownloadUIAdapter::OfflinePageAdded(OfflinePageModel* model,
83 const OfflinePageItem& added_page) { 83 const OfflinePageItem& added_page) {
84 DCHECK(model == model_); 84 DCHECK(model == model_);
85 model_->GetAllPages(base::Bind(&DownloadUIAdapter::OnOfflinePagesChanged, 85 if (!IsVisibleInUI(added_page.client_id))
86 weak_ptr_factory_.GetWeakPtr())); 86 return;
87
88 const std::string& guid = added_page.client_id.id;
89 DCHECK(items_.find(guid) == items_.end());
90
91 items_[guid] = base::MakeUnique<ItemInfo>(added_page);
92 const DownloadUIItem& item = *(items_[guid]->ui_item);
93 for (Observer& observer : observers_)
94 observer.ItemAdded(item);
87 } 95 }
88 96
89 void DownloadUIAdapter::OfflinePageDeleted(int64_t offline_id, 97 void DownloadUIAdapter::OfflinePageDeleted(int64_t offline_id,
90 const ClientId& client_id) { 98 const ClientId& client_id) {
91 if (!IsVisibleInUI(client_id)) 99 if (!IsVisibleInUI(client_id))
92 return; 100 return;
93 std::string guid = client_id.id; 101 std::string guid = client_id.id;
94 DownloadUIItems::const_iterator it = items_.find(guid); 102 DownloadUIItems::const_iterator it = items_.find(guid);
95 if (it == items_.end()) 103 if (it == items_.end())
96 return; 104 return;
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 state_ = State::LOADED; 180 state_ = State::LOADED;
173 for (Observer& observer : observers_) 181 for (Observer& observer : observers_)
174 observer.ItemsLoaded(); 182 observer.ItemsLoaded();
175 } 183 }
176 184
177 void DownloadUIAdapter::NotifyItemsLoaded(Observer* observer) { 185 void DownloadUIAdapter::NotifyItemsLoaded(Observer* observer) {
178 if (observer && observers_.HasObserver(observer)) 186 if (observer && observers_.HasObserver(observer))
179 observer->ItemsLoaded(); 187 observer->ItemsLoaded();
180 } 188 }
181 189
182 // This method is only called by OPM when a single item added.
183 // TODO(dimich): change OPM to have real OnPageAdded/OnPageUpdated and
184 // simplify this code.
185 void DownloadUIAdapter::OnOfflinePagesChanged(
186 const MultipleOfflinePageItemResult& pages) {
187 std::vector<std::string> added_guids;
188 for (const auto& page : pages) {
189 if (!IsVisibleInUI(page.client_id)) // Item should be filtered out.
190 continue;
191 const std::string& guid = page.client_id.id;
192 if (items_.find(guid) != items_.end()) // Item already exists.
193 continue;
194 items_[guid] = base::MakeUnique<ItemInfo>(page);
195 added_guids.push_back(guid);
196 }
197 for (auto& guid : added_guids) {
198 const DownloadUIItem& item = *(items_.find(guid)->second->ui_item.get());
199 for (Observer& observer : observers_)
200 observer.ItemAdded(item);
201 }
202 }
203 190
204 void DownloadUIAdapter::OnDeletePagesDone(DeletePageResult result) { 191 void DownloadUIAdapter::OnDeletePagesDone(DeletePageResult result) {
205 // TODO(dimich): Consider adding UMA to record user actions. 192 // TODO(dimich): Consider adding UMA to record user actions.
206 } 193 }
207 194
208 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) { 195 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) {
209 const std::string& name_space = client_id.name_space; 196 const std::string& name_space = client_id.name_space;
210 return model_->GetPolicyController()->IsSupportedByDownload(name_space) && 197 return model_->GetPolicyController()->IsSupportedByDownload(name_space) &&
211 base::IsValidGUID(client_id.id); 198 base::IsValidGUID(client_id.id);
212 } 199 }
213 200
214 } // namespace offline_pages 201 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698