Skip to content

Commit

Permalink
Fix browser crash when resetting default search engine.
Browse files Browse the repository at this point in the history
R=pkasting@chromium.org
BUG=258551
NOTRY=True

Review URL: https://codereview.chromium.org/19971006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@213727 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
dbeam@chromium.org committed Jul 26, 2013
1 parent 644cc3f commit b808951
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
47 changes: 25 additions & 22 deletions chrome/browser/ui/search_engines/template_url_table_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,7 @@ void TemplateURLTableModel::Remove(int index) {
template_url_service_->RemoveObserver(this);
TemplateURL* template_url = GetTemplateURL(index);

scoped_ptr<ModelEntry> entry(entries_[index]);
entries_.erase(entries_.begin() + index);
if (index < last_search_engine_index_)
--last_search_engine_index_;
if (index < last_other_engine_index_)
--last_other_engine_index_;
if (observer_)
observer_->OnItemsRemoved(index, 1);
scoped_ptr<ModelEntry> entry(RemoveEntry(index));

// Make sure to remove from the table model first, otherwise the
// TemplateURL would be freed.
Expand All @@ -280,13 +273,9 @@ void TemplateURLTableModel::Add(int index,
data.SetURL(url);
TemplateURL* turl = new TemplateURL(template_url_service_->profile(), data);
template_url_service_->Add(turl);
ModelEntry* entry = new ModelEntry(this, turl);
scoped_ptr<ModelEntry> entry(new ModelEntry(this, turl));
template_url_service_->AddObserver(this);
entries_.insert(entries_.begin() + index, entry);
if (index <= last_other_engine_index_)
++last_other_engine_index_;
if (observer_)
observer_->OnItemsAdded(index, 1);
AddEntry(index, entry.Pass());
}

void TemplateURLTableModel::ModifyTemplateURL(int index,
Expand Down Expand Up @@ -332,15 +321,9 @@ int TemplateURLTableModel::MoveToMainGroup(int index) {
if (index < last_search_engine_index_)
return index; // Already in the main group.

ModelEntry* current_entry = entries_[index];
entries_.erase(index + entries_.begin());
if (observer_)
observer_->OnItemsRemoved(index, 1);

scoped_ptr<ModelEntry> current_entry(RemoveEntry(index));
const int new_index = last_search_engine_index_++;
entries_.insert(entries_.begin() + new_index, current_entry);
if (observer_)
observer_->OnItemsAdded(new_index, 1);
AddEntry(new_index, current_entry.Pass());
return new_index;
}

Expand Down Expand Up @@ -393,3 +376,23 @@ void TemplateURLTableModel::FaviconAvailable(ModelEntry* entry) {
void TemplateURLTableModel::OnTemplateURLServiceChanged() {
Reload();
}

scoped_ptr<ModelEntry> TemplateURLTableModel::RemoveEntry(int index) {
scoped_ptr<ModelEntry> entry(entries_[index]);
entries_.erase(index + entries_.begin());
if (index < last_search_engine_index_)
--last_search_engine_index_;
if (index < last_other_engine_index_)
--last_other_engine_index_;
if (observer_)
observer_->OnItemsRemoved(index, 1);
return entry.Pass();
}

void TemplateURLTableModel::AddEntry(int index, scoped_ptr<ModelEntry> entry) {
entries_.insert(entries_.begin() + index, entry.release());
if (index <= last_other_engine_index_)
++last_other_engine_index_;
if (observer_)
observer_->OnItemsAdded(index, 1);
}
7 changes: 7 additions & 0 deletions chrome/browser/ui/search_engines/template_url_table_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <vector>

#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
#include "chrome/browser/search_engines/template_url_service_observer.h"
#include "ui/base/models/table_model.h"
Expand Down Expand Up @@ -110,6 +111,12 @@ class TemplateURLTableModel : public ui::TableModel,
// TemplateURLServiceObserver notification.
virtual void OnTemplateURLServiceChanged() OVERRIDE;

// Removes the entry at |index| from |entries_| and returns the removed item.
scoped_ptr<ModelEntry> RemoveEntry(int index);

// Adds |entry| to |entries_| at |index| and takes ownership.
void AddEntry(int index, scoped_ptr<ModelEntry> entry);

ui::TableModelObserver* observer_;

// The entries.
Expand Down

0 comments on commit b808951

Please sign in to comment.