Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.cc

Issue 23499006: Media Galleries API Picasa: Add file watch to invalidate database data on disk write. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/data_provider_helper.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
28 const char kPicasaTempDirName[] = "tmp";
29
25 void RunAllCallbacks( 30 void RunAllCallbacks(
26 std::queue<PicasaDataProvider::ReadyCallback>* ready_callbacks_queue, 31 std::queue<PicasaDataProvider::ReadyCallback>* ready_callbacks_queue,
27 bool success) { 32 bool success) {
28 while (!ready_callbacks_queue->empty()) { 33 while (!ready_callbacks_queue->empty()) {
29 ready_callbacks_queue->front().Run(success); 34 ready_callbacks_queue->front().Run(success);
30 ready_callbacks_queue->pop(); 35 ready_callbacks_queue->pop();
31 } 36 }
32 } 37 }
33 38
34 } // namespace 39 } // namespace
35 40
36 PicasaDataProvider::PicasaDataProvider(const base::FilePath& database_path) 41 PicasaDataProvider::PicasaDataProvider(const base::FilePath& database_path)
37 : database_path_(database_path), 42 : database_path_(database_path),
38 state_(STALE_DATA_STATE), 43 state_(STALE_DATA_STATE),
39 weak_factory_(this) { 44 weak_factory_(this) {
40 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 45 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
46
47 chrome::StartFilePathWatchOnMediaTaskRunner(
48 database_path_.DirName().AppendASCII(kPicasaTempDirName),
49 true /* recursive */,
50 base::Bind(&PicasaDataProvider::OnTempDirWatchStarted,
51 weak_factory_.GetWeakPtr()),
52 base::Bind(&PicasaDataProvider::OnTempDirChanged,
53 weak_factory_.GetWeakPtr()));
54 }
55
56 PicasaDataProvider::PicasaDataProvider(const base::FilePath& database_path,
57 const base::FilePath& temp_dir_path)
58 : database_path_(database_path),
59 state_(STALE_DATA_STATE),
60 weak_factory_(this) {
61 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
62
63 chrome::StartFilePathWatchOnMediaTaskRunner(
64 temp_dir_path,
65 true /* recursive */,
66 base::Bind(&PicasaDataProvider::OnTempDirWatchStarted,
67 weak_factory_.GetWeakPtr()),
68 base::Bind(&PicasaDataProvider::OnTempDirChanged,
69 weak_factory_.GetWeakPtr()));
41 } 70 }
42 71
43 PicasaDataProvider::~PicasaDataProvider() {} 72 PicasaDataProvider::~PicasaDataProvider() {}
44 73
45 void PicasaDataProvider::RefreshData(DataType needed_data, 74 void PicasaDataProvider::RefreshData(DataType needed_data,
46 const ReadyCallback& ready_callback) { 75 const ReadyCallback& ready_callback) {
47 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); 76 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
48 // TODO(tommycli): Need to watch the database_path_ folder and handle 77 // TODO(tommycli): Need to watch the database_path_ folder and handle
49 // rereading the data when it changes. 78 // rereading the data when it changes.
50 79
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 // Set data state to stale and ignore responses from any in-flight processes. 137 // Set data state to stale and ignore responses from any in-flight processes.
109 // TODO(tommycli): Implement and call Cancel function for these 138 // TODO(tommycli): Implement and call Cancel function for these
110 // UtilityProcessHostClients to actually kill the in-flight processes. 139 // UtilityProcessHostClients to actually kill the in-flight processes.
111 state_ = STALE_DATA_STATE; 140 state_ = STALE_DATA_STATE;
112 album_table_reader_ = NULL; 141 album_table_reader_ = NULL;
113 albums_indexer_ = NULL; 142 albums_indexer_ = NULL;
114 143
115 DoRefreshIfNecessary(); 144 DoRefreshIfNecessary();
116 } 145 }
117 146
147 void PicasaDataProvider::OnTempDirWatchStarted(
148 scoped_ptr<base::FilePathWatcher> temp_dir_watcher) {
149 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
150 temp_dir_watcher_.reset(temp_dir_watcher.release());
151 }
152
153 void PicasaDataProvider::OnTempDirChanged(const base::FilePath& temp_dir_path,
154 bool error) {
155 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
156 if (file_util::IsDirectoryEmpty(temp_dir_path))
157 InvalidateData();
158 }
159
118 void PicasaDataProvider::DoRefreshIfNecessary() { 160 void PicasaDataProvider::DoRefreshIfNecessary() {
119 DCHECK(state_ != INVALID_DATA_STATE); 161 DCHECK(state_ != INVALID_DATA_STATE);
120 DCHECK(state_ != ALBUMS_IMAGES_FRESH_STATE); 162 DCHECK(state_ != ALBUMS_IMAGES_FRESH_STATE);
121 DCHECK(!(album_table_reader_ && albums_indexer_)); 163 DCHECK(!(album_table_reader_ && albums_indexer_));
122 164
123 if (album_list_ready_callbacks_.empty() && 165 if (album_list_ready_callbacks_.empty() &&
124 albums_index_ready_callbacks_.empty()) { 166 albums_index_ready_callbacks_.empty()) {
125 return; 167 return;
126 } 168 }
127 169
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 275
234 if (total_counts[name] != 1) { 276 if (total_counts[name] != 1) {
235 name = base::StringPrintf("%s (%d)", name.c_str(), 277 name = base::StringPrintf("%s (%d)", name.c_str(),
236 ++current_counts[name]); 278 ++current_counts[name]);
237 } 279 }
238 280
239 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); 281 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i]));
240 } 282 }
241 } 283 }
242 284
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 285 } // namespace picasa
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698