- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / fileapi / picasa_data_provider.h
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_PICASA_DATA_PROVIDER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_PICASA_DATA_PROVIDER_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
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/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/time/time.h"
20 #include "chrome/common/media_galleries/picasa_types.h"
21
22 namespace picasa {
23
24 class SafePicasaAlbumTableReader;
25 class SafePicasaAlbumsIndexer;
26
27 // Created and owned by ImportedMediaGalleryRegistryTaskRunnerValues
28 class PicasaDataProvider {
29  public:
30   typedef base::Callback<void(bool)> ReadyCallback;
31
32   enum DataType {
33     LIST_OF_ALBUMS_AND_FOLDERS_DATA,
34     ALBUMS_IMAGES_DATA
35   };
36
37   explicit PicasaDataProvider(const base::FilePath& database_path);
38   virtual ~PicasaDataProvider();
39
40   // Ask the data provider to refresh the data if necessary. |ready_callback|
41   // will be called when the data is up to date.
42   void RefreshData(DataType needed_data, const ReadyCallback& ready_callback);
43
44   // These methods return scoped_ptrs because we want to return a copy that
45   // will not change to the caller.
46   scoped_ptr<AlbumMap> GetAlbums();
47   scoped_ptr<AlbumMap> GetFolders();
48   // |error| must be non-NULL.
49   scoped_ptr<AlbumImages> FindAlbumImages(const std::string& key,
50                                           base::PlatformFileError* error);
51
52  protected:
53   // Notifies data provider that any currently cached data is stale.
54   virtual void InvalidateData();
55
56  private:
57   enum State {
58     STALE_DATA_STATE,
59     INVALID_DATA_STATE,
60     LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE,
61     ALBUMS_IMAGES_FRESH_STATE
62   };
63
64   friend class PicasaFileUtilTest;
65   friend class TestPicasaDataProvider;
66
67   // Called when the FilePathWatcher for Picasa's temp directory has started.
68   virtual void OnTempDirWatchStarted(
69       scoped_ptr<base::FilePathWatcher> temp_dir_watcher);
70
71   // Called when Picasa's temp directory has changed. Virtual for testing.
72   virtual void OnTempDirChanged(const base::FilePath& temp_dir_path,
73                                 bool error);
74
75   // Kicks off utility processes needed to fulfill any pending callbacks.
76   void DoRefreshIfNecessary();
77
78   void OnAlbumTableReaderDone(scoped_refptr<SafePicasaAlbumTableReader> reader,
79                               bool parse_success,
80                               const std::vector<AlbumInfo>& albums,
81                               const std::vector<AlbumInfo>& folder);
82
83   void OnAlbumsIndexerDone(scoped_refptr<SafePicasaAlbumsIndexer> indexer,
84                            bool success,
85                            const picasa::AlbumImagesMap& albums_images);
86
87   static std::string DateToPathString(const base::Time& time);
88   static void UniquifyNames(const std::vector<AlbumInfo>& info_list,
89                             AlbumMap* result_map);
90
91   AlbumMap album_map_;
92   AlbumMap folder_map_;
93   AlbumImagesMap albums_images_;
94
95   base::FilePath database_path_;
96
97   State state_;
98
99   // Callbacks that are waiting for their requested data to be ready.
100   std::vector<ReadyCallback> album_list_ready_callbacks_;
101   std::vector<ReadyCallback> albums_index_ready_callbacks_;
102
103   // Stores the "live" in-flight utility processes. Callbacks from other
104   // (older) utility processes are stale and ignored. Only one of these at a
105   // time should be non-NULL.
106   scoped_refptr<SafePicasaAlbumTableReader> album_table_reader_;
107   scoped_refptr<SafePicasaAlbumsIndexer> albums_indexer_;
108
109   // We watch the temp dir, as we can't detect database file modifications on
110   // Mac, but we are able to detect creation and deletion of temporary files.
111   scoped_ptr<base::FilePathWatcher> temp_dir_watcher_;
112
113   base::WeakPtrFactory<PicasaDataProvider> weak_factory_;
114
115   DISALLOW_COPY_AND_ASSIGN(PicasaDataProvider);
116 };
117
118 }  // namespace picasa
119
120 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_PICASA_DATA_PROVIDER_H_