15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/search_engines/keyword_editor_controller.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_registry_simple.h" 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/search_engines/template_url.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/search_engines/template_url_service.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/search_engines/template_url_service_factory.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/search_engines/template_url_table_model.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/user_metrics.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::UserMetricsAction; 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)KeywordEditorController::KeywordEditorController(Profile* profile) 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : profile_(profile) { 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) table_model_.reset(new TemplateURLTableModel( 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TemplateURLServiceFactory::GetForProfile(profile))); 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)KeywordEditorController::~KeywordEditorController() { 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(rsesek): Other platforms besides Mac should remember window 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// placement. http://crbug.com/22269 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void KeywordEditorController::RegisterPrefs(PrefRegistrySimple* registry) { 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) registry->RegisterDictionaryPref(prefs::kKeywordEditorWindowPlacement); 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)int KeywordEditorController::AddTemplateURL(const base::string16& title, 36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& keyword, 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& url) { 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(!url.empty()); 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::RecordAction(UserMetricsAction("KeywordEditor_AddKeyword")); 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const int new_index = table_model_->last_other_engine_index(); 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) table_model_->Add(new_index, title, keyword, url); 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return new_index; 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void KeywordEditorController::ModifyTemplateURL(TemplateURL* template_url, 49a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& title, 50a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& keyword, 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& url) { 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(!url.empty()); 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const int index = table_model_->IndexOfTemplateURL(template_url); 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (index == -1) { 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Will happen if url was deleted out from under us while the user was 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // editing it. 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Don't do anything if the entry didn't change. 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if ((template_url->short_name() == title) && 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (template_url->keyword() == keyword) && (template_url->url() == url)) 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) table_model_->ModifyTemplateURL(index, title, keyword, url); 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::RecordAction(UserMetricsAction("KeywordEditor_ModifiedKeyword")); 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool KeywordEditorController::CanEdit(const TemplateURL* url) const { 71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return (url->GetType() != TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && 72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) (!url_model()->is_default_search_managed() || 73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) (url != url_model()->GetDefaultSearchProvider())); 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool KeywordEditorController::CanMakeDefault(const TemplateURL* url) const { 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return url_model()->CanMakeDefault(url); 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool KeywordEditorController::CanRemove(const TemplateURL* url) const { 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return url != url_model()->GetDefaultSearchProvider(); 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void KeywordEditorController::RemoveTemplateURL(int index) { 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) table_model_->Remove(index); 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::RecordAction(UserMetricsAction("KeywordEditor_RemoveKeyword")); 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int KeywordEditorController::MakeDefaultTemplateURL(int index) { 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return table_model_->MakeDefaultTemplateURL(index); 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool KeywordEditorController::loaded() const { 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return url_model()->loaded(); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TemplateURL* KeywordEditorController::GetTemplateURL(int index) { 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return table_model_->GetTemplateURL(index); 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TemplateURLService* KeywordEditorController::url_model() const { 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return table_model_->template_url_service(); 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 104