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 "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/search_engines/template_url.h"
11 #include "chrome/browser/search_engines/template_url_service.h"
12 #include "chrome/browser/search_engines/template_url_service_factory.h"
13 #include "chrome/browser/ui/search_engines/template_url_table_model.h"
14 #include "chrome/common/pref_names.h"
15 #include "content/public/browser/user_metrics.h"
17 using base::UserMetricsAction
;
19 KeywordEditorController::KeywordEditorController(Profile
* profile
)
21 table_model_
.reset(new TemplateURLTableModel(
22 TemplateURLServiceFactory::GetForProfile(profile
)));
25 KeywordEditorController::~KeywordEditorController() {
29 // TODO(rsesek): Other platforms besides Mac should remember window
30 // placement. http://crbug.com/22269
31 void KeywordEditorController::RegisterPrefs(PrefRegistrySimple
* registry
) {
32 registry
->RegisterDictionaryPref(prefs::kKeywordEditorWindowPlacement
);
35 int KeywordEditorController::AddTemplateURL(const base::string16
& title
,
36 const base::string16
& keyword
,
37 const std::string
& url
) {
40 content::RecordAction(UserMetricsAction("KeywordEditor_AddKeyword"));
42 const int new_index
= table_model_
->last_other_engine_index();
43 table_model_
->Add(new_index
, title
, keyword
, url
);
48 void KeywordEditorController::ModifyTemplateURL(TemplateURL
* template_url
,
49 const base::string16
& title
,
50 const base::string16
& keyword
,
51 const std::string
& url
) {
53 const int index
= table_model_
->IndexOfTemplateURL(template_url
);
55 // Will happen if url was deleted out from under us while the user was
60 // Don't do anything if the entry didn't change.
61 if ((template_url
->short_name() == title
) &&
62 (template_url
->keyword() == keyword
) && (template_url
->url() == url
))
65 table_model_
->ModifyTemplateURL(index
, title
, keyword
, url
);
67 content::RecordAction(UserMetricsAction("KeywordEditor_ModifiedKeyword"));
70 bool KeywordEditorController::CanEdit(const TemplateURL
* url
) const {
71 return (url
->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
) &&
72 (!url_model()->is_default_search_managed() ||
73 (url
!= url_model()->GetDefaultSearchProvider()));
76 bool KeywordEditorController::CanMakeDefault(const TemplateURL
* url
) const {
77 return url_model()->CanMakeDefault(url
);
80 bool KeywordEditorController::CanRemove(const TemplateURL
* url
) const {
81 return url
!= url_model()->GetDefaultSearchProvider();
84 void KeywordEditorController::RemoveTemplateURL(int index
) {
85 table_model_
->Remove(index
);
86 content::RecordAction(UserMetricsAction("KeywordEditor_RemoveKeyword"));
89 int KeywordEditorController::MakeDefaultTemplateURL(int index
) {
90 return table_model_
->MakeDefaultTemplateURL(index
);
93 bool KeywordEditorController::loaded() const {
94 return url_model()->loaded();
97 TemplateURL
* KeywordEditorController::GetTemplateURL(int index
) {
98 return table_model_
->GetTemplateURL(index
);
101 TemplateURLService
* KeywordEditorController::url_model() const {
102 return table_model_
->template_url_service();