NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / search_engines / edit_search_engine_controller.cc
blobe608484b7afb337b3cf3bf71ed381a10d9102bdd
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.
5 #include "chrome/browser/ui/search_engines/edit_search_engine_controller.h"
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"
17 using base::UserMetricsAction;
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_);
29 bool EditSearchEngineController::IsTitleValid(
30 const base::string16& title_input) const {
31 return !CollapseWhitespace(title_input, true).empty();
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;
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;
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;
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();
64 bool EditSearchEngineController::IsKeywordValid(
65 const base::string16& keyword_input) const {
66 base::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_);
75 void EditSearchEngineController::AcceptAddOrEdit(
76 const base::string16& title_input,
77 const base::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());
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;
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);
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;
122 std::string EditSearchEngineController::GetFixedUpURL(
123 const std::string& url_input) const {
124 std::string url;
125 TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(
126 base::UTF8ToUTF16(url_input)),
127 TRIM_ALL, &url);
128 if (url.empty())
129 return url;
131 // Parse the string as a URL to determine the scheme. If we need to, add the
132 // scheme. As the scheme may be expanded (as happens with {google:baseURL})
133 // we need to replace the search terms before testing for the scheme.
134 TemplateURLData data;
135 data.SetURL(url);
136 TemplateURL t_url(profile_, data);
137 std::string expanded_url(t_url.url_ref().ReplaceSearchTerms(
138 TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x"))));
139 url_parse::Parsed parts;
140 std::string scheme(URLFixerUpper::SegmentURL(expanded_url, &parts));
141 if (!parts.scheme.is_valid())
142 url.insert(0, scheme + "://");
144 return url;