Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/offline_pages/downloads/download_ui_adapter.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/guid.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/threading/thread_task_runner_handle.h" | |
| 12 #include "components/offline_pages/client_namespace_constants.h" | |
| 13 #include "components/offline_pages/downloads/download_ui_item.h" | |
| 14 #include "components/offline_pages/offline_page_model.h" | |
| 15 | |
| 16 namespace offline_pages { | |
| 17 | |
| 18 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model) | |
| 19 : model_(model), | |
| 20 is_loaded_(false), | |
| 21 weak_ptr_factory_(this) { | |
| 22 } | |
| 23 | |
| 24 DownloadUIAdapter::~DownloadUIAdapter() { } | |
| 25 | |
| 26 void DownloadUIAdapter::AddObserver(Observer* observer) { | |
|
fgorski
2016/07/25 17:09:56
Consider DCHECKing the observer.
Dmitry Titov
2016/07/26 00:41:24
Done, not sure it is useful since it'll fail fast
| |
| 27 if (!observers_.might_have_observers()) | |
| 28 LoadCache(); | |
| 29 observers_.AddObserver(observer); | |
| 30 // If the items are already loaded, post the notification right away. | |
| 31 // Don't just invoke it from here to avoid reentrancy in the client. | |
| 32 if (is_loaded_) { | |
| 33 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 34 FROM_HERE, base::Bind(&DownloadUIAdapter::NotifyOnLoad, | |
| 35 weak_ptr_factory_.GetWeakPtr(), | |
| 36 base::Unretained(observer))); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 void DownloadUIAdapter::RemoveObserver(Observer* observer) { | |
| 41 observers_.RemoveObserver(observer); | |
| 42 // Once the last observer is gone, clear cached data. | |
| 43 if (!observers_.might_have_observers()) | |
| 44 ClearCache(); | |
| 45 } | |
| 46 | |
| 47 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { | |
| 48 // This signal is not used here. | |
| 49 } | |
| 50 | |
| 51 void DownloadUIAdapter::OfflinePageModelChanged(OfflinePageModel* model) { | |
| 52 DCHECK(model == model_); | |
| 53 model_->GetAllPages( | |
| 54 base::Bind(&DownloadUIAdapter::OnOfflinePagesChanged, | |
| 55 weak_ptr_factory_.GetWeakPtr())); | |
| 56 } | |
| 57 | |
| 58 void DownloadUIAdapter::OfflinePageDeleted( | |
| 59 int64_t offline_id, const ClientId& client_id) { | |
| 60 for(const auto& item : items_) { | |
| 61 if (item.second->guid == client_id.id) { | |
| 62 items_.erase(item.first); | |
| 63 FOR_EACH_OBSERVER(Observer, observers_, ItemDeleted(client_id.id)); | |
| 64 return; | |
| 65 } | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 const DownloadUIItemsMap& DownloadUIAdapter::GetAllItems() const { | |
| 70 return items_; | |
| 71 } | |
| 72 | |
| 73 const DownloadUIItem* DownloadUIAdapter::GetItem(std::string guid) const { | |
| 74 DownloadUIItemsMap::const_iterator it = items_.find(guid); | |
| 75 if (it == items_.end()) | |
| 76 return nullptr; | |
| 77 return (*it).second.get(); | |
| 78 } | |
| 79 | |
| 80 void DownloadUIAdapter::LoadCache() { | |
| 81 DCHECK(!is_loaded_); | |
| 82 // TODO(dimich): Add fetching from RequestQueue as well. | |
| 83 model_->AddObserver(this); | |
| 84 model_->GetAllPages( | |
| 85 base::Bind(&DownloadUIAdapter::OnOfflinePagesLoaded, | |
| 86 weak_ptr_factory_.GetWeakPtr())); | |
| 87 } | |
| 88 | |
| 89 void DownloadUIAdapter::ClearCache() { | |
| 90 model_->RemoveObserver(this); | |
| 91 items_.clear(); | |
| 92 is_loaded_ = false; | |
| 93 } | |
| 94 | |
| 95 void DownloadUIAdapter::OnOfflinePagesLoaded( | |
| 96 const MultipleOfflinePageItemResult& pages) { | |
| 97 for (const auto& page : pages) { | |
| 98 if (IsVisibleInUI(page)) { | |
| 99 std::unique_ptr<DownloadUIItem> new_item(new DownloadUIItem(page)); | |
| 100 DCHECK(items_.find(new_item->guid) == items_.end()); | |
| 101 items_[new_item->guid] = std::move(new_item); | |
| 102 } | |
| 103 } | |
| 104 is_loaded_ = true; | |
| 105 FOR_EACH_OBSERVER(Observer, observers_, ItemsLoaded()); | |
| 106 } | |
| 107 | |
| 108 void DownloadUIAdapter::NotifyOnLoad(Observer* observer) { | |
| 109 if (observer && observers_.HasObserver(observer)) | |
| 110 observer->ItemsLoaded(); | |
| 111 } | |
| 112 | |
| 113 void DownloadUIAdapter::OnOfflinePagesChanged( | |
| 114 const MultipleOfflinePageItemResult& pages) { | |
| 115 | |
|
fgorski
2016/07/25 17:09:56
nit: this empty line probably not needed.
Dmitry Titov
2016/07/26 00:41:24
Done.
| |
| 116 std::vector<std::string> added_guids, changed_guids; | |
| 117 | |
| 118 for (const auto& page : pages) { | |
| 119 if (IsVisibleInUI(page)) { | |
| 120 std::unique_ptr<DownloadUIItem> item(new DownloadUIItem(page)); | |
| 121 DownloadUIItemsMap::const_iterator it = items_.find(item->guid); | |
|
fgorski
2016/07/25 17:28:41
this is not used.
Dmitry Titov
2016/07/26 00:41:24
Done.
| |
| 122 std::string guid = item->guid; | |
|
fgorski
2016/07/25 17:09:56
this can probably be const &
Dmitry Titov
2016/07/26 00:41:24
Done.
| |
| 123 if (items_.find(guid) == items_.end()) { | |
|
fgorski
2016/07/25 17:09:56
This code does not do the validation of duplicate
Dmitry Titov
2016/07/26 00:41:24
It's not clear to me what this method should enfor
| |
| 124 added_guids.push_back(guid); | |
| 125 items_[guid] = std::move(item); | |
|
fgorski
2016/07/25 17:09:56
shouldn't this happen for both changed and added c
Dmitry Titov
2016/07/26 00:41:24
Done.
| |
| 126 } else { | |
| 127 changed_guids.push_back(guid); | |
| 128 } | |
| 129 } | |
| 130 } | |
| 131 for (auto& guid : added_guids) { | |
| 132 FOR_EACH_OBSERVER(Observer, observers_, | |
| 133 ItemAdded(*((*items_.find(guid)).second.get()))); | |
|
fgorski
2016/07/25 17:09:56
How about the two options below for readability.
Dmitry Titov
2016/07/26 00:41:24
Done.
| |
| 134 } | |
| 135 for (auto& guid : changed_guids) { | |
| 136 FOR_EACH_OBSERVER(Observer, observers_, | |
| 137 ItemUpdated(*((*items_.find(guid)).second.get()))); | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 bool DownloadUIAdapter::IsVisibleInUI(const OfflinePageItem& page) { | |
| 142 // TODO(dimich): set up the right filter here. | |
| 143 return page.client_id.name_space == kAsyncNamespace && | |
| 144 base::IsValidGUID(page.client_id.id); | |
| 145 } | |
| 146 | |
| 147 } // namespace offline_pages | |
| OLD | NEW |