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

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

Issue 941373002: [Extensions] Fix extension toolbar crash (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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 | « chrome/browser/extensions/extension_toolbar_model.h ('k') | no next file » | 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 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 // For the last-known position, we use the index of the extension that is 321 // For the last-known position, we use the index of the extension that is
322 // just before this extension, plus one. (Note that this isn't the same 322 // just before this extension, plus one. (Note that this isn't the same
323 // as new_index + 1, because last_known_positions_ can include disabled 323 // as new_index + 1, because last_known_positions_ can include disabled
324 // extensions.) 324 // extensions.)
325 int new_last_known_index = 325 int new_last_known_index =
326 new_index == 0 ? 0 : 326 new_index == 0 ? 0 :
327 std::find(last_known_positions_.begin(), 327 std::find(last_known_positions_.begin(),
328 last_known_positions_.end(), 328 last_known_positions_.end(),
329 toolbar_items_[new_index - 1]->id()) - 329 toolbar_items_[new_index - 1]->id()) -
330 last_known_positions_.begin() + 1; 330 last_known_positions_.begin() + 1;
331 // In theory, the extension before this one should always
332 // be in last known positions, but if something funny happened with prefs,
333 // make sure we handle it.
334 // TODO(devlin): Track down these cases so we can CHECK this.
335 new_last_known_index =
336 std::min(new_last_known_index,
337 static_cast<int>(last_known_positions_.size()));
Marijn Kruisselbrink 2015/02/20 18:26:41 I personally prefer std::min<int>(a, b) over std::
Devlin 2015/02/20 21:50:00 Done.
331 last_known_positions_.insert( 338 last_known_positions_.insert(
332 last_known_positions_.begin() + new_last_known_index, extension->id()); 339 last_known_positions_.begin() + new_last_known_index, extension->id());
333 UpdatePrefs(); 340 UpdatePrefs();
334 } else { 341 } else {
335 new_index = FindNewPositionFromLastKnownGood(extension); 342 new_index = FindNewPositionFromLastKnownGood(extension);
336 } 343 }
337 344
338 toolbar_items_.insert(toolbar_items_.begin() + new_index, extension); 345 toolbar_items_.insert(toolbar_items_.begin() + new_index, extension);
339 346
340 // If we're currently highlighting, then even though we add a browser action 347 // If we're currently highlighting, then even though we add a browser action
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 // have holes. 423 // have holes.
417 // 2. Create a vector of extensions that did not have a pref value. 424 // 2. Create a vector of extensions that did not have a pref value.
418 // 3. Remove holes from the sorted vector and append the unsorted vector. 425 // 3. Remove holes from the sorted vector and append the unsorted vector.
419 void ExtensionToolbarModel::InitializeExtensionList() { 426 void ExtensionToolbarModel::InitializeExtensionList() {
420 DCHECK(toolbar_items_.empty()); // We shouldn't have any items yet. 427 DCHECK(toolbar_items_.empty()); // We shouldn't have any items yet.
421 428
422 last_known_positions_ = extension_prefs_->GetToolbarOrder(); 429 last_known_positions_ = extension_prefs_->GetToolbarOrder();
423 if (profile_->IsOffTheRecord()) 430 if (profile_->IsOffTheRecord())
424 IncognitoPopulate(); 431 IncognitoPopulate();
425 else 432 else
426 Populate(last_known_positions_); 433 Populate(&last_known_positions_);
427 434
428 extensions_initialized_ = true; 435 extensions_initialized_ = true;
429 MaybeUpdateVisibilityPrefs(); 436 MaybeUpdateVisibilityPrefs();
430 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarModelInitialized()); 437 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarModelInitialized());
431 } 438 }
432 439
433 void ExtensionToolbarModel::Populate(const ExtensionIdList& positions) { 440 void ExtensionToolbarModel::Populate(ExtensionIdList* positions) {
434 DCHECK(!profile_->IsOffTheRecord()); 441 DCHECK(!profile_->IsOffTheRecord());
435 const ExtensionSet& extensions = 442 const ExtensionSet& extensions =
436 ExtensionRegistry::Get(profile_)->enabled_extensions(); 443 ExtensionRegistry::Get(profile_)->enabled_extensions();
437 // Items that have explicit positions. 444 // Items that have explicit positions.
438 ExtensionList sorted(positions.size(), NULL); 445 ExtensionList sorted(positions->size(), NULL);
439 // The items that don't have explicit positions. 446 // The items that don't have explicit positions.
440 ExtensionList unsorted; 447 ExtensionList unsorted;
441 448
442 // Create the lists. 449 // Create the lists.
443 int hidden = 0; 450 int hidden = 0;
444 for (const scoped_refptr<const Extension>& extension : extensions) { 451 for (const scoped_refptr<const Extension>& extension : extensions) {
445 if (!ShouldAddExtension(extension.get())) { 452 if (!ShouldAddExtension(extension.get())) {
446 if (!ExtensionActionAPI::GetBrowserActionVisibility(extension_prefs_, 453 if (!ExtensionActionAPI::GetBrowserActionVisibility(extension_prefs_,
447 extension->id())) 454 extension->id()))
448 ++hidden; 455 ++hidden;
449 continue; 456 continue;
450 } 457 }
451 458
452 ExtensionIdList::const_iterator pos = 459 ExtensionIdList::const_iterator pos =
453 std::find(positions.begin(), positions.end(), extension->id()); 460 std::find(positions->begin(), positions->end(), extension->id());
454 if (pos != positions.end()) 461 if (pos != positions->end()) {
455 sorted[pos - positions.begin()] = extension; 462 sorted[pos - positions->begin()] = extension;
456 else 463 } else {
464 // Unknown extension - push it to the back of unsorted, and add it to the
465 // of ids at the end.
Marijn Kruisselbrink 2015/02/20 18:26:41 "and add it to the of ids at the end"? I think yo
Devlin 2015/02/20 21:50:00 Done.
457 unsorted.push_back(extension); 466 unsorted.push_back(extension);
467 positions->push_back(extension->id());
468 }
458 } 469 }
459 470
460 // Merge the lists. 471 // Merge the lists.
461 sorted.insert(sorted.end(), unsorted.begin(), unsorted.end()); 472 sorted.insert(sorted.end(), unsorted.begin(), unsorted.end());
462 toolbar_items_.reserve(sorted.size()); 473 toolbar_items_.reserve(sorted.size());
463 474
464 for (const scoped_refptr<const Extension>& extension : sorted) { 475 for (const scoped_refptr<const Extension>& extension : sorted) {
465 // It's possible for the extension order to contain items that aren't 476 // It's possible for the extension order to contain items that aren't
466 // actually loaded on this machine. For example, when extension sync is on, 477 // actually loaded on this machine. For example, when extension sync is on,
467 // we sync the extension order as-is but double-check with the user before 478 // we sync the extension order as-is but double-check with the user before
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 // Clear out the old... 594 // Clear out the old...
584 while (!toolbar_items_.empty()) { 595 while (!toolbar_items_.empty()) {
585 scoped_refptr<const Extension> extension = toolbar_items_.back(); 596 scoped_refptr<const Extension> extension = toolbar_items_.back();
586 toolbar_items_.pop_back(); 597 toolbar_items_.pop_back();
587 FOR_EACH_OBSERVER( 598 FOR_EACH_OBSERVER(
588 Observer, observers_, ToolbarExtensionRemoved(extension.get())); 599 Observer, observers_, ToolbarExtensionRemoved(extension.get()));
589 } 600 }
590 DCHECK(toolbar_items_.empty()); 601 DCHECK(toolbar_items_.empty());
591 602
592 // ...Add the new... 603 // ...Add the new...
593 Populate(last_known_positions_); 604 Populate(&last_known_positions_);
594 605
595 // ...And notify. 606 // ...And notify.
596 for (size_t i = 0; i < toolbar_items().size(); ++i) { 607 for (size_t i = 0; i < toolbar_items().size(); ++i) {
597 FOR_EACH_OBSERVER(Observer, 608 FOR_EACH_OBSERVER(Observer,
598 observers_, 609 observers_,
599 ToolbarExtensionAdded(toolbar_items()[i].get(), i)); 610 ToolbarExtensionAdded(toolbar_items()[i].get(), i));
600 } 611 }
601 612
602 if (last_known_positions_.size() > pref_position_size) { 613 if (last_known_positions_.size() > pref_position_size) {
603 // Need to update pref because we have extra icons. But can't call 614 // Need to update pref because we have extra icons. But can't call
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
699 if (is_highlighting_) { 710 if (is_highlighting_) {
700 highlighted_items_.clear(); 711 highlighted_items_.clear();
701 is_highlighting_ = false; 712 is_highlighting_ = false;
702 if (old_visible_icon_count_ != visible_icon_count_) 713 if (old_visible_icon_count_ != visible_icon_count_)
703 SetVisibleIconCount(old_visible_icon_count_); 714 SetVisibleIconCount(old_visible_icon_count_);
704 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); 715 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false));
705 } 716 }
706 } 717 }
707 718
708 } // namespace extensions 719 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_toolbar_model.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698