summaryrefslogtreecommitdiffstats
path: root/chrome/browser/importer/firefox2_importer.h
blob: 62fcac31aba26c976e6ca602e7233f28efd96516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_IMPORTER_FIREFOX2_IMPORTER_H_
#define CHROME_BROWSER_IMPORTER_FIREFOX2_IMPORTER_H_
#pragma once

#include <set>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/string16.h"
#include "chrome/browser/importer/importer.h"
#include "chrome/browser/importer/profile_writer.h"

class GURL;
class TemplateURL;

// Importer for Mozilla Firefox 2.
class Firefox2Importer : public Importer {
 public:
  Firefox2Importer();

  // Importer:
  virtual void StartImport(const importer::SourceProfile& source_profile,
                           uint16 items,
                           ImporterBridge* bridge) OVERRIDE;

  // Loads the default bookmarks in the Firefox installed at |firefox_app_path|,
  // and stores their locations in |urls|.
  static void LoadDefaultBookmarks(const FilePath& firefox_app_path,
                                   std::set<GURL>* urls);

  // Creates a TemplateURL with the |keyword| and |url|. |title| may be empty.
  // This function transfers ownership of the created TemplateURL to the caller.
  static TemplateURL* CreateTemplateURL(const string16& title,
                                        const string16& keyword,
                                        const GURL& url);

  // Imports the bookmarks from the specified file. |template_urls| and
  // |favicons| may be null, in which case TemplateURLs and favicons are
  // not parsed. Any bookmarks in |default_urls| are ignored.
  static void ImportBookmarksFile(
      const FilePath& file_path,
      const std::set<GURL>& default_urls,
      Importer* importer,
      std::vector<ProfileWriter::BookmarkEntry>* bookmarks,
      std::vector<TemplateURL*>* template_urls,
      std::vector<history::ImportedFaviconUsage>* favicons);

 private:
  FRIEND_TEST_ALL_PREFIXES(FirefoxImporterTest, Firefox2BookmarkParse);
  FRIEND_TEST_ALL_PREFIXES(FirefoxImporterTest, Firefox2CookesParse);
  FRIEND_TEST_ALL_PREFIXES(FirefoxImporterTest, Firefox2BookmarkFileImport);

  virtual ~Firefox2Importer();

  void ImportBookmarks();
  void ImportPasswords();
  void ImportHistory();
  void ImportSearchEngines();
  // Import the user's home page, unless it is set to default home page as
  // defined in browserconfig.properties.
  void ImportHomepage();

  // Fills |files| with the paths to the files containing the search engine
  // descriptions.
  void GetSearchEnginesXMLFiles(std::vector<FilePath>* files);

  // Helper methods for parsing bookmark file.
  // Firefox 2 saves its bookmarks in a html file. We are interested in the
  // bookmarks and folders, and their hierarchy. A folder starts with a
  // heading tag, which contains it title. All bookmarks and sub-folders is
  // following, and bracketed by a <DL> tag:
  //   <DT><H3 PERSONAL_TOOLBAR_FOLDER="true" ...>title</H3>
  //   <DL><p>
  //      ... container ...
  //   </DL><p>
  // And a bookmark is presented by a <A> tag:
  //   <DT><A HREF="url" SHORTCUTURL="shortcut" ADD_DATE="11213014"...>name</A>
  // Reference: http://kb.mozillazine.org/Bookmarks.html
  static bool ParseCharsetFromLine(const std::string& line,
                                   std::string* charset);
  static bool ParseFolderNameFromLine(const std::string& line,
                                      const std::string& charset,
                                      string16* folder_name,
                                      bool* is_toolbar_folder,
                                      base::Time* add_date);
  // See above, this will also put the data: URL of the favicon into *favicon
  // if there is a favicon given.  |post_data| is set for POST base keywords to
  // the contents of the actual POST (with %s for the search term).
  static bool ParseBookmarkFromLine(const std::string& line,
                                    const std::string& charset,
                                    string16* title,
                                    GURL* url,
                                    GURL* favicon,
                                    string16* shortcut,
                                    base::Time* add_date,
                                    string16* post_data);
  // Save bookmarks imported from browsers with Firefox2 compatible bookmark
  // systems such as Epiphany. This bookmark format is the same as that of the
  // basic Firefox bookmark, but it misses additional properties and uses
  // lower-case tag:
  //   ...<h1>Bookmarks</h1><dl>
  //   <dt><a href="url">name</a></dt>
  //   <dt><a href="url">name</a></dt>
  //   </dl>
  static bool ParseMinimumBookmarkFromLine(const std::string& line,
                                           const std::string& charset,
                                           string16* title,
                                           GURL* url);

  // Fetches the given attribute value from the |tag|. Returns true if
  // successful, and |value| will contain the value.
  static bool GetAttribute(const std::string& tag,
                           const std::string& attribute,
                           std::string* value);

  // There are some characters in html file will be escaped:
  //   '<', '>', '"', '\', '&'
  // Un-escapes them if the bookmark name has those characters.
  static void HTMLUnescape(string16* text);

  // Fills |xml_files| with the file with an xml extension found under |dir|.
  static void FindXMLFilesInDir(const FilePath& dir,
                                std::vector<FilePath>* xml_files);

  // Given the URL of a page and a favicon data URL, adds an appropriate record
  // to the given favicon usage vector. Will do nothing if the favicon is not
  // valid.
  static void DataURLToFaviconUsage(
      const GURL& link_url,
      const GURL& favicon_data,
      std::vector<history::ImportedFaviconUsage>* favicons);

  FilePath source_path_;
  FilePath app_path_;
  // If true, we only parse the bookmarks.html file specified as source_path_.
  bool parsing_bookmarks_html_file_;

  DISALLOW_COPY_AND_ASSIGN(Firefox2Importer);
};

#endif  // CHROME_BROWSER_IMPORTER_FIREFOX2_IMPORTER_H_