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/bookmarks_file_importer.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/url_constants.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "chrome/utility/importer/bookmark_html_reader.h"
15 #include "components/url_fixer/url_fixer.h"
16 #include "content/public/common/url_constants.h"
20 bool IsImporterCancelled(BookmarksFileImporter* importer) {
21 return importer->cancelled();
28 // Returns true if |url| has a valid scheme that we allow to import. We
29 // filter out the URL with a unsupported scheme.
30 bool CanImportURL(const GURL& url) {
31 // The URL is not valid.
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]))
42 // Check if |url| is about:blank.
43 if (url == GURL(url::kAboutBlankURL))
46 // If |url| starts with chrome:// or about:, check if it's one of the URLs
48 if (url.SchemeIs(content::kChromeUIScheme) ||
49 url.SchemeIs(url::kAboutScheme)) {
50 if (url.host() == chrome::kChromeUIUberHost ||
51 url.host() == chrome::kChromeUIAboutHost)
54 GURL fixed_url(url_fixer::FixupURL(url.spec(), std::string()));
55 for (size_t i = 0; i < chrome::kNumberOfChromeHostURLs; ++i) {
56 if (fixed_url.DomainIs(chrome::kChromeHostURLs[i]))
60 for (int i = 0; i < chrome::kNumberOfChromeDebugURLs; ++i) {
61 if (fixed_url == GURL(chrome::kChromeDebugURLs[i]))
65 // If url has either chrome:// or about: schemes but wasn't found in the
66 // above lists, it means we don't support it, so we don't allow the user
71 // Otherwise, we assume the url has a valid (importable) scheme.
75 } // namespace internal
77 BookmarksFileImporter::BookmarksFileImporter() {}
79 BookmarksFileImporter::~BookmarksFileImporter() {}
81 void BookmarksFileImporter::StartImport(
82 const importer::SourceProfile& source_profile,
84 ImporterBridge* bridge) {
85 // The only thing this importer can import is a bookmarks file, aka
87 DCHECK_EQ(importer::FAVORITES, items);
89 bridge->NotifyStarted();
90 bridge->NotifyItemStarted(importer::FAVORITES);
92 std::vector<ImportedBookmarkEntry> bookmarks;
93 std::vector<ImportedFaviconUsage> favicons;
95 bookmark_html_reader::ImportBookmarksFile(
96 base::Bind(IsImporterCancelled, base::Unretained(this)),
97 base::Bind(internal::CanImportURL),
98 source_profile.source_path,
102 if (!bookmarks.empty() && !cancelled()) {
103 base::string16 first_folder_name =
104 bridge->GetLocalizedString(IDS_BOOKMARK_GROUP);
105 bridge->AddBookmarks(bookmarks, first_folder_name);
107 if (!favicons.empty())
108 bridge->SetFavicons(favicons);
110 bridge->NotifyItemEnded(importer::FAVORITES);
111 bridge->NotifyEnded();