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