1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/utility/importer/edge_importer_win.h"
#include <Shlobj.h>
#include <algorithm>
#include <map>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
#include "base/compiler_specific.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "base/win/windows_version.h"
#include "chrome/common/importer/edge_importer_utils_win.h"
#include "chrome/common/importer/importer_bridge.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/utility/importer/edge_database_reader_win.h"
#include "chrome/utility/importer/favicon_reencode.h"
#include "components/user_data_importer/common/imported_bookmark_entry.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
namespace {
// Toolbar favorites are placed under this special folder name.
const char16_t kFavoritesBarTitle[] = u"_Favorites_Bar_";
const wchar_t kSpartanDatabaseFile[] = L"spartan.edb";
struct EdgeFavoriteEntry {
EdgeFavoriteEntry()
: is_folder(false),
order_number(0),
item_id(GUID_NULL),
parent_id(GUID_NULL) {}
std::u16string title;
GURL url;
base::FilePath favicon_file;
bool is_folder;
int64_t order_number;
base::Time date_updated;
GUID item_id;
GUID parent_id;
std::vector<raw_ptr<const EdgeFavoriteEntry, VectorExperimental>> children;
user_data_importer::ImportedBookmarkEntry ToBookmarkEntry(
bool in_toolbar,
const std::vector<std::u16string>& path) const {
user_data_importer::ImportedBookmarkEntry entry;
entry.in_toolbar = in_toolbar;
entry.is_folder = is_folder;
entry.url = url;
entry.path = path;
entry.title = title;
entry.creation_time = date_updated;
return entry;
}
};
struct EdgeFavoriteEntryComparator {
bool operator()(const EdgeFavoriteEntry* lhs,
const EdgeFavoriteEntry* rhs) const {
return std::tie(lhs->order_number, lhs->title) <
std::tie(rhs->order_number, rhs->title);
}
};
// The name of the database file is spartan.edb, however it isn't clear how
// the intermediate path between the DataStore and the database is generated.
// Therefore we just do a simple recursive search until we find a matching name.
base::FilePath FindSpartanDatabase(const base::FilePath& profile_path) {
base::FilePath data_path =
profile_path.empty() ? importer::GetEdgeDataFilePath() : profile_path;
if (data_path.empty())
return base::FilePath();
base::FileEnumerator enumerator(data_path.Append(L"DataStore\\Data"), true,
base::FileEnumerator::FILES);
base::FilePath path = enumerator.Next();
while (!path.empty()) {
if (base::EqualsCaseInsensitiveASCII(path.BaseName().value(),
kSpartanDatabaseFile))
return path;
path = enumerator.Next();
}
return base::FilePath();
}
struct GuidComparator {
bool operator()(const GUID& a, const GUID& b) const {
return UNSAFE_TODO(memcmp(&a, &b, sizeof(a))) < 0;
}
};
std::optional<std::vector<uint8_t>> ReadFaviconData(
const base::FilePath& file) {
std::optional<std::vector<uint8_t>> image_data = base::ReadFileToBytes(file);
if (!image_data) {
return std::nullopt;
}
return importer::ReencodeFavicon(image_data.value());
}
void BuildBookmarkEntries(
const EdgeFavoriteEntry& current_entry,
bool is_toolbar,
std::vector<user_data_importer::ImportedBookmarkEntry>* bookmarks,
favicon_base::FaviconUsageDataList* favicons,
std::vector<std::u16string>* path) {
for (const EdgeFavoriteEntry* entry : current_entry.children) {
if (entry->is_folder) {
// If the favorites bar then load all children as toolbar items.
if (base::EqualsCaseInsensitiveASCII(entry->title, kFavoritesBarTitle)) {
// Replace name with Links similar to IE.
path->push_back(u"Links");
BuildBookmarkEntries(*entry, true, bookmarks, favicons, path);
path->pop_back();
} else {
path->push_back(entry->title);
BuildBookmarkEntries(*entry, is_toolbar, bookmarks, favicons, path);
path->pop_back();
}
} else {
bookmarks->push_back(entry->ToBookmarkEntry(is_toolbar, *path));
favicon_base::FaviconUsageData favicon;
if (entry->url.is_valid() && !entry->favicon_file.empty()) {
std::optional<std::vector<uint8_t>> png_data =
ReadFaviconData(entry->favicon_file);
if (png_data) {
favicon.png_data = std::move(png_data).value();
// As the database doesn't provide us a favicon URL we'll fake one.
GURL::Replacements path_replace;
path_replace.SetPathStr("/favicon.ico");
favicon.favicon_url =
entry->url.GetWithEmptyPath().ReplaceComponents(path_replace);
favicon.urls.insert(entry->url);
favicons->push_back(favicon);
}
}
}
}
}
} // namespace
EdgeImporter::EdgeImporter() = default;
void EdgeImporter::StartImport(
const user_data_importer::SourceProfile& source_profile,
uint16_t items,
ImporterBridge* bridge) {
bridge_ = bridge;
bridge_->NotifyStarted();
source_path_ = source_profile.source_path;
if ((items & user_data_importer::FAVORITES) && !cancelled()) {
bridge_->NotifyItemStarted(user_data_importer::FAVORITES);
ImportFavorites();
bridge_->NotifyItemEnded(user_data_importer::FAVORITES);
}
bridge_->NotifyEnded();
}
EdgeImporter::~EdgeImporter() = default;
void EdgeImporter::ImportFavorites() {
std::vector<user_data_importer::ImportedBookmarkEntry> bookmarks;
favicon_base::FaviconUsageDataList favicons;
ParseFavoritesDatabase(&bookmarks, &favicons);
if (!bookmarks.empty() && !cancelled()) {
const std::u16string& first_folder_name =
l10n_util::GetStringUTF16(IDS_BOOKMARK_GROUP_FROM_EDGE);
bridge_->AddBookmarks(bookmarks, first_folder_name);
}
if (!favicons.empty() && !cancelled())
bridge_->SetFavicons(favicons);
}
// From Edge 13 (released with Windows 10 TH2), Favorites are stored in a JET
// database within the Edge local storage. The import uses the ESE library to
// open and read the data file. The data is stored in a Favorites table with
// the following schema.
// Column Name Column Type
// ------------------------------------------
// DateUpdated LongLong - FILETIME
// FaviconFile LongText - Relative path
// HashedUrl ULong
// IsDeleted Bit
// IsFolder Bit
// ItemId Guid
// OrderNumber LongLong
// ParentId Guid
// RoamDisabled Bit
// RowId Long
// Title LongText
// URL LongText
void EdgeImporter::ParseFavoritesDatabase(
std::vector<user_data_importer::ImportedBookmarkEntry>* bookmarks,
favicon_base::FaviconUsageDataList* favicons) {
base::FilePath database_path = FindSpartanDatabase(source_path_);
if (database_path.empty())
return;
base::FilePath log_folder = database_path.DirName().Append(L"LogFiles");
EdgeDatabaseReader database;
// If the log file directory does not exist, don't set the log_folder
// attribute, as the open database operation will fail in such cases.
// The log folder will usually not be present when running the unit tests.
if (base::PathExists(log_folder))
database.set_log_folder(log_folder);
if (!database.OpenDatabase(database_path)) {
DVLOG(1) << "Error opening database " << database.GetErrorMessage();
return;
}
std::unique_ptr<EdgeDatabaseTableEnumerator> enumerator =
database.OpenTableEnumerator(L"Favorites");
if (!enumerator) {
DVLOG(1) << "Error opening database table " << database.GetErrorMessage();
return;
}
if (!enumerator->Reset())
return;
std::map<GUID, EdgeFavoriteEntry, GuidComparator> database_entries;
base::FilePath favicon_base =
source_path_.empty() ? importer::GetEdgeDataFilePath() : source_path_;
favicon_base = favicon_base.Append(L"DataStore");
do {
EdgeFavoriteEntry entry;
bool is_deleted = false;
if (!enumerator->RetrieveColumn(L"IsDeleted", &is_deleted))
continue;
if (is_deleted)
continue;
if (!enumerator->RetrieveColumn(L"IsFolder", &entry.is_folder))
continue;
std::u16string url;
if (!enumerator->RetrieveColumn(L"URL", &url))
continue;
entry.url = GURL(url);
if (!entry.is_folder && !entry.url.is_valid())
continue;
if (!enumerator->RetrieveColumn(L"Title", &entry.title))
continue;
std::u16string favicon_file;
if (!enumerator->RetrieveColumn(L"FaviconFile", &favicon_file))
continue;
if (!favicon_file.empty()) {
entry.favicon_file =
favicon_base.Append(base::FilePath::FromUTF16Unsafe(favicon_file));
}
if (!enumerator->RetrieveColumn(L"ParentId", &entry.parent_id))
continue;
if (!enumerator->RetrieveColumn(L"ItemId", &entry.item_id))
continue;
if (!enumerator->RetrieveColumn(L"OrderNumber", &entry.order_number))
continue;
FILETIME data_updated;
if (!enumerator->RetrieveColumn(L"DateUpdated", &data_updated))
continue;
entry.date_updated = base::Time::FromFileTime(data_updated);
database_entries[entry.item_id] = entry;
} while (enumerator->Next() && !cancelled());
// Build simple tree.
EdgeFavoriteEntry root_entry;
for (auto& entry : database_entries) {
auto found_parent = database_entries.find(entry.second.parent_id);
if (found_parent == database_entries.end() ||
!found_parent->second.is_folder) {
root_entry.children.push_back(&entry.second);
} else {
found_parent->second.children.push_back(&entry.second);
}
}
// With tree built sort the children of each node including the root.
std::sort(root_entry.children.begin(), root_entry.children.end(),
EdgeFavoriteEntryComparator());
for (auto& entry : database_entries) {
std::sort(entry.second.children.begin(), entry.second.children.end(),
EdgeFavoriteEntryComparator());
}
std::vector<std::u16string> path;
BuildBookmarkEntries(root_entry, false, bookmarks, favicons, &path);
}
|