summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media_galleries/fileapi/picasa_data_provider.h
blob: 8da1ae4e9111c20e59bdb58aab9a748d94078568 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_PICASA_DATA_PROVIDER_H_
#define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_PICASA_DATA_PROVIDER_H_

#include <map>
#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/callback_forward.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_path_watcher.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "chrome/common/media_galleries/picasa_types.h"

namespace picasa {

class SafePicasaAlbumTableReader;
class SafePicasaAlbumsIndexer;

// Created and owned by ImportedMediaGalleryRegistryTaskRunnerValues
class PicasaDataProvider {
 public:
  typedef base::Callback<void(bool)> ReadyCallback;

  enum DataType {
    LIST_OF_ALBUMS_AND_FOLDERS_DATA,
    ALBUMS_IMAGES_DATA
  };

  explicit PicasaDataProvider(const base::FilePath& database_path);
  virtual ~PicasaDataProvider();

  // Ask the data provider to refresh the data if necessary. |ready_callback|
  // will be called when the data is up to date.
  void RefreshData(DataType needed_data, const ReadyCallback& ready_callback);

  // These methods return scoped_ptrs because we want to return a copy that
  // will not change to the caller.
  scoped_ptr<AlbumMap> GetAlbums();
  scoped_ptr<AlbumMap> GetFolders();
  // |error| must be non-NULL.
  scoped_ptr<AlbumImages> FindAlbumImages(const std::string& key,
                                          base::File::Error* error);

 protected:
  // Notifies data provider that any currently cached data is stale.
  virtual void InvalidateData();

 private:
  enum State {
    STALE_DATA_STATE,
    INVALID_DATA_STATE,
    LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE,
    ALBUMS_IMAGES_FRESH_STATE
  };

  friend class PicasaFileUtilTest;
  friend class TestPicasaDataProvider;

  // Called when the FilePathWatcher for Picasa's temp directory has started.
  virtual void OnTempDirWatchStarted(
      scoped_ptr<base::FilePathWatcher> temp_dir_watcher);

  // Called when Picasa's temp directory has changed. Virtual for testing.
  virtual void OnTempDirChanged(const base::FilePath& temp_dir_path,
                                bool error);

  // Kicks off utility processes needed to fulfill any pending callbacks.
  void DoRefreshIfNecessary();

  void OnAlbumTableReaderDone(scoped_refptr<SafePicasaAlbumTableReader> reader,
                              bool parse_success,
                              const std::vector<AlbumInfo>& albums,
                              const std::vector<AlbumInfo>& folder);

  void OnAlbumsIndexerDone(scoped_refptr<SafePicasaAlbumsIndexer> indexer,
                           bool success,
                           const picasa::AlbumImagesMap& albums_images);

  static std::string DateToPathString(const base::Time& time);
  static void UniquifyNames(const std::vector<AlbumInfo>& info_list,
                            AlbumMap* result_map);

  AlbumMap album_map_;
  AlbumMap folder_map_;
  AlbumImagesMap albums_images_;

  base::FilePath database_path_;

  State state_;

  // Callbacks that are waiting for their requested data to be ready.
  std::vector<ReadyCallback> album_list_ready_callbacks_;
  std::vector<ReadyCallback> albums_index_ready_callbacks_;

  // Stores the "live" in-flight utility processes. Callbacks from other
  // (older) utility processes are stale and ignored. Only one of these at a
  // time should be non-NULL.
  scoped_refptr<SafePicasaAlbumTableReader> album_table_reader_;
  scoped_refptr<SafePicasaAlbumsIndexer> albums_indexer_;

  // We watch the temp dir, as we can't detect database file modifications on
  // Mac, but we are able to detect creation and deletion of temporary files.
  scoped_ptr<base::FilePathWatcher> temp_dir_watcher_;

  base::WeakPtrFactory<PicasaDataProvider> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(PicasaDataProvider);
};

}  // namespace picasa

#endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_PICASA_DATA_PROVIDER_H_