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_IAPPS_DATA_PROVIDER_H_
      6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_IAPPS_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 "base/memory/weak_ptr.h"
     17 
     18 namespace iapps {
     19 
     20 // This class is the holder for iTunes parsed data. Given a path to the iTunes
     21 // library XML file it will read it in, parse the data, and provide convenient
     22 // methods to access it.  When the file changes, it will update the data.
     23 // It is not thread safe, but can be run on any thread with IO access.
     24 class IAppsDataProvider {
     25  public:
     26   typedef base::Callback<void(bool)> ReadyCallback;
     27 
     28   explicit IAppsDataProvider(const base::FilePath& library_path);
     29   virtual ~IAppsDataProvider();
     30 
     31   // Ask the data provider to refresh the data if necessary. |ready_callback|
     32   // will be called with the result; false if unable to parse the XML file.
     33   virtual void RefreshData(const ReadyCallback& ready_callback);
     34 
     35   // Get the platform path for the library XML file.
     36   const base::FilePath& library_path() const;
     37 
     38  protected:
     39   bool valid() const;
     40 
     41   // Subclass should set this to true if the last parse succeeded.
     42   void set_valid(bool valid);
     43 
     44   // Signal to subclass to parse the |library_path|. The |ready_callback|
     45   // should be called when parsing has finished.
     46   virtual void DoParseLibrary(const base::FilePath& library_path,
     47                               const ReadyCallback& ready_callback) = 0;
     48 
     49   // Called when |library_path_| has changed. Virtual for testing.
     50   virtual void OnLibraryChanged(const base::FilePath& path, bool error);
     51 
     52  private:
     53   // Called when the FilePathWatcher for |library_path_| has tried to add an
     54   // watch.
     55   void OnLibraryWatchStarted(scoped_ptr<base::FilePathWatcher> library_watcher);
     56 
     57   // Path to the library XML file.
     58   const base::FilePath library_path_;
     59 
     60   // True if the data needs to be refreshed from disk.
     61   bool needs_refresh_;
     62 
     63   // True if |library_| contains valid data. False at construction and if
     64   // reading or parsing the XML file fails.
     65   bool is_valid_;
     66 
     67   // A watcher on the library xml file.
     68   scoped_ptr<base::FilePathWatcher> library_watcher_;
     69 
     70   base::WeakPtrFactory<IAppsDataProvider> weak_factory_;
     71 
     72   DISALLOW_COPY_AND_ASSIGN(IAppsDataProvider);
     73 };
     74 
     75 }  // namespace iapps
     76 
     77 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_IAPPS_DATA_PROVIDER_H_
     78