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 "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 "chrome/common/pref_names.h" 12#include "components/search_engines/template_url.h" 13#include "components/search_engines/template_url_service.h" 14#include "content/public/browser/user_metrics.h" 15 16using base::UserMetricsAction; 17 18KeywordEditorController::KeywordEditorController(Profile* profile) 19 : url_model_(TemplateURLServiceFactory::GetForProfile(profile)) { 20 table_model_.reset(new TemplateURLTableModel( 21 url_model_, 22 FaviconServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS))); 23} 24 25KeywordEditorController::~KeywordEditorController() { 26} 27 28int KeywordEditorController::AddTemplateURL(const base::string16& title, 29 const base::string16& keyword, 30 const std::string& url) { 31 DCHECK(!url.empty()); 32 33 content::RecordAction(UserMetricsAction("KeywordEditor_AddKeyword")); 34 35 const int new_index = table_model_->last_other_engine_index(); 36 table_model_->Add(new_index, title, keyword, url); 37 38 return new_index; 39} 40 41void KeywordEditorController::ModifyTemplateURL(TemplateURL* template_url, 42 const base::string16& title, 43 const base::string16& keyword, 44 const std::string& url) { 45 DCHECK(!url.empty()); 46 const int index = table_model_->IndexOfTemplateURL(template_url); 47 if (index == -1) { 48 // Will happen if url was deleted out from under us while the user was 49 // editing it. 50 return; 51 } 52 53 // Don't do anything if the entry didn't change. 54 if ((template_url->short_name() == title) && 55 (template_url->keyword() == keyword) && (template_url->url() == url)) 56 return; 57 58 table_model_->ModifyTemplateURL(index, title, keyword, url); 59 60 content::RecordAction(UserMetricsAction("KeywordEditor_ModifiedKeyword")); 61} 62 63bool KeywordEditorController::CanEdit(const TemplateURL* url) const { 64 return (url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && 65 (!url_model_->is_default_search_managed() || 66 (url != url_model_->GetDefaultSearchProvider())); 67} 68 69bool KeywordEditorController::CanMakeDefault(const TemplateURL* url) const { 70 return url_model_->CanMakeDefault(url); 71} 72 73bool KeywordEditorController::CanRemove(const TemplateURL* url) const { 74 return url != url_model_->GetDefaultSearchProvider(); 75} 76 77void KeywordEditorController::RemoveTemplateURL(int index) { 78 table_model_->Remove(index); 79 content::RecordAction(UserMetricsAction("KeywordEditor_RemoveKeyword")); 80} 81 82TemplateURL* KeywordEditorController::GetDefaultSearchProvider() { 83 return url_model_->GetDefaultSearchProvider(); 84} 85 86int KeywordEditorController::MakeDefaultTemplateURL(int index) { 87 return table_model_->MakeDefaultTemplateURL(index); 88} 89 90bool KeywordEditorController::loaded() const { 91 return url_model_->loaded(); 92} 93 94TemplateURL* KeywordEditorController::GetTemplateURL(int index) { 95 return table_model_->GetTemplateURL(index); 96} 97