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
3// found in the LICENSE file.
4
5#ifndef CHROME_UTILITY_IMPORTER_SAFARI_IMPORTER_H_
6#define CHROME_UTILITY_IMPORTER_SAFARI_IMPORTER_H_
7
8#include <map>
9#include <set>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/compiler_specific.h"
14#include "base/files/file_path.h"
15#include "base/gtest_prod_util.h"
16#include "chrome/common/importer/importer_url_row.h"
17#include "chrome/utility/importer/importer.h"
18
19#if __OBJC__
20@class NSDictionary;
21@class NSString;
22#else
23class NSDictionary;
24class NSString;
25#endif
26
27class GURL;
28struct ImportedBookmarkEntry;
29struct ImportedFaviconUsage;
30
31namespace sql {
32class Connection;
33}
34
35// Importer for Safari on OS X.
36class SafariImporter : public Importer {
37 public:
38  // |library_dir| is the full path to the ~/Library directory,
39  // We pass it in as a parameter for testing purposes.
40  explicit SafariImporter(const base::FilePath& library_dir);
41
42  // Importer:
43  virtual void StartImport(const importer::SourceProfile& source_profile,
44                           uint16 items,
45                           ImporterBridge* bridge) OVERRIDE;
46
47 private:
48  FRIEND_TEST_ALL_PREFIXES(SafariImporterTest, BookmarkImport);
49  FRIEND_TEST_ALL_PREFIXES(SafariImporterTest, FaviconImport);
50  FRIEND_TEST_ALL_PREFIXES(SafariImporterTest, HistoryImport);
51
52  virtual ~SafariImporter();
53
54  // Multiple URLs can share the same favicon; this is a map
55  // of URLs -> IconIDs that we load as a temporary step before
56  // actually loading the icons.
57  typedef std::map<int64, std::set<GURL> > FaviconMap;
58
59  void ImportBookmarks();
60  void ImportPasswords();
61  void ImportHistory();
62
63  // Parse Safari's stored bookmarks.
64  void ParseBookmarks(const string16& toolbar_name,
65                      std::vector<ImportedBookmarkEntry>* bookmarks);
66
67  // Function to recursively read Bookmarks out of Safari plist.
68  // |bookmark_folder| The dictionary containing a folder to parse.
69  // |parent_path_elements| Path elements up to this point.
70  // |is_in_toolbar| Is this folder in the toolbar.
71  // |out_bookmarks| BookMark element array to write into.
72  void RecursiveReadBookmarksFolder(
73      NSDictionary* bookmark_folder,
74      const std::vector<string16>& parent_path_elements,
75      bool is_in_toolbar,
76      const string16& toolbar_name,
77      std::vector<ImportedBookmarkEntry>* out_bookmarks);
78
79  // Converts history time stored by Safari as a double serialized as a string,
80  // to seconds-since-UNIX-Ephoch-format used by Chrome.
81  double HistoryTimeToEpochTime(NSString* history_time);
82
83  // Parses Safari's history and loads it into the input array.
84  void ParseHistoryItems(std::vector<ImporterURLRow>* history_items);
85
86  // Opens the favicon database file.
87  bool OpenDatabase(sql::Connection* db);
88
89  // Loads the urls associated with the favicons into favicon_map;
90  void ImportFaviconURLs(sql::Connection* db, FaviconMap* favicon_map);
91
92  // Loads and reencodes the individual favicons.
93  void LoadFaviconData(sql::Connection* db,
94                       const FaviconMap& favicon_map,
95                       std::vector<ImportedFaviconUsage>* favicons);
96
97  base::FilePath library_dir_;
98
99  DISALLOW_COPY_AND_ASSIGN(SafariImporter);
100};
101
102#endif  // CHROME_UTILITY_IMPORTER_SAFARI_IMPORTER_H_
103