This source file includes following definitions.
- profile_
- IsTitleValid
- IsURLValid
- IsKeywordValid
- AcceptAddOrEdit
- CleanUpCancelledAdd
- GetFixedUpURL
#include "chrome/browser/ui/search_engines/edit_search_engine_controller.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/common/net/url_fixer_upper.h"
#include "content/public/browser/user_metrics.h"
#include "url/gurl.h"
using base::UserMetricsAction;
EditSearchEngineController::EditSearchEngineController(
TemplateURL* template_url,
EditSearchEngineControllerDelegate* edit_keyword_delegate,
Profile* profile)
: template_url_(template_url),
edit_keyword_delegate_(edit_keyword_delegate),
profile_(profile) {
DCHECK(profile_);
}
bool EditSearchEngineController::IsTitleValid(
const base::string16& title_input) const {
return !base::CollapseWhitespace(title_input, true).empty();
}
bool EditSearchEngineController::IsURLValid(
const std::string& url_input) const {
std::string url = GetFixedUpURL(url_input);
if (url.empty())
return false;
TemplateURLData data;
data.SetURL(url);
TemplateURL t_url(profile_, data);
const TemplateURLRef& template_ref = t_url.url_ref();
if (!template_ref.IsValid())
return false;
if (!template_ref.SupportsReplacement() &&
(template_url_ == TemplateURLServiceFactory::GetForProfile(profile_)->
GetDefaultSearchProvider()))
return false;
return GURL(template_ref.ReplaceSearchTerms(
TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x")))).is_valid();
}
bool EditSearchEngineController::IsKeywordValid(
const base::string16& keyword_input) const {
base::string16 keyword_input_trimmed(
base::CollapseWhitespace(keyword_input, true));
if (keyword_input_trimmed.empty())
return false;
const TemplateURL* turl_with_keyword =
TemplateURLServiceFactory::GetForProfile(profile_)->
GetTemplateURLForKeyword(keyword_input_trimmed);
return (turl_with_keyword == NULL || turl_with_keyword == template_url_);
}
void EditSearchEngineController::AcceptAddOrEdit(
const base::string16& title_input,
const base::string16& keyword_input,
const std::string& url_input) {
DCHECK(!keyword_input.empty());
std::string url_string = GetFixedUpURL(url_input);
DCHECK(!url_string.empty());
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile_);
TemplateURL* existing =
template_url_service->GetTemplateURLForKeyword(keyword_input);
if (existing && (!edit_keyword_delegate_ || existing != template_url_)) {
CleanUpCancelledAdd();
return;
}
if (!edit_keyword_delegate_) {
DCHECK(template_url_);
template_url_service->AddWithOverrides(template_url_, title_input,
keyword_input, url_string);
content::RecordAction(UserMetricsAction("KeywordEditor_AddKeywordJS"));
} else {
edit_keyword_delegate_->OnEditedKeyword(template_url_, title_input,
keyword_input, url_string);
}
}
void EditSearchEngineController::CleanUpCancelledAdd() {
if (!edit_keyword_delegate_ && template_url_) {
delete template_url_;
template_url_ = NULL;
}
}
std::string EditSearchEngineController::GetFixedUpURL(
const std::string& url_input) const {
std::string url;
base::TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(
base::UTF8ToUTF16(url_input)),
base::TRIM_ALL, &url);
if (url.empty())
return url;
TemplateURLData data;
data.SetURL(url);
TemplateURL t_url(profile_, data);
std::string expanded_url(t_url.url_ref().ReplaceSearchTerms(
TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x"))));
url_parse::Parsed parts;
std::string scheme(URLFixerUpper::SegmentURL(expanded_url, &parts));
if (!parts.scheme.is_valid())
url.insert(0, scheme + "://");
return url;
}