Home | History | Annotate | Download | only in importer
      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
     23 class NSDictionary;
     24 class NSString;
     25 #endif
     26 
     27 class GURL;
     28 struct ImportedBookmarkEntry;
     29 struct ImportedFaviconUsage;
     30 
     31 namespace sql {
     32 class Connection;
     33 }
     34 
     35 // Importer for Safari on OS X.
     36 class 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,
     50                            BookmarkImportWithEmptyBookmarksMenu);
     51   FRIEND_TEST_ALL_PREFIXES(SafariImporterTest, FaviconImport);
     52   FRIEND_TEST_ALL_PREFIXES(SafariImporterTest, HistoryImport);
     53 
     54   virtual ~SafariImporter();
     55 
     56   // Multiple URLs can share the same favicon; this is a map
     57   // of URLs -> IconIDs that we load as a temporary step before
     58   // actually loading the icons.
     59   typedef std::map<int64, std::set<GURL> > FaviconMap;
     60 
     61   void ImportBookmarks();
     62   void ImportPasswords();
     63   void ImportHistory();
     64 
     65   // Parse Safari's stored bookmarks.
     66   void ParseBookmarks(const base::string16& toolbar_name,
     67                       std::vector<ImportedBookmarkEntry>* bookmarks);
     68 
     69   // Function to recursively read Bookmarks out of Safari plist.
     70   // |bookmark_folder| The dictionary containing a folder to parse.
     71   // |parent_path_elements| Path elements up to this point.
     72   // |is_in_toolbar| Is this folder in the toolbar.
     73   // |out_bookmarks| BookMark element array to write into.
     74   void RecursiveReadBookmarksFolder(
     75       NSDictionary* bookmark_folder,
     76       const std::vector<base::string16>& parent_path_elements,
     77       bool is_in_toolbar,
     78       const base::string16& toolbar_name,
     79       std::vector<ImportedBookmarkEntry>* out_bookmarks);
     80 
     81   // Converts history time stored by Safari as a double serialized as a string,
     82   // to seconds-since-UNIX-Ephoch-format used by Chrome.
     83   double HistoryTimeToEpochTime(NSString* history_time);
     84 
     85   // Parses Safari's history and loads it into the input array.
     86   void ParseHistoryItems(std::vector<ImporterURLRow>* history_items);
     87 
     88   // Opens the favicon database file.
     89   bool OpenDatabase(sql::Connection* db);
     90 
     91   // Loads the urls associated with the favicons into favicon_map;
     92   void ImportFaviconURLs(sql::Connection* db, FaviconMap* favicon_map);
     93 
     94   // Loads and reencodes the individual favicons.
     95   void LoadFaviconData(sql::Connection* db,
     96                        const FaviconMap& favicon_map,
     97                        std::vector<ImportedFaviconUsage>* favicons);
     98 
     99   base::FilePath library_dir_;
    100 
    101   DISALLOW_COPY_AND_ASSIGN(SafariImporter);
    102 };
    103 
    104 #endif  // CHROME_UTILITY_IMPORTER_SAFARI_IMPORTER_H_
    105