| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/ui/webui/md_downloads/downloads_list_tracker.h" | 5 #include "chrome/browser/ui/webui/md_downloads/downloads_list_tracker.h" |
| 6 | 6 |
| 7 #include <iterator> | 7 #include <iterator> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 std::advance(it, sent_to_page_); | 122 std::advance(it, sent_to_page_); |
| 123 | 123 |
| 124 base::ListValue list; | 124 base::ListValue list; |
| 125 while (it != sorted_items_.end() && list.GetSize() < chunk_size_) { | 125 while (it != sorted_items_.end() && list.GetSize() < chunk_size_) { |
| 126 list.Append(CreateDownloadItemValue(*it)); | 126 list.Append(CreateDownloadItemValue(*it)); |
| 127 ++it; | 127 ++it; |
| 128 } | 128 } |
| 129 | 129 |
| 130 web_ui_->CallJavascriptFunctionUnsafe( | 130 web_ui_->CallJavascriptFunctionUnsafe( |
| 131 "downloads.Manager.insertItems", | 131 "downloads.Manager.insertItems", |
| 132 base::FundamentalValue(static_cast<int>(sent_to_page_)), list); | 132 base::Value(static_cast<int>(sent_to_page_)), list); |
| 133 | 133 |
| 134 sent_to_page_ += list.GetSize(); | 134 sent_to_page_ += list.GetSize(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void DownloadsListTracker::Stop() { | 137 void DownloadsListTracker::Stop() { |
| 138 sending_updates_ = false; | 138 sending_updates_ = false; |
| 139 } | 139 } |
| 140 | 140 |
| 141 DownloadManager* DownloadsListTracker::GetMainNotifierManager() const { | 141 DownloadManager* DownloadsListTracker::GetMainNotifierManager() const { |
| 142 return main_notifier_.GetManager(); | 142 return main_notifier_.GetManager(); |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 if (!sending_updates_) | 382 if (!sending_updates_) |
| 383 return; | 383 return; |
| 384 | 384 |
| 385 size_t index = GetIndex(insert); | 385 size_t index = GetIndex(insert); |
| 386 if (index >= chunk_size_ && index >= sent_to_page_) | 386 if (index >= chunk_size_ && index >= sent_to_page_) |
| 387 return; | 387 return; |
| 388 | 388 |
| 389 base::ListValue list; | 389 base::ListValue list; |
| 390 list.Append(CreateDownloadItemValue(*insert)); | 390 list.Append(CreateDownloadItemValue(*insert)); |
| 391 | 391 |
| 392 web_ui_->CallJavascriptFunctionUnsafe( | 392 web_ui_->CallJavascriptFunctionUnsafe("downloads.Manager.insertItems", |
| 393 "downloads.Manager.insertItems", | 393 base::Value(static_cast<int>(index)), |
| 394 base::FundamentalValue(static_cast<int>(index)), list); | 394 list); |
| 395 | 395 |
| 396 sent_to_page_++; | 396 sent_to_page_++; |
| 397 } | 397 } |
| 398 | 398 |
| 399 void DownloadsListTracker::UpdateItem(const SortedSet::iterator& update) { | 399 void DownloadsListTracker::UpdateItem(const SortedSet::iterator& update) { |
| 400 if (!sending_updates_ || GetIndex(update) >= sent_to_page_) | 400 if (!sending_updates_ || GetIndex(update) >= sent_to_page_) |
| 401 return; | 401 return; |
| 402 | 402 |
| 403 web_ui_->CallJavascriptFunctionUnsafe( | 403 web_ui_->CallJavascriptFunctionUnsafe( |
| 404 "downloads.Manager.updateItem", | 404 "downloads.Manager.updateItem", |
| 405 base::FundamentalValue(static_cast<int>(GetIndex(update))), | 405 base::Value(static_cast<int>(GetIndex(update))), |
| 406 *CreateDownloadItemValue(*update)); | 406 *CreateDownloadItemValue(*update)); |
| 407 } | 407 } |
| 408 | 408 |
| 409 size_t DownloadsListTracker::GetIndex(const SortedSet::iterator& item) const { | 409 size_t DownloadsListTracker::GetIndex(const SortedSet::iterator& item) const { |
| 410 // TODO(dbeam): this could be log(N) if |item| was random access. | 410 // TODO(dbeam): this could be log(N) if |item| was random access. |
| 411 return std::distance(sorted_items_.begin(), item); | 411 return std::distance(sorted_items_.begin(), item); |
| 412 } | 412 } |
| 413 | 413 |
| 414 void DownloadsListTracker::RemoveItem(const SortedSet::iterator& remove) { | 414 void DownloadsListTracker::RemoveItem(const SortedSet::iterator& remove) { |
| 415 if (sending_updates_) { | 415 if (sending_updates_) { |
| 416 size_t index = GetIndex(remove); | 416 size_t index = GetIndex(remove); |
| 417 if (index < sent_to_page_) { | 417 if (index < sent_to_page_) { |
| 418 web_ui_->CallJavascriptFunctionUnsafe( | 418 web_ui_->CallJavascriptFunctionUnsafe( |
| 419 "downloads.Manager.removeItem", | 419 "downloads.Manager.removeItem", base::Value(static_cast<int>(index))); |
| 420 base::FundamentalValue(static_cast<int>(index))); | |
| 421 sent_to_page_--; | 420 sent_to_page_--; |
| 422 } | 421 } |
| 423 } | 422 } |
| 424 sorted_items_.erase(remove); | 423 sorted_items_.erase(remove); |
| 425 } | 424 } |
| OLD | NEW |