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