Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: chrome/utility/importer/firefox_importer.cc

Issue 247223003: Parsed search.json for importing search engine settings from FireFox. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added test coverage and addressed feedback Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
Ilya Sherman 2014/05/01 21:37:02 nit: I'd omit this blank line.
491 if (!root || !root->GetAsDictionary(&search_root))
492 return;
493
494 const std::string kDirectories("directories");
495 const base::DictionaryValue* search_directories = NULL;
496
Ilya Sherman 2014/05/01 21:37:02 nit: I'd omit this blank line.
497 if (!search_root->GetDictionary(kDirectories, &search_directories))
498 return;
499
500 // Dictionary "search_directories" contains a list of search engines.
Ilya Sherman 2014/05/01 21:37:02 nit: Rather than using quotation marks to denote v
501 // The list can be found from key <engines> of the dictionary.
502 // Key <engines> is a grandchild of key <directories>.
503 // However, key <engines> parent's key is dynamic which
504 // depends on operating systems. For example,
505 // Ubuntu: /usr/lib/firefox/distribution/searchplugins/locale/en-US
506 // Windows: C:\\Program Files (x86)\\Mozilla Firefox\\browser\\searchplugins
507 // Therefore, it needs to be retrieved by using iterator
Ilya Sherman 2014/05/01 21:37:02 nit: "by using iterator" -> "by searching."
508
509 base::DictionaryValue::Iterator it(*search_directories);
510 const std::string kEngines(it.key() + ".engines");
511 const base::ListValue* search_engines = NULL;
512
513 if (search_directories->GetList(kEngines, &search_engines)) {
Ilya Sherman 2014/05/01 21:37:02 nit: I'd suggest reversing this condition and usin
514 const std::string kFilePath("filePath");
515 for (size_t i = 0; i < search_engines->GetSize(); ++i) {
516 const base::DictionaryValue* engine_info = NULL;
517 if (search_engines->GetDictionary(i, &engine_info)) {
518 std::string file_path;
519 if (engine_info->GetString(kFilePath, &file_path)) {
520 std::string file_data;
521 base::FilePath xml_file = base::FilePath::FromUTF8Unsafe(file_path);
522 base::ReadFileToString(xml_file, &file_data);
523 search_engine_data->push_back(file_data);
524 }
525 }
526 }
527 }
528 }
529
479 void FirefoxImporter::LoadRootNodeID(sql::Connection* db, 530 void FirefoxImporter::LoadRootNodeID(sql::Connection* db,
480 int* toolbar_folder_id, 531 int* toolbar_folder_id,
481 int* menu_folder_id, 532 int* menu_folder_id,
482 int* unsorted_folder_id) { 533 int* unsorted_folder_id) {
483 static const char* kToolbarFolderName = "toolbar"; 534 static const char* kToolbarFolderName = "toolbar";
484 static const char* kMenuFolderName = "menu"; 535 static const char* kMenuFolderName = "menu";
485 static const char* kUnsortedFolderName = "unfiled"; 536 static const char* kUnsortedFolderName = "unfiled";
486 537
487 const char* query = "SELECT root_name, folder_id FROM moz_bookmarks_roots"; 538 const char* query = "SELECT root_name, folder_id FROM moz_bookmarks_roots";
488 sql::Statement s(db->GetUniqueStatement(query)); 539 sql::Statement s(db->GetUniqueStatement(query));
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 662
612 if (!importer::ReencodeFavicon(&data[0], data.size(), &usage.png_data)) 663 if (!importer::ReencodeFavicon(&data[0], data.size(), &usage.png_data))
613 continue; // Unable to decode. 664 continue; // Unable to decode.
614 665
615 usage.urls = i->second; 666 usage.urls = i->second;
616 favicons->push_back(usage); 667 favicons->push_back(usage);
617 } 668 }
618 s.Reset(true); 669 s.Reset(true);
619 } 670 }
620 } 671 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698