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/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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 89 |
90 void DownloadUIAdapter::OfflinePageDeleted( | 90 void DownloadUIAdapter::OfflinePageDeleted( |
91 int64_t offline_id, const ClientId& client_id) { | 91 int64_t offline_id, const ClientId& client_id) { |
92 if (!IsVisibleInUI(client_id)) | 92 if (!IsVisibleInUI(client_id)) |
93 return; | 93 return; |
94 std::string guid = client_id.id; | 94 std::string guid = client_id.id; |
95 DownloadUIItems::const_iterator it = items_.find(guid); | 95 DownloadUIItems::const_iterator it = items_.find(guid); |
96 if (it == items_.end()) | 96 if (it == items_.end()) |
97 return; | 97 return; |
98 items_.erase(it); | 98 items_.erase(it); |
99 FOR_EACH_OBSERVER(Observer, observers_, ItemDeleted(guid)); | 99 for (Observer& observer : observers_) |
| 100 observer.ItemDeleted(guid); |
100 } | 101 } |
101 | 102 |
102 std::vector<const DownloadUIItem*> DownloadUIAdapter::GetAllItems() const { | 103 std::vector<const DownloadUIItem*> DownloadUIAdapter::GetAllItems() const { |
103 std::vector<const DownloadUIItem*> result; | 104 std::vector<const DownloadUIItem*> result; |
104 for (const auto& item : items_) | 105 for (const auto& item : items_) |
105 result.push_back(item.second->ui_item.get()); | 106 result.push_back(item.second->ui_item.get()); |
106 return result; | 107 return result; |
107 } | 108 } |
108 | 109 |
109 const DownloadUIItem* | 110 const DownloadUIItem* |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 return; | 169 return; |
169 for (const auto& page : pages) { | 170 for (const auto& page : pages) { |
170 if (IsVisibleInUI(page.client_id)) { | 171 if (IsVisibleInUI(page.client_id)) { |
171 std::string guid = page.client_id.id; | 172 std::string guid = page.client_id.id; |
172 DCHECK(items_.find(guid) == items_.end()); | 173 DCHECK(items_.find(guid) == items_.end()); |
173 items_[guid] = base::MakeUnique<ItemInfo>(page); | 174 items_[guid] = base::MakeUnique<ItemInfo>(page); |
174 } | 175 } |
175 } | 176 } |
176 model_->AddObserver(this); | 177 model_->AddObserver(this); |
177 state_ = State::LOADED; | 178 state_ = State::LOADED; |
178 FOR_EACH_OBSERVER(Observer, observers_, ItemsLoaded()); | 179 for (Observer& observer : observers_) |
| 180 observer.ItemsLoaded(); |
179 } | 181 } |
180 | 182 |
181 void DownloadUIAdapter::NotifyItemsLoaded(Observer* observer) { | 183 void DownloadUIAdapter::NotifyItemsLoaded(Observer* observer) { |
182 if (observer && observers_.HasObserver(observer)) | 184 if (observer && observers_.HasObserver(observer)) |
183 observer->ItemsLoaded(); | 185 observer->ItemsLoaded(); |
184 } | 186 } |
185 | 187 |
186 // This method is only called by OPM when a single item added. | 188 // This method is only called by OPM when a single item added. |
187 // TODO(dimich): change OPM to have real OnPageAdded/OnPageUpdated and | 189 // TODO(dimich): change OPM to have real OnPageAdded/OnPageUpdated and |
188 // simplify this code. | 190 // simplify this code. |
189 void DownloadUIAdapter::OnOfflinePagesChanged( | 191 void DownloadUIAdapter::OnOfflinePagesChanged( |
190 const MultipleOfflinePageItemResult& pages) { | 192 const MultipleOfflinePageItemResult& pages) { |
191 std::vector<std::string> added_guids; | 193 std::vector<std::string> added_guids; |
192 for (const auto& page : pages) { | 194 for (const auto& page : pages) { |
193 if (!IsVisibleInUI(page.client_id)) // Item should be filtered out. | 195 if (!IsVisibleInUI(page.client_id)) // Item should be filtered out. |
194 continue; | 196 continue; |
195 const std::string& guid = page.client_id.id; | 197 const std::string& guid = page.client_id.id; |
196 if (items_.find(guid) != items_.end()) // Item already exists. | 198 if (items_.find(guid) != items_.end()) // Item already exists. |
197 continue; | 199 continue; |
198 items_[guid] = base::MakeUnique<ItemInfo>(page); | 200 items_[guid] = base::MakeUnique<ItemInfo>(page); |
199 added_guids.push_back(guid); | 201 added_guids.push_back(guid); |
200 } | 202 } |
201 for (auto& guid : added_guids) { | 203 for (auto& guid : added_guids) { |
202 const DownloadUIItem& item = *(items_.find(guid)->second->ui_item.get()); | 204 const DownloadUIItem& item = *(items_.find(guid)->second->ui_item.get()); |
203 FOR_EACH_OBSERVER(Observer, observers_, ItemAdded(item)); | 205 for (Observer& observer : observers_) |
| 206 observer.ItemAdded(item); |
204 } | 207 } |
205 } | 208 } |
206 | 209 |
207 void DownloadUIAdapter::OnDeletePagesDone(DeletePageResult result) { | 210 void DownloadUIAdapter::OnDeletePagesDone(DeletePageResult result) { |
208 // TODO(dimich): Consider adding UMA to record user actions. | 211 // TODO(dimich): Consider adding UMA to record user actions. |
209 } | 212 } |
210 | 213 |
211 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) { | 214 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) { |
212 const std::string& name_space = client_id.name_space; | 215 const std::string& name_space = client_id.name_space; |
213 return model_->GetPolicyController()->IsSupportedByDownload(name_space) && | 216 return model_->GetPolicyController()->IsSupportedByDownload(name_space) && |
214 base::IsValidGUID(client_id.id); | 217 base::IsValidGUID(client_id.id); |
215 } | 218 } |
216 | 219 |
217 } // namespace offline_pages | 220 } // namespace offline_pages |
OLD | NEW |