Add ICU message format support
[chromium-blink-merge.git] / chrome / utility / importer / firefox_importer.cc
blobb9238d6085237b569e01569c1d63ab07999880f7
1 // Copyright 2013 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/utility/importer/firefox_importer.h"
7 #include <set>
9 #include "base/files/file_enumerator.h"
10 #include "base/files/file_util.h"
11 #include "base/json/json_file_value_serializer.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/stl_util.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "chrome/common/importer/firefox_importer_utils.h"
18 #include "chrome/common/importer/imported_bookmark_entry.h"
19 #include "chrome/common/importer/importer_autofill_form_data_entry.h"
20 #include "chrome/common/importer/importer_bridge.h"
21 #include "chrome/common/importer/importer_url_row.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "chrome/utility/importer/bookmark_html_reader.h"
24 #include "chrome/utility/importer/favicon_reencode.h"
25 #include "chrome/utility/importer/nss_decryptor.h"
26 #include "components/autofill/core/common/password_form.h"
27 #include "sql/connection.h"
28 #include "sql/statement.h"
29 #include "url/gurl.h"
31 namespace {
33 // Original definition is in http://mxr.mozilla.org/firefox/source/toolkit/
34 // components/places/public/nsINavBookmarksService.idl
35 enum BookmarkItemType {
36 TYPE_BOOKMARK = 1,
37 TYPE_FOLDER = 2,
38 TYPE_SEPARATOR = 3,
39 TYPE_DYNAMIC_CONTAINER = 4
42 // Loads the default bookmarks in the Firefox installed at |app_path|,
43 // and stores their locations in |urls|.
44 void LoadDefaultBookmarks(const base::FilePath& app_path,
45 std::set<GURL>* urls) {
46 base::FilePath file = app_path.AppendASCII("defaults")
47 .AppendASCII("profile")
48 .AppendASCII("bookmarks.html");
49 urls->clear();
51 std::vector<ImportedBookmarkEntry> bookmarks;
52 std::vector<importer::SearchEngineInfo> search_engines;
53 bookmark_html_reader::ImportBookmarksFile(base::Callback<bool(void)>(),
54 base::Callback<bool(const GURL&)>(),
55 file,
56 &bookmarks,
57 &search_engines,
58 NULL);
59 for (size_t i = 0; i < bookmarks.size(); ++i)
60 urls->insert(bookmarks[i].url);
63 // Returns true if |url| has a valid scheme that we allow to import. We
64 // filter out the URL with a unsupported scheme.
65 bool CanImportURL(const GURL& url) {
66 // The URL is not valid.
67 if (!url.is_valid())
68 return false;
70 // Filter out the URLs with unsupported schemes.
71 const char* const kInvalidSchemes[] = {"wyciwyg", "place", "about", "chrome"};
72 for (size_t i = 0; i < arraysize(kInvalidSchemes); ++i) {
73 if (url.SchemeIs(kInvalidSchemes[i]))
74 return false;
77 return true;
80 } // namespace
82 struct FirefoxImporter::BookmarkItem {
83 int parent;
84 int id;
85 GURL url;
86 base::string16 title;
87 BookmarkItemType type;
88 std::string keyword;
89 base::Time date_added;
90 int64 favicon;
91 bool empty_folder;
94 FirefoxImporter::FirefoxImporter() {
97 FirefoxImporter::~FirefoxImporter() {
100 void FirefoxImporter::StartImport(
101 const importer::SourceProfile& source_profile,
102 uint16 items,
103 ImporterBridge* bridge) {
104 bridge_ = bridge;
105 source_path_ = source_profile.source_path;
106 app_path_ = source_profile.app_path;
108 #if defined(OS_POSIX)
109 locale_ = source_profile.locale;
110 #endif
112 // The order here is important!
113 bridge_->NotifyStarted();
114 if ((items & importer::HOME_PAGE) && !cancelled()) {
115 bridge_->NotifyItemStarted(importer::HOME_PAGE);
116 ImportHomepage(); // Doesn't have a UI item.
117 bridge_->NotifyItemEnded(importer::HOME_PAGE);
120 // Note history should be imported before bookmarks because bookmark import
121 // will also import favicons and we store favicon for a URL only if the URL
122 // exist in history or bookmarks.
123 if ((items & importer::HISTORY) && !cancelled()) {
124 bridge_->NotifyItemStarted(importer::HISTORY);
125 ImportHistory();
126 bridge_->NotifyItemEnded(importer::HISTORY);
129 if ((items & importer::FAVORITES) && !cancelled()) {
130 bridge_->NotifyItemStarted(importer::FAVORITES);
131 ImportBookmarks();
132 bridge_->NotifyItemEnded(importer::FAVORITES);
134 if ((items & importer::SEARCH_ENGINES) && !cancelled()) {
135 bridge_->NotifyItemStarted(importer::SEARCH_ENGINES);
136 ImportSearchEngines();
137 bridge_->NotifyItemEnded(importer::SEARCH_ENGINES);
139 if ((items & importer::PASSWORDS) && !cancelled()) {
140 bridge_->NotifyItemStarted(importer::PASSWORDS);
141 ImportPasswords();
142 bridge_->NotifyItemEnded(importer::PASSWORDS);
144 if ((items & importer::AUTOFILL_FORM_DATA) && !cancelled()) {
145 bridge_->NotifyItemStarted(importer::AUTOFILL_FORM_DATA);
146 ImportAutofillFormData();
147 bridge_->NotifyItemEnded(importer::AUTOFILL_FORM_DATA);
149 bridge_->NotifyEnded();
152 void FirefoxImporter::ImportHistory() {
153 base::FilePath file = source_path_.AppendASCII("places.sqlite");
154 if (!base::PathExists(file))
155 return;
157 sql::Connection db;
158 if (!db.Open(file))
159 return;
161 // |visit_type| represent the transition type of URLs (typed, click,
162 // redirect, bookmark, etc.) We eliminate some URLs like sub-frames and
163 // redirects, since we don't want them to appear in history.
164 // Firefox transition types are defined in:
165 // toolkit/components/places/public/nsINavHistoryService.idl
166 const char query[] =
167 "SELECT h.url, h.title, h.visit_count, "
168 "h.hidden, h.typed, v.visit_date "
169 "FROM moz_places h JOIN moz_historyvisits v "
170 "ON h.id = v.place_id "
171 "WHERE v.visit_type <= 3";
173 sql::Statement s(db.GetUniqueStatement(query));
175 std::vector<ImporterURLRow> rows;
176 while (s.Step() && !cancelled()) {
177 GURL url(s.ColumnString(0));
179 // Filter out unwanted URLs.
180 if (!CanImportURL(url))
181 continue;
183 ImporterURLRow row(url);
184 row.title = s.ColumnString16(1);
185 row.visit_count = s.ColumnInt(2);
186 row.hidden = s.ColumnInt(3) == 1;
187 row.typed_count = s.ColumnInt(4);
188 row.last_visit = base::Time::FromTimeT(s.ColumnInt64(5)/1000000);
190 rows.push_back(row);
193 if (!rows.empty() && !cancelled())
194 bridge_->SetHistoryItems(rows, importer::VISIT_SOURCE_FIREFOX_IMPORTED);
197 void FirefoxImporter::ImportBookmarks() {
198 base::FilePath file = source_path_.AppendASCII("places.sqlite");
199 if (!base::PathExists(file))
200 return;
202 sql::Connection db;
203 if (!db.Open(file))
204 return;
206 // Get the bookmark folders that we are interested in.
207 int toolbar_folder_id = -1;
208 int menu_folder_id = -1;
209 int unsorted_folder_id = -1;
210 LoadRootNodeID(&db, &toolbar_folder_id, &menu_folder_id, &unsorted_folder_id);
212 // Load livemark IDs.
213 std::set<int> livemark_id;
214 LoadLivemarkIDs(&db, &livemark_id);
216 // Load the default bookmarks.
217 std::set<GURL> default_urls;
218 LoadDefaultBookmarks(app_path_, &default_urls);
220 BookmarkList list;
221 GetTopBookmarkFolder(&db, toolbar_folder_id, &list);
222 GetTopBookmarkFolder(&db, menu_folder_id, &list);
223 GetTopBookmarkFolder(&db, unsorted_folder_id, &list);
224 size_t count = list.size();
225 for (size_t i = 0; i < count; ++i)
226 GetWholeBookmarkFolder(&db, &list, i, NULL);
228 std::vector<ImportedBookmarkEntry> bookmarks;
229 std::vector<importer::SearchEngineInfo> search_engines;
230 FaviconMap favicon_map;
232 // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based
233 // keywords yet. We won't include them in the list.
234 std::set<int> post_keyword_ids;
235 const char query[] =
236 "SELECT b.id FROM moz_bookmarks b "
237 "INNER JOIN moz_items_annos ia ON ia.item_id = b.id "
238 "INNER JOIN moz_anno_attributes aa ON ia.anno_attribute_id = aa.id "
239 "WHERE aa.name = 'bookmarkProperties/POSTData'";
240 sql::Statement s(db.GetUniqueStatement(query));
242 if (!s.is_valid())
243 return;
245 while (s.Step() && !cancelled())
246 post_keyword_ids.insert(s.ColumnInt(0));
248 for (size_t i = 0; i < list.size(); ++i) {
249 BookmarkItem* item = list[i];
251 // Folders are added implicitly on adding children, so we only explicitly
252 // add empty folders.
253 if (item->type != TYPE_BOOKMARK &&
254 ((item->type != TYPE_FOLDER) || !item->empty_folder))
255 continue;
257 if (CanImportURL(item->url)) {
258 // Skip the default bookmarks and unwanted URLs.
259 if (default_urls.find(item->url) != default_urls.end() ||
260 post_keyword_ids.find(item->id) != post_keyword_ids.end())
261 continue;
263 // Find the bookmark path by tracing their links to parent folders.
264 std::vector<base::string16> path;
265 BookmarkItem* child = item;
266 bool found_path = false;
267 bool is_in_toolbar = false;
268 while (child->parent >= 0) {
269 BookmarkItem* parent = list[child->parent];
270 if (livemark_id.find(parent->id) != livemark_id.end()) {
271 // Don't import live bookmarks.
272 break;
275 if (parent->id != menu_folder_id) {
276 // To avoid excessive nesting, omit the name for the bookmarks menu
277 // folder.
278 path.insert(path.begin(), parent->title);
281 if (parent->id == toolbar_folder_id)
282 is_in_toolbar = true;
284 if (parent->id == toolbar_folder_id ||
285 parent->id == menu_folder_id ||
286 parent->id == unsorted_folder_id) {
287 // We've reached a root node, hooray!
288 found_path = true;
289 break;
292 child = parent;
295 if (!found_path)
296 continue;
298 ImportedBookmarkEntry entry;
299 entry.creation_time = item->date_added;
300 entry.title = item->title;
301 entry.url = item->url;
302 entry.path = path;
303 entry.in_toolbar = is_in_toolbar;
304 entry.is_folder = item->type == TYPE_FOLDER;
306 bookmarks.push_back(entry);
309 if (item->type == TYPE_BOOKMARK) {
310 if (item->favicon)
311 favicon_map[item->favicon].insert(item->url);
313 // Import this bookmark as a search engine if it has a keyword and its URL
314 // is usable as a search engine URL. (Even if the URL doesn't allow
315 // substitution, importing as a "search engine" allows users to trigger
316 // the bookmark by entering its keyword in the omnibox.)
317 if (item->keyword.empty())
318 continue;
319 importer::SearchEngineInfo search_engine_info;
320 std::string search_engine_url;
321 if (item->url.is_valid())
322 search_engine_info.url = base::UTF8ToUTF16(item->url.spec());
323 else if (bookmark_html_reader::CanImportURLAsSearchEngine(
324 item->url,
325 &search_engine_url))
326 search_engine_info.url = base::UTF8ToUTF16(search_engine_url);
327 else
328 continue;
329 search_engine_info.keyword = base::UTF8ToUTF16(item->keyword);
330 search_engine_info.display_name = item->title;
331 search_engines.push_back(search_engine_info);
335 STLDeleteElements(&list);
337 // Write into profile.
338 if (!bookmarks.empty() && !cancelled()) {
339 const base::string16& first_folder_name =
340 bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_FIREFOX);
341 bridge_->AddBookmarks(bookmarks, first_folder_name);
343 if (!search_engines.empty() && !cancelled()) {
344 bridge_->SetKeywords(search_engines, false);
346 if (!favicon_map.empty() && !cancelled()) {
347 favicon_base::FaviconUsageDataList favicons;
348 LoadFavicons(&db, favicon_map, &favicons);
349 bridge_->SetFavicons(favicons);
353 void FirefoxImporter::ImportPasswords() {
354 // Initializes NSS3.
355 NSSDecryptor decryptor;
356 if (!decryptor.Init(source_path_, source_path_) &&
357 !decryptor.Init(app_path_, source_path_)) {
358 return;
361 std::vector<autofill::PasswordForm> forms;
362 base::FilePath source_path = source_path_;
363 const base::FilePath sqlite_file = source_path.AppendASCII("signons.sqlite");
364 const base::FilePath json_file = source_path.AppendASCII("logins.json");
365 const base::FilePath signon3_file = source_path.AppendASCII("signons3.txt");
366 const base::FilePath signon2_file = source_path.AppendASCII("signons2.txt");
367 if (base::PathExists(json_file)) {
368 // Since Firefox 32, passwords are in logins.json.
369 decryptor.ReadAndParseLogins(json_file, &forms);
370 } else if (base::PathExists(sqlite_file)) {
371 // Since Firefox 3.1, passwords are in signons.sqlite db.
372 decryptor.ReadAndParseSignons(sqlite_file, &forms);
373 } else if (base::PathExists(signon3_file)) {
374 // Firefox 3.0 uses signons3.txt to store the passwords.
375 decryptor.ParseSignons(signon3_file, &forms);
376 } else {
377 decryptor.ParseSignons(signon2_file, &forms);
380 if (!cancelled()) {
381 for (size_t i = 0; i < forms.size(); ++i) {
382 if (!forms[i].username_value.empty() ||
383 !forms[i].password_value.empty() ||
384 forms[i].blacklisted_by_user) {
385 bridge_->SetPasswordForm(forms[i]);
391 void FirefoxImporter::ImportSearchEngines() {
392 std::vector<std::string> search_engine_data;
393 GetSearchEnginesXMLData(&search_engine_data);
395 bridge_->SetFirefoxSearchEnginesXMLData(search_engine_data);
398 void FirefoxImporter::ImportHomepage() {
399 GURL home_page = GetHomepage(source_path_);
400 if (home_page.is_valid() && !IsDefaultHomepage(home_page, app_path_)) {
401 bridge_->AddHomePage(home_page);
405 void FirefoxImporter::ImportAutofillFormData() {
406 base::FilePath file = source_path_.AppendASCII("formhistory.sqlite");
407 if (!base::PathExists(file))
408 return;
410 sql::Connection db;
411 if (!db.Open(file))
412 return;
414 const char query[] =
415 "SELECT fieldname, value, timesUsed, firstUsed, lastUsed FROM "
416 "moz_formhistory";
418 sql::Statement s(db.GetUniqueStatement(query));
420 std::vector<ImporterAutofillFormDataEntry> form_entries;
421 while (s.Step() && !cancelled()) {
422 ImporterAutofillFormDataEntry form_entry;
423 form_entry.name = s.ColumnString16(0);
424 form_entry.value = s.ColumnString16(1);
425 form_entry.times_used = s.ColumnInt(2);
426 form_entry.first_used = base::Time::FromTimeT(s.ColumnInt64(3) / 1000000);
427 form_entry.last_used = base::Time::FromTimeT(s.ColumnInt64(4) / 1000000);
429 // Don't import search bar history.
430 if (base::UTF16ToUTF8(form_entry.name) == "searchbar-history")
431 continue;
433 form_entries.push_back(form_entry);
436 if (!form_entries.empty() && !cancelled())
437 bridge_->SetAutofillFormData(form_entries);
440 void FirefoxImporter::GetSearchEnginesXMLData(
441 std::vector<std::string>* search_engine_data) {
442 base::FilePath file = source_path_.AppendASCII("search.sqlite");
443 if (!base::PathExists(file)) {
444 // Since Firefox 3.5, search engines are no longer stored in search.sqlite.
445 // Instead, search.json is used for storing search engines.
446 GetSearchEnginesXMLDataFromJSON(search_engine_data);
447 return;
450 sql::Connection db;
451 if (!db.Open(file))
452 return;
454 const char query[] =
455 "SELECT engineid FROM engine_data "
456 "WHERE engineid NOT IN "
457 "(SELECT engineid FROM engine_data "
458 "WHERE name='hidden') "
459 "ORDER BY value ASC";
461 sql::Statement s(db.GetUniqueStatement(query));
462 if (!s.is_valid())
463 return;
465 const base::FilePath searchplugins_path(FILE_PATH_LITERAL("searchplugins"));
466 // Search engine definitions are XMLs stored in two directories. Default
467 // engines are in the app directory (app_path_) and custom engines are
468 // in the profile directory (source_path_).
470 // Since Firefox 21, app_path_ engines are in 'browser' subdirectory:
471 base::FilePath app_path =
472 app_path_.AppendASCII("browser").Append(searchplugins_path);
473 if (!base::PathExists(app_path)) {
474 // This might be an older Firefox, try old location without the 'browser'
475 // path component:
476 app_path = app_path_.Append(searchplugins_path);
479 base::FilePath profile_path = source_path_.Append(searchplugins_path);
481 // Firefox doesn't store a search engine in its sqlite database unless the
482 // user has added a engine. So we get search engines from sqlite db as well
483 // as from the file system.
484 if (s.Step()) {
485 const std::string kAppPrefix("[app]/");
486 const std::string kProfilePrefix("[profile]/");
487 do {
488 base::FilePath file;
489 std::string engine(s.ColumnString(0));
491 // The string contains [app]/<name>.xml or [profile]/<name>.xml where
492 // the [app] and [profile] need to be replaced with the actual app or
493 // profile path.
494 size_t index = engine.find(kAppPrefix);
495 if (index != std::string::npos) {
496 // Remove '[app]/'.
497 file = app_path.AppendASCII(engine.substr(index + kAppPrefix.length()));
498 } else if ((index = engine.find(kProfilePrefix)) != std::string::npos) {
499 // Remove '[profile]/'.
500 file = profile_path.AppendASCII(
501 engine.substr(index + kProfilePrefix.length()));
502 } else {
503 // Looks like absolute path to the file.
504 file = base::FilePath::FromUTF8Unsafe(engine);
506 std::string file_data;
507 base::ReadFileToString(file, &file_data);
508 search_engine_data->push_back(file_data);
509 } while (s.Step() && !cancelled());
512 #if defined(OS_POSIX)
513 // Ubuntu-flavored Firefox supports locale-specific search engines via
514 // locale-named subdirectories. They fall back to en-US.
515 // See http://crbug.com/53899
516 // TODO(jshin): we need to make sure our locale code matches that of
517 // Firefox.
518 DCHECK(!locale_.empty());
519 base::FilePath locale_app_path = app_path.AppendASCII(locale_);
520 base::FilePath default_locale_app_path = app_path.AppendASCII("en-US");
521 if (base::DirectoryExists(locale_app_path))
522 app_path = locale_app_path;
523 else if (base::DirectoryExists(default_locale_app_path))
524 app_path = default_locale_app_path;
525 #endif
527 // Get search engine definition from file system.
528 base::FileEnumerator engines(app_path, false, base::FileEnumerator::FILES);
529 for (base::FilePath engine_path = engines.Next();
530 !engine_path.value().empty(); engine_path = engines.Next()) {
531 std::string file_data;
532 base::ReadFileToString(file, &file_data);
533 search_engine_data->push_back(file_data);
537 void FirefoxImporter::GetSearchEnginesXMLDataFromJSON(
538 std::vector<std::string>* search_engine_data) {
539 // search-metadata.json contains keywords for search engines. This
540 // file exists only if the user has set keywords for search engines.
541 base::FilePath search_metadata_json_file =
542 source_path_.AppendASCII("search-metadata.json");
543 JSONFileValueDeserializer metadata_deserializer(search_metadata_json_file);
544 scoped_ptr<base::Value> metadata_root(
545 metadata_deserializer.Deserialize(NULL, NULL));
546 const base::DictionaryValue* search_metadata_root = NULL;
547 if (metadata_root)
548 metadata_root->GetAsDictionary(&search_metadata_root);
550 // search.json contains information about search engines to import.
551 base::FilePath search_json_file = source_path_.AppendASCII("search.json");
552 if (!base::PathExists(search_json_file))
553 return;
555 JSONFileValueDeserializer deserializer(search_json_file);
556 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL));
557 const base::DictionaryValue* search_root = NULL;
558 if (!root || !root->GetAsDictionary(&search_root))
559 return;
561 const std::string kDirectories("directories");
562 const base::DictionaryValue* search_directories = NULL;
563 if (!search_root->GetDictionary(kDirectories, &search_directories))
564 return;
566 // Dictionary |search_directories| contains a list of search engines
567 // (default and installed). The list can be found from key <engines>
568 // of the dictionary. Key <engines> is a grandchild of key <directories>.
569 // However, key <engines> parent's key is dynamic which depends on
570 // operating systems. For example,
571 // Ubuntu (for default search engine):
572 // /usr/lib/firefox/distribution/searchplugins/locale/en-US
573 // Ubuntu (for installed search engines):
574 // /home/<username>/.mozilla/firefox/lcd50n4n.default/searchplugins
575 // Windows (for default search engine):
576 // C:\\Program Files (x86)\\Mozilla Firefox\\browser\\searchplugins
577 // Therefore, it needs to be retrieved by searching.
579 for (base::DictionaryValue::Iterator it(*search_directories); !it.IsAtEnd();
580 it.Advance()) {
581 // The key of |it| may contains dot (.) which cannot be used as <key>
582 // for retrieving <engines>. Hence, it is needed to get |it| as dictionary.
583 // The resulted dictionary can be used for retrieving <engines>.
584 const std::string kEngines("engines");
585 const base::DictionaryValue* search_directory = NULL;
586 if (!it.value().GetAsDictionary(&search_directory))
587 continue;
589 const base::ListValue* search_engines = NULL;
590 if (!search_directory->GetList(kEngines, &search_engines))
591 continue;
593 const std::string kFilePath("filePath");
594 const std::string kHidden("_hidden");
595 for (size_t i = 0; i < search_engines->GetSize(); ++i) {
596 const base::DictionaryValue* engine_info = NULL;
597 if (!search_engines->GetDictionary(i, &engine_info))
598 continue;
600 bool is_hidden = false;
601 std::string file_path;
602 if (!engine_info->GetBoolean(kHidden, &is_hidden) ||
603 !engine_info->GetString(kFilePath, &file_path))
604 continue;
606 if (!is_hidden) {
607 const std::string kAppPrefix("[app]/");
608 const std::string kProfilePrefix("[profile]/");
609 base::FilePath xml_file = base::FilePath::FromUTF8Unsafe(file_path);
611 // If |file_path| contains [app] or [profile] then they need to be
612 // replaced with the actual app or profile path.
613 size_t index = file_path.find(kAppPrefix);
614 if (index != std::string::npos) {
615 // Replace '[app]/' with actual app path.
616 xml_file = app_path_.AppendASCII("searchplugins").AppendASCII(
617 file_path.substr(index + kAppPrefix.length()));
618 } else if ((index = file_path.find(kProfilePrefix)) !=
619 std::string::npos) {
620 // Replace '[profile]/' with actual profile path.
621 xml_file = source_path_.AppendASCII("searchplugins").AppendASCII(
622 file_path.substr(index + kProfilePrefix.length()));
625 std::string file_data;
626 base::ReadFileToString(xml_file, &file_data);
628 // If a keyword is mentioned for this search engine, then add
629 // it to the XML string as an <Alias> element and use this updated
630 // string.
631 const base::DictionaryValue* search_xml_path = NULL;
632 if (search_metadata_root && search_metadata_root->HasKey(file_path) &&
633 search_metadata_root->GetDictionaryWithoutPathExpansion(
634 file_path, &search_xml_path)) {
635 std::string alias;
636 search_xml_path->GetString("alias", &alias);
638 // Add <Alias> element as the last child element.
639 size_t end_of_parent = file_data.find("</SearchPlugin>");
640 if (end_of_parent != std::string::npos && !alias.empty())
641 file_data.insert(end_of_parent, "<Alias>" + alias + "</Alias> \n");
643 search_engine_data->push_back(file_data);
649 void FirefoxImporter::LoadRootNodeID(sql::Connection* db,
650 int* toolbar_folder_id,
651 int* menu_folder_id,
652 int* unsorted_folder_id) {
653 static const char kToolbarFolderName[] = "toolbar";
654 static const char kMenuFolderName[] = "menu";
655 static const char kUnsortedFolderName[] = "unfiled";
657 const char query[] = "SELECT root_name, folder_id FROM moz_bookmarks_roots";
658 sql::Statement s(db->GetUniqueStatement(query));
660 while (s.Step()) {
661 std::string folder = s.ColumnString(0);
662 int id = s.ColumnInt(1);
663 if (folder == kToolbarFolderName)
664 *toolbar_folder_id = id;
665 else if (folder == kMenuFolderName)
666 *menu_folder_id = id;
667 else if (folder == kUnsortedFolderName)
668 *unsorted_folder_id = id;
672 void FirefoxImporter::LoadLivemarkIDs(sql::Connection* db,
673 std::set<int>* livemark) {
674 static const char kFeedAnnotation[] = "livemark/feedURI";
675 livemark->clear();
677 const char query[] =
678 "SELECT b.item_id "
679 "FROM moz_anno_attributes a "
680 "JOIN moz_items_annos b ON a.id = b.anno_attribute_id "
681 "WHERE a.name = ? ";
682 sql::Statement s(db->GetUniqueStatement(query));
683 s.BindString(0, kFeedAnnotation);
685 while (s.Step() && !cancelled())
686 livemark->insert(s.ColumnInt(0));
689 void FirefoxImporter::GetTopBookmarkFolder(sql::Connection* db,
690 int folder_id,
691 BookmarkList* list) {
692 const char query[] =
693 "SELECT b.title "
694 "FROM moz_bookmarks b "
695 "WHERE b.type = 2 AND b.id = ? "
696 "ORDER BY b.position";
697 sql::Statement s(db->GetUniqueStatement(query));
698 s.BindInt(0, folder_id);
700 if (s.Step()) {
701 BookmarkItem* item = new BookmarkItem;
702 item->parent = -1; // The top level folder has no parent.
703 item->id = folder_id;
704 item->title = s.ColumnString16(0);
705 item->type = TYPE_FOLDER;
706 item->favicon = 0;
707 item->empty_folder = true;
708 list->push_back(item);
712 void FirefoxImporter::GetWholeBookmarkFolder(sql::Connection* db,
713 BookmarkList* list,
714 size_t position,
715 bool* empty_folder) {
716 if (position >= list->size()) {
717 NOTREACHED();
718 return;
721 const char query[] =
722 "SELECT b.id, h.url, COALESCE(b.title, h.title), "
723 "b.type, k.keyword, b.dateAdded, h.favicon_id "
724 "FROM moz_bookmarks b "
725 "LEFT JOIN moz_places h ON b.fk = h.id "
726 "LEFT JOIN moz_keywords k ON k.id = b.keyword_id "
727 "WHERE b.type IN (1,2) AND b.parent = ? "
728 "ORDER BY b.position";
729 sql::Statement s(db->GetUniqueStatement(query));
730 s.BindInt(0, (*list)[position]->id);
732 BookmarkList temp_list;
733 while (s.Step()) {
734 BookmarkItem* item = new BookmarkItem;
735 item->parent = static_cast<int>(position);
736 item->id = s.ColumnInt(0);
737 item->url = GURL(s.ColumnString(1));
738 item->title = s.ColumnString16(2);
739 item->type = static_cast<BookmarkItemType>(s.ColumnInt(3));
740 item->keyword = s.ColumnString(4);
741 item->date_added = base::Time::FromTimeT(s.ColumnInt64(5)/1000000);
742 item->favicon = s.ColumnInt64(6);
743 item->empty_folder = true;
745 temp_list.push_back(item);
746 if (empty_folder != NULL)
747 *empty_folder = false;
750 // Appends all items to the list.
751 for (BookmarkList::iterator i = temp_list.begin();
752 i != temp_list.end(); ++i) {
753 list->push_back(*i);
754 // Recursive add bookmarks in sub-folders.
755 if ((*i)->type == TYPE_FOLDER)
756 GetWholeBookmarkFolder(db, list, list->size() - 1, &(*i)->empty_folder);
760 void FirefoxImporter::LoadFavicons(
761 sql::Connection* db,
762 const FaviconMap& favicon_map,
763 favicon_base::FaviconUsageDataList* favicons) {
764 const char query[] = "SELECT url, data FROM moz_favicons WHERE id=?";
765 sql::Statement s(db->GetUniqueStatement(query));
767 if (!s.is_valid())
768 return;
770 for (FaviconMap::const_iterator i = favicon_map.begin();
771 i != favicon_map.end(); ++i) {
772 s.BindInt64(0, i->first);
773 if (s.Step()) {
774 favicon_base::FaviconUsageData usage;
776 usage.favicon_url = GURL(s.ColumnString(0));
777 if (!usage.favicon_url.is_valid())
778 continue; // Don't bother importing favicons with invalid URLs.
780 std::vector<unsigned char> data;
781 s.ColumnBlobAsVector(1, &data);
782 if (data.empty())
783 continue; // Data definitely invalid.
785 if (!importer::ReencodeFavicon(&data[0], data.size(), &usage.png_data))
786 continue; // Unable to decode.
788 usage.urls = i->second;
789 favicons->push_back(usage);
791 s.Reset(true);