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