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

Side by Side Diff: chrome/browser/extensions/extension_toolbar_model.cc

Issue 1007083003: [Extensions Toolbar] Make pref update non-destructive (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix comment Created 5 years, 9 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
« no previous file with comments | « no previous file | chrome/browser/ui/toolbar/toolbar_actions_bar_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/toolbar/toolbar_actions_bar_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698