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.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/common/net/url_fixer_upper.h"
     14 #include "content/public/browser/user_metrics.h"
     15 #include "url/gurl.h"
     16 
     17 using content::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 string16& title_input) const {
     31   return !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(profile_, data);
     47   const TemplateURLRef& template_ref = t_url.url_ref();
     48   if (!template_ref.IsValid())
     49     return false;
     50 
     51   // If this is going to be the default search engine, it must support
     52   // replacement.
     53   if (!template_ref.SupportsReplacement() &&
     54       (template_url_ == TemplateURLServiceFactory::GetForProfile(profile_)->
     55           GetDefaultSearchProvider()))
     56     return false;
     57 
     58   // Replace any search term with a placeholder string and make sure the
     59   // resulting URL is valid.
     60   return GURL(template_ref.ReplaceSearchTerms(
     61       TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")))).is_valid();
     62 }
     63 
     64 bool EditSearchEngineController::IsKeywordValid(
     65     const string16& keyword_input) const {
     66   string16 keyword_input_trimmed(CollapseWhitespace(keyword_input, true));
     67   if (keyword_input_trimmed.empty())
     68     return false;  // Do not allow empty keyword.
     69   const TemplateURL* turl_with_keyword =
     70       TemplateURLServiceFactory::GetForProfile(profile_)->
     71       GetTemplateURLForKeyword(keyword_input_trimmed);
     72   return (turl_with_keyword == NULL || turl_with_keyword == template_url_);
     73 }
     74 
     75 void EditSearchEngineController::AcceptAddOrEdit(
     76     const string16& title_input,
     77     const string16& keyword_input,
     78     const std::string& url_input) {
     79   DCHECK(!keyword_input.empty());
     80   std::string url_string = GetFixedUpURL(url_input);
     81   DCHECK(!url_string.empty());
     82 
     83   TemplateURLService* template_url_service =
     84       TemplateURLServiceFactory::GetForProfile(profile_);
     85   TemplateURL* existing =
     86       template_url_service->GetTemplateURLForKeyword(keyword_input);
     87   if (existing && (!edit_keyword_delegate_ || existing != template_url_)) {
     88     // An entry may have been added with the same keyword string while the
     89     // user edited the dialog, either automatically or by the user (if we're
     90     // confirming a JS addition, they could have the Options dialog open at the
     91     // same time). If so, just ignore this add.
     92     // TODO(pamg): Really, we should modify the entry so this later one
     93     // overwrites it. But we don't expect this case to be common.
     94     CleanUpCancelledAdd();
     95     return;
     96   }
     97 
     98   if (!edit_keyword_delegate_) {
     99     // Confiming an entry we got from JS. We have a template_url_, but it
    100     // hasn't yet been added to the model.
    101     DCHECK(template_url_);
    102     // TemplateURLService takes ownership of template_url_.
    103     template_url_service->AddWithOverrides(template_url_, title_input,
    104                                            keyword_input, url_string);
    105     content::RecordAction(UserMetricsAction("KeywordEditor_AddKeywordJS"));
    106   } else {
    107     // Adding or modifying an entry via the Delegate.
    108     edit_keyword_delegate_->OnEditedKeyword(template_url_, title_input,
    109                                             keyword_input, url_string);
    110   }
    111 }
    112 
    113 void EditSearchEngineController::CleanUpCancelledAdd() {
    114   if (!edit_keyword_delegate_ && template_url_) {
    115     // When we have no Delegate, we know that the template_url_ hasn't yet been
    116     // added to the model, so we need to clean it up.
    117     delete template_url_;
    118     template_url_ = NULL;
    119   }
    120 }
    121 
    122 std::string EditSearchEngineController::GetFixedUpURL(
    123     const std::string& url_input) const {
    124   std::string url;
    125   TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(UTF8ToUTF16(url_input)),
    126                  TRIM_ALL, &url);
    127   if (url.empty())
    128     return url;
    129 
    130   // Parse the string as a URL to determine the scheme. If we need to, add the
    131   // scheme. As the scheme may be expanded (as happens with {google:baseURL})
    132   // we need to replace the search terms before testing for the scheme.
    133   TemplateURLData data;
    134   data.SetURL(url);
    135   TemplateURL t_url(profile_, data);
    136   std::string expanded_url(t_url.url_ref().ReplaceSearchTerms(
    137       TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x"))));
    138   url_parse::Parsed parts;
    139   std::string scheme(URLFixerUpper::SegmentURL(expanded_url, &parts));
    140   if (!parts.scheme.is_valid())
    141     url.insert(0, scheme + "://");
    142 
    143   return url;
    144 }
    145