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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/search_engines/keyword_editor_controller.h"
7 #include "base/prefs/pref_registry_simple.h"
8 #include "chrome/browser/favicon/favicon_service_factory.h"
9 #include "chrome/browser/search_engines/template_url_service_factory.h"
10 #include "chrome/browser/ui/search_engines/template_url_table_model.h"
11 #include "components/search_engines/template_url.h"
12 #include "components/search_engines/template_url_service.h"
13 #include "content/public/browser/user_metrics.h"
15 using base::UserMetricsAction
;
17 KeywordEditorController::KeywordEditorController(Profile
* profile
)
18 : url_model_(TemplateURLServiceFactory::GetForProfile(profile
)) {
19 table_model_
.reset(new TemplateURLTableModel(
20 url_model_
, FaviconServiceFactory::GetForProfile(
21 profile
, ServiceAccessType::EXPLICIT_ACCESS
)));
24 KeywordEditorController::~KeywordEditorController() {
27 int KeywordEditorController::AddTemplateURL(const base::string16
& title
,
28 const base::string16
& keyword
,
29 const std::string
& url
) {
32 content::RecordAction(UserMetricsAction("KeywordEditor_AddKeyword"));
34 const int new_index
= table_model_
->last_other_engine_index();
35 table_model_
->Add(new_index
, title
, keyword
, url
);
40 void KeywordEditorController::ModifyTemplateURL(TemplateURL
* template_url
,
41 const base::string16
& title
,
42 const base::string16
& keyword
,
43 const std::string
& url
) {
45 const int index
= table_model_
->IndexOfTemplateURL(template_url
);
47 // Will happen if url was deleted out from under us while the user was
52 // Don't do anything if the entry didn't change.
53 if ((template_url
->short_name() == title
) &&
54 (template_url
->keyword() == keyword
) && (template_url
->url() == url
))
57 table_model_
->ModifyTemplateURL(index
, title
, keyword
, url
);
59 content::RecordAction(UserMetricsAction("KeywordEditor_ModifiedKeyword"));
62 bool KeywordEditorController::CanEdit(const TemplateURL
* url
) const {
63 return (url
->GetType() == TemplateURL::NORMAL
) &&
64 (url
!= url_model_
->GetDefaultSearchProvider() ||
65 !url_model_
->is_default_search_managed());
68 bool KeywordEditorController::CanMakeDefault(const TemplateURL
* url
) const {
69 return url_model_
->CanMakeDefault(url
);
72 bool KeywordEditorController::CanRemove(const TemplateURL
* url
) const {
73 return (url
->GetType() == TemplateURL::NORMAL
) &&
74 (url
!= url_model_
->GetDefaultSearchProvider());
77 void KeywordEditorController::RemoveTemplateURL(int index
) {
78 table_model_
->Remove(index
);
79 content::RecordAction(UserMetricsAction("KeywordEditor_RemoveKeyword"));
82 TemplateURL
* KeywordEditorController::GetDefaultSearchProvider() {
83 return url_model_
->GetDefaultSearchProvider();
86 int KeywordEditorController::MakeDefaultTemplateURL(int index
) {
87 return table_model_
->MakeDefaultTemplateURL(index
);
90 bool KeywordEditorController::loaded() const {
91 return url_model_
->loaded();
94 TemplateURL
* KeywordEditorController::GetTemplateURL(int index
) {
95 return table_model_
->GetTemplateURL(index
);