Home | History | Annotate | Download | only in fileapi
      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/safe_itunes_library_parser.h"
     17 
     18 namespace itunes {
     19 
     20 class TestITunesDataProvider;
     21 
     22 // This class is the holder for iTunes parsed data. Given a path to the iTunes
     23 // library XML file it will read it in, parse the data, and provide convenient
     24 // methods to access it.  When the file changes, it will update the data.
     25 // It is not thread safe, but can be run on any thread with IO access.
     26 class ITunesDataProvider {
     27  public:
     28   typedef std::string ArtistName;
     29   typedef std::string AlbumName;
     30   typedef std::string TrackName;
     31   typedef std::map<TrackName, base::FilePath> Album;
     32   typedef base::Callback<void(bool)> ReadyCallback;
     33 
     34   explicit ITunesDataProvider(const base::FilePath& library_path);
     35   virtual ~ITunesDataProvider();
     36 
     37   // Ask the data provider to refresh the data if necessary. |ready_callback|
     38   // will be called with the result; false if unable to parse the XML file.
     39   virtual void RefreshData(const ReadyCallback& ready_callback);
     40 
     41   // Get the platform path for the library XML file.
     42   const base::FilePath& library_path() const;
     43 
     44   // Get the platform path for the auto-add directory.
     45   virtual const base::FilePath& auto_add_path() const;
     46 
     47   // Returns true if |artist| exists in the library.
     48   bool KnownArtist(const ArtistName& artist) const;
     49 
     50   // Returns true if |artist| has an album by the name |album| in the library.
     51   bool KnownAlbum(const ArtistName& artist, const AlbumName& album) const;
     52 
     53   // Get the track named (filename basename) |track| in |album| by |artist|.
     54   // If no such track exists, an empty FilePath is returned.
     55   base::FilePath GetTrackLocation(const ArtistName& artist,
     56                                   const AlbumName& album,
     57                                   const TrackName& track) const;
     58 
     59   // Get the set of artists.
     60   std::set<ArtistName> GetArtistNames() const;
     61 
     62   // Get the set of albums for |artist|.
     63   std::set<AlbumName> GetAlbumNames(const ArtistName& artist) const;
     64 
     65   // Get the tracks for the |album| by |artist|.
     66   Album GetAlbum(const ArtistName& artist, const AlbumName& album) const;
     67 
     68  private:
     69   friend class TestITunesDataProvider;
     70 
     71   typedef std::map<AlbumName, Album> Artist;
     72   typedef std::map<ArtistName, Artist> Library;
     73 
     74   // These are hacks to work around http://crbug.com/165590. Otherwise a
     75   // WeakPtrFactory would be the obvious answer here.
     76   // static so they can call their real counterparts.
     77   // TODO(vandebo) Remove these when the bug is fixed.
     78   static void OnLibraryWatchStartedCallback(
     79       scoped_ptr<base::FilePathWatcher> library_watcher);
     80   static void OnLibraryChangedCallback(const base::FilePath& path, bool error);
     81   static void OnLibraryParsedCallback(const ReadyCallback& ready_callback,
     82                                       bool result,
     83                                       const parser::Library& library);
     84 
     85   // Called when the FilePathWatcher for |library_path_| has tried to add an
     86   // watch.
     87   void OnLibraryWatchStarted(scoped_ptr<base::FilePathWatcher> library_watcher);
     88 
     89   // Called when |library_path_| has changed. Virtual for testing.
     90   virtual void OnLibraryChanged(const base::FilePath& path, bool error);
     91 
     92   // Called when the utility process finishes parsing the library XML file.
     93   void OnLibraryParsed(const ReadyCallback& ready_callback,
     94                        bool result,
     95                        const parser::Library& library);
     96 
     97   // Path to the library XML file.
     98   const base::FilePath library_path_;
     99 
    100   // Path to the auto-add directory.
    101   const base::FilePath auto_add_path_;
    102 
    103   // The parsed and uniquified data.
    104   Library library_;
    105 
    106   // True if the data needs to be refreshed from disk.
    107   bool needs_refresh_;
    108 
    109   // True if |library_| contain valid data.  False at construction and if
    110   // reading or parsing the XML file fails.
    111   bool is_valid_;
    112 
    113   // A watcher on the library xml file.
    114   scoped_ptr<base::FilePathWatcher> library_watcher_;
    115 
    116   scoped_refptr<SafeITunesLibraryParser> xml_parser_;
    117 
    118   DISALLOW_COPY_AND_ASSIGN(ITunesDataProvider);
    119 };
    120 
    121 }  // namespace itunes
    122 
    123 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_ITUNES_DATA_PROVIDER_H_
    124