Chromium Code Reviews| 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 10 matching lines...) Expand all Loading... | |
| 21 | 21 |
| 22 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page) | 22 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page) |
| 23 : ui_item(base::MakeUnique<DownloadUIItem>(page)), | 23 : ui_item(base::MakeUnique<DownloadUIItem>(page)), |
| 24 offline_id(page.offline_id), | 24 offline_id(page.offline_id), |
| 25 offline_url(page.GetOfflineURL()) {} | 25 offline_url(page.GetOfflineURL()) {} |
| 26 | 26 |
| 27 DownloadUIAdapter::ItemInfo::~ItemInfo() {} | 27 DownloadUIAdapter::ItemInfo::~ItemInfo() {} |
| 28 | 28 |
| 29 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model) | 29 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model) |
| 30 : model_(model), | 30 : model_(model), |
| 31 is_loaded_(false), | 31 state_(State::NOT_LOADED), |
| 32 observers_count_(0), | |
| 32 weak_ptr_factory_(this) { | 33 weak_ptr_factory_(this) { |
| 33 } | 34 } |
| 34 | 35 |
| 35 DownloadUIAdapter::~DownloadUIAdapter() { } | 36 DownloadUIAdapter::~DownloadUIAdapter() { } |
| 36 | 37 |
| 37 // static | 38 // static |
| 38 DownloadUIAdapter* DownloadUIAdapter::FromOfflinePageModel( | 39 DownloadUIAdapter* DownloadUIAdapter::FromOfflinePageModel( |
| 39 OfflinePageModel* offline_page_model) { | 40 OfflinePageModel* offline_page_model) { |
| 40 DownloadUIAdapter* adapter = static_cast<DownloadUIAdapter*>( | 41 DownloadUIAdapter* adapter = static_cast<DownloadUIAdapter*>( |
| 41 offline_page_model->GetUserData(kDownloadUIAdapterKey)); | 42 offline_page_model->GetUserData(kDownloadUIAdapterKey)); |
| 42 if (!adapter) { | 43 if (!adapter) { |
| 43 adapter = new DownloadUIAdapter(offline_page_model); | 44 adapter = new DownloadUIAdapter(offline_page_model); |
| 44 offline_page_model->SetUserData(kDownloadUIAdapterKey, adapter); | 45 offline_page_model->SetUserData(kDownloadUIAdapterKey, adapter); |
| 45 } | 46 } |
| 46 return adapter; | 47 return adapter; |
| 47 } | 48 } |
| 48 | 49 |
| 49 void DownloadUIAdapter::AddObserver(Observer* observer) { | 50 void DownloadUIAdapter::AddObserver(Observer* observer) { |
| 50 DCHECK(observer); | 51 DCHECK(observer); |
| 51 if (!observers_.might_have_observers()) | 52 if (observers_count_ == 0) |
| 52 LoadCache(); | 53 LoadCache(); |
| 53 observers_.AddObserver(observer); | 54 observers_.AddObserver(observer); |
| 55 ++observers_count_; | |
|
fgorski
2016/08/22 22:52:22
It may make sense to use HasObserver as a guard wh
Dmitry Titov
2016/08/23 01:21:34
Done. Indeed. Crazy.
| |
| 54 // If the items are already loaded, post the notification right away. | 56 // If the items are already loaded, post the notification right away. |
| 55 // Don't just invoke it from here to avoid reentrancy in the client. | 57 // Don't just invoke it from here to avoid reentrancy in the client. |
| 56 if (is_loaded_) { | 58 if (state_ == State::LOADED) { |
| 57 base::ThreadTaskRunnerHandle::Get()->PostTask( | 59 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 58 FROM_HERE, base::Bind(&DownloadUIAdapter::NotifyItemsLoaded, | 60 FROM_HERE, base::Bind(&DownloadUIAdapter::NotifyItemsLoaded, |
| 59 weak_ptr_factory_.GetWeakPtr(), | 61 weak_ptr_factory_.GetWeakPtr(), |
| 60 base::Unretained(observer))); | 62 base::Unretained(observer))); |
| 61 } | 63 } |
| 62 } | 64 } |
| 63 | 65 |
| 64 void DownloadUIAdapter::RemoveObserver(Observer* observer) { | 66 void DownloadUIAdapter::RemoveObserver(Observer* observer) { |
| 65 observers_.RemoveObserver(observer); | 67 observers_.RemoveObserver(observer); |
| 68 --observers_count_; | |
| 66 // Once the last observer is gone, clear cached data. | 69 // Once the last observer is gone, clear cached data. |
| 67 if (!observers_.might_have_observers()) | 70 if (observers_count_ == 0) |
| 68 ClearCache(); | 71 ClearCache(); |
| 69 } | 72 } |
| 70 | 73 |
| 71 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { | 74 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { |
| 72 // This signal is not used here. | 75 // This signal is not used here. |
| 73 } | 76 } |
| 74 | 77 |
| 75 void DownloadUIAdapter::OfflinePageModelChanged(OfflinePageModel* model) { | 78 void DownloadUIAdapter::OfflinePageModelChanged(OfflinePageModel* model) { |
| 76 DCHECK(model == model_); | 79 DCHECK(model == model_); |
| 77 model_->GetAllPages( | 80 model_->GetAllPages( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 GURL DownloadUIAdapter::GetOfflineUrlByGuid( | 128 GURL DownloadUIAdapter::GetOfflineUrlByGuid( |
| 126 const std::string& guid) const { | 129 const std::string& guid) const { |
| 127 // TODO(dimich): when requests are also in the cache, filter them out. | 130 // TODO(dimich): when requests are also in the cache, filter them out. |
| 128 // Requests do not yet have offline URL. | 131 // Requests do not yet have offline URL. |
| 129 DownloadUIItems::const_iterator it = items_.find(guid); | 132 DownloadUIItems::const_iterator it = items_.find(guid); |
| 130 if (it != items_.end()) | 133 if (it != items_.end()) |
| 131 return it->second->offline_url; | 134 return it->second->offline_url; |
| 132 return GURL(); | 135 return GURL(); |
| 133 } | 136 } |
| 134 | 137 |
| 138 // Note that several LoadCache calls may be issued before the async GetAllPages | |
| 139 // comes back. | |
| 135 void DownloadUIAdapter::LoadCache() { | 140 void DownloadUIAdapter::LoadCache() { |
| 136 DCHECK(!is_loaded_); | |
| 137 // TODO(dimich): Add fetching from RequestQueue as well. | 141 // TODO(dimich): Add fetching from RequestQueue as well. |
| 138 model_->AddObserver(this); | 142 state_ = State::LOADING; |
| 139 model_->GetAllPages( | 143 model_->GetAllPages( |
| 140 base::Bind(&DownloadUIAdapter::OnOfflinePagesLoaded, | 144 base::Bind(&DownloadUIAdapter::OnOfflinePagesLoaded, |
| 141 weak_ptr_factory_.GetWeakPtr())); | 145 weak_ptr_factory_.GetWeakPtr())); |
| 142 } | 146 } |
| 143 | 147 |
| 144 void DownloadUIAdapter::ClearCache() { | 148 void DownloadUIAdapter::ClearCache() { |
| 145 model_->RemoveObserver(this); | 149 model_->RemoveObserver(this); |
| 146 items_.clear(); | 150 items_.clear(); |
| 147 is_loaded_ = false; | 151 state_ = State::NOT_LOADED; |
|
fgorski
2016/08/22 22:52:22
perhaps invalidate weak pointers here to prevent a
Dmitry Titov
2016/08/23 01:21:34
Ack. But I think that also will stop other operati
| |
| 148 } | 152 } |
| 149 | 153 |
| 150 void DownloadUIAdapter::OnOfflinePagesLoaded( | 154 void DownloadUIAdapter::OnOfflinePagesLoaded( |
| 151 const MultipleOfflinePageItemResult& pages) { | 155 const MultipleOfflinePageItemResult& pages) { |
| 156 // If multiple observers register quickly, the cache might be already loaded | |
| 157 // by the previous LoadCache call. At the same time, if all observers already | |
| 158 // left, there is no reason to populate the cache. | |
| 159 if (state_ != State::LOADING) | |
| 160 return; | |
| 152 for (const auto& page : pages) { | 161 for (const auto& page : pages) { |
| 153 if (IsVisibleInUI(page.client_id)) { | 162 if (IsVisibleInUI(page.client_id)) { |
| 154 std::string guid = page.client_id.id; | 163 std::string guid = page.client_id.id; |
| 155 DCHECK(items_.find(guid) == items_.end()); | 164 DCHECK(items_.find(guid) == items_.end()); |
| 156 items_[guid] = base::MakeUnique<ItemInfo>(page); | 165 items_[guid] = base::MakeUnique<ItemInfo>(page); |
| 157 } | 166 } |
| 158 } | 167 } |
| 159 is_loaded_ = true; | 168 model_->AddObserver(this); |
| 169 state_ = State::LOADED; | |
| 160 FOR_EACH_OBSERVER(Observer, observers_, ItemsLoaded()); | 170 FOR_EACH_OBSERVER(Observer, observers_, ItemsLoaded()); |
| 161 } | 171 } |
| 162 | 172 |
| 163 void DownloadUIAdapter::NotifyItemsLoaded(Observer* observer) { | 173 void DownloadUIAdapter::NotifyItemsLoaded(Observer* observer) { |
| 164 if (observer && observers_.HasObserver(observer)) | 174 if (observer && observers_.HasObserver(observer)) |
| 165 observer->ItemsLoaded(); | 175 observer->ItemsLoaded(); |
| 166 } | 176 } |
| 167 | 177 |
| 168 // This method is only called by OPM when a single item added. | 178 // This method is only called by OPM when a single item added. |
| 169 // TODO(dimich): change OPM to have real OnPageAdded/OnPageUpdated and | 179 // TODO(dimich): change OPM to have real OnPageAdded/OnPageUpdated and |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 192 // TODO(dimich): Consider adding UMA to record user actions. | 202 // TODO(dimich): Consider adding UMA to record user actions. |
| 193 } | 203 } |
| 194 | 204 |
| 195 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) { | 205 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) { |
| 196 const std::string& name_space = client_id.name_space; | 206 const std::string& name_space = client_id.name_space; |
| 197 return (name_space == kAsyncNamespace || name_space == kDownloadNamespace) && | 207 return (name_space == kAsyncNamespace || name_space == kDownloadNamespace) && |
| 198 base::IsValidGUID(client_id.id); | 208 base::IsValidGUID(client_id.id); |
| 199 } | 209 } |
| 200 | 210 |
| 201 } // namespace offline_pages | 211 } // namespace offline_pages |
| OLD | NEW |