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

Side by Side Diff: chrome/browser/importer/firefox2_importer.cc

Issue 14575004: Extract BookmarksFileImporter from Firefox2Importer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: test cleanup Created 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/browser/importer/firefox2_importer.h" 5 #include "chrome/browser/importer/firefox2_importer.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/i18n/icu_string_conversions.h" 12 #include "base/i18n/icu_string_conversions.h"
13 #include "base/message_loop.h" 13 #include "base/message_loop.h"
14 #include "base/path_service.h" 14 #include "base/path_service.h"
15 #include "base/stl_util.h" 15 #include "base/stl_util.h"
16 #include "base/string_util.h" 16 #include "base/string_util.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h" 18 #include "base/strings/string_split.h"
19 #include "base/utf_string_conversions.h" 19 #include "base/utf_string_conversions.h"
20 #include "chrome/browser/history/history_types.h" 20 #include "chrome/browser/bookmarks/imported_bookmark_entry.h"
21 #include "chrome/browser/favicon/favicon_util.h"
22 #include "chrome/browser/favicon/imported_favicon_usage.h"
21 #include "chrome/browser/importer/firefox_importer_utils.h" 23 #include "chrome/browser/importer/firefox_importer_utils.h"
22 #include "chrome/browser/importer/importer_bridge.h" 24 #include "chrome/browser/importer/importer_bridge.h"
23 #include "chrome/browser/importer/importer_util.h"
24 #include "chrome/browser/importer/mork_reader.h" 25 #include "chrome/browser/importer/mork_reader.h"
25 #include "chrome/browser/importer/nss_decryptor.h" 26 #include "chrome/browser/importer/nss_decryptor.h"
26 #include "chrome/browser/search_engines/template_url.h" 27 #include "chrome/browser/search_engines/template_url.h"
27 #include "chrome/common/time_format.h" 28 #include "chrome/common/time_format.h"
28 #include "chrome/common/url_constants.h" 29 #include "chrome/common/url_constants.h"
29 #include "content/public/common/password_form.h" 30 #include "content/public/common/password_form.h"
30 #include "googleurl/src/gurl.h" 31 #include "googleurl/src/gurl.h"
31 #include "grit/generated_resources.h" 32 #include "grit/generated_resources.h"
32 #include "net/base/data_url.h" 33 #include "net/base/data_url.h"
33 34
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 data.SetKeyword(keyword); 142 data.SetKeyword(keyword);
142 data.SetURL(TemplateURLRef::DisplayURLToURLRef(UTF8ToUTF16(url.spec()))); 143 data.SetURL(TemplateURLRef::DisplayURLToURLRef(UTF8ToUTF16(url.spec())));
143 return new TemplateURL(NULL, data); 144 return new TemplateURL(NULL, data);
144 } 145 }
145 146
146 // static 147 // static
147 void Firefox2Importer::ImportBookmarksFile( 148 void Firefox2Importer::ImportBookmarksFile(
148 const base::FilePath& file_path, 149 const base::FilePath& file_path,
149 const std::set<GURL>& default_urls, 150 const std::set<GURL>& default_urls,
150 Importer* importer, 151 Importer* importer,
151 std::vector<ProfileWriter::BookmarkEntry>* bookmarks, 152 std::vector<ImportedBookmarkEntry>* bookmarks,
152 std::vector<TemplateURL*>* template_urls, 153 std::vector<TemplateURL*>* template_urls,
153 std::vector<history::ImportedFaviconUsage>* favicons) { 154 std::vector<ImportedFaviconUsage>* favicons) {
154 std::string content; 155 std::string content;
155 file_util::ReadFileToString(file_path, &content); 156 file_util::ReadFileToString(file_path, &content);
156 std::vector<std::string> lines; 157 std::vector<std::string> lines;
157 base::SplitString(content, '\n', &lines); 158 base::SplitString(content, '\n', &lines);
158 159
159 string16 last_folder; 160 string16 last_folder;
160 bool last_folder_on_toolbar = false; 161 bool last_folder_on_toolbar = false;
161 bool last_folder_is_empty = true; 162 bool last_folder_is_empty = true;
162 bool has_subfolder = false; 163 bool has_subfolder = false;
163 base::Time last_folder_add_date; 164 base::Time last_folder_add_date;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 199
199 if (is_bookmark && 200 if (is_bookmark &&
200 post_data.empty() && 201 post_data.empty() &&
201 CanImportURL(GURL(url)) && 202 CanImportURL(GURL(url)) &&
202 default_urls.find(url) == default_urls.end()) { 203 default_urls.find(url) == default_urls.end()) {
203 if (toolbar_folder > path.size() && !path.empty()) { 204 if (toolbar_folder > path.size() && !path.empty()) {
204 NOTREACHED(); // error in parsing. 205 NOTREACHED(); // error in parsing.
205 break; 206 break;
206 } 207 }
207 208
208 ProfileWriter::BookmarkEntry entry; 209 ImportedBookmarkEntry entry;
209 entry.creation_time = add_date; 210 entry.creation_time = add_date;
210 entry.url = url; 211 entry.url = url;
211 entry.title = title; 212 entry.title = title;
212 213
213 if (toolbar_folder) { 214 if (toolbar_folder) {
214 // The toolbar folder should be at the top level. 215 // The toolbar folder should be at the top level.
215 entry.in_toolbar = true; 216 entry.in_toolbar = true;
216 entry.path.assign(path.begin() + toolbar_folder - 1, path.end()); 217 entry.path.assign(path.begin() + toolbar_folder - 1, path.end());
217 } else { 218 } else {
218 // Add this bookmark to the list of |bookmarks|. 219 // Add this bookmark to the list of |bookmarks|.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 last_folder_is_empty = true; 255 last_folder_is_empty = true;
255 } else if (StartsWithASCII(line, "</DL>", false)) { 256 } else if (StartsWithASCII(line, "</DL>", false)) {
256 if (path.empty()) 257 if (path.empty())
257 break; // Mismatch <DL>. 258 break; // Mismatch <DL>.
258 259
259 string16 folder_title = path.back(); 260 string16 folder_title = path.back();
260 path.pop_back(); 261 path.pop_back();
261 262
262 if (last_folder_is_empty) { 263 if (last_folder_is_empty) {
263 // Empty folder should be added explicitly. 264 // Empty folder should be added explicitly.
264 ProfileWriter::BookmarkEntry entry; 265 ImportedBookmarkEntry entry;
265 entry.is_folder = true; 266 entry.is_folder = true;
266 entry.creation_time = last_folder_add_date; 267 entry.creation_time = last_folder_add_date;
267 entry.title = folder_title; 268 entry.title = folder_title;
268 if (toolbar_folder) { 269 if (toolbar_folder) {
269 // The toolbar folder should be at the top level. 270 // The toolbar folder should be at the top level.
270 // Make sure we don't add the toolbar folder itself if it is empty. 271 // Make sure we don't add the toolbar folder itself if it is empty.
271 if (toolbar_folder <= path.size()) { 272 if (toolbar_folder <= path.size()) {
272 entry.in_toolbar = true; 273 entry.in_toolbar = true;
273 entry.path.assign(path.begin() + toolbar_folder - 1, path.end()); 274 entry.path.assign(path.begin() + toolbar_folder - 1, path.end());
274 bookmarks->push_back(entry); 275 bookmarks->push_back(entry);
(...skipping 14 matching lines...) Expand all
289 } 290 }
290 } 291 }
291 292
292 void Firefox2Importer::ImportBookmarks() { 293 void Firefox2Importer::ImportBookmarks() {
293 // Load the default bookmarks. 294 // Load the default bookmarks.
294 std::set<GURL> default_urls; 295 std::set<GURL> default_urls;
295 if (!parsing_bookmarks_html_file_) 296 if (!parsing_bookmarks_html_file_)
296 LoadDefaultBookmarks(app_path_, &default_urls); 297 LoadDefaultBookmarks(app_path_, &default_urls);
297 298
298 // Parse the bookmarks.html file. 299 // Parse the bookmarks.html file.
299 std::vector<ProfileWriter::BookmarkEntry> bookmarks, toolbar_bookmarks; 300 std::vector<ImportedBookmarkEntry> bookmarks, toolbar_bookmarks;
300 std::vector<TemplateURL*> template_urls; 301 std::vector<TemplateURL*> template_urls;
301 std::vector<history::ImportedFaviconUsage> favicons; 302 std::vector<ImportedFaviconUsage> favicons;
302 base::FilePath file = source_path_; 303 base::FilePath file = source_path_;
303 if (!parsing_bookmarks_html_file_) 304 if (!parsing_bookmarks_html_file_)
304 file = file.AppendASCII("bookmarks.html"); 305 file = file.AppendASCII("bookmarks.html");
305 306
306 ImportBookmarksFile(file, default_urls, this, &bookmarks, &template_urls, 307 ImportBookmarksFile(file, default_urls, this, &bookmarks, &template_urls,
307 &favicons); 308 &favicons);
308 309
309 // Write data into profile. 310 // Write data into profile.
310 if (!bookmarks.empty() && !cancelled()) { 311 if (!bookmarks.empty() && !cancelled()) {
311 string16 first_folder_name = bridge_->GetLocalizedString( 312 string16 first_folder_name = bridge_->GetLocalizedString(
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 while (!file.empty()) { 632 while (!file.empty()) {
632 xml_files->push_back(file); 633 xml_files->push_back(file);
633 file = file_enum.Next(); 634 file = file_enum.Next();
634 } 635 }
635 } 636 }
636 637
637 // static 638 // static
638 void Firefox2Importer::DataURLToFaviconUsage( 639 void Firefox2Importer::DataURLToFaviconUsage(
639 const GURL& link_url, 640 const GURL& link_url,
640 const GURL& favicon_data, 641 const GURL& favicon_data,
641 std::vector<history::ImportedFaviconUsage>* favicons) { 642 std::vector<ImportedFaviconUsage>* favicons) {
642 if (!link_url.is_valid() || !favicon_data.is_valid() || 643 if (!link_url.is_valid() || !favicon_data.is_valid() ||
643 !favicon_data.SchemeIs(chrome::kDataScheme)) 644 !favicon_data.SchemeIs(chrome::kDataScheme))
644 return; 645 return;
645 646
646 // Parse the data URL. 647 // Parse the data URL.
647 std::string mime_type, char_set, data; 648 std::string mime_type, char_set, data;
648 if (!net::DataURL::Parse(favicon_data, &mime_type, &char_set, &data) || 649 if (!net::DataURL::Parse(favicon_data, &mime_type, &char_set, &data) ||
649 data.empty()) 650 data.empty())
650 return; 651 return;
651 652
652 history::ImportedFaviconUsage usage; 653 ImportedFaviconUsage usage;
653 if (!importer::ReencodeFavicon( 654 if (!FaviconUtil::ReencodeFavicon(
654 reinterpret_cast<const unsigned char*>(&data[0]), 655 reinterpret_cast<const unsigned char*>(&data[0]),
655 data.size(), &usage.png_data)) 656 data.size(), &usage.png_data))
656 return; // Unable to decode. 657 return; // Unable to decode.
657 658
658 // We need to make up a URL for the favicon. We use a version of the page's 659 // We need to make up a URL for the favicon. We use a version of the page's
659 // URL so that we can be sure it will not collide. 660 // URL so that we can be sure it will not collide.
660 usage.favicon_url = GURL(std::string("made-up-favicon:") + link_url.spec()); 661 usage.favicon_url = GURL(std::string("made-up-favicon:") + link_url.spec());
661 662
662 // We only have one URL per favicon for Firefox 2 bookmarks. 663 // We only have one URL per favicon for Firefox 2 bookmarks.
663 usage.urls.insert(link_url); 664 usage.urls.insert(link_url);
664 665
665 favicons->push_back(usage); 666 favicons->push_back(usage);
666 } 667 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698