• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/ui/search_engines/keyword_editor_controller.h"
6 
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"
16 
17 using base::UserMetricsAction;
18 
KeywordEditorController(Profile * profile)19 KeywordEditorController::KeywordEditorController(Profile* profile)
20     : profile_(profile) {
21   table_model_.reset(new TemplateURLTableModel(
22       TemplateURLServiceFactory::GetForProfile(profile)));
23 }
24 
~KeywordEditorController()25 KeywordEditorController::~KeywordEditorController() {
26 }
27 
28 // static
29 // TODO(rsesek): Other platforms besides Mac should remember window
30 // placement. http://crbug.com/22269
RegisterPrefs(PrefRegistrySimple * registry)31 void KeywordEditorController::RegisterPrefs(PrefRegistrySimple* registry) {
32   registry->RegisterDictionaryPref(prefs::kKeywordEditorWindowPlacement);
33 }
34 
AddTemplateURL(const base::string16 & title,const base::string16 & keyword,const std::string & url)35 int KeywordEditorController::AddTemplateURL(const base::string16& title,
36                                             const base::string16& keyword,
37                                             const std::string& url) {
38   DCHECK(!url.empty());
39 
40   content::RecordAction(UserMetricsAction("KeywordEditor_AddKeyword"));
41 
42   const int new_index = table_model_->last_other_engine_index();
43   table_model_->Add(new_index, title, keyword, url);
44 
45   return new_index;
46 }
47 
ModifyTemplateURL(TemplateURL * template_url,const base::string16 & title,const base::string16 & keyword,const std::string & url)48 void KeywordEditorController::ModifyTemplateURL(TemplateURL* template_url,
49                                                 const base::string16& title,
50                                                 const base::string16& keyword,
51                                                 const std::string& url) {
52   DCHECK(!url.empty());
53   const int index = table_model_->IndexOfTemplateURL(template_url);
54   if (index == -1) {
55     // Will happen if url was deleted out from under us while the user was
56     // editing it.
57     return;
58   }
59 
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))
63     return;
64 
65   table_model_->ModifyTemplateURL(index, title, keyword, url);
66 
67   content::RecordAction(UserMetricsAction("KeywordEditor_ModifiedKeyword"));
68 }
69 
CanEdit(const TemplateURL * url) const70 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()));
74 }
75 
CanMakeDefault(const TemplateURL * url) const76 bool KeywordEditorController::CanMakeDefault(const TemplateURL* url) const {
77   return url_model()->CanMakeDefault(url);
78 }
79 
CanRemove(const TemplateURL * url) const80 bool KeywordEditorController::CanRemove(const TemplateURL* url) const {
81   return url != url_model()->GetDefaultSearchProvider();
82 }
83 
RemoveTemplateURL(int index)84 void KeywordEditorController::RemoveTemplateURL(int index) {
85   table_model_->Remove(index);
86   content::RecordAction(UserMetricsAction("KeywordEditor_RemoveKeyword"));
87 }
88 
MakeDefaultTemplateURL(int index)89 int KeywordEditorController::MakeDefaultTemplateURL(int index) {
90   return table_model_->MakeDefaultTemplateURL(index);
91 }
92 
loaded() const93 bool KeywordEditorController::loaded() const {
94   return url_model()->loaded();
95 }
96 
GetTemplateURL(int index)97 TemplateURL* KeywordEditorController::GetTemplateURL(int index) {
98   return table_model_->GetTemplateURL(index);
99 }
100 
url_model() const101 TemplateURLService* KeywordEditorController::url_model() const {
102   return table_model_->template_url_service();
103 }
104