Fix broken path in extensions/common/PRESUBMIT.py
[chromium-blink-merge.git] / chrome / utility / importer / bookmarks_file_importer.cc
blob1ae2fb1afc4de40233f1a68d75c577b3d235195a
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"
7 #include "base/bind.h"
8 #include "chrome/common/importer/imported_bookmark_entry.h"
9 #include "chrome/common/importer/importer_bridge.h"
10 #include "chrome/common/importer/importer_data_types.h"
11 #include "chrome/common/url_constants.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "chrome/utility/importer/bookmark_html_reader.h"
14 #include "components/favicon_base/favicon_usage_data.h"
15 #include "components/url_fixer/url_fixer.h"
16 #include "content/public/common/url_constants.h"
18 namespace {
20 bool IsImporterCancelled(BookmarksFileImporter* importer) {
21 return importer->cancelled();
24 } // namespace
26 namespace internal {
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.
32 if (!url.is_valid())
33 return false;
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;
42 // Check if |url| is about:blank.
43 if (url == GURL(url::kAboutBlankURL))
44 return true;
46 // If |url| starts with chrome:// or about:, check if it's one of the URLs
47 // that we support.
48 if (url.SchemeIs(content::kChromeUIScheme) ||
49 url.SchemeIs(url::kAboutScheme)) {
50 if (url.host() == chrome::kChromeUIUberHost ||
51 url.host() == chrome::kChromeUIAboutHost)
52 return true;
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]))
57 return true;
60 for (int i = 0; i < chrome::kNumberOfChromeDebugURLs; ++i) {
61 if (fixed_url == GURL(chrome::kChromeDebugURLs[i]))
62 return true;
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
67 // to import it.
68 return false;
71 // Otherwise, we assume the url has a valid (importable) scheme.
72 return true;
75 } // namespace internal
77 BookmarksFileImporter::BookmarksFileImporter() {}
79 BookmarksFileImporter::~BookmarksFileImporter() {}
81 void BookmarksFileImporter::StartImport(
82 const importer::SourceProfile& source_profile,
83 uint16 items,
84 ImporterBridge* bridge) {
85 // The only thing this importer can import is a bookmarks file, aka
86 // "favorites".
87 DCHECK_EQ(importer::FAVORITES, items);
89 bridge->NotifyStarted();
90 bridge->NotifyItemStarted(importer::FAVORITES);
92 std::vector<ImportedBookmarkEntry> bookmarks;
93 std::vector<importer::SearchEngineInfo> search_engines;
94 favicon_base::FaviconUsageDataList favicons;
96 bookmark_html_reader::ImportBookmarksFile(
97 base::Bind(IsImporterCancelled, base::Unretained(this)),
98 base::Bind(internal::CanImportURL),
99 source_profile.source_path,
100 &bookmarks,
101 &search_engines,
102 &favicons);
104 if (!bookmarks.empty() && !cancelled()) {
105 base::string16 first_folder_name =
106 bridge->GetLocalizedString(IDS_BOOKMARK_GROUP);
107 bridge->AddBookmarks(bookmarks, first_folder_name);
109 if (!search_engines.empty())
110 bridge->SetKeywords(search_engines, false);
111 if (!favicons.empty())
112 bridge->SetFavicons(favicons);
114 bridge->NotifyItemEnded(importer::FAVORITES);
115 bridge->NotifyEnded();