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/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"
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
<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
,
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();