| 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/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" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "components/offline_pages/client_namespace_constants.h" | 12 #include "components/offline_pages/core/client_namespace_constants.h" |
| 13 #include "components/offline_pages/client_policy_controller.h" | 13 #include "components/offline_pages/core/client_policy_controller.h" |
| 14 #include "components/offline_pages/downloads/download_ui_item.h" | 14 #include "components/offline_pages/core/downloads/download_ui_item.h" |
| 15 #include "components/offline_pages/offline_page_model.h" | 15 #include "components/offline_pages/core/offline_page_model.h" |
| 16 | 16 |
| 17 namespace offline_pages { | 17 namespace offline_pages { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 const char kDownloadUIAdapterKey[] = "download-ui-adapter"; | 20 const char kDownloadUIAdapterKey[] = "download-ui-adapter"; |
| 21 } | 21 } |
| 22 | 22 |
| 23 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page) | 23 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page) |
| 24 : ui_item(base::MakeUnique<DownloadUIItem>(page)), | 24 : ui_item(base::MakeUnique<DownloadUIItem>(page)), |
| 25 offline_id(page.offline_id) {} | 25 offline_id(page.offline_id) {} |
| 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 state_(State::NOT_LOADED), | 31 state_(State::NOT_LOADED), |
| 32 observers_count_(0), | 32 observers_count_(0), |
| 33 weak_ptr_factory_(this) { | 33 weak_ptr_factory_(this) {} |
| 34 } | |
| 35 | 34 |
| 36 DownloadUIAdapter::~DownloadUIAdapter() { } | 35 DownloadUIAdapter::~DownloadUIAdapter() {} |
| 37 | 36 |
| 38 // static | 37 // static |
| 39 DownloadUIAdapter* DownloadUIAdapter::FromOfflinePageModel( | 38 DownloadUIAdapter* DownloadUIAdapter::FromOfflinePageModel( |
| 40 OfflinePageModel* offline_page_model) { | 39 OfflinePageModel* offline_page_model) { |
| 41 DownloadUIAdapter* adapter = static_cast<DownloadUIAdapter*>( | 40 DownloadUIAdapter* adapter = static_cast<DownloadUIAdapter*>( |
| 42 offline_page_model->GetUserData(kDownloadUIAdapterKey)); | 41 offline_page_model->GetUserData(kDownloadUIAdapterKey)); |
| 43 if (!adapter) { | 42 if (!adapter) { |
| 44 adapter = new DownloadUIAdapter(offline_page_model); | 43 adapter = new DownloadUIAdapter(offline_page_model); |
| 45 offline_page_model->SetUserData(kDownloadUIAdapterKey, adapter); | 44 offline_page_model->SetUserData(kDownloadUIAdapterKey, adapter); |
| 46 } | 45 } |
| 47 return adapter; | 46 return adapter; |
| 48 } | 47 } |
| 49 | 48 |
| 50 void DownloadUIAdapter::AddObserver(Observer* observer) { | 49 void DownloadUIAdapter::AddObserver(Observer* observer) { |
| 51 DCHECK(observer); | 50 DCHECK(observer); |
| 52 if (observers_.HasObserver(observer)) | 51 if (observers_.HasObserver(observer)) |
| 53 return; | 52 return; |
| 54 if (observers_count_ == 0) | 53 if (observers_count_ == 0) |
| 55 LoadCache(); | 54 LoadCache(); |
| 56 observers_.AddObserver(observer); | 55 observers_.AddObserver(observer); |
| 57 ++observers_count_; | 56 ++observers_count_; |
| 58 // If the items are already loaded, post the notification right away. | 57 // If the items are already loaded, post the notification right away. |
| 59 // Don't just invoke it from here to avoid reentrancy in the client. | 58 // Don't just invoke it from here to avoid reentrancy in the client. |
| 60 if (state_ == State::LOADED) { | 59 if (state_ == State::LOADED) { |
| 61 base::ThreadTaskRunnerHandle::Get()->PostTask( | 60 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 62 FROM_HERE, base::Bind(&DownloadUIAdapter::NotifyItemsLoaded, | 61 FROM_HERE, |
| 63 weak_ptr_factory_.GetWeakPtr(), | 62 base::Bind(&DownloadUIAdapter::NotifyItemsLoaded, |
| 64 base::Unretained(observer))); | 63 weak_ptr_factory_.GetWeakPtr(), base::Unretained(observer))); |
| 65 } | 64 } |
| 66 } | 65 } |
| 67 | 66 |
| 68 void DownloadUIAdapter::RemoveObserver(Observer* observer) { | 67 void DownloadUIAdapter::RemoveObserver(Observer* observer) { |
| 69 DCHECK(observer); | 68 DCHECK(observer); |
| 70 if (!observers_.HasObserver(observer)) | 69 if (!observers_.HasObserver(observer)) |
| 71 return; | 70 return; |
| 72 observers_.RemoveObserver(observer); | 71 observers_.RemoveObserver(observer); |
| 73 --observers_count_; | 72 --observers_count_; |
| 74 // Once the last observer is gone, clear cached data. | 73 // Once the last observer is gone, clear cached data. |
| 75 if (observers_count_ == 0) | 74 if (observers_count_ == 0) |
| 76 ClearCache(); | 75 ClearCache(); |
| 77 } | 76 } |
| 78 | 77 |
| 79 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { | 78 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { |
| 80 // This signal is not used here. | 79 // This signal is not used here. |
| 81 } | 80 } |
| 82 | 81 |
| 83 void DownloadUIAdapter::OfflinePageModelChanged(OfflinePageModel* model) { | 82 void DownloadUIAdapter::OfflinePageModelChanged(OfflinePageModel* model) { |
| 84 DCHECK(model == model_); | 83 DCHECK(model == model_); |
| 85 model_->GetAllPages( | 84 model_->GetAllPages(base::Bind(&DownloadUIAdapter::OnOfflinePagesChanged, |
| 86 base::Bind(&DownloadUIAdapter::OnOfflinePagesChanged, | 85 weak_ptr_factory_.GetWeakPtr())); |
| 87 weak_ptr_factory_.GetWeakPtr())); | |
| 88 } | 86 } |
| 89 | 87 |
| 90 void DownloadUIAdapter::OfflinePageDeleted( | 88 void DownloadUIAdapter::OfflinePageDeleted(int64_t offline_id, |
| 91 int64_t offline_id, const ClientId& client_id) { | 89 const ClientId& client_id) { |
| 92 if (!IsVisibleInUI(client_id)) | 90 if (!IsVisibleInUI(client_id)) |
| 93 return; | 91 return; |
| 94 std::string guid = client_id.id; | 92 std::string guid = client_id.id; |
| 95 DownloadUIItems::const_iterator it = items_.find(guid); | 93 DownloadUIItems::const_iterator it = items_.find(guid); |
| 96 if (it == items_.end()) | 94 if (it == items_.end()) |
| 97 return; | 95 return; |
| 98 items_.erase(it); | 96 items_.erase(it); |
| 99 for (Observer& observer : observers_) | 97 for (Observer& observer : observers_) |
| 100 observer.ItemDeleted(guid); | 98 observer.ItemDeleted(guid); |
| 101 } | 99 } |
| 102 | 100 |
| 103 std::vector<const DownloadUIItem*> DownloadUIAdapter::GetAllItems() const { | 101 std::vector<const DownloadUIItem*> DownloadUIAdapter::GetAllItems() const { |
| 104 std::vector<const DownloadUIItem*> result; | 102 std::vector<const DownloadUIItem*> result; |
| 105 for (const auto& item : items_) | 103 for (const auto& item : items_) |
| 106 result.push_back(item.second->ui_item.get()); | 104 result.push_back(item.second->ui_item.get()); |
| 107 return result; | 105 return result; |
| 108 } | 106 } |
| 109 | 107 |
| 110 const DownloadUIItem* | 108 const DownloadUIItem* DownloadUIAdapter::GetItem( |
| 111 DownloadUIAdapter::GetItem(const std::string& guid) const { | 109 const std::string& guid) const { |
| 112 DownloadUIItems::const_iterator it = items_.find(guid); | 110 DownloadUIItems::const_iterator it = items_.find(guid); |
| 113 if (it == items_.end()) | 111 if (it == items_.end()) |
| 114 return nullptr; | 112 return nullptr; |
| 115 return it->second->ui_item.get(); | 113 return it->second->ui_item.get(); |
| 116 } | 114 } |
| 117 | 115 |
| 118 void DownloadUIAdapter::DeleteItem(const std::string& guid) { | 116 void DownloadUIAdapter::DeleteItem(const std::string& guid) { |
| 119 // TODO(dimich): Also remove pending request from RequestQueue. | 117 // TODO(dimich): Also remove pending request from RequestQueue. |
| 120 DownloadUIItems::const_iterator it = items_.find(guid); | 118 DownloadUIItems::const_iterator it = items_.find(guid); |
| 121 if (it == items_.end()) | 119 if (it == items_.end()) |
| 122 return; | 120 return; |
| 123 | 121 |
| 124 std::vector<int64_t> page_ids; | 122 std::vector<int64_t> page_ids; |
| 125 page_ids.push_back(it->second->offline_id); | 123 page_ids.push_back(it->second->offline_id); |
| 126 model_->DeletePagesByOfflineId( | 124 model_->DeletePagesByOfflineId( |
| 127 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone, | 125 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone, |
| 128 weak_ptr_factory_.GetWeakPtr())); | 126 weak_ptr_factory_.GetWeakPtr())); |
| 129 } | 127 } |
| 130 | 128 |
| 131 int64_t DownloadUIAdapter::GetOfflineIdByGuid( | 129 int64_t DownloadUIAdapter::GetOfflineIdByGuid(const std::string& guid) const { |
| 132 const std::string& guid) const { | |
| 133 // 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. |
| 134 // Requests do not yet have offline ID. | 131 // Requests do not yet have offline ID. |
| 135 DownloadUIItems::const_iterator it = items_.find(guid); | 132 DownloadUIItems::const_iterator it = items_.find(guid); |
| 136 if (it != items_.end()) | 133 if (it != items_.end()) |
| 137 return it->second->offline_id; | 134 return it->second->offline_id; |
| 138 return 0; | 135 return 0; |
| 139 } | 136 } |
| 140 | 137 |
| 141 // Note that several LoadCache calls may be issued before the async GetAllPages | 138 // Note that several LoadCache calls may be issued before the async GetAllPages |
| 142 // comes back. | 139 // comes back. |
| 143 void DownloadUIAdapter::LoadCache() { | 140 void DownloadUIAdapter::LoadCache() { |
| 144 // TODO(dimich): Add fetching from RequestQueue as well. | 141 // TODO(dimich): Add fetching from RequestQueue as well. |
| 145 state_ = State::LOADING; | 142 state_ = State::LOADING; |
| 146 model_->GetAllPages( | 143 model_->GetAllPages(base::Bind(&DownloadUIAdapter::OnOfflinePagesLoaded, |
| 147 base::Bind(&DownloadUIAdapter::OnOfflinePagesLoaded, | 144 weak_ptr_factory_.GetWeakPtr())); |
| 148 weak_ptr_factory_.GetWeakPtr())); | |
| 149 } | 145 } |
| 150 | 146 |
| 151 void DownloadUIAdapter::ClearCache() { | 147 void DownloadUIAdapter::ClearCache() { |
| 152 // Once loaded, this class starts to observe the model. Only remove observer | 148 // Once loaded, this class starts to observe the model. Only remove observer |
| 153 // if it was added. | 149 // if it was added. |
| 154 if (state_ == State::LOADED) | 150 if (state_ == State::LOADED) |
| 155 model_->RemoveObserver(this); | 151 model_->RemoveObserver(this); |
| 156 items_.clear(); | 152 items_.clear(); |
| 157 state_ = State::NOT_LOADED; | 153 state_ = State::NOT_LOADED; |
| 158 } | 154 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 // TODO(dimich): Consider adding UMA to record user actions. | 204 // TODO(dimich): Consider adding UMA to record user actions. |
| 209 } | 205 } |
| 210 | 206 |
| 211 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) { | 207 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) { |
| 212 const std::string& name_space = client_id.name_space; | 208 const std::string& name_space = client_id.name_space; |
| 213 return model_->GetPolicyController()->IsSupportedByDownload(name_space) && | 209 return model_->GetPolicyController()->IsSupportedByDownload(name_space) && |
| 214 base::IsValidGUID(client_id.id); | 210 base::IsValidGUID(client_id.id); |
| 215 } | 211 } |
| 216 | 212 |
| 217 } // namespace offline_pages | 213 } // namespace offline_pages |
| OLD | NEW |