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. 4 5#include "chrome/utility/importer/bookmarks_file_importer.h" 6 7#include "base/bind.h" 8#include "chrome/common/importer/imported_bookmark_entry.h" 9#include "chrome/common/importer/imported_favicon_usage.h" 10#include "chrome/common/importer/importer_bridge.h" 11#include "chrome/common/importer/importer_data_types.h" 12#include "chrome/common/net/url_fixer_upper.h" 13#include "chrome/common/url_constants.h" 14#include "chrome/utility/importer/bookmark_html_reader.h" 15#include "content/public/common/url_constants.h" 16#include "grit/generated_resources.h" 17 18namespace { 19 20bool IsImporterCancelled(BookmarksFileImporter* importer) { 21 return importer->cancelled(); 22} 23 24} // namespace 25 26namespace internal { 27 28// Returns true if |url| has a valid scheme that we allow to import. We 29// filter out the URL with a unsupported scheme. 30bool CanImportURL(const GURL& url) { 31 // The URL is not valid. 32 if (!url.is_valid()) 33 return false; 34 35 // Filter out the URLs with unsupported schemes. 36 const char* const kInvalidSchemes[] = {"wyciwyg", "place"}; 37 for (size_t i = 0; i < arraysize(kInvalidSchemes); ++i) { 38 if (url.SchemeIs(kInvalidSchemes[i])) 39 return false; 40 } 41 42 // Check if |url| is about:blank. 43 if (url == GURL(content::kAboutBlankURL)) 44 return true; 45 46 // If |url| starts with chrome:// or about:, check if it's one of the URLs 47 // that we support. 48 if (url.SchemeIs(chrome::kChromeUIScheme) || 49 url.SchemeIs(chrome::kAboutScheme)) { 50 GURL fixed_url(URLFixerUpper::FixupURL(url.spec(), std::string())); 51 for (size_t i = 0; i < chrome::kNumberOfChromeHostURLs; ++i) { 52 if (fixed_url.DomainIs(chrome::kChromeHostURLs[i])) 53 return true; 54 } 55 56 for (int i = 0; i < chrome::kNumberOfChromeDebugURLs; ++i) { 57 if (fixed_url == GURL(chrome::kChromeDebugURLs[i])) 58 return true; 59 } 60 61 // If url has either chrome:// or about: schemes but wasn't found in the 62 // above lists, it means we don't support it, so we don't allow the user 63 // to import it. 64 return false; 65 } 66 67 // Otherwise, we assume the url has a valid (importable) scheme. 68 return true; 69} 70 71} // namespace internal 72 73BookmarksFileImporter::BookmarksFileImporter() {} 74 75BookmarksFileImporter::~BookmarksFileImporter() {} 76 77void BookmarksFileImporter::StartImport( 78 const importer::SourceProfile& source_profile, 79 uint16 items, 80 ImporterBridge* bridge) { 81 // The only thing this importer can import is a bookmarks file, aka 82 // "favorites". 83 DCHECK_EQ(importer::FAVORITES, items); 84 85 bridge->NotifyStarted(); 86 bridge->NotifyItemStarted(importer::FAVORITES); 87 88 std::vector<ImportedBookmarkEntry> bookmarks; 89 std::vector<ImportedFaviconUsage> favicons; 90 91 bookmark_html_reader::ImportBookmarksFile( 92 base::Bind(IsImporterCancelled, base::Unretained(this)), 93 base::Bind(internal::CanImportURL), 94 source_profile.source_path, 95 &bookmarks, 96 &favicons); 97 98 if (!bookmarks.empty() && !cancelled()) { 99 base::string16 first_folder_name = 100 bridge->GetLocalizedString(IDS_BOOKMARK_GROUP); 101 bridge->AddBookmarks(bookmarks, first_folder_name); 102 } 103 if (!favicons.empty()) 104 bridge->SetFavicons(favicons); 105 106 bridge->NotifyItemEnded(importer::FAVORITES); 107 bridge->NotifyEnded(); 108} 109