Home | History | Annotate | Download | only in search_engines
      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 content::UserMetricsAction;
     18 
     19 KeywordEditorController::KeywordEditorController(Profile* profile)
     20     : profile_(profile) {
     21   table_model_.reset(new TemplateURLTableModel(
     22       TemplateURLServiceFactory::GetForProfile(profile)));
     23 }
     24 
     25 KeywordEditorController::~KeywordEditorController() {
     26 }
     27 
     28 // static
     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);
     33 }
     34 
     35 int KeywordEditorController::AddTemplateURL(const string16& title,
     36                                             const 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 
     48 void KeywordEditorController::ModifyTemplateURL(TemplateURL* template_url,
     49                                                 const string16& title,
     50                                                 const 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 
     70 bool KeywordEditorController::CanEdit(const TemplateURL* url) const {
     71   return !url_model()->is_default_search_managed() ||
     72       url != url_model()->GetDefaultSearchProvider();
     73 }
     74 
     75 bool KeywordEditorController::CanMakeDefault(const TemplateURL* url) const {
     76   return url_model()->CanMakeDefault(url);
     77 }
     78 
     79 bool KeywordEditorController::CanRemove(const TemplateURL* url) const {
     80   return url != url_model()->GetDefaultSearchProvider();
     81 }
     82 
     83 void KeywordEditorController::RemoveTemplateURL(int index) {
     84   table_model_->Remove(index);
     85   content::RecordAction(UserMetricsAction("KeywordEditor_RemoveKeyword"));
     86 }
     87 
     88 int KeywordEditorController::MakeDefaultTemplateURL(int index) {
     89   return table_model_->MakeDefaultTemplateURL(index);
     90 }
     91 
     92 bool KeywordEditorController::loaded() const {
     93   return url_model()->loaded();
     94 }
     95 
     96 TemplateURL* KeywordEditorController::GetTemplateURL(int index) {
     97   return table_model_->GetTemplateURL(index);
     98 }
     99 
    100 TemplateURLService* KeywordEditorController::url_model() const {
    101   return table_model_->template_url_service();
    102 }
    103