1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_IMPORTER_FIREFOX2_IMPORTER_H_ 6 #define CHROME_BROWSER_IMPORTER_FIREFOX2_IMPORTER_H_ 7 #pragma once 8 9 #include <set> 10 11 #include "base/basictypes.h" 12 #include "base/compiler_specific.h" 13 #include "base/file_path.h" 14 #include "base/gtest_prod_util.h" 15 #include "base/string16.h" 16 #include "chrome/browser/importer/importer.h" 17 #include "chrome/browser/importer/profile_writer.h" 18 19 class GURL; 20 class TemplateURL; 21 22 // Importer for Mozilla Firefox 2. 23 class Firefox2Importer : public Importer { 24 public: 25 Firefox2Importer(); 26 27 // Importer: 28 virtual void StartImport(const importer::SourceProfile& source_profile, 29 uint16 items, 30 ImporterBridge* bridge) OVERRIDE; 31 32 // Loads the default bookmarks in the Firefox installed at |firefox_app_path|, 33 // and stores their locations in |urls|. 34 static void LoadDefaultBookmarks(const FilePath& firefox_app_path, 35 std::set<GURL>* urls); 36 37 // Creates a TemplateURL with the |keyword| and |url|. |title| may be empty. 38 // This function transfers ownership of the created TemplateURL to the caller. 39 static TemplateURL* CreateTemplateURL(const string16& title, 40 const string16& keyword, 41 const GURL& url); 42 43 // Imports the bookmarks from the specified file. |template_urls| and 44 // |favicons| may be null, in which case TemplateURLs and favicons are 45 // not parsed. Any bookmarks in |default_urls| are ignored. 46 static void ImportBookmarksFile( 47 const FilePath& file_path, 48 const std::set<GURL>& default_urls, 49 bool import_to_bookmark_bar, 50 const string16& first_folder_name, 51 Importer* importer, 52 std::vector<ProfileWriter::BookmarkEntry>* bookmarks, 53 std::vector<TemplateURL*>* template_urls, 54 std::vector<history::ImportedFaviconUsage>* favicons); 55 56 private: 57 FRIEND_TEST_ALL_PREFIXES(FirefoxImporterTest, Firefox2BookmarkParse); 58 FRIEND_TEST_ALL_PREFIXES(FirefoxImporterTest, Firefox2CookesParse); 59 FRIEND_TEST_ALL_PREFIXES(FirefoxImporterTest, Firefox2BookmarkFileImport); 60 61 virtual ~Firefox2Importer(); 62 63 void ImportBookmarks(); 64 void ImportPasswords(); 65 void ImportHistory(); 66 void ImportSearchEngines(); 67 // Import the user's home page, unless it is set to default home page as 68 // defined in browserconfig.properties. 69 void ImportHomepage(); 70 71 // Fills |files| with the paths to the files containing the search engine 72 // descriptions. 73 void GetSearchEnginesXMLFiles(std::vector<FilePath>* files); 74 75 // Helper methods for parsing bookmark file. 76 // Firefox 2 saves its bookmarks in a html file. We are interested in the 77 // bookmarks and folders, and their hierarchy. A folder starts with a 78 // heading tag, which contains it title. All bookmarks and sub-folders is 79 // following, and bracketed by a <DL> tag: 80 // <DT><H3 PERSONAL_TOOLBAR_FOLDER="true" ...>title</H3> 81 // <DL><p> 82 // ... container ... 83 // </DL><p> 84 // And a bookmark is presented by a <A> tag: 85 // <DT><A HREF="url" SHORTCUTURL="shortcut" ADD_DATE="11213014"...>name</A> 86 // Reference: http://kb.mozillazine.org/Bookmarks.html 87 static bool ParseCharsetFromLine(const std::string& line, 88 std::string* charset); 89 static bool ParseFolderNameFromLine(const std::string& line, 90 const std::string& charset, 91 string16* folder_name, 92 bool* is_toolbar_folder, 93 base::Time* add_date); 94 // See above, this will also put the data: URL of the favicon into *favicon 95 // if there is a favicon given. |post_data| is set for POST base keywords to 96 // the contents of the actual POST (with %s for the search term). 97 static bool ParseBookmarkFromLine(const std::string& line, 98 const std::string& charset, 99 string16* title, 100 GURL* url, 101 GURL* favicon, 102 string16* shortcut, 103 base::Time* add_date, 104 string16* post_data); 105 // Save bookmarks imported from browsers with Firefox2 compatible bookmark 106 // systems such as Epiphany. This bookmark format is the same as that of the 107 // basic Firefox bookmark, but it misses additional properties and uses 108 // lower-case tag: 109 // ...<h1>Bookmarks</h1><dl> 110 // <dt><a href="url">name</a></dt> 111 // <dt><a href="url">name</a></dt> 112 // </dl> 113 static bool ParseMinimumBookmarkFromLine(const std::string& line, 114 const std::string& charset, 115 string16* title, 116 GURL* url); 117 118 // Fetches the given attribute value from the |tag|. Returns true if 119 // successful, and |value| will contain the value. 120 static bool GetAttribute(const std::string& tag, 121 const std::string& attribute, 122 std::string* value); 123 124 // There are some characters in html file will be escaped: 125 // '<', '>', '"', '\', '&' 126 // Un-escapes them if the bookmark name has those characters. 127 static void HTMLUnescape(string16* text); 128 129 // Fills |xml_files| with the file with an xml extension found under |dir|. 130 static void FindXMLFilesInDir(const FilePath& dir, 131 std::vector<FilePath>* xml_files); 132 133 // Given the URL of a page and a favicon data URL, adds an appropriate record 134 // to the given favicon usage vector. Will do nothing if the favicon is not 135 // valid. 136 static void DataURLToFaviconUsage( 137 const GURL& link_url, 138 const GURL& favicon_data, 139 std::vector<history::ImportedFaviconUsage>* favicons); 140 141 FilePath source_path_; 142 FilePath app_path_; 143 // If true, we only parse the bookmarks.html file specified as source_path_. 144 bool parsing_bookmarks_html_file_; 145 146 DISALLOW_COPY_AND_ASSIGN(Firefox2Importer); 147 }; 148 149 #endif // CHROME_BROWSER_IMPORTER_FIREFOX2_IMPORTER_H_ 150