Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / search_engines / edit_search_engine_controller.cc
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 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(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(base::ASCIIToUTF16("x")))).is_valid();
62 }
63
64 bool EditSearchEngineController::IsKeywordValid(
65     const base::string16& keyword_input) const {
66   base::string16 keyword_input_trimmed(
67       base::CollapseWhitespace(keyword_input, true));
68   if (keyword_input_trimmed.empty())
69     return false;  // Do not allow empty keyword.
70   const TemplateURL* turl_with_keyword =
71       TemplateURLServiceFactory::GetForProfile(profile_)->
72       GetTemplateURLForKeyword(keyword_input_trimmed);
73   return (turl_with_keyword == NULL || turl_with_keyword == template_url_);
74 }
75
76 void EditSearchEngineController::AcceptAddOrEdit(
77     const base::string16& title_input,
78     const base::string16& keyword_input,
79     const std::string& url_input) {
80   DCHECK(!keyword_input.empty());
81   std::string url_string = GetFixedUpURL(url_input);
82   DCHECK(!url_string.empty());
83
84   TemplateURLService* template_url_service =
85       TemplateURLServiceFactory::GetForProfile(profile_);
86   TemplateURL* existing =
87       template_url_service->GetTemplateURLForKeyword(keyword_input);
88   if (existing && (!edit_keyword_delegate_ || existing != template_url_)) {
89     // An entry may have been added with the same keyword string while the
90     // user edited the dialog, either automatically or by the user (if we're
91     // confirming a JS addition, they could have the Options dialog open at the
92     // same time). If so, just ignore this add.
93     // TODO(pamg): Really, we should modify the entry so this later one
94     // overwrites it. But we don't expect this case to be common.
95     CleanUpCancelledAdd();
96     return;
97   }
98
99   if (!edit_keyword_delegate_) {
100     // Confiming an entry we got from JS. We have a template_url_, but it
101     // hasn't yet been added to the model.
102     DCHECK(template_url_);
103     // TemplateURLService takes ownership of template_url_.
104     template_url_service->AddWithOverrides(template_url_, title_input,
105                                            keyword_input, url_string);
106     content::RecordAction(UserMetricsAction("KeywordEditor_AddKeywordJS"));
107   } else {
108     // Adding or modifying an entry via the Delegate.
109     edit_keyword_delegate_->OnEditedKeyword(template_url_, title_input,
110                                             keyword_input, url_string);
111   }
112 }
113
114 void EditSearchEngineController::CleanUpCancelledAdd() {
115   if (!edit_keyword_delegate_ && template_url_) {
116     // When we have no Delegate, we know that the template_url_ hasn't yet been
117     // added to the model, so we need to clean it up.
118     delete template_url_;
119     template_url_ = NULL;
120   }
121 }
122
123 std::string EditSearchEngineController::GetFixedUpURL(
124     const std::string& url_input) const {
125   std::string url;
126   base::TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(
127                            base::UTF8ToUTF16(url_input)),
128                        base::TRIM_ALL, &url);
129   if (url.empty())
130     return url;
131
132   // Parse the string as a URL to determine the scheme. If we need to, add the
133   // scheme. As the scheme may be expanded (as happens with {google:baseURL})
134   // we need to replace the search terms before testing for the scheme.
135   TemplateURLData data;
136   data.SetURL(url);
137   TemplateURL t_url(profile_, data);
138   std::string expanded_url(t_url.url_ref().ReplaceSearchTerms(
139       TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x"))));
140   url_parse::Parsed parts;
141   std::string scheme(URLFixerUpper::SegmentURL(expanded_url, &parts));
142   if (!parts.scheme.is_valid())
143     url.insert(0, scheme + "://");
144
145   return url;
146 }