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

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

Issue 2469933002: Offline Pages: Replace Observer::OfflinePageModelChanged with OfflinePageAdded. (Closed)
Patch Set: Touch ups. Created 4 years, 1 month 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/downloads/download_ui_adapter.h" 5 #include "components/offline_pages/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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 DCHECK(observer); 69 DCHECK(observer);
70 if (!observers_.HasObserver(observer)) 70 if (!observers_.HasObserver(observer))
71 return; 71 return;
72 observers_.RemoveObserver(observer); 72 observers_.RemoveObserver(observer);
73 --observers_count_; 73 --observers_count_;
74 // Once the last observer is gone, clear cached data. 74 // Once the last observer is gone, clear cached data.
75 if (observers_count_ == 0) 75 if (observers_count_ == 0)
76 ClearCache(); 76 ClearCache();
77 } 77 }
78 78
79 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { 79 void DownloadUIAdapter::OfflinePageAdded(OfflinePageModel* model,
80 // This signal is not used here. 80 SavePageResult result,
81 } 81 const OfflinePageItem* added) {
82 DCHECK(model == model_);
83 if (!added || !IsVisibleInUI(added->client_id))
84 return;
82 85
83 void DownloadUIAdapter::OfflinePageModelChanged(OfflinePageModel* model) { 86 const std::string& guid = added->client_id.id;
84 DCHECK(model == model_); 87 if (items_.find(guid) != items_.end()) // Item already exists.
fgorski 2016/11/02 15:51:06 If item already exists this should probably never
dewittj 2016/11/02 16:34:37 I was trying not to change the logic from OnOfflin
85 model_->GetAllPages( 88 return;
86 base::Bind(&DownloadUIAdapter::OnOfflinePagesChanged, 89
87 weak_ptr_factory_.GetWeakPtr())); 90 items_[guid] = base::MakeUnique<ItemInfo>(*added);
91 const DownloadUIItem& item = *(items_[guid]->ui_item);
92 for (Observer& observer : observers_)
93 observer.ItemAdded(item);
88 } 94 }
89 95
90 void DownloadUIAdapter::OfflinePageDeleted( 96 void DownloadUIAdapter::OfflinePageDeleted(
91 int64_t offline_id, const ClientId& client_id) { 97 int64_t offline_id, const ClientId& client_id) {
92 if (!IsVisibleInUI(client_id)) 98 if (!IsVisibleInUI(client_id))
93 return; 99 return;
94 std::string guid = client_id.id; 100 std::string guid = client_id.id;
95 DownloadUIItems::const_iterator it = items_.find(guid); 101 DownloadUIItems::const_iterator it = items_.find(guid);
96 if (it == items_.end()) 102 if (it == items_.end())
97 return; 103 return;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 state_ = State::LOADED; 184 state_ = State::LOADED;
179 for (Observer& observer : observers_) 185 for (Observer& observer : observers_)
180 observer.ItemsLoaded(); 186 observer.ItemsLoaded();
181 } 187 }
182 188
183 void DownloadUIAdapter::NotifyItemsLoaded(Observer* observer) { 189 void DownloadUIAdapter::NotifyItemsLoaded(Observer* observer) {
184 if (observer && observers_.HasObserver(observer)) 190 if (observer && observers_.HasObserver(observer))
185 observer->ItemsLoaded(); 191 observer->ItemsLoaded();
186 } 192 }
187 193
188 // This method is only called by OPM when a single item added.
189 // TODO(dimich): change OPM to have real OnPageAdded/OnPageUpdated and
190 // simplify this code.
191 void DownloadUIAdapter::OnOfflinePagesChanged(
192 const MultipleOfflinePageItemResult& pages) {
193 std::vector<std::string> added_guids;
194 for (const auto& page : pages) {
195 if (!IsVisibleInUI(page.client_id)) // Item should be filtered out.
196 continue;
197 const std::string& guid = page.client_id.id;
198 if (items_.find(guid) != items_.end()) // Item already exists.
199 continue;
200 items_[guid] = base::MakeUnique<ItemInfo>(page);
201 added_guids.push_back(guid);
202 }
203 for (auto& guid : added_guids) {
204 const DownloadUIItem& item = *(items_.find(guid)->second->ui_item.get());
205 for (Observer& observer : observers_)
206 observer.ItemAdded(item);
207 }
208 }
209 194
210 void DownloadUIAdapter::OnDeletePagesDone(DeletePageResult result) { 195 void DownloadUIAdapter::OnDeletePagesDone(DeletePageResult result) {
211 // TODO(dimich): Consider adding UMA to record user actions. 196 // TODO(dimich): Consider adding UMA to record user actions.
212 } 197 }
213 198
214 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) { 199 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) {
215 const std::string& name_space = client_id.name_space; 200 const std::string& name_space = client_id.name_space;
216 return model_->GetPolicyController()->IsSupportedByDownload(name_space) && 201 return model_->GetPolicyController()->IsSupportedByDownload(name_space) &&
217 base::IsValidGUID(client_id.id); 202 base::IsValidGUID(client_id.id);
218 } 203 }
219 204
220 } // namespace offline_pages 205 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698