| 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 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 ExtensionIdList pref_positions = extension_prefs_->GetToolbarOrder(); | 610 ExtensionIdList pref_positions = extension_prefs_->GetToolbarOrder(); |
| 611 size_t pref_position_size = pref_positions.size(); | 611 size_t pref_position_size = pref_positions.size(); |
| 612 for (size_t i = 0; i < last_known_positions_.size(); ++i) { | 612 for (size_t i = 0; i < last_known_positions_.size(); ++i) { |
| 613 if (std::find(pref_positions.begin(), pref_positions.end(), | 613 if (std::find(pref_positions.begin(), pref_positions.end(), |
| 614 last_known_positions_[i]) == pref_positions.end()) { | 614 last_known_positions_[i]) == pref_positions.end()) { |
| 615 pref_positions.push_back(last_known_positions_[i]); | 615 pref_positions.push_back(last_known_positions_[i]); |
| 616 } | 616 } |
| 617 } | 617 } |
| 618 last_known_positions_.swap(pref_positions); | 618 last_known_positions_.swap(pref_positions); |
| 619 | 619 |
| 620 // Clear out the old... | 620 int desired_index = 0; |
| 621 while (!toolbar_items_.empty()) { | 621 // Loop over the updated list of last known positions, moving any extensions |
| 622 scoped_refptr<const Extension> extension = toolbar_items_.back(); | 622 // that are in the wrong place. |
| 623 toolbar_items_.pop_back(); | 623 for (const std::string& id : last_known_positions_) { |
| 624 FOR_EACH_OBSERVER(Observer, observers_, | 624 int current_index = GetIndexForId(id); |
| 625 OnToolbarExtensionRemoved(extension.get())); | 625 if (current_index == -1) |
| 626 } | 626 continue; |
| 627 DCHECK(toolbar_items_.empty()); | 627 if (current_index != desired_index) { |
| 628 | 628 scoped_refptr<const Extension> extension = toolbar_items_[current_index]; |
| 629 // ...Add the new... | 629 toolbar_items_.erase(toolbar_items_.begin() + current_index); |
| 630 Populate(&last_known_positions_); | 630 toolbar_items_.insert(toolbar_items_.begin() + desired_index, extension); |
| 631 | 631 // Notify the observers to keep them up-to-date. |
| 632 // ...And notify. | 632 FOR_EACH_OBSERVER( |
| 633 for (size_t i = 0; i < toolbar_items().size(); ++i) { | 633 Observer, observers_, |
| 634 FOR_EACH_OBSERVER(Observer, observers_, | 634 OnToolbarExtensionMoved(extension.get(), desired_index)); |
| 635 OnToolbarExtensionAdded(toolbar_items()[i].get(), i)); | 635 } |
| 636 ++desired_index; |
| 636 } | 637 } |
| 637 | 638 |
| 638 if (last_known_positions_.size() > pref_position_size) { | 639 if (last_known_positions_.size() > pref_position_size) { |
| 639 // Need to update pref because we have extra icons. But can't call | 640 // Need to update pref because we have extra icons. But can't call |
| 640 // UpdatePrefs() directly within observation closure. | 641 // UpdatePrefs() directly within observation closure. |
| 641 base::MessageLoop::current()->PostTask( | 642 base::MessageLoop::current()->PostTask( |
| 642 FROM_HERE, | 643 FROM_HERE, |
| 643 base::Bind(&ExtensionToolbarModel::UpdatePrefs, | 644 base::Bind(&ExtensionToolbarModel::UpdatePrefs, |
| 644 weak_ptr_factory_.GetWeakPtr())); | 645 weak_ptr_factory_.GetWeakPtr())); |
| 645 } | 646 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 // Without the redesign, we only show extensions with browser actions. | 749 // Without the redesign, we only show extensions with browser actions. |
| 749 // Any extension without a browser action is an indication that we're | 750 // Any extension without a browser action is an indication that we're |
| 750 // showing something new. | 751 // showing something new. |
| 751 if (!extension->manifest()->HasKey(manifest_keys::kBrowserAction)) | 752 if (!extension->manifest()->HasKey(manifest_keys::kBrowserAction)) |
| 752 return true; | 753 return true; |
| 753 } | 754 } |
| 754 return false; | 755 return false; |
| 755 } | 756 } |
| 756 | 757 |
| 757 } // namespace extensions | 758 } // namespace extensions |
| OLD | NEW |