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/edit_search_engine_controller.h"
      6 
      7 #include "base/strings/string_util.h"
      8 #include "base/strings/utf_string_conversions.h"
      9 #include "chrome/browser/profiles/profile.h"
     10 #include "chrome/browser/search_engines/template_url_service_factory.h"
     11 #include "components/search_engines/template_url.h"
     12 #include "components/search_engines/template_url_service.h"
     13 #include "components/url_fixer/url_fixer.h"
     14 #include "content/public/browser/user_metrics.h"
     15 #include "url/gurl.h"
     16 
     17 using base::UserMetricsAction;
     18 
     19 EditSearchEngineController::EditSearchEngineController(
     20     TemplateURL* template_url,
     21     EditSearchEngineControllerDelegate* edit_keyword_delegate,
     22     Profile* profile)
     23     : template_url_(template_url),
     24       edit_keyword_delegate_(edit_keyword_delegate),
     25       profile_(profile) {
     26   DCHECK(profile_);
     27 }
     28 
     29 bool EditSearchEngineController::IsTitleValid(
     30     const base::string16& title_input) const {
     31   return !base::CollapseWhitespace(title_input, true).empty();
     32 }
     33 
     34 bool EditSearchEngineController::IsURLValid(
     35     const std::string& url_input) const {
     36   std::string url = GetFixedUpURL(url_input);
     37   if (url.empty())
     38     return false;
     39 
     40   // Convert |url| to a TemplateURLRef so we can check its validity even if it
     41   // contains replacement strings.  We do this by constructing a dummy
     42   // TemplateURL owner because |template_url_| might be NULL and we can't call
     43   // TemplateURLRef::IsValid() when its owner is NULL.
     44   TemplateURLData data;
     45   data.SetURL(url);
     46   TemplateURL t_url(data);
     47   const TemplateURLRef& template_ref = t_url.url_ref();
     48   TemplateURLService* service =
     49       TemplateURLServiceFactory::GetForProfile(profile_);
     50   if (!template_ref.IsValid(service->search_terms_data()))
     51     return false;
     52 
     53   // If this is going to be the default search engine, it must support
     54   // replacement.
     55   if (!template_ref.SupportsReplacement(service->search_terms_data()) &&
     56       template_url_ &&
     57       template_url_ == service->GetDefaultSearchProvider())
     58     return false;
     59 
     60   // Replace any search term with a placeholder string and make sure the
     61   // resulting URL is valid.
     62   return GURL(template_ref.ReplaceSearchTerms(
     63       TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x")),
     64       service->search_terms_data())).is_valid();
     65 }
     66 
     67 bool EditSearchEngineController::IsKeywordValid(
     68     const base::string16& keyword_input) const {
     69   base::string16 keyword_input_trimmed(
     70       base::CollapseWhitespace(keyword_input, true));
     71   if (keyword_input_trimmed.empty())
     72     return false;  // Do not allow empty keyword.
     73   const TemplateURL* turl_with_keyword =
     74       TemplateURLServiceFactory::GetForProfile(profile_)->
     75       GetTemplateURLForKeyword(keyword_input_trimmed);
     76   return (turl_with_keyword == NULL || turl_with_keyword == template_url_);
     77 }
     78 
     79 void EditSearchEngineController::AcceptAddOrEdit(
     80     const base::string16& title_input,
     81     const base::string16& keyword_input,
     82     const std::string& url_input) {
     83   DCHECK(!keyword_input.empty());
     84   std::string url_string = GetFixedUpURL(url_input);
     85   DCHECK(!url_string.empty());
     86 
     87   TemplateURLService* template_url_service =
     88       TemplateURLServiceFactory::GetForProfile(profile_);
     89   TemplateURL* existing =
     90       template_url_service->GetTemplateURLForKeyword(keyword_input);
     91   if (existing && (!edit_keyword_delegate_ || existing != template_url_)) {
     92     // An entry may have been added with the same keyword string while the
     93     // user edited the dialog, either automatically or by the user (if we're
     94     // confirming a JS addition, they could have the Options dialog open at the
     95     // same time). If so, just ignore this add.
     96     // TODO(pamg): Really, we should modify the entry so this later one
     97     // overwrites it. But we don't expect this case to be common.
     98     CleanUpCancelledAdd();
     99     return;
    100   }
    101 
    102   if (!edit_keyword_delegate_) {
    103     // Confiming an entry we got from JS. We have a template_url_, but it
    104     // hasn't yet been added to the model.
    105     DCHECK(template_url_);
    106     // TemplateURLService takes ownership of template_url_.
    107     template_url_service->AddWithOverrides(template_url_, title_input,
    108                                            keyword_input, url_string);
    109     content::RecordAction(UserMetricsAction("KeywordEditor_AddKeywordJS"));
    110   } else {
    111     // Adding or modifying an entry via the Delegate.
    112     edit_keyword_delegate_->OnEditedKeyword(template_url_, title_input,
    113                                             keyword_input, url_string);
    114   }
    115 }
    116 
    117 void EditSearchEngineController::CleanUpCancelledAdd() {
    118   if (!edit_keyword_delegate_ && template_url_) {
    119     // When we have no Delegate, we know that the template_url_ hasn't yet been
    120     // added to the model, so we need to clean it up.
    121     delete template_url_;
    122     template_url_ = NULL;
    123   }
    124 }
    125 
    126 std::string EditSearchEngineController::GetFixedUpURL(
    127     const std::string& url_input) const {
    128   std::string url;
    129   base::TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(
    130                            base::UTF8ToUTF16(url_input)),
    131                        base::TRIM_ALL, &url);
    132   if (url.empty())
    133     return url;
    134 
    135   // Parse the string as a URL to determine the scheme. If we need to, add the
    136   // scheme. As the scheme may be expanded (as happens with {google:baseURL})
    137   // we need to replace the search terms before testing for the scheme.
    138   TemplateURLData data;
    139   data.SetURL(url);
    140   TemplateURL t_url(data);
    141   std::string expanded_url(t_url.url_ref().ReplaceSearchTerms(
    142       TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x")),
    143       TemplateURLServiceFactory::GetForProfile(profile_)->search_terms_data()));
    144   url::Parsed parts;
    145   std::string scheme(url_fixer::SegmentURL(expanded_url, &parts));
    146   if (!parts.scheme.is_valid())
    147     url.insert(0, scheme + "://");
    148 
    149   return url;
    150 }
    151