1 // Copyright 2013 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_MEDIA_GALLERIES_FILEAPI_ITUNES_DATA_PROVIDER_H_ 6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_ITUNES_DATA_PROVIDER_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 12 #include "base/basictypes.h" 13 #include "base/callback_forward.h" 14 #include "base/files/file_path.h" 15 #include "base/files/file_path_watcher.h" 16 #include "chrome/browser/media_galleries/fileapi/iapps_data_provider.h" 17 #include "chrome/browser/media_galleries/fileapi/safe_iapps_library_parser.h" 18 19 namespace itunes { 20 21 class TestITunesDataProvider; 22 23 // This class is the holder for iTunes parsed data. Given a path to the iTunes 24 // library XML file it will read it in, parse the data, and provide convenient 25 // methods to access it. When the file changes, it will update the data. 26 // It is not thread safe, but can be run on any thread with IO access. 27 class ITunesDataProvider : public iapps::IAppsDataProvider { 28 public: 29 typedef std::string ArtistName; 30 typedef std::string AlbumName; 31 typedef std::string TrackName; 32 typedef std::map<TrackName, base::FilePath> Album; 33 34 explicit ITunesDataProvider(const base::FilePath& library_path); 35 virtual ~ITunesDataProvider(); 36 37 // Get the platform path for the auto-add directory. 38 virtual const base::FilePath& auto_add_path() const; 39 40 // Returns true if |artist| exists in the library. 41 bool KnownArtist(const ArtistName& artist) const; 42 43 // Returns true if |artist| has an album by the name |album| in the library. 44 bool KnownAlbum(const ArtistName& artist, const AlbumName& album) const; 45 46 // Get the track named (filename basename) |track| in |album| by |artist|. 47 // If no such track exists, an empty FilePath is returned. 48 base::FilePath GetTrackLocation(const ArtistName& artist, 49 const AlbumName& album, 50 const TrackName& track) const; 51 52 // Get the set of artists. 53 std::set<ArtistName> GetArtistNames() const; 54 55 // Get the set of albums for |artist|. 56 std::set<AlbumName> GetAlbumNames(const ArtistName& artist) const; 57 58 // Get the tracks for the |album| by |artist|. 59 Album GetAlbum(const ArtistName& artist, const AlbumName& album) const; 60 61 private: 62 friend class TestITunesDataProvider; 63 64 typedef std::map<AlbumName, Album> Artist; 65 typedef std::map<ArtistName, Artist> Library; 66 67 // Parse the library xml file. 68 virtual void DoParseLibrary(const base::FilePath& library_path, 69 const ReadyCallback& ready_callback) OVERRIDE; 70 71 // Called when the utility process finishes parsing the library XML file. 72 void OnLibraryParsed(const ReadyCallback& ready_callback, 73 bool result, 74 const parser::Library& library); 75 76 // Path to the auto-add directory. 77 const base::FilePath auto_add_path_; 78 79 // The parsed and uniquified data. 80 Library library_; 81 82 scoped_refptr<iapps::SafeIAppsLibraryParser> xml_parser_; 83 84 // Hides parent class member, but it is private, and there's no way to get a 85 // WeakPtr<Derived> from a WeakPtr<Base> without using SupportsWeakPtr. 86 base::WeakPtrFactory<ITunesDataProvider> weak_factory_; 87 88 DISALLOW_COPY_AND_ASSIGN(ITunesDataProvider); 89 }; 90 91 } // namespace itunes 92 93 #endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_ITUNES_DATA_PROVIDER_H_ 94