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