OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.h" | 5 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/file_util.h" |
11 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "chrome/browser/media_galleries/fileapi/file_path_watcher_util.h" |
12 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" | 15 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
13 #include "chrome/browser/media_galleries/fileapi/safe_picasa_album_table_reader.
h" | 16 #include "chrome/browser/media_galleries/fileapi/safe_picasa_album_table_reader.
h" |
14 #include "chrome/browser/media_galleries/fileapi/safe_picasa_albums_indexer.h" | 17 #include "chrome/browser/media_galleries/fileapi/safe_picasa_albums_indexer.h" |
15 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" | 18 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" |
16 #include "webkit/browser/fileapi/file_system_operation_context.h" | 19 #include "webkit/browser/fileapi/file_system_operation_context.h" |
17 #include "webkit/browser/fileapi/file_system_url.h" | 20 #include "webkit/browser/fileapi/file_system_url.h" |
18 | 21 |
19 using chrome::MediaFileSystemBackend; | 22 using chrome::MediaFileSystemBackend; |
20 | 23 |
21 namespace picasa { | 24 namespace picasa { |
22 | 25 |
23 namespace { | 26 namespace { |
24 | 27 |
25 void RunAllCallbacks( | 28 void RunAllCallbacks( |
26 std::queue<PicasaDataProvider::ReadyCallback>* ready_callbacks_queue, | 29 std::queue<PicasaDataProvider::ReadyCallback>* ready_callbacks_queue, |
27 bool success) { | 30 bool success) { |
28 while (!ready_callbacks_queue->empty()) { | 31 while (!ready_callbacks_queue->empty()) { |
29 ready_callbacks_queue->front().Run(success); | 32 ready_callbacks_queue->front().Run(success); |
30 ready_callbacks_queue->pop(); | 33 ready_callbacks_queue->pop(); |
31 } | 34 } |
32 } | 35 } |
33 | 36 |
34 } // namespace | 37 } // namespace |
35 | 38 |
36 PicasaDataProvider::PicasaDataProvider(const base::FilePath& database_path) | 39 PicasaDataProvider::PicasaDataProvider(const base::FilePath& database_path) |
37 : database_path_(database_path), | 40 : database_path_(database_path), |
38 state_(STALE_DATA_STATE), | 41 state_(STALE_DATA_STATE), |
39 weak_factory_(this) { | 42 weak_factory_(this) { |
40 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 43 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 44 |
| 45 chrome::StartFilePathWatchOnMediaTaskRunner( |
| 46 database_path_.DirName().AppendASCII(kPicasaTempDirName), |
| 47 base::Bind(&PicasaDataProvider::OnTempDirWatchStarted, |
| 48 weak_factory_.GetWeakPtr()), |
| 49 base::Bind(&PicasaDataProvider::OnTempDirChanged, |
| 50 weak_factory_.GetWeakPtr())); |
41 } | 51 } |
42 | 52 |
43 PicasaDataProvider::~PicasaDataProvider() {} | 53 PicasaDataProvider::~PicasaDataProvider() {} |
44 | 54 |
45 void PicasaDataProvider::RefreshData(DataType needed_data, | 55 void PicasaDataProvider::RefreshData(DataType needed_data, |
46 const ReadyCallback& ready_callback) { | 56 const ReadyCallback& ready_callback) { |
47 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 57 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
48 // TODO(tommycli): Need to watch the database_path_ folder and handle | 58 // TODO(tommycli): Need to watch the database_path_ folder and handle |
49 // rereading the data when it changes. | 59 // rereading the data when it changes. |
50 | 60 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 // Set data state to stale and ignore responses from any in-flight processes. | 118 // Set data state to stale and ignore responses from any in-flight processes. |
109 // TODO(tommycli): Implement and call Cancel function for these | 119 // TODO(tommycli): Implement and call Cancel function for these |
110 // UtilityProcessHostClients to actually kill the in-flight processes. | 120 // UtilityProcessHostClients to actually kill the in-flight processes. |
111 state_ = STALE_DATA_STATE; | 121 state_ = STALE_DATA_STATE; |
112 album_table_reader_ = NULL; | 122 album_table_reader_ = NULL; |
113 albums_indexer_ = NULL; | 123 albums_indexer_ = NULL; |
114 | 124 |
115 DoRefreshIfNecessary(); | 125 DoRefreshIfNecessary(); |
116 } | 126 } |
117 | 127 |
| 128 void PicasaDataProvider::OnTempDirWatchStarted( |
| 129 scoped_ptr<base::FilePathWatcher> temp_dir_watcher) { |
| 130 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 131 temp_dir_watcher_.reset(temp_dir_watcher.release()); |
| 132 } |
| 133 |
| 134 void PicasaDataProvider::OnTempDirChanged(const base::FilePath& temp_dir_path, |
| 135 bool error) { |
| 136 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 137 if (file_util::IsDirectoryEmpty(temp_dir_path)) |
| 138 InvalidateData(); |
| 139 } |
| 140 |
118 void PicasaDataProvider::DoRefreshIfNecessary() { | 141 void PicasaDataProvider::DoRefreshIfNecessary() { |
119 DCHECK(state_ != INVALID_DATA_STATE); | 142 DCHECK(state_ != INVALID_DATA_STATE); |
120 DCHECK(state_ != ALBUMS_IMAGES_FRESH_STATE); | 143 DCHECK(state_ != ALBUMS_IMAGES_FRESH_STATE); |
121 DCHECK(!(album_table_reader_ && albums_indexer_)); | 144 DCHECK(!(album_table_reader_ && albums_indexer_)); |
122 | 145 |
123 if (album_list_ready_callbacks_.empty() && | 146 if (album_list_ready_callbacks_.empty() && |
124 albums_index_ready_callbacks_.empty()) { | 147 albums_index_ready_callbacks_.empty()) { |
125 return; | 148 return; |
126 } | 149 } |
127 | 150 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 | 256 |
234 if (total_counts[name] != 1) { | 257 if (total_counts[name] != 1) { |
235 name = base::StringPrintf("%s (%d)", name.c_str(), | 258 name = base::StringPrintf("%s (%d)", name.c_str(), |
236 ++current_counts[name]); | 259 ++current_counts[name]); |
237 } | 260 } |
238 | 261 |
239 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); | 262 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); |
240 } | 263 } |
241 } | 264 } |
242 | 265 |
243 void PicasaDataProvider::SetDatabasePathForTesting( | |
244 const base::FilePath& database_path) { | |
245 database_path_ = database_path; | |
246 } | |
247 | |
248 void PicasaDataProvider::SetAlbumMapsForTesting(const AlbumMap& album_map, | |
249 const AlbumMap& folder_map) { | |
250 album_map_ = album_map; | |
251 folder_map_ = folder_map; | |
252 } | |
253 | |
254 } // namespace picasa | 266 } // namespace picasa |
OLD | NEW |