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

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

Issue 2684973014: Only show Last N Pages in the UI when the corresponding tab is visible. (Closed)
Patch Set: Add unit tests Created 3 years, 10 months 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/core/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/core/background/request_coordinator.h" 12 #include "components/offline_pages/core/background/request_coordinator.h"
13 #include "components/offline_pages/core/background/save_page_request.h" 13 #include "components/offline_pages/core/background/save_page_request.h"
14 #include "components/offline_pages/core/client_namespace_constants.h" 14 #include "components/offline_pages/core/client_namespace_constants.h"
15 #include "components/offline_pages/core/client_policy_controller.h" 15 #include "components/offline_pages/core/client_policy_controller.h"
16 #include "components/offline_pages/core/downloads/download_ui_item.h" 16 #include "components/offline_pages/core/downloads/download_ui_item.h"
17 #include "components/offline_pages/core/offline_page_model.h" 17 #include "components/offline_pages/core/offline_page_model.h"
18 18
19 namespace offline_pages { 19 namespace offline_pages {
20 20
21 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page) 21 DownloadUIAdapter::ItemInfo::ItemInfo(Delegate* delegate,
22 const OfflinePageItem& page)
22 : ui_item(base::MakeUnique<DownloadUIItem>(page)), 23 : ui_item(base::MakeUnique<DownloadUIItem>(page)),
23 is_request(false), 24 is_request(false),
24 offline_id(page.offline_id), 25 offline_id(page.offline_id),
25 client_id(page.client_id), 26 client_id(page.client_id),
26 temporarily_hidden(false) {} 27 temporarily_hidden(delegate->IsTemporarilyHiddenInUI(page.client_id)) {}
27 28
28 DownloadUIAdapter::ItemInfo::ItemInfo(const SavePageRequest& request) 29 DownloadUIAdapter::ItemInfo::ItemInfo(Delegate* delegate,
30 const SavePageRequest& request)
29 : ui_item(base::MakeUnique<DownloadUIItem>(request)), 31 : ui_item(base::MakeUnique<DownloadUIItem>(request)),
30 is_request(true), 32 is_request(true),
31 offline_id(request.request_id()), 33 offline_id(request.request_id()),
32 client_id(request.client_id()), 34 client_id(request.client_id()),
33 temporarily_hidden(false) {} 35 temporarily_hidden(
36 delegate->IsTemporarilyHiddenInUI(request.client_id())) {}
34 37
35 DownloadUIAdapter::ItemInfo::~ItemInfo() {} 38 DownloadUIAdapter::ItemInfo::~ItemInfo() {}
36 39
37 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model, 40 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model,
38 RequestCoordinator* request_coordinator, 41 RequestCoordinator* request_coordinator,
39 std::unique_ptr<Delegate> delegate) 42 std::unique_ptr<Delegate> delegate)
40 : model_(model), 43 : model_(model),
41 request_coordinator_(request_coordinator), 44 request_coordinator_(request_coordinator),
42 delegate_(std::move(delegate)), 45 delegate_(std::move(delegate)),
43 state_(State::NOT_LOADED), 46 state_(State::NOT_LOADED),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { 81 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) {
79 // This signal is not used here. 82 // This signal is not used here.
80 } 83 }
81 84
82 void DownloadUIAdapter::OfflinePageAdded(OfflinePageModel* model, 85 void DownloadUIAdapter::OfflinePageAdded(OfflinePageModel* model,
83 const OfflinePageItem& added_page) { 86 const OfflinePageItem& added_page) {
84 DCHECK(model == model_); 87 DCHECK(model == model_);
85 if (!delegate_->IsVisibleInUI(added_page.client_id)) 88 if (!delegate_->IsVisibleInUI(added_page.client_id))
86 return; 89 return;
87 90
88 AddItemHelper(base::MakeUnique<ItemInfo>(added_page)); 91 AddItemHelper(base::MakeUnique<ItemInfo>(delegate_.get(), added_page));
89 } 92 }
90 93
91 void DownloadUIAdapter::OfflinePageDeleted(int64_t offline_id, 94 void DownloadUIAdapter::OfflinePageDeleted(int64_t offline_id,
92 const ClientId& client_id) { 95 const ClientId& client_id) {
93 if (!delegate_->IsVisibleInUI(client_id)) 96 if (!delegate_->IsVisibleInUI(client_id))
94 return; 97 return;
95 DeleteItemHelper(client_id.id); 98 DeleteItemHelper(client_id.id);
96 } 99 }
97 100
98 // RequestCoordinator::Observer 101 // RequestCoordinator::Observer
99 void DownloadUIAdapter::OnAdded(const SavePageRequest& added_request) { 102 void DownloadUIAdapter::OnAdded(const SavePageRequest& added_request) {
100 if (!delegate_->IsVisibleInUI(added_request.client_id())) 103 if (!delegate_->IsVisibleInUI(added_request.client_id()))
101 return; 104 return;
102 105
103 AddItemHelper(base::MakeUnique<ItemInfo>(added_request)); 106 AddItemHelper(base::MakeUnique<ItemInfo>(delegate_.get(), added_request));
104 } 107 }
105 108
106 // RequestCoordinator::Observer 109 // RequestCoordinator::Observer
107 void DownloadUIAdapter::OnCompleted( 110 void DownloadUIAdapter::OnCompleted(
108 const SavePageRequest& request, 111 const SavePageRequest& request,
109 RequestNotifier::BackgroundSavePageResult status) { 112 RequestNotifier::BackgroundSavePageResult status) {
110 if (!delegate_->IsVisibleInUI(request.client_id())) 113 if (!delegate_->IsVisibleInUI(request.client_id()))
111 return; 114 return;
112 115
113 // If request completed successfully, report ItemUpdated when a page is added 116 // If request completed successfully, report ItemUpdated when a page is added
114 // to the model. If the request failed, tell UI that the item is gone. 117 // to the model. If the request failed, tell UI that the item is gone.
115 if (status == RequestNotifier::BackgroundSavePageResult::SUCCESS) 118 if (status == RequestNotifier::BackgroundSavePageResult::SUCCESS)
116 return; 119 return;
117 DeleteItemHelper(request.client_id().id); 120 DeleteItemHelper(request.client_id().id);
118 } 121 }
119 122
120 // RequestCoordinator::Observer 123 // RequestCoordinator::Observer
121 void DownloadUIAdapter::OnChanged(const SavePageRequest& request) { 124 void DownloadUIAdapter::OnChanged(const SavePageRequest& request) {
122 if (!delegate_->IsVisibleInUI(request.client_id())) 125 if (!delegate_->IsVisibleInUI(request.client_id()))
123 return; 126 return;
124 127
125 std::string guid = request.client_id().id; 128 std::string guid = request.client_id().id;
126 items_[guid] = base::MakeUnique<ItemInfo>(request); 129 items_[guid] = base::MakeUnique<ItemInfo>(delegate_.get(), request);
127 130
128 if (state_ != State::LOADED) 131 if (state_ != State::LOADED)
129 return; 132 return;
130 133
131 const DownloadUIItem& download_ui_item = *(items_[guid]->ui_item); 134 const DownloadUIItem& download_ui_item = *(items_[guid]->ui_item);
132 for (Observer& observer : observers_) 135 for (Observer& observer : observers_)
133 observer.ItemUpdated(download_ui_item); 136 observer.ItemUpdated(download_ui_item);
134 } 137 }
135 138
136 void DownloadUIAdapter::TemporaryHiddenStatusChanged( 139 void DownloadUIAdapter::TemporaryHiddenStatusChanged(
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 return; 182 return;
180 183
181 std::vector<int64_t> page_ids; 184 std::vector<int64_t> page_ids;
182 page_ids.push_back(it->second->offline_id); 185 page_ids.push_back(it->second->offline_id);
183 model_->DeletePagesByOfflineId( 186 model_->DeletePagesByOfflineId(
184 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone, 187 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone,
185 weak_ptr_factory_.GetWeakPtr())); 188 weak_ptr_factory_.GetWeakPtr()));
186 } 189 }
187 190
188 int64_t DownloadUIAdapter::GetOfflineIdByGuid(const std::string& guid) const { 191 int64_t DownloadUIAdapter::GetOfflineIdByGuid(const std::string& guid) const {
192 if (deleting_item_ && deleting_item_->ui_item->guid == guid)
193 return deleting_item_->offline_id;
194
189 DownloadUIItems::const_iterator it = items_.find(guid); 195 DownloadUIItems::const_iterator it = items_.find(guid);
190 if (it != items_.end()) 196 if (it != items_.end())
191 return it->second->offline_id; 197 return it->second->offline_id;
192 return 0; 198 return 0;
193 } 199 }
194 200
195 // Note that several LoadCache calls may be issued before the async GetAllPages 201 // Note that several LoadCache calls may be issued before the async GetAllPages
196 // comes back. 202 // comes back.
197 void DownloadUIAdapter::LoadCache() { 203 void DownloadUIAdapter::LoadCache() {
198 state_ = State::LOADING_PAGES; 204 state_ = State::LOADING_PAGES;
(...skipping 14 matching lines...) Expand all
213 const MultipleOfflinePageItemResult& pages) { 219 const MultipleOfflinePageItemResult& pages) {
214 // If multiple observers register quickly, the cache might be already loaded 220 // If multiple observers register quickly, the cache might be already loaded
215 // by the previous LoadCache call. At the same time, if all observers already 221 // by the previous LoadCache call. At the same time, if all observers already
216 // left, there is no reason to populate the cache. 222 // left, there is no reason to populate the cache.
217 if (state_ != State::LOADING_PAGES) 223 if (state_ != State::LOADING_PAGES)
218 return; 224 return;
219 for (const auto& page : pages) { 225 for (const auto& page : pages) {
220 if (delegate_->IsVisibleInUI(page.client_id)) { 226 if (delegate_->IsVisibleInUI(page.client_id)) {
221 std::string guid = page.client_id.id; 227 std::string guid = page.client_id.id;
222 DCHECK(items_.find(guid) == items_.end()); 228 DCHECK(items_.find(guid) == items_.end());
223 std::unique_ptr<ItemInfo> item = base::MakeUnique<ItemInfo>(page); 229 std::unique_ptr<ItemInfo> item =
224 item->temporarily_hidden = 230 base::MakeUnique<ItemInfo>(delegate_.get(), page);
225 delegate_->IsTemporarilyHiddenInUI(page.client_id);
226 items_[guid] = std::move(item); 231 items_[guid] = std::move(item);
227 } 232 }
228 } 233 }
229 model_->AddObserver(this); 234 model_->AddObserver(this);
230 235
231 state_ = State::LOADING_REQUESTS; 236 state_ = State::LOADING_REQUESTS;
232 request_coordinator_->GetAllRequests(base::Bind( 237 request_coordinator_->GetAllRequests(base::Bind(
233 &DownloadUIAdapter::OnRequestsLoaded, weak_ptr_factory_.GetWeakPtr())); 238 &DownloadUIAdapter::OnRequestsLoaded, weak_ptr_factory_.GetWeakPtr()));
234 } 239 }
235 240
236 void DownloadUIAdapter::OnRequestsLoaded( 241 void DownloadUIAdapter::OnRequestsLoaded(
237 std::vector<std::unique_ptr<SavePageRequest>> requests) { 242 std::vector<std::unique_ptr<SavePageRequest>> requests) {
238 // If multiple observers register quickly, the cache might be already loaded 243 // If multiple observers register quickly, the cache might be already loaded
239 // by the previous LoadCache call. At the same time, if all observers already 244 // by the previous LoadCache call. At the same time, if all observers already
240 // left, there is no reason to populate the cache. 245 // left, there is no reason to populate the cache.
241 if (state_ != State::LOADING_REQUESTS) 246 if (state_ != State::LOADING_REQUESTS)
242 return; 247 return;
243 248
244 for (const auto& request : requests) { 249 for (const auto& request : requests) {
245 if (delegate_->IsVisibleInUI(request->client_id())) { 250 if (delegate_->IsVisibleInUI(request->client_id())) {
246 std::string guid = request->client_id().id; 251 std::string guid = request->client_id().id;
247 DCHECK(items_.find(guid) == items_.end()); 252 DCHECK(items_.find(guid) == items_.end());
248 std::unique_ptr<ItemInfo> item = 253 std::unique_ptr<ItemInfo> item =
249 base::MakeUnique<ItemInfo>(*request.get()); 254 base::MakeUnique<ItemInfo>(delegate_.get(), *request.get());
250 item->temporarily_hidden =
251 delegate_->IsTemporarilyHiddenInUI(request->client_id());
252 items_[guid] = std::move(item); 255 items_[guid] = std::move(item);
253 } 256 }
254 } 257 }
255 request_coordinator_->AddObserver(this); 258 request_coordinator_->AddObserver(this);
256 259
257 state_ = State::LOADED; 260 state_ = State::LOADED;
258 for (Observer& observer : observers_) 261 for (Observer& observer : observers_)
259 observer.ItemsLoaded(); 262 observer.ItemsLoaded();
260 } 263 }
261 264
(...skipping 16 matching lines...) Expand all
278 // notification and when page is added, fire 'updated' instead of 'added'. 281 // notification and when page is added, fire 'updated' instead of 'added'.
279 bool request_to_page_transition = 282 bool request_to_page_transition =
280 (it != items_.end() && it->second->is_request && !item_info->is_request); 283 (it != items_.end() && it->second->is_request && !item_info->is_request);
281 284
282 items_[guid] = std::move(item_info); 285 items_[guid] = std::move(item_info);
283 286
284 if (state_ != State::LOADED) 287 if (state_ != State::LOADED)
285 return; 288 return;
286 289
287 DownloadUIItem* download_ui_item = items_[guid]->ui_item.get(); 290 DownloadUIItem* download_ui_item = items_[guid]->ui_item.get();
291
288 if (request_to_page_transition) { 292 if (request_to_page_transition) {
289 download_ui_item->download_state = DownloadUIItem::DownloadState::COMPLETE; 293 download_ui_item->download_state = DownloadUIItem::DownloadState::COMPLETE;
290 for (Observer& observer : observers_) 294 if (!items_[guid]->temporarily_hidden) {
291 observer.ItemUpdated(*download_ui_item); 295 for (Observer& observer : observers_)
296 observer.ItemUpdated(*download_ui_item);
297 }
292 } else { 298 } else {
293 for (Observer& observer : observers_) 299 if (!items_[guid]->temporarily_hidden) {
294 observer.ItemAdded(*download_ui_item); 300 for (Observer& observer : observers_)
301 observer.ItemAdded(*download_ui_item);
302 }
295 } 303 }
296 } 304 }
297 305
298 void DownloadUIAdapter::DeleteItemHelper(const std::string& guid) { 306 void DownloadUIAdapter::DeleteItemHelper(const std::string& guid) {
299 DownloadUIItems::const_iterator it = items_.find(guid); 307 DownloadUIItems::iterator it = items_.find(guid);
300 if (it == items_.end()) 308 if (it == items_.end())
301 return; 309 return;
310 DCHECK(deleting_item_ == nullptr);
311 deleting_item_ = std::move(it->second);
vitaliii 2017/02/15 10:12:16 This deleting_item_ approach feels a bit weird. Si
dewittj 2017/02/15 19:49:45 I'm thinking of a future scenario where we stop us
302 items_.erase(it); 312 items_.erase(it);
303 313
304 if (state_ != State::LOADED) 314 if (state_ == State::LOADED) {
305 return; 315 for (Observer& observer : observers_)
316 observer.ItemDeleted(guid);
317 }
306 318
307 for (Observer& observer : observers_) 319 deleting_item_.reset();
308 observer.ItemDeleted(guid);
309 } 320 }
310 321
311 } // namespace offline_pages 322 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698