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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 web_ui_(web_ui), | 87 web_ui_(web_ui), |
88 should_show_(base::Bind(&DownloadsListTracker::ShouldShow, | 88 should_show_(base::Bind(&DownloadsListTracker::ShouldShow, |
89 base::Unretained(this))) { | 89 base::Unretained(this))) { |
90 Init(); | 90 Init(); |
91 } | 91 } |
92 | 92 |
93 DownloadsListTracker::~DownloadsListTracker() {} | 93 DownloadsListTracker::~DownloadsListTracker() {} |
94 | 94 |
95 void DownloadsListTracker::Reset() { | 95 void DownloadsListTracker::Reset() { |
96 if (sending_updates_) | 96 if (sending_updates_) |
97 web_ui_->CallJavascriptFunction("downloads.Manager.clearAll"); | 97 web_ui_->CallJavascriptFunctionUnsafe("downloads.Manager.clearAll"); |
98 sent_to_page_ = 0u; | 98 sent_to_page_ = 0u; |
99 } | 99 } |
100 | 100 |
101 bool DownloadsListTracker::SetSearchTerms(const base::ListValue& search_terms) { | 101 bool DownloadsListTracker::SetSearchTerms(const base::ListValue& search_terms) { |
102 std::vector<base::string16> new_terms; | 102 std::vector<base::string16> new_terms; |
103 new_terms.resize(search_terms.GetSize()); | 103 new_terms.resize(search_terms.GetSize()); |
104 | 104 |
105 for (size_t i = 0; i < search_terms.GetSize(); ++i) | 105 for (size_t i = 0; i < search_terms.GetSize(); ++i) |
106 search_terms.GetString(i, &new_terms[i]); | 106 search_terms.GetString(i, &new_terms[i]); |
107 | 107 |
(...skipping 12 matching lines...) Expand all Loading... |
120 | 120 |
121 SortedSet::iterator it = sorted_items_.begin(); | 121 SortedSet::iterator it = sorted_items_.begin(); |
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_->CallJavascriptFunction( | 130 web_ui_->CallJavascriptFunctionUnsafe( |
131 "downloads.Manager.insertItems", | 131 "downloads.Manager.insertItems", |
132 base::FundamentalValue(static_cast<int>(sent_to_page_)), | 132 base::FundamentalValue(static_cast<int>(sent_to_page_)), list); |
133 list); | |
134 | 133 |
135 sent_to_page_ += list.GetSize(); | 134 sent_to_page_ += list.GetSize(); |
136 } | 135 } |
137 | 136 |
138 void DownloadsListTracker::Stop() { | 137 void DownloadsListTracker::Stop() { |
139 sending_updates_ = false; | 138 sending_updates_ = false; |
140 } | 139 } |
141 | 140 |
142 DownloadManager* DownloadsListTracker::GetMainNotifierManager() const { | 141 DownloadManager* DownloadsListTracker::GetMainNotifierManager() const { |
143 return main_notifier_.GetManager(); | 142 return main_notifier_.GetManager(); |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 if (!sending_updates_) | 381 if (!sending_updates_) |
383 return; | 382 return; |
384 | 383 |
385 size_t index = GetIndex(insert); | 384 size_t index = GetIndex(insert); |
386 if (index >= chunk_size_ && index >= sent_to_page_) | 385 if (index >= chunk_size_ && index >= sent_to_page_) |
387 return; | 386 return; |
388 | 387 |
389 base::ListValue list; | 388 base::ListValue list; |
390 list.Append(CreateDownloadItemValue(*insert)); | 389 list.Append(CreateDownloadItemValue(*insert)); |
391 | 390 |
392 web_ui_->CallJavascriptFunction( | 391 web_ui_->CallJavascriptFunctionUnsafe( |
393 "downloads.Manager.insertItems", | 392 "downloads.Manager.insertItems", |
394 base::FundamentalValue(static_cast<int>(index)), | 393 base::FundamentalValue(static_cast<int>(index)), list); |
395 list); | |
396 | 394 |
397 sent_to_page_++; | 395 sent_to_page_++; |
398 } | 396 } |
399 | 397 |
400 void DownloadsListTracker::UpdateItem(const SortedSet::iterator& update) { | 398 void DownloadsListTracker::UpdateItem(const SortedSet::iterator& update) { |
401 if (!sending_updates_ || GetIndex(update) >= sent_to_page_) | 399 if (!sending_updates_ || GetIndex(update) >= sent_to_page_) |
402 return; | 400 return; |
403 | 401 |
404 web_ui_->CallJavascriptFunction( | 402 web_ui_->CallJavascriptFunctionUnsafe( |
405 "downloads.Manager.updateItem", | 403 "downloads.Manager.updateItem", |
406 base::FundamentalValue(static_cast<int>(GetIndex(update))), | 404 base::FundamentalValue(static_cast<int>(GetIndex(update))), |
407 *CreateDownloadItemValue(*update)); | 405 *CreateDownloadItemValue(*update)); |
408 } | 406 } |
409 | 407 |
410 size_t DownloadsListTracker::GetIndex(const SortedSet::iterator& item) const { | 408 size_t DownloadsListTracker::GetIndex(const SortedSet::iterator& item) const { |
411 // TODO(dbeam): this could be log(N) if |item| was random access. | 409 // TODO(dbeam): this could be log(N) if |item| was random access. |
412 return std::distance(sorted_items_.begin(), item); | 410 return std::distance(sorted_items_.begin(), item); |
413 } | 411 } |
414 | 412 |
415 void DownloadsListTracker::RemoveItem(const SortedSet::iterator& remove) { | 413 void DownloadsListTracker::RemoveItem(const SortedSet::iterator& remove) { |
416 if (sending_updates_) { | 414 if (sending_updates_) { |
417 size_t index = GetIndex(remove); | 415 size_t index = GetIndex(remove); |
418 if (index < sent_to_page_) { | 416 if (index < sent_to_page_) { |
419 web_ui_->CallJavascriptFunction( | 417 web_ui_->CallJavascriptFunctionUnsafe( |
420 "downloads.Manager.removeItem", | 418 "downloads.Manager.removeItem", |
421 base::FundamentalValue(static_cast<int>(index))); | 419 base::FundamentalValue(static_cast<int>(index))); |
422 sent_to_page_--; | 420 sent_to_page_--; |
423 } | 421 } |
424 } | 422 } |
425 sorted_items_.erase(remove); | 423 sorted_items_.erase(remove); |
426 } | 424 } |
OLD | NEW |