Commit d29d39c3 authored by avi's avatar avi Committed by Commit bot

Remove ScopedVector from importer.

BUG=554289

Review-Url: https://codereview.chromium.org/2305813002
Cr-Commit-Position: refs/heads/master@{#416326}
parent 10f51929
......@@ -218,20 +218,19 @@ class FirefoxObserver : public ProfileWriter,
}
}
void AddKeywords(ScopedVector<TemplateURL> template_urls,
void AddKeywords(TemplateURLService::OwnedTemplateURLVector template_urls,
bool unique_on_host_and_path) override {
for (size_t i = 0; i < template_urls.size(); ++i) {
for (const auto& turl : template_urls) {
// The order might not be deterministic, look in the expected list for
// that template URL.
bool found = false;
const base::string16& imported_keyword = template_urls[i]->keyword();
for (size_t j = 0; j < arraysize(kFirefoxKeywords); ++j) {
const base::string16 expected_keyword = base::WideToUTF16(
use_keyword_in_json_ ?
kFirefoxKeywords[j].keyword_in_json :
kFirefoxKeywords[j].keyword_in_sqlite);
const base::string16& imported_keyword = turl->keyword();
for (const auto& keyword : kFirefoxKeywords) {
const base::string16 expected_keyword =
base::WideToUTF16(use_keyword_in_json_ ? keyword.keyword_in_json
: keyword.keyword_in_sqlite);
if (imported_keyword == expected_keyword) {
EXPECT_EQ(kFirefoxKeywords[j].url, template_urls[i]->url());
EXPECT_EQ(keyword.url, turl->url());
found = true;
break;
}
......
......@@ -402,7 +402,7 @@ class MalformedFavoritesRegistryTestObserver
void AddPasswordForm(const autofill::PasswordForm& form) override {}
void AddHistoryPage(const history::URLRows& page,
history::VisitSource visit_source) override {}
void AddKeywords(ScopedVector<TemplateURL> template_urls,
void AddKeywords(TemplateURLService::OwnedTemplateURLVector template_urls,
bool unique_on_host_and_path) override {}
void AddBookmarks(const std::vector<ImportedBookmarkEntry>& bookmarks,
const base::string16& top_level_folder_name) override {
......
......@@ -9,6 +9,7 @@
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
......@@ -96,53 +97,51 @@ class FirefoxURLParameterFilter : public TemplateURLParser::ParameterFilter {
};
// Attempts to create a TemplateURL from the provided data. |title| is optional.
// If TemplateURL creation fails, returns NULL.
// This function transfers ownership of the created TemplateURL to the caller.
TemplateURL* CreateTemplateURL(const base::string16& url,
const base::string16& keyword,
const base::string16& title) {
// If TemplateURL creation fails, returns null.
std::unique_ptr<TemplateURL> CreateTemplateURL(const base::string16& url,
const base::string16& keyword,
const base::string16& title) {
if (url.empty() || keyword.empty())
return NULL;
return nullptr;
TemplateURLData data;
data.SetKeyword(keyword);
// We set short name by using the title if it exists.
// Otherwise, we use the shortcut.
data.SetShortName(title.empty() ? keyword : title);
data.SetURL(TemplateURLRef::DisplayURLToURLRef(url));
return new TemplateURL(data);
return base::MakeUnique<TemplateURL>(data);
}
// Parses the OpenSearch XML files in |xml_files| and populates |search_engines|
// with the resulting TemplateURLs.
void ParseSearchEnginesFromFirefoxXMLData(
const std::vector<std::string>& xml_data,
std::vector<TemplateURL*>* search_engines) {
TemplateURLService::OwnedTemplateURLVector* search_engines) {
DCHECK(search_engines);
typedef std::map<std::string, TemplateURL*> SearchEnginesMap;
SearchEnginesMap search_engine_for_url;
std::map<std::string, std::unique_ptr<TemplateURL>> search_engine_for_url;
FirefoxURLParameterFilter param_filter;
// The first XML file represents the default search engine in Firefox 3, so we
// need to keep it on top of the list.
SearchEnginesMap::const_iterator default_turl = search_engine_for_url.end();
auto default_turl = search_engine_for_url.end();
for (std::vector<std::string>::const_iterator xml_iter =
xml_data.begin(); xml_iter != xml_data.end(); ++xml_iter) {
TemplateURL* template_url = TemplateURLParser::Parse(
UIThreadSearchTermsData(NULL), true,
xml_iter->data(), xml_iter->length(), &param_filter);
std::unique_ptr<TemplateURL> template_url = TemplateURLParser::Parse(
UIThreadSearchTermsData(nullptr), true, xml_iter->data(),
xml_iter->length(), &param_filter);
if (template_url) {
SearchEnginesMap::iterator iter =
search_engine_for_url.find(template_url->url());
auto iter = search_engine_for_url.find(template_url->url());
if (iter == search_engine_for_url.end()) {
iter = search_engine_for_url.insert(
std::make_pair(template_url->url(), template_url)).first;
iter = search_engine_for_url
.insert(std::make_pair(template_url->url(),
std::move(template_url)))
.first;
} else {
// We have already found a search engine with the same URL. We give
// priority to the latest one found, as GetSearchEnginesXMLFiles()
// returns a vector with first Firefox default search engines and then
// the user's ones. We want to give priority to the user ones.
delete iter->second;
iter->second = template_url;
iter->second = std::move(template_url);
}
if (default_turl == search_engine_for_url.end())
default_turl = iter;
......@@ -150,12 +149,13 @@ void ParseSearchEnginesFromFirefoxXMLData(
}
// Put the results in the |search_engines| vector.
for (SearchEnginesMap::iterator t_iter = search_engine_for_url.begin();
for (auto t_iter = search_engine_for_url.begin();
t_iter != search_engine_for_url.end(); ++t_iter) {
if (t_iter == default_turl)
search_engines->insert(search_engines->begin(), default_turl->second);
search_engines->insert(search_engines->begin(),
std::move(default_turl->second));
else
search_engines->push_back(t_iter->second);
search_engines->push_back(std::move(t_iter->second));
}
}
......@@ -222,14 +222,12 @@ void InProcessImporterBridge::SetHistoryItems(
void InProcessImporterBridge::SetKeywords(
const std::vector<importer::SearchEngineInfo>& search_engines,
bool unique_on_host_and_path) {
ScopedVector<TemplateURL> owned_template_urls;
TemplateURLService::OwnedTemplateURLVector owned_template_urls;
for (const auto& search_engine : search_engines) {
TemplateURL* owned_template_url =
CreateTemplateURL(search_engine.url,
search_engine.keyword,
search_engine.display_name);
std::unique_ptr<TemplateURL> owned_template_url = CreateTemplateURL(
search_engine.url, search_engine.keyword, search_engine.display_name);
if (owned_template_url)
owned_template_urls.push_back(owned_template_url);
owned_template_urls.push_back(std::move(owned_template_url));
}
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&ProfileWriter::AddKeywords, writer_,
......@@ -238,16 +236,12 @@ void InProcessImporterBridge::SetKeywords(
void InProcessImporterBridge::SetFirefoxSearchEnginesXMLData(
const std::vector<std::string>& search_engine_data) {
std::vector<TemplateURL*> search_engines;
TemplateURLService::OwnedTemplateURLVector search_engines;
ParseSearchEnginesFromFirefoxXMLData(search_engine_data, &search_engines);
ScopedVector<TemplateURL> owned_template_urls;
std::copy(search_engines.begin(), search_engines.end(),
std::back_inserter(owned_template_urls));
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&ProfileWriter::AddKeywords, writer_,
base::Passed(&owned_template_urls), true));
base::Bind(&ProfileWriter::AddKeywords, writer_,
base::Passed(&search_engines), true));
}
void InProcessImporterBridge::SetPasswordForm(
......
......@@ -315,19 +315,19 @@ static void BuildHostPathMap(TemplateURLService* model,
}
}
void ProfileWriter::AddKeywords(ScopedVector<TemplateURL> template_urls,
bool unique_on_host_and_path) {
void ProfileWriter::AddKeywords(
TemplateURLService::OwnedTemplateURLVector template_urls,
bool unique_on_host_and_path) {
TemplateURLService* model =
TemplateURLServiceFactory::GetForProfile(profile_);
HostPathMap host_path_map;
if (unique_on_host_and_path)
BuildHostPathMap(model, &host_path_map);
for (ScopedVector<TemplateURL>::iterator i = template_urls.begin();
i != template_urls.end(); ++i) {
for (auto& turl : template_urls) {
// TemplateURLService requires keywords to be unique. If there is already a
// TemplateURL with this keyword, don't import it again.
if (model->GetTemplateURLForKeyword((*i)->keyword()) != nullptr)
if (model->GetTemplateURLForKeyword(turl->keyword()) != nullptr)
continue;
// For search engines if there is already a keyword with the same
......@@ -335,16 +335,14 @@ void ProfileWriter::AddKeywords(ScopedVector<TemplateURL> template_urls,
// search providers (such as two Googles, or two Yahoos) as well as making
// sure the search engines we provide aren't replaced by those from the
// imported browser.
if (unique_on_host_and_path &&
(host_path_map.find(BuildHostPathKey(
*i, model->search_terms_data(), true)) != host_path_map.end()))
if (unique_on_host_and_path && (host_path_map.find(BuildHostPathKey(
turl.get(), model->search_terms_data(),
true)) != host_path_map.end()))
continue;
// Only add valid TemplateURLs to the model.
if ((*i)->url_ref().IsValid(model->search_terms_data())) {
model->Add(base::WrapUnique(*i));
*i = nullptr; // Prevent the vector from deleting *i later.
}
if (turl->url_ref().IsValid(model->search_terms_data()))
model->Add(std::move(turl));
}
}
......
......@@ -9,17 +9,16 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "base/strings/string16.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/favicon_base/favicon_usage_data.h"
#include "components/history/core/browser/history_types.h"
#include "components/search_engines/template_url_service.h"
#include "url/gurl.h"
struct ImportedBookmarkEntry;
class Profile;
class TemplateURL;
namespace autofill {
struct PasswordForm;
......@@ -78,16 +77,16 @@ class ProfileWriter : public base::RefCountedThreadSafe<ProfileWriter> {
virtual void AddFavicons(const favicon_base::FaviconUsageDataList& favicons);
// Adds the TemplateURLs in |template_urls| to the local store. The local
// store becomes the owner of the TemplateURLs. Some TemplateURLs in
// |template_urls| may conflict (same keyword or same host name in the URL)
// with existing TemplateURLs in the local store, in which case the existing
// ones take precedence and the duplicates in |template_urls| are deleted.
// If |unique_on_host_and_path| is true, a TemplateURL is only added if there
// is not an existing TemplateURL that has a replaceable search url with the
// same host+path combination.
virtual void AddKeywords(ScopedVector<TemplateURL> template_urls,
bool unique_on_host_and_path);
// Adds the TemplateURLs in |template_urls| to the local store.
// Some TemplateURLs in |template_urls| may conflict (same keyword or same
// host name in the URL) with existing TemplateURLs in the local store, in
// which case the existing ones take precedence and the duplicates in
// |template_urls| are deleted. If |unique_on_host_and_path| is true, a
// TemplateURL is only added if there is not an existing TemplateURL that has
// a replaceable search url with the same host+path combination.
virtual void AddKeywords(
TemplateURLService::OwnedTemplateURLVector template_urls,
bool unique_on_host_and_path);
// Adds the imported autofill entries to the autofill database.
virtual void AddAutofillFormDataEntries(
......
......@@ -101,8 +101,8 @@ void TemplateURLParserTest::ParseFile(
std::string contents;
ASSERT_TRUE(base::ReadFileToString(full_path, &contents));
template_url_.reset(TemplateURLParser::Parse(
SearchTermsData(), false, contents.data(), contents.length(), filter));
template_url_ = TemplateURLParser::Parse(
SearchTermsData(), false, contents.data(), contents.length(), filter);
}
......
......@@ -111,9 +111,9 @@ void TemplateURLFetcher::RequestDelegate::OnURLFetchComplete(
return;
}
template_url_.reset(TemplateURLParser::Parse(
fetcher_->template_url_service_->search_terms_data(), false,
data.data(), data.length(), NULL));
template_url_ = TemplateURLParser::Parse(
fetcher_->template_url_service_->search_terms_data(), false, data.data(),
data.length(), nullptr);
if (!template_url_.get() ||
!template_url_->url_ref().SupportsReplacement(
fetcher_->template_url_service_->search_terms_data())) {
......
......@@ -13,6 +13,7 @@
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
......@@ -140,12 +141,13 @@ class TemplateURLParsingContext {
static void EndElementImpl(void* ctx, const xmlChar* name);
static void CharactersImpl(void* ctx, const xmlChar* ch, int len);
// Returns a heap-allocated TemplateURL representing the result of parsing.
// This will be NULL if parsing failed or if the results were invalid for some
// reason (e.g. the resulting URL was not HTTP[S], a name wasn't supplied,
// a resulting TemplateURLRef was invalid, etc.).
TemplateURL* GetTemplateURL(const SearchTermsData& search_terms_data,
bool show_in_default_list);
// Returns a TemplateURL representing the result of parsing. This will be
// null if parsing failed or if the results were invalid for some reason (e.g.
// the resulting URL was not HTTP[S], a name wasn't supplied, a resulting
// TemplateURLRef was invalid, etc.).
std::unique_ptr<TemplateURL> GetTemplateURL(
const SearchTermsData& search_terms_data,
bool show_in_default_list);
private:
// Key is UTF8 encoded.
......@@ -299,14 +301,14 @@ void TemplateURLParsingContext::CharactersImpl(void* ctx,
base::StringPiece(reinterpret_cast<const char*>(ch), len));
}
TemplateURL* TemplateURLParsingContext::GetTemplateURL(
std::unique_ptr<TemplateURL> TemplateURLParsingContext::GetTemplateURL(
const SearchTermsData& search_terms_data,
bool show_in_default_list) {
// TODO(jcampan): Support engines that use POST; see http://crbug.com/18107
if (method_ == TemplateURLParsingContext::POST ||
data_.short_name().empty() || !IsHTTPRef(data_.url()) ||
!IsHTTPRef(data_.suggestions_url))
return NULL;
return nullptr;
if (suggestion_method_ == TemplateURLParsingContext::POST)
data_.suggestions_url.clear();
......@@ -324,15 +326,16 @@ TemplateURL* TemplateURLParsingContext::GetTemplateURL(
data_.show_in_default_list = show_in_default_list;
// Bail if the search URL is empty or if either TemplateURLRef is invalid.
std::unique_ptr<TemplateURL> template_url(new TemplateURL(data_));
std::unique_ptr<TemplateURL> template_url =
base::MakeUnique<TemplateURL>(data_);
if (template_url->url().empty() ||
!template_url->url_ref().IsValid(search_terms_data) ||
(!template_url->suggestions_url().empty() &&
!template_url->suggestions_url_ref().IsValid(search_terms_data))) {
return NULL;
return nullptr;
}
return template_url.release();
return template_url;
}
// static
......@@ -490,7 +493,7 @@ TemplateURLParsingContext::ElementType
// TemplateURLParser ----------------------------------------------------------
// static
TemplateURL* TemplateURLParser::Parse(
std::unique_ptr<TemplateURL> TemplateURLParser::Parse(
const SearchTermsData& search_terms_data,
bool show_in_default_list,
const char* data,
......@@ -511,6 +514,6 @@ TemplateURL* TemplateURLParser::Parse(
static_cast<int>(length));
xmlSubstituteEntitiesDefault(last_sub_entities_value);
return error ?
NULL : context.GetTemplateURL(search_terms_data, show_in_default_list);
return error ? nullptr : context.GetTemplateURL(search_terms_data,
show_in_default_list);
}
......@@ -7,6 +7,7 @@
#include <stddef.h>
#include <memory>
#include <string>
#include "base/macros.h"
......@@ -30,19 +31,19 @@ class TemplateURLParser {
};
// Decodes the chunk of data representing a TemplateURL, creates the
// TemplateURL, and returns it. The caller owns the returned object.
// Returns NULL if data does not describe a valid TemplateURL, the
// URLs referenced do not point to valid http/https resources, or for some
// other reason we do not support the described TemplateURL.
// |parameter_filter| can be used if you want to filter some parameters out of
// the URL. For example, when importing from another browser, we remove any
// parameter identifying that browser. If set to NULL, the URL is not
// modified.
static TemplateURL* Parse(const SearchTermsData& search_terms_data,
bool show_in_default_list,
const char* data,
size_t length,
ParameterFilter* parameter_filter);
// TemplateURL, and returns it. Returns null if the data does not describe a
// valid TemplateURL, the URLs referenced do not point to valid http/https
// resources, or for some other reason we do not support the described
// TemplateURL. |parameter_filter| can be used if you want to filter some
// parameters out of the URL. For example, when importing from another
// browser, we remove any parameter identifying that browser. If set to null,
// the URL is not modified.
static std::unique_ptr<TemplateURL> Parse(
const SearchTermsData& search_terms_data,
bool show_in_default_list,
const char* data,
size_t length,
ParameterFilter* parameter_filter);
private:
// No one should create one of these.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment