This source file includes following definitions.
- GenerateUniqueFolderName
- ShowBookmarkBar
- BookmarkModelIsLoaded
- TemplateURLServiceIsLoaded
- AddPasswordForm
- AddIE7PasswordInfo
- AddHistoryPage
- AddHomepage
- AddBookmarks
- AddFavicons
- HostPathKeyForURL
- BuildHostPathKey
- BuildHostPathMap
- AddKeywords
#include "chrome/browser/importer/profile_writer.h"
#include <map>
#include <set>
#include <string>
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/favicon/favicon_service.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/password_manager/password_store_factory.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/browser/webdata/web_data_service.h"
#include "chrome/common/importer/imported_bookmark_entry.h"
#include "chrome/common/importer/imported_favicon_usage.h"
#include "chrome/common/pref_names.h"
#include "components/password_manager/core/browser/password_store.h"
namespace {
base::string16 GenerateUniqueFolderName(BookmarkModel* model,
const base::string16& folder_name) {
std::set<base::string16> existing_folder_names;
const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
for (int i = 0; i < bookmark_bar->child_count(); ++i) {
const BookmarkNode* node = bookmark_bar->GetChild(i);
if (node->is_folder())
existing_folder_names.insert(node->GetTitle());
}
if (existing_folder_names.find(folder_name) == existing_folder_names.end())
return folder_name;
for (size_t i = 1; i <= existing_folder_names.size(); ++i) {
base::string16 name = folder_name + base::ASCIIToUTF16(" (") +
base::IntToString16(i) + base::ASCIIToUTF16(")");
if (existing_folder_names.find(name) == existing_folder_names.end())
return name;
}
NOTREACHED();
return folder_name;
}
void ShowBookmarkBar(Profile* profile) {
profile->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true);
}
}
ProfileWriter::ProfileWriter(Profile* profile) : profile_(profile) {}
bool ProfileWriter::BookmarkModelIsLoaded() const {
return BookmarkModelFactory::GetForProfile(profile_)->loaded();
}
bool ProfileWriter::TemplateURLServiceIsLoaded() const {
return TemplateURLServiceFactory::GetForProfile(profile_)->loaded();
}
void ProfileWriter::AddPasswordForm(const autofill::PasswordForm& form) {
PasswordStoreFactory::GetForProfile(
profile_, Profile::EXPLICIT_ACCESS)->AddLogin(form);
}
#if defined(OS_WIN)
void ProfileWriter::AddIE7PasswordInfo(const IE7PasswordInfo& info) {
WebDataService::FromBrowserContext(profile_)->AddIE7Login(info);
}
#endif
void ProfileWriter::AddHistoryPage(const history::URLRows& page,
history::VisitSource visit_source) {
HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS)->
AddPagesWithDetails(page, visit_source);
}
void ProfileWriter::AddHomepage(const GURL& home_page) {
DCHECK(profile_);
PrefService* prefs = profile_->GetPrefs();
const PrefService::Preference* pref = prefs->FindPreference(prefs::kHomePage);
if (pref && !pref->IsManaged()) {
prefs->SetString(prefs::kHomePage, home_page.spec());
}
}
void ProfileWriter::AddBookmarks(
const std::vector<ImportedBookmarkEntry>& bookmarks,
const base::string16& top_level_folder_name) {
if (bookmarks.empty())
return;
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
DCHECK(model->loaded());
const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
bool import_to_top_level = bookmark_bar->empty();
std::vector<ImportedBookmarkEntry> toolbar_bookmarks;
std::vector<ImportedBookmarkEntry> reordered_bookmarks;
for (std::vector<ImportedBookmarkEntry>::const_iterator it =
bookmarks.begin();
it != bookmarks.end(); ++it) {
if (it->in_toolbar)
toolbar_bookmarks.push_back(*it);
else
reordered_bookmarks.push_back(*it);
}
reordered_bookmarks.insert(reordered_bookmarks.begin(),
toolbar_bookmarks.begin(),
toolbar_bookmarks.end());
bool add_all_to_top_level = import_to_top_level && toolbar_bookmarks.empty();
model->BeginExtensiveChanges();
std::set<const BookmarkNode*> folders_added_to;
const BookmarkNode* top_level_folder = NULL;
for (std::vector<ImportedBookmarkEntry>::const_iterator bookmark =
reordered_bookmarks.begin();
bookmark != reordered_bookmarks.end(); ++bookmark) {
if (!bookmark->is_folder && !bookmark->url.is_valid())
continue;
const BookmarkNode* parent = NULL;
if (import_to_top_level && (add_all_to_top_level || bookmark->in_toolbar)) {
parent = bookmark_bar;
} else {
if (!top_level_folder) {
base::string16 name =
GenerateUniqueFolderName(model,top_level_folder_name);
top_level_folder = model->AddFolder(bookmark_bar,
bookmark_bar->child_count(),
name);
}
parent = top_level_folder;
}
for (std::vector<base::string16>::const_iterator folder_name =
bookmark->path.begin();
folder_name != bookmark->path.end(); ++folder_name) {
if (bookmark->in_toolbar && parent == bookmark_bar &&
folder_name == bookmark->path.begin()) {
continue;
}
const BookmarkNode* child = NULL;
for (int index = 0; index < parent->child_count(); ++index) {
const BookmarkNode* node = parent->GetChild(index);
if (node->is_folder() && node->GetTitle() == *folder_name) {
child = node;
break;
}
}
if (!child)
child = model->AddFolder(parent, parent->child_count(), *folder_name);
parent = child;
}
folders_added_to.insert(parent);
if (bookmark->is_folder) {
model->AddFolder(parent, parent->child_count(), bookmark->title);
} else {
model->AddURLWithCreationTime(parent, parent->child_count(),
bookmark->title, bookmark->url,
bookmark->creation_time);
}
}
for (std::set<const BookmarkNode*>::const_iterator i =
folders_added_to.begin();
i != folders_added_to.end(); ++i) {
model->ResetDateFolderModified(*i);
}
model->EndExtensiveChanges();
if (import_to_top_level && !add_all_to_top_level)
ShowBookmarkBar(profile_);
}
void ProfileWriter::AddFavicons(
const std::vector<ImportedFaviconUsage>& favicons) {
FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS)->
SetImportedFavicons(favicons);
}
typedef std::map<std::string, TemplateURL*> HostPathMap;
static std::string HostPathKeyForURL(const GURL& url) {
return url.is_valid() ? url.host() + url.path() : std::string();
}
static std::string BuildHostPathKey(const TemplateURL* t_url,
bool try_url_if_invalid) {
if (try_url_if_invalid && !t_url->url_ref().IsValid())
return HostPathKeyForURL(GURL(t_url->url()));
if (t_url->url_ref().SupportsReplacement()) {
return HostPathKeyForURL(GURL(
t_url->url_ref().ReplaceSearchTerms(
TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x")))));
}
return std::string();
}
static void BuildHostPathMap(TemplateURLService* model,
HostPathMap* host_path_map) {
TemplateURLService::TemplateURLVector template_urls =
model->GetTemplateURLs();
for (size_t i = 0; i < template_urls.size(); ++i) {
const std::string host_path = BuildHostPathKey(template_urls[i], false);
if (!host_path.empty()) {
const TemplateURL* existing_turl = (*host_path_map)[host_path];
if (!existing_turl ||
(template_urls[i]->show_in_default_list() &&
!existing_turl->show_in_default_list())) {
(*host_path_map)[host_path] = template_urls[i];
}
}
}
}
void ProfileWriter::AddKeywords(ScopedVector<TemplateURL> 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) {
if (model->GetTemplateURLForKeyword((*i)->keyword()) != NULL)
continue;
if (unique_on_host_and_path &&
(host_path_map.find(BuildHostPathKey(*i, true)) != host_path_map.end()))
continue;
if ((*i)->url_ref().IsValid()) {
model->AddAndSetProfile(*i, profile_);
*i = NULL;
}
}
}
ProfileWriter::~ProfileWriter() {}