OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/utility/importer/firefox_importer.h" | 5 #include "chrome/utility/importer/firefox_importer.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_enumerator.h" | 10 #include "base/files/file_enumerator.h" |
| 11 #include "base/json/json_file_value_serializer.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
16 #include "chrome/common/importer/firefox_importer_utils.h" | 17 #include "chrome/common/importer/firefox_importer_utils.h" |
17 #include "chrome/common/importer/firefox_importer_utils.h" | 18 #include "chrome/common/importer/firefox_importer_utils.h" |
18 #include "chrome/common/importer/imported_bookmark_entry.h" | 19 #include "chrome/common/importer/imported_bookmark_entry.h" |
19 #include "chrome/common/importer/imported_favicon_usage.h" | 20 #include "chrome/common/importer/imported_favicon_usage.h" |
20 #include "chrome/common/importer/importer_bridge.h" | 21 #include "chrome/common/importer/importer_bridge.h" |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 377 |
377 void FirefoxImporter::ImportHomepage() { | 378 void FirefoxImporter::ImportHomepage() { |
378 GURL home_page = GetHomepage(source_path_); | 379 GURL home_page = GetHomepage(source_path_); |
379 if (home_page.is_valid() && !IsDefaultHomepage(home_page, app_path_)) { | 380 if (home_page.is_valid() && !IsDefaultHomepage(home_page, app_path_)) { |
380 bridge_->AddHomePage(home_page); | 381 bridge_->AddHomePage(home_page); |
381 } | 382 } |
382 } | 383 } |
383 | 384 |
384 void FirefoxImporter::GetSearchEnginesXMLData( | 385 void FirefoxImporter::GetSearchEnginesXMLData( |
385 std::vector<std::string>* search_engine_data) { | 386 std::vector<std::string>* search_engine_data) { |
386 // TODO(mpawlowski): This may no longer work, search engines are stored in | |
387 // search.json since Firefox 3.5, not in search.sqlite. XML definitions are | |
388 // still necessary. http://crbug.com/329175 | |
389 base::FilePath file = source_path_.AppendASCII("search.sqlite"); | 387 base::FilePath file = source_path_.AppendASCII("search.sqlite"); |
390 if (!base::PathExists(file)) | 388 if (!base::PathExists(file)) { |
| 389 // Since Firefox 3.5, search engines are no longer stored in search.sqlite. |
| 390 // Instead, search.json is used for storing search engines. |
| 391 GetSearchEnginesXMLDataFromJSON(search_engine_data); |
391 return; | 392 return; |
| 393 } |
392 | 394 |
393 sql::Connection db; | 395 sql::Connection db; |
394 if (!db.Open(file)) | 396 if (!db.Open(file)) |
395 return; | 397 return; |
396 | 398 |
397 const char* query = "SELECT engineid FROM engine_data " | 399 const char* query = "SELECT engineid FROM engine_data " |
398 "WHERE engineid NOT IN " | 400 "WHERE engineid NOT IN " |
399 "(SELECT engineid FROM engine_data " | 401 "(SELECT engineid FROM engine_data " |
400 "WHERE name='hidden') " | 402 "WHERE name='hidden') " |
401 "ORDER BY value ASC"; | 403 "ORDER BY value ASC"; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 // Get search engine definition from file system. | 471 // Get search engine definition from file system. |
470 base::FileEnumerator engines(app_path, false, base::FileEnumerator::FILES); | 472 base::FileEnumerator engines(app_path, false, base::FileEnumerator::FILES); |
471 for (base::FilePath engine_path = engines.Next(); | 473 for (base::FilePath engine_path = engines.Next(); |
472 !engine_path.value().empty(); engine_path = engines.Next()) { | 474 !engine_path.value().empty(); engine_path = engines.Next()) { |
473 std::string file_data; | 475 std::string file_data; |
474 base::ReadFileToString(file, &file_data); | 476 base::ReadFileToString(file, &file_data); |
475 search_engine_data->push_back(file_data); | 477 search_engine_data->push_back(file_data); |
476 } | 478 } |
477 } | 479 } |
478 | 480 |
| 481 void FirefoxImporter::GetSearchEnginesXMLDataFromJSON( |
| 482 std::vector<std::string>* search_engine_data) { |
| 483 base::FilePath search_json_file = source_path_.AppendASCII("search.json"); |
| 484 if (!base::PathExists(search_json_file)) |
| 485 return; |
| 486 |
| 487 JSONFileValueSerializer serializer(search_json_file); |
| 488 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); |
| 489 const base::DictionaryValue* search_root = NULL; |
| 490 if (!root || !root->GetAsDictionary(&search_root)) |
| 491 return; |
| 492 |
| 493 const std::string kDirectories("directories"); |
| 494 const base::DictionaryValue* search_directories = NULL; |
| 495 if (!search_root->GetDictionary(kDirectories, &search_directories)) |
| 496 return; |
| 497 |
| 498 // Dictionary |search_directories| contains a list of search engines |
| 499 // (default and installed). The list can be found from key <engines> |
| 500 // of the dictionary. Key <engines> is a grandchild of key <directories>. |
| 501 // However, key <engines> parent's key is dynamic which depends on |
| 502 // operating systems. For example, |
| 503 // Ubuntu (for default search engine): |
| 504 // /usr/lib/firefox/distribution/searchplugins/locale/en-US |
| 505 // Ubuntu (for installed search engines): |
| 506 // /home/<username>/.mozilla/firefox/lcd50n4n.default/searchplugins |
| 507 // Windows (for default search engine): |
| 508 // C:\\Program Files (x86)\\Mozilla Firefox\\browser\\searchplugins |
| 509 // Therefore, it needs to be retrieved by searching. |
| 510 |
| 511 for (base::DictionaryValue::Iterator it(*search_directories); !it.IsAtEnd(); |
| 512 it.Advance()) { |
| 513 // The key of |it| may contains dot (.) which cannot be used as <key> |
| 514 // for retrieving <engines>. Hence, it is needed to get |it| as dictionary. |
| 515 // The resulted dictionary can be used for retrieving <engines>. |
| 516 const std::string kEngines("engines"); |
| 517 const base::DictionaryValue* search_directory = NULL; |
| 518 if (!it.value().GetAsDictionary(&search_directory)) |
| 519 continue; |
| 520 |
| 521 const base::ListValue* search_engines = NULL; |
| 522 if (!search_directory->GetList(kEngines, &search_engines)) |
| 523 continue; |
| 524 |
| 525 const std::string kFilePath("filePath"); |
| 526 const std::string kHidden("_hidden"); |
| 527 for (size_t i = 0; i < search_engines->GetSize(); ++i) { |
| 528 const base::DictionaryValue* engine_info = NULL; |
| 529 if (!search_engines->GetDictionary(i, &engine_info)) |
| 530 continue; |
| 531 |
| 532 bool is_hidden = false; |
| 533 std::string file_path; |
| 534 if (!engine_info->GetBoolean(kHidden, &is_hidden) || |
| 535 !engine_info->GetString(kFilePath, &file_path)) |
| 536 continue; |
| 537 |
| 538 if (!is_hidden) { |
| 539 const std::string kAppPrefix("[app]/"); |
| 540 const std::string kProfilePrefix("[profile]/"); |
| 541 base::FilePath xml_file = base::FilePath::FromUTF8Unsafe(file_path); |
| 542 |
| 543 // If |file_path| contains [app] or [profile] then they need to be |
| 544 // replaced with the actual app or profile path. |
| 545 size_t index = file_path.find(kAppPrefix); |
| 546 if (index != std::string::npos) { |
| 547 // Replace '[app]/' with actual app path. |
| 548 xml_file = app_path_.AppendASCII("searchplugins").AppendASCII( |
| 549 file_path.substr(index + kAppPrefix.length())); |
| 550 } else if ((index = file_path.find(kProfilePrefix)) != |
| 551 std::string::npos) { |
| 552 // Replace '[profile]/' with actual profile path. |
| 553 xml_file = source_path_.AppendASCII("searchplugins").AppendASCII( |
| 554 file_path.substr(index + kProfilePrefix.length())); |
| 555 } |
| 556 |
| 557 std::string file_data; |
| 558 base::ReadFileToString(xml_file, &file_data); |
| 559 search_engine_data->push_back(file_data); |
| 560 } |
| 561 } |
| 562 } |
| 563 } |
| 564 |
479 void FirefoxImporter::LoadRootNodeID(sql::Connection* db, | 565 void FirefoxImporter::LoadRootNodeID(sql::Connection* db, |
480 int* toolbar_folder_id, | 566 int* toolbar_folder_id, |
481 int* menu_folder_id, | 567 int* menu_folder_id, |
482 int* unsorted_folder_id) { | 568 int* unsorted_folder_id) { |
483 static const char* kToolbarFolderName = "toolbar"; | 569 static const char* kToolbarFolderName = "toolbar"; |
484 static const char* kMenuFolderName = "menu"; | 570 static const char* kMenuFolderName = "menu"; |
485 static const char* kUnsortedFolderName = "unfiled"; | 571 static const char* kUnsortedFolderName = "unfiled"; |
486 | 572 |
487 const char* query = "SELECT root_name, folder_id FROM moz_bookmarks_roots"; | 573 const char* query = "SELECT root_name, folder_id FROM moz_bookmarks_roots"; |
488 sql::Statement s(db->GetUniqueStatement(query)); | 574 sql::Statement s(db->GetUniqueStatement(query)); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 | 697 |
612 if (!importer::ReencodeFavicon(&data[0], data.size(), &usage.png_data)) | 698 if (!importer::ReencodeFavicon(&data[0], data.size(), &usage.png_data)) |
613 continue; // Unable to decode. | 699 continue; // Unable to decode. |
614 | 700 |
615 usage.urls = i->second; | 701 usage.urls = i->second; |
616 favicons->push_back(usage); | 702 favicons->push_back(usage); |
617 } | 703 } |
618 s.Reset(true); | 704 s.Reset(true); |
619 } | 705 } |
620 } | 706 } |
OLD | NEW |