| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/gallery_watch_manager.h" | 5 #include "chrome/browser/media_galleries/gallery_watch_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <tuple> | 9 #include <tuple> |
| 10 | 10 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 } | 104 } |
| 105 | 105 |
| 106 void GalleryWatchManager::FileWatchManager::AddFileWatch( | 106 void GalleryWatchManager::FileWatchManager::AddFileWatch( |
| 107 const base::FilePath& path, | 107 const base::FilePath& path, |
| 108 const base::Callback<void(bool)>& callback) { | 108 const base::Callback<void(bool)>& callback) { |
| 109 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 109 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 110 | 110 |
| 111 // This can occur if the GalleryWatchManager attempts to watch the same path | 111 // This can occur if the GalleryWatchManager attempts to watch the same path |
| 112 // again before recieving the callback. It's benign. | 112 // again before recieving the callback. It's benign. |
| 113 if (base::ContainsKey(watchers_, path)) { | 113 if (base::ContainsKey(watchers_, path)) { |
| 114 BrowserThread::PostTask( | 114 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 115 BrowserThread::UI, FROM_HERE, base::Bind(callback, false)); | 115 base::BindOnce(callback, false)); |
| 116 return; | 116 return; |
| 117 } | 117 } |
| 118 | 118 |
| 119 linked_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher); | 119 linked_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher); |
| 120 bool success = watcher->Watch(path, | 120 bool success = watcher->Watch(path, |
| 121 true /*recursive*/, | 121 true /*recursive*/, |
| 122 base::Bind(&FileWatchManager::OnFilePathChanged, | 122 base::Bind(&FileWatchManager::OnFilePathChanged, |
| 123 weak_factory_.GetWeakPtr())); | 123 weak_factory_.GetWeakPtr())); |
| 124 | 124 |
| 125 if (success) | 125 if (success) |
| 126 watchers_[path] = watcher; | 126 watchers_[path] = watcher; |
| 127 | 127 |
| 128 BrowserThread::PostTask( | 128 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 129 BrowserThread::UI, FROM_HERE, base::Bind(callback, success)); | 129 base::BindOnce(callback, success)); |
| 130 } | 130 } |
| 131 | 131 |
| 132 void GalleryWatchManager::FileWatchManager::RemoveFileWatch( | 132 void GalleryWatchManager::FileWatchManager::RemoveFileWatch( |
| 133 const base::FilePath& path) { | 133 const base::FilePath& path) { |
| 134 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 134 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 135 size_t erased = watchers_.erase(path); | 135 size_t erased = watchers_.erase(path); |
| 136 DCHECK_EQ(erased, 1u); | 136 DCHECK_EQ(erased, 1u); |
| 137 } | 137 } |
| 138 | 138 |
| 139 base::WeakPtr<GalleryWatchManager::FileWatchManager> | 139 base::WeakPtr<GalleryWatchManager::FileWatchManager> |
| 140 GalleryWatchManager::FileWatchManager::GetWeakPtr() { | 140 GalleryWatchManager::FileWatchManager::GetWeakPtr() { |
| 141 return weak_factory_.GetWeakPtr(); | 141 return weak_factory_.GetWeakPtr(); |
| 142 } | 142 } |
| 143 | 143 |
| 144 void GalleryWatchManager::FileWatchManager::OnFilePathChanged( | 144 void GalleryWatchManager::FileWatchManager::OnFilePathChanged( |
| 145 const base::FilePath& path, | 145 const base::FilePath& path, |
| 146 bool error) { | 146 bool error) { |
| 147 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 147 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 148 if (error) | 148 if (error) |
| 149 RemoveFileWatch(path); | 149 RemoveFileWatch(path); |
| 150 BrowserThread::PostTask( | 150 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 151 BrowserThread::UI, FROM_HERE, base::Bind(callback_, path, error)); | 151 base::BindOnce(callback_, path, error)); |
| 152 } | 152 } |
| 153 | 153 |
| 154 GalleryWatchManager::WatchOwner::WatchOwner(BrowserContext* browser_context, | 154 GalleryWatchManager::WatchOwner::WatchOwner(BrowserContext* browser_context, |
| 155 const std::string& extension_id, | 155 const std::string& extension_id, |
| 156 MediaGalleryPrefId gallery_id) | 156 MediaGalleryPrefId gallery_id) |
| 157 : browser_context(browser_context), | 157 : browser_context(browser_context), |
| 158 extension_id(extension_id), | 158 extension_id(extension_id), |
| 159 gallery_id(gallery_id) { | 159 gallery_id(gallery_id) { |
| 160 } | 160 } |
| 161 | 161 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 // Start the FilePathWatcher on |gallery_path| if necessary. | 283 // Start the FilePathWatcher on |gallery_path| if necessary. |
| 284 if (base::ContainsKey(watched_paths_, path)) { | 284 if (base::ContainsKey(watched_paths_, path)) { |
| 285 OnFileWatchActivated(owner, path, callback, true); | 285 OnFileWatchActivated(owner, path, callback, true); |
| 286 } else { | 286 } else { |
| 287 base::Callback<void(bool)> on_watch_added = | 287 base::Callback<void(bool)> on_watch_added = |
| 288 base::Bind(&GalleryWatchManager::OnFileWatchActivated, | 288 base::Bind(&GalleryWatchManager::OnFileWatchActivated, |
| 289 weak_factory_.GetWeakPtr(), | 289 weak_factory_.GetWeakPtr(), |
| 290 owner, | 290 owner, |
| 291 path, | 291 path, |
| 292 callback); | 292 callback); |
| 293 BrowserThread::PostTask(BrowserThread::FILE, | 293 BrowserThread::PostTask( |
| 294 FROM_HERE, | 294 BrowserThread::FILE, FROM_HERE, |
| 295 base::Bind(&FileWatchManager::AddFileWatch, | 295 base::BindOnce(&FileWatchManager::AddFileWatch, |
| 296 watch_manager_->GetWeakPtr(), | 296 watch_manager_->GetWeakPtr(), path, on_watch_added)); |
| 297 path, | |
| 298 on_watch_added)); | |
| 299 } | 297 } |
| 300 } | 298 } |
| 301 | 299 |
| 302 void GalleryWatchManager::RemoveWatch(BrowserContext* browser_context, | 300 void GalleryWatchManager::RemoveWatch(BrowserContext* browser_context, |
| 303 const std::string& extension_id, | 301 const std::string& extension_id, |
| 304 MediaGalleryPrefId gallery_id) { | 302 MediaGalleryPrefId gallery_id) { |
| 305 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 303 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 306 DCHECK(browser_context); | 304 DCHECK(browser_context); |
| 307 | 305 |
| 308 WatchOwner owner(browser_context, extension_id, gallery_id); | 306 WatchOwner owner(browser_context, extension_id, gallery_id); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 void GalleryWatchManager::DeactivateFileWatch(const WatchOwner& owner, | 360 void GalleryWatchManager::DeactivateFileWatch(const WatchOwner& owner, |
| 363 const base::FilePath& path) { | 361 const base::FilePath& path) { |
| 364 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 362 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 365 WatchedPaths::iterator it = watched_paths_.find(path); | 363 WatchedPaths::iterator it = watched_paths_.find(path); |
| 366 if (it == watched_paths_.end()) | 364 if (it == watched_paths_.end()) |
| 367 return; | 365 return; |
| 368 | 366 |
| 369 it->second.owners.erase(owner); | 367 it->second.owners.erase(owner); |
| 370 if (it->second.owners.empty()) { | 368 if (it->second.owners.empty()) { |
| 371 watched_paths_.erase(it); | 369 watched_paths_.erase(it); |
| 372 BrowserThread::PostTask(BrowserThread::FILE, | 370 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 373 FROM_HERE, | 371 base::BindOnce(&FileWatchManager::RemoveFileWatch, |
| 374 base::Bind(&FileWatchManager::RemoveFileWatch, | 372 watch_manager_->GetWeakPtr(), path)); |
| 375 watch_manager_->GetWeakPtr(), | |
| 376 path)); | |
| 377 } | 373 } |
| 378 } | 374 } |
| 379 | 375 |
| 380 void GalleryWatchManager::OnFileWatchActivated(const WatchOwner& owner, | 376 void GalleryWatchManager::OnFileWatchActivated(const WatchOwner& owner, |
| 381 const base::FilePath& path, | 377 const base::FilePath& path, |
| 382 const ResultCallback& callback, | 378 const ResultCallback& callback, |
| 383 bool success) { | 379 bool success) { |
| 384 if (success) { | 380 if (success) { |
| 385 // |watched_paths_| doesn't necessarily to contain |path| yet. | 381 // |watched_paths_| doesn't necessarily to contain |path| yet. |
| 386 // In that case, it calls the default constructor for NotificationInfo. | 382 // In that case, it calls the default constructor for NotificationInfo. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 base::Time::Now() - notification_info->second.last_notify_time; | 414 base::Time::Now() - notification_info->second.last_notify_time; |
| 419 if (time_since_last_notify < | 415 if (time_since_last_notify < |
| 420 base::TimeDelta::FromSeconds(kMinNotificationDelayInSeconds)) { | 416 base::TimeDelta::FromSeconds(kMinNotificationDelayInSeconds)) { |
| 421 if (!notification_info->second.delayed_notification_pending) { | 417 if (!notification_info->second.delayed_notification_pending) { |
| 422 notification_info->second.delayed_notification_pending = true; | 418 notification_info->second.delayed_notification_pending = true; |
| 423 base::TimeDelta delay_to_next_valid_time = | 419 base::TimeDelta delay_to_next_valid_time = |
| 424 notification_info->second.last_notify_time + | 420 notification_info->second.last_notify_time + |
| 425 base::TimeDelta::FromSeconds(kMinNotificationDelayInSeconds) - | 421 base::TimeDelta::FromSeconds(kMinNotificationDelayInSeconds) - |
| 426 base::Time::Now(); | 422 base::Time::Now(); |
| 427 BrowserThread::PostDelayedTask( | 423 BrowserThread::PostDelayedTask( |
| 428 BrowserThread::UI, | 424 BrowserThread::UI, FROM_HERE, |
| 429 FROM_HERE, | 425 base::BindOnce(&GalleryWatchManager::OnFilePathChanged, |
| 430 base::Bind(&GalleryWatchManager::OnFilePathChanged, | 426 weak_factory_.GetWeakPtr(), path, error), |
| 431 weak_factory_.GetWeakPtr(), | |
| 432 path, | |
| 433 error), | |
| 434 delay_to_next_valid_time); | 427 delay_to_next_valid_time); |
| 435 } | 428 } |
| 436 return; | 429 return; |
| 437 } | 430 } |
| 438 notification_info->second.delayed_notification_pending = false; | 431 notification_info->second.delayed_notification_pending = false; |
| 439 notification_info->second.last_notify_time = base::Time::Now(); | 432 notification_info->second.last_notify_time = base::Time::Now(); |
| 440 | 433 |
| 441 std::set<WatchOwner>::const_iterator it; | 434 std::set<WatchOwner>::const_iterator it; |
| 442 for (it = notification_info->second.owners.begin(); | 435 for (it = notification_info->second.owners.begin(); |
| 443 it != notification_info->second.owners.end(); | 436 it != notification_info->second.owners.end(); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 DeactivateFileWatch(owner, it->second); | 487 DeactivateFileWatch(owner, it->second); |
| 495 // Post increment moves iterator to next element while deleting current. | 488 // Post increment moves iterator to next element while deleting current. |
| 496 watches_.erase(it++); | 489 watches_.erase(it++); |
| 497 observers_[preferences->profile()]->OnGalleryWatchDropped( | 490 observers_[preferences->profile()]->OnGalleryWatchDropped( |
| 498 owner.extension_id, owner.gallery_id); | 491 owner.extension_id, owner.gallery_id); |
| 499 } else { | 492 } else { |
| 500 ++it; | 493 ++it; |
| 501 } | 494 } |
| 502 } | 495 } |
| 503 } | 496 } |
| OLD | NEW |