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/ui/search_engines/template_url_table_model.h" | 5 #include "chrome/browser/ui/search_engines/template_url_table_model.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 template_url_service_(template_url_service), | 124 template_url_service_(template_url_service), |
125 favicon_service_(favicon_service) { | 125 favicon_service_(favicon_service) { |
126 DCHECK(template_url_service); | 126 DCHECK(template_url_service); |
127 template_url_service_->Load(); | 127 template_url_service_->Load(); |
128 template_url_service_->AddObserver(this); | 128 template_url_service_->AddObserver(this); |
129 Reload(); | 129 Reload(); |
130 } | 130 } |
131 | 131 |
132 TemplateURLTableModel::~TemplateURLTableModel() { | 132 TemplateURLTableModel::~TemplateURLTableModel() { |
133 template_url_service_->RemoveObserver(this); | 133 template_url_service_->RemoveObserver(this); |
134 base::STLDeleteElements(&entries_); | |
135 } | 134 } |
136 | 135 |
137 void TemplateURLTableModel::Reload() { | 136 void TemplateURLTableModel::Reload() { |
138 base::STLDeleteElements(&entries_); | 137 entries_.clear(); |
139 | 138 |
140 TemplateURLService::TemplateURLVector urls = | 139 TemplateURLService::TemplateURLVector urls = |
141 template_url_service_->GetTemplateURLs(); | 140 template_url_service_->GetTemplateURLs(); |
142 | 141 |
143 std::vector<ModelEntry*> default_entries, other_entries, extension_entries; | 142 std::vector<std::unique_ptr<ModelEntry>> default_entries, other_entries, |
| 143 extension_entries; |
144 // Keywords that can be made the default first. | 144 // Keywords that can be made the default first. |
145 for (TemplateURLService::TemplateURLVector::iterator i = urls.begin(); | 145 for (auto* template_url : urls) { |
146 i != urls.end(); ++i) { | |
147 TemplateURL* template_url = *i; | |
148 // NOTE: we don't use ShowInDefaultList here to avoid items bouncing around | 146 // NOTE: we don't use ShowInDefaultList here to avoid items bouncing around |
149 // the lists while editing. | 147 // the lists while editing. |
150 if (template_url->show_in_default_list()) | 148 if (template_url->show_in_default_list()) |
151 default_entries.push_back(new ModelEntry(this, template_url)); | 149 default_entries.push_back( |
| 150 base::MakeUnique<ModelEntry>(this, template_url)); |
152 else if (template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION) | 151 else if (template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION) |
153 extension_entries.push_back(new ModelEntry(this, template_url)); | 152 extension_entries.push_back( |
| 153 base::MakeUnique<ModelEntry>(this, template_url)); |
154 else | 154 else |
155 other_entries.push_back(new ModelEntry(this, template_url)); | 155 other_entries.push_back(base::MakeUnique<ModelEntry>(this, template_url)); |
156 } | 156 } |
157 | 157 |
158 last_search_engine_index_ = static_cast<int>(default_entries.size()); | 158 last_search_engine_index_ = static_cast<int>(default_entries.size()); |
159 last_other_engine_index_ = last_search_engine_index_ + | 159 last_other_engine_index_ = last_search_engine_index_ + |
160 static_cast<int>(other_entries.size()); | 160 static_cast<int>(other_entries.size()); |
161 | 161 |
162 entries_.insert(entries_.end(), | 162 std::move(default_entries.begin(), default_entries.end(), |
163 default_entries.begin(), | 163 std::back_inserter(entries_)); |
164 default_entries.end()); | |
165 | 164 |
166 entries_.insert(entries_.end(), | 165 std::move(other_entries.begin(), other_entries.end(), |
167 other_entries.begin(), | 166 std::back_inserter(entries_)); |
168 other_entries.end()); | |
169 | 167 |
170 entries_.insert(entries_.end(), | 168 std::move(extension_entries.begin(), extension_entries.end(), |
171 extension_entries.begin(), | 169 std::back_inserter(entries_)); |
172 extension_entries.end()); | |
173 | 170 |
174 if (observer_) | 171 if (observer_) |
175 observer_->OnModelChanged(); | 172 observer_->OnModelChanged(); |
176 } | 173 } |
177 | 174 |
178 int TemplateURLTableModel::RowCount() { | 175 int TemplateURLTableModel::RowCount() { |
179 return static_cast<int>(entries_.size()); | 176 return static_cast<int>(entries_.size()); |
180 } | 177 } |
181 | 178 |
182 base::string16 TemplateURLTableModel::GetText(int row, int col_id) { | 179 base::string16 TemplateURLTableModel::GetText(int row, int col_id) { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 | 295 |
299 NotifyChanged(index); | 296 NotifyChanged(index); |
300 } | 297 } |
301 | 298 |
302 TemplateURL* TemplateURLTableModel::GetTemplateURL(int index) { | 299 TemplateURL* TemplateURLTableModel::GetTemplateURL(int index) { |
303 return entries_[index]->template_url(); | 300 return entries_[index]->template_url(); |
304 } | 301 } |
305 | 302 |
306 int TemplateURLTableModel::IndexOfTemplateURL( | 303 int TemplateURLTableModel::IndexOfTemplateURL( |
307 const TemplateURL* template_url) { | 304 const TemplateURL* template_url) { |
308 for (std::vector<ModelEntry*>::iterator i = entries_.begin(); | 305 for (auto i = entries_.begin(); i != entries_.end(); ++i) { |
309 i != entries_.end(); ++i) { | 306 ModelEntry* entry = i->get(); |
310 ModelEntry* entry = *i; | |
311 if (entry->template_url() == template_url) | 307 if (entry->template_url() == template_url) |
312 return static_cast<int>(i - entries_.begin()); | 308 return static_cast<int>(i - entries_.begin()); |
313 } | 309 } |
314 return -1; | 310 return -1; |
315 } | 311 } |
316 | 312 |
317 int TemplateURLTableModel::MoveToMainGroup(int index) { | 313 int TemplateURLTableModel::MoveToMainGroup(int index) { |
318 if (index < last_search_engine_index_) | 314 if (index < last_search_engine_index_) |
319 return index; // Already in the main group. | 315 return index; // Already in the main group. |
320 | 316 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 } | 353 } |
358 | 354 |
359 void TemplateURLTableModel::NotifyChanged(int index) { | 355 void TemplateURLTableModel::NotifyChanged(int index) { |
360 if (observer_) { | 356 if (observer_) { |
361 DCHECK_GE(index, 0); | 357 DCHECK_GE(index, 0); |
362 observer_->OnItemsChanged(index, 1); | 358 observer_->OnItemsChanged(index, 1); |
363 } | 359 } |
364 } | 360 } |
365 | 361 |
366 void TemplateURLTableModel::FaviconAvailable(ModelEntry* entry) { | 362 void TemplateURLTableModel::FaviconAvailable(ModelEntry* entry) { |
367 std::vector<ModelEntry*>::iterator i = | 363 auto i = std::find_if(entries_.begin(), entries_.end(), |
368 std::find(entries_.begin(), entries_.end(), entry); | 364 [entry](const std::unique_ptr<ModelEntry>& ptr) { |
| 365 return ptr.get() == entry; |
| 366 }); |
369 DCHECK(i != entries_.end()); | 367 DCHECK(i != entries_.end()); |
370 NotifyChanged(static_cast<int>(i - entries_.begin())); | 368 NotifyChanged(static_cast<int>(i - entries_.begin())); |
371 } | 369 } |
372 | 370 |
373 void TemplateURLTableModel::OnTemplateURLServiceChanged() { | 371 void TemplateURLTableModel::OnTemplateURLServiceChanged() { |
374 Reload(); | 372 Reload(); |
375 } | 373 } |
376 | 374 |
377 std::unique_ptr<TemplateURLTableModel::ModelEntry> | 375 std::unique_ptr<TemplateURLTableModel::ModelEntry> |
378 TemplateURLTableModel::RemoveEntry(int index) { | 376 TemplateURLTableModel::RemoveEntry(int index) { |
379 std::unique_ptr<ModelEntry> entry(entries_[index]); | 377 std::unique_ptr<ModelEntry> entry = std::move(entries_[index]); |
380 entries_.erase(index + entries_.begin()); | 378 entries_.erase(index + entries_.begin()); |
381 if (index < last_search_engine_index_) | 379 if (index < last_search_engine_index_) |
382 --last_search_engine_index_; | 380 --last_search_engine_index_; |
383 if (index < last_other_engine_index_) | 381 if (index < last_other_engine_index_) |
384 --last_other_engine_index_; | 382 --last_other_engine_index_; |
385 if (observer_) | 383 if (observer_) |
386 observer_->OnItemsRemoved(index, 1); | 384 observer_->OnItemsRemoved(index, 1); |
387 return entry; | 385 return entry; |
388 } | 386 } |
389 | 387 |
390 void TemplateURLTableModel::AddEntry(int index, | 388 void TemplateURLTableModel::AddEntry(int index, |
391 std::unique_ptr<ModelEntry> entry) { | 389 std::unique_ptr<ModelEntry> entry) { |
392 entries_.insert(entries_.begin() + index, entry.release()); | 390 entries_.insert(entries_.begin() + index, std::move(entry)); |
393 if (index <= last_other_engine_index_) | 391 if (index <= last_other_engine_index_) |
394 ++last_other_engine_index_; | 392 ++last_other_engine_index_; |
395 if (observer_) | 393 if (observer_) |
396 observer_->OnItemsAdded(index, 1); | 394 observer_->OnItemsAdded(index, 1); |
397 } | 395 } |
OLD | NEW |