OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/extension_toolbar_model.h" | 5 #include "chrome/browser/extensions/extension_toolbar_model.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 if (highlighted_items_.empty()) | 387 if (highlighted_items_.empty()) |
388 StopHighlighting(); | 388 StopHighlighting(); |
389 } | 389 } |
390 } else { | 390 } else { |
391 FOR_EACH_OBSERVER(Observer, observers_, ToolbarExtensionRemoved(extension)); | 391 FOR_EACH_OBSERVER(Observer, observers_, ToolbarExtensionRemoved(extension)); |
392 } | 392 } |
393 | 393 |
394 UpdatePrefs(); | 394 UpdatePrefs(); |
395 } | 395 } |
396 | 396 |
397 void ExtensionToolbarModel::ClearItems() { | |
398 size_t items_count = toolbar_items_.size(); | |
399 for (size_t i = 0; i < items_count; ++i) { | |
400 const Extension* extension = toolbar_items_.back().get(); | |
401 toolbar_items_.pop_back(); | |
402 FOR_EACH_OBSERVER(Observer, observers_, ToolbarExtensionRemoved(extension)); | |
403 } | |
404 DCHECK(toolbar_items_.empty()); | |
405 } | |
406 | |
407 // Combine the currently enabled extensions that have browser actions (which | 397 // Combine the currently enabled extensions that have browser actions (which |
408 // we get from the ExtensionRegistry) with the ordering we get from the | 398 // we get from the ExtensionRegistry) with the ordering we get from the |
409 // pref service. For robustness we use a somewhat inefficient process: | 399 // pref service. For robustness we use a somewhat inefficient process: |
410 // 1. Create a vector of extensions sorted by their pref values. This vector may | 400 // 1. Create a vector of extensions sorted by their pref values. This vector may |
411 // have holes. | 401 // have holes. |
412 // 2. Create a vector of extensions that did not have a pref value. | 402 // 2. Create a vector of extensions that did not have a pref value. |
413 // 3. Remove holes from the sorted vector and append the unsorted vector. | 403 // 3. Remove holes from the sorted vector and append the unsorted vector. |
414 void ExtensionToolbarModel::InitializeExtensionList() { | 404 void ExtensionToolbarModel::InitializeExtensionList() { |
415 DCHECK(toolbar_items_.empty()); // We shouldn't have any items yet. | 405 DCHECK(toolbar_items_.empty()); // We shouldn't have any items yet. |
416 | 406 |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 ExtensionIdList pref_positions = extension_prefs_->GetToolbarOrder(); | 558 ExtensionIdList pref_positions = extension_prefs_->GetToolbarOrder(); |
569 size_t pref_position_size = pref_positions.size(); | 559 size_t pref_position_size = pref_positions.size(); |
570 for (size_t i = 0; i < last_known_positions_.size(); ++i) { | 560 for (size_t i = 0; i < last_known_positions_.size(); ++i) { |
571 if (std::find(pref_positions.begin(), pref_positions.end(), | 561 if (std::find(pref_positions.begin(), pref_positions.end(), |
572 last_known_positions_[i]) == pref_positions.end()) { | 562 last_known_positions_[i]) == pref_positions.end()) { |
573 pref_positions.push_back(last_known_positions_[i]); | 563 pref_positions.push_back(last_known_positions_[i]); |
574 } | 564 } |
575 } | 565 } |
576 last_known_positions_.swap(pref_positions); | 566 last_known_positions_.swap(pref_positions); |
577 | 567 |
578 ClearItems(); | 568 // Clear out the old... |
| 569 while (!toolbar_items_.empty()) { |
| 570 scoped_refptr<const Extension> extension = toolbar_items_.back(); |
| 571 toolbar_items_.pop_back(); |
| 572 FOR_EACH_OBSERVER( |
| 573 Observer, observers_, ToolbarExtensionRemoved(extension.get())); |
| 574 } |
| 575 DCHECK(toolbar_items_.empty()); |
579 | 576 |
580 // Re-populate. | 577 // ...Add the new... |
581 Populate(last_known_positions_); | 578 Populate(last_known_positions_); |
582 | 579 |
| 580 // ...And notify. |
| 581 for (size_t i = 0; i < toolbar_items().size(); ++i) { |
| 582 FOR_EACH_OBSERVER(Observer, |
| 583 observers_, |
| 584 ToolbarExtensionAdded(toolbar_items()[i].get(), i)); |
| 585 } |
| 586 |
583 if (last_known_positions_.size() > pref_position_size) { | 587 if (last_known_positions_.size() > pref_position_size) { |
584 // Need to update pref because we have extra icons. But can't call | 588 // Need to update pref because we have extra icons. But can't call |
585 // UpdatePrefs() directly within observation closure. | 589 // UpdatePrefs() directly within observation closure. |
586 base::MessageLoop::current()->PostTask( | 590 base::MessageLoop::current()->PostTask( |
587 FROM_HERE, | 591 FROM_HERE, |
588 base::Bind(&ExtensionToolbarModel::UpdatePrefs, | 592 base::Bind(&ExtensionToolbarModel::UpdatePrefs, |
589 weak_ptr_factory_.GetWeakPtr())); | 593 weak_ptr_factory_.GetWeakPtr())); |
590 } | 594 } |
591 } | 595 } |
592 | 596 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 if (is_highlighting_) { | 719 if (is_highlighting_) { |
716 highlighted_items_.clear(); | 720 highlighted_items_.clear(); |
717 is_highlighting_ = false; | 721 is_highlighting_ = false; |
718 if (old_visible_icon_count_ != visible_icon_count_) | 722 if (old_visible_icon_count_ != visible_icon_count_) |
719 SetVisibleIconCount(old_visible_icon_count_); | 723 SetVisibleIconCount(old_visible_icon_count_); |
720 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); | 724 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); |
721 } | 725 } |
722 } | 726 } |
723 | 727 |
724 } // namespace extensions | 728 } // namespace extensions |
OLD | NEW |