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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 102 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
103 } | 103 } |
104 | 104 |
105 void GalleryWatchManager::FileWatchManager::AddFileWatch( | 105 void GalleryWatchManager::FileWatchManager::AddFileWatch( |
106 const base::FilePath& path, | 106 const base::FilePath& path, |
107 const base::Callback<void(bool)>& callback) { | 107 const base::Callback<void(bool)>& callback) { |
108 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 108 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
109 | 109 |
110 // This can occur if the GalleryWatchManager attempts to watch the same path | 110 // This can occur if the GalleryWatchManager attempts to watch the same path |
111 // again before recieving the callback. It's benign. | 111 // again before recieving the callback. It's benign. |
112 if (ContainsKey(watchers_, path)) { | 112 if (base::ContainsKey(watchers_, path)) { |
113 BrowserThread::PostTask( | 113 BrowserThread::PostTask( |
114 BrowserThread::UI, FROM_HERE, base::Bind(callback, false)); | 114 BrowserThread::UI, FROM_HERE, base::Bind(callback, false)); |
115 return; | 115 return; |
116 } | 116 } |
117 | 117 |
118 linked_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher); | 118 linked_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher); |
119 bool success = watcher->Watch(path, | 119 bool success = watcher->Watch(path, |
120 true /*recursive*/, | 120 true /*recursive*/, |
121 base::Bind(&FileWatchManager::OnFilePathChanged, | 121 base::Bind(&FileWatchManager::OnFilePathChanged, |
122 weak_factory_.GetWeakPtr())); | 122 weak_factory_.GetWeakPtr())); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 } | 189 } |
190 | 190 |
191 BrowserThread::DeleteSoon( | 191 BrowserThread::DeleteSoon( |
192 BrowserThread::FILE, FROM_HERE, watch_manager_.release()); | 192 BrowserThread::FILE, FROM_HERE, watch_manager_.release()); |
193 } | 193 } |
194 | 194 |
195 void GalleryWatchManager::AddObserver(BrowserContext* browser_context, | 195 void GalleryWatchManager::AddObserver(BrowserContext* browser_context, |
196 GalleryWatchManagerObserver* observer) { | 196 GalleryWatchManagerObserver* observer) { |
197 DCHECK(browser_context); | 197 DCHECK(browser_context); |
198 DCHECK(observer); | 198 DCHECK(observer); |
199 DCHECK(!ContainsKey(observers_, browser_context)); | 199 DCHECK(!base::ContainsKey(observers_, browser_context)); |
200 observers_[browser_context] = observer; | 200 observers_[browser_context] = observer; |
201 } | 201 } |
202 | 202 |
203 void GalleryWatchManager::RemoveObserver(BrowserContext* browser_context) { | 203 void GalleryWatchManager::RemoveObserver(BrowserContext* browser_context) { |
204 DCHECK(browser_context); | 204 DCHECK(browser_context); |
205 size_t erased = observers_.erase(browser_context); | 205 size_t erased = observers_.erase(browser_context); |
206 DCHECK_EQ(erased, 1u); | 206 DCHECK_EQ(erased, 1u); |
207 } | 207 } |
208 | 208 |
209 void GalleryWatchManager::ShutdownBrowserContext( | 209 void GalleryWatchManager::ShutdownBrowserContext( |
(...skipping 24 matching lines...) Expand all Loading... |
234 | 234 |
235 void GalleryWatchManager::AddWatch(BrowserContext* browser_context, | 235 void GalleryWatchManager::AddWatch(BrowserContext* browser_context, |
236 const extensions::Extension* extension, | 236 const extensions::Extension* extension, |
237 MediaGalleryPrefId gallery_id, | 237 MediaGalleryPrefId gallery_id, |
238 const ResultCallback& callback) { | 238 const ResultCallback& callback) { |
239 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 239 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
240 DCHECK(browser_context); | 240 DCHECK(browser_context); |
241 DCHECK(extension); | 241 DCHECK(extension); |
242 | 242 |
243 WatchOwner owner(browser_context, extension->id(), gallery_id); | 243 WatchOwner owner(browser_context, extension->id(), gallery_id); |
244 if (ContainsKey(watches_, owner)) { | 244 if (base::ContainsKey(watches_, owner)) { |
245 callback.Run(std::string()); | 245 callback.Run(std::string()); |
246 return; | 246 return; |
247 } | 247 } |
248 | 248 |
249 MediaGalleriesPreferences* preferences = | 249 MediaGalleriesPreferences* preferences = |
250 g_browser_process->media_file_system_registry()->GetPreferences( | 250 g_browser_process->media_file_system_registry()->GetPreferences( |
251 Profile::FromBrowserContext(browser_context)); | 251 Profile::FromBrowserContext(browser_context)); |
252 | 252 |
253 if (!ContainsKey(preferences->known_galleries(), gallery_id)) { | 253 if (!base::ContainsKey(preferences->known_galleries(), gallery_id)) { |
254 callback.Run(kInvalidGalleryIDError); | 254 callback.Run(kInvalidGalleryIDError); |
255 return; | 255 return; |
256 } | 256 } |
257 | 257 |
258 MediaGalleryPrefIdSet permitted = | 258 MediaGalleryPrefIdSet permitted = |
259 preferences->GalleriesForExtension(*extension); | 259 preferences->GalleriesForExtension(*extension); |
260 if (!ContainsKey(permitted, gallery_id)) { | 260 if (!base::ContainsKey(permitted, gallery_id)) { |
261 callback.Run(kNoPermissionError); | 261 callback.Run(kNoPermissionError); |
262 return; | 262 return; |
263 } | 263 } |
264 | 264 |
265 base::FilePath path = | 265 base::FilePath path = |
266 preferences->known_galleries().find(gallery_id)->second.AbsolutePath(); | 266 preferences->known_galleries().find(gallery_id)->second.AbsolutePath(); |
267 | 267 |
268 if (!storage_monitor_observed_) { | 268 if (!storage_monitor_observed_) { |
269 storage_monitor_observed_ = true; | 269 storage_monitor_observed_ = true; |
270 storage_monitor::StorageMonitor::GetInstance()->AddObserver(this); | 270 storage_monitor::StorageMonitor::GetInstance()->AddObserver(this); |
271 } | 271 } |
272 | 272 |
273 // Observe the preferences if we haven't already. | 273 // Observe the preferences if we haven't already. |
274 if (!ContainsKey(observed_preferences_, preferences)) { | 274 if (!base::ContainsKey(observed_preferences_, preferences)) { |
275 observed_preferences_.insert(preferences); | 275 observed_preferences_.insert(preferences); |
276 preferences->AddGalleryChangeObserver(this); | 276 preferences->AddGalleryChangeObserver(this); |
277 } | 277 } |
278 | 278 |
279 watches_[owner] = path; | 279 watches_[owner] = path; |
280 EnsureBrowserContextSubscription(owner.browser_context); | 280 EnsureBrowserContextSubscription(owner.browser_context); |
281 | 281 |
282 // Start the FilePathWatcher on |gallery_path| if necessary. | 282 // Start the FilePathWatcher on |gallery_path| if necessary. |
283 if (ContainsKey(watched_paths_, path)) { | 283 if (base::ContainsKey(watched_paths_, path)) { |
284 OnFileWatchActivated(owner, path, callback, true); | 284 OnFileWatchActivated(owner, path, callback, true); |
285 } else { | 285 } else { |
286 base::Callback<void(bool)> on_watch_added = | 286 base::Callback<void(bool)> on_watch_added = |
287 base::Bind(&GalleryWatchManager::OnFileWatchActivated, | 287 base::Bind(&GalleryWatchManager::OnFileWatchActivated, |
288 weak_factory_.GetWeakPtr(), | 288 weak_factory_.GetWeakPtr(), |
289 owner, | 289 owner, |
290 path, | 290 path, |
291 callback); | 291 callback); |
292 BrowserThread::PostTask(BrowserThread::FILE, | 292 BrowserThread::PostTask(BrowserThread::FILE, |
293 FROM_HERE, | 293 FROM_HERE, |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 | 398 |
399 // On error, all watches on that path are dropped, so update records and | 399 // On error, all watches on that path are dropped, so update records and |
400 // notify observers. | 400 // notify observers. |
401 if (error) { | 401 if (error) { |
402 // Make a copy, as |watched_paths_| is modified as we erase watches. | 402 // Make a copy, as |watched_paths_| is modified as we erase watches. |
403 std::set<WatchOwner> owners = notification_info->second.owners; | 403 std::set<WatchOwner> owners = notification_info->second.owners; |
404 for (std::set<WatchOwner>::iterator it = owners.begin(); it != owners.end(); | 404 for (std::set<WatchOwner>::iterator it = owners.begin(); it != owners.end(); |
405 ++it) { | 405 ++it) { |
406 Profile* profile = Profile::FromBrowserContext(it->browser_context); | 406 Profile* profile = Profile::FromBrowserContext(it->browser_context); |
407 RemoveWatch(it->browser_context, it->extension_id, it->gallery_id); | 407 RemoveWatch(it->browser_context, it->extension_id, it->gallery_id); |
408 if (ContainsKey(observers_, profile)) | 408 if (base::ContainsKey(observers_, profile)) |
409 observers_[profile]->OnGalleryWatchDropped(it->extension_id, | 409 observers_[profile]->OnGalleryWatchDropped(it->extension_id, |
410 it->gallery_id); | 410 it->gallery_id); |
411 } | 411 } |
412 | 412 |
413 return; | 413 return; |
414 } | 414 } |
415 | 415 |
416 base::TimeDelta time_since_last_notify = | 416 base::TimeDelta time_since_last_notify = |
417 base::Time::Now() - notification_info->second.last_notify_time; | 417 base::Time::Now() - notification_info->second.last_notify_time; |
418 if (time_since_last_notify < | 418 if (time_since_last_notify < |
(...skipping 15 matching lines...) Expand all Loading... |
434 } | 434 } |
435 return; | 435 return; |
436 } | 436 } |
437 notification_info->second.delayed_notification_pending = false; | 437 notification_info->second.delayed_notification_pending = false; |
438 notification_info->second.last_notify_time = base::Time::Now(); | 438 notification_info->second.last_notify_time = base::Time::Now(); |
439 | 439 |
440 std::set<WatchOwner>::const_iterator it; | 440 std::set<WatchOwner>::const_iterator it; |
441 for (it = notification_info->second.owners.begin(); | 441 for (it = notification_info->second.owners.begin(); |
442 it != notification_info->second.owners.end(); | 442 it != notification_info->second.owners.end(); |
443 ++it) { | 443 ++it) { |
444 DCHECK(ContainsKey(watches_, *it)); | 444 DCHECK(base::ContainsKey(watches_, *it)); |
445 if (ContainsKey(observers_, it->browser_context)) { | 445 if (base::ContainsKey(observers_, it->browser_context)) { |
446 observers_[it->browser_context]->OnGalleryChanged(it->extension_id, | 446 observers_[it->browser_context]->OnGalleryChanged(it->extension_id, |
447 it->gallery_id); | 447 it->gallery_id); |
448 } | 448 } |
449 } | 449 } |
450 } | 450 } |
451 | 451 |
452 void GalleryWatchManager::OnPermissionRemoved(MediaGalleriesPreferences* pref, | 452 void GalleryWatchManager::OnPermissionRemoved(MediaGalleriesPreferences* pref, |
453 const std::string& extension_id, | 453 const std::string& extension_id, |
454 MediaGalleryPrefId pref_id) { | 454 MediaGalleryPrefId pref_id) { |
455 RemoveWatch(pref->profile(), extension_id, pref_id); | 455 RemoveWatch(pref->profile(), extension_id, pref_id); |
456 if (ContainsKey(observers_, pref->profile())) | 456 if (base::ContainsKey(observers_, pref->profile())) |
457 observers_[pref->profile()]->OnGalleryWatchDropped(extension_id, pref_id); | 457 observers_[pref->profile()]->OnGalleryWatchDropped(extension_id, pref_id); |
458 } | 458 } |
459 | 459 |
460 void GalleryWatchManager::OnGalleryRemoved(MediaGalleriesPreferences* pref, | 460 void GalleryWatchManager::OnGalleryRemoved(MediaGalleriesPreferences* pref, |
461 MediaGalleryPrefId pref_id) { | 461 MediaGalleryPrefId pref_id) { |
462 // Removing a watch may update |watches_|, so extract the extension ids first. | 462 // Removing a watch may update |watches_|, so extract the extension ids first. |
463 std::set<std::string> extension_ids; | 463 std::set<std::string> extension_ids; |
464 for (WatchesMap::const_iterator it = watches_.begin(); it != watches_.end(); | 464 for (WatchesMap::const_iterator it = watches_.begin(); it != watches_.end(); |
465 ++it) { | 465 ++it) { |
466 if (it->first.browser_context == pref->profile() && | 466 if (it->first.browser_context == pref->profile() && |
467 it->first.gallery_id == pref_id) { | 467 it->first.gallery_id == pref_id) { |
468 extension_ids.insert(it->first.extension_id); | 468 extension_ids.insert(it->first.extension_id); |
469 } | 469 } |
470 } | 470 } |
471 | 471 |
472 for (std::set<std::string>::const_iterator it = extension_ids.begin(); | 472 for (std::set<std::string>::const_iterator it = extension_ids.begin(); |
473 it != extension_ids.end(); | 473 it != extension_ids.end(); |
474 ++it) { | 474 ++it) { |
475 RemoveWatch(pref->profile(), *it, pref_id); | 475 RemoveWatch(pref->profile(), *it, pref_id); |
476 if (ContainsKey(observers_, pref->profile())) | 476 if (base::ContainsKey(observers_, pref->profile())) |
477 observers_[pref->profile()]->OnGalleryWatchDropped(*it, pref_id); | 477 observers_[pref->profile()]->OnGalleryWatchDropped(*it, pref_id); |
478 } | 478 } |
479 } | 479 } |
480 | 480 |
481 void GalleryWatchManager::OnRemovableStorageDetached( | 481 void GalleryWatchManager::OnRemovableStorageDetached( |
482 const storage_monitor::StorageInfo& info) { | 482 const storage_monitor::StorageInfo& info) { |
483 WatchesMap::iterator it = watches_.begin(); | 483 WatchesMap::iterator it = watches_.begin(); |
484 while (it != watches_.end()) { | 484 while (it != watches_.end()) { |
485 MediaGalleriesPreferences* preferences = | 485 MediaGalleriesPreferences* preferences = |
486 g_browser_process->media_file_system_registry()->GetPreferences( | 486 g_browser_process->media_file_system_registry()->GetPreferences( |
487 Profile::FromBrowserContext(it->first.browser_context)); | 487 Profile::FromBrowserContext(it->first.browser_context)); |
488 MediaGalleryPrefIdSet detached_ids = | 488 MediaGalleryPrefIdSet detached_ids = |
489 preferences->LookUpGalleriesByDeviceId(info.device_id()); | 489 preferences->LookUpGalleriesByDeviceId(info.device_id()); |
490 | 490 |
491 if (ContainsKey(detached_ids, it->first.gallery_id)) { | 491 if (base::ContainsKey(detached_ids, it->first.gallery_id)) { |
492 WatchOwner owner = it->first; | 492 WatchOwner owner = it->first; |
493 DeactivateFileWatch(owner, it->second); | 493 DeactivateFileWatch(owner, it->second); |
494 // Post increment moves iterator to next element while deleting current. | 494 // Post increment moves iterator to next element while deleting current. |
495 watches_.erase(it++); | 495 watches_.erase(it++); |
496 observers_[preferences->profile()]->OnGalleryWatchDropped( | 496 observers_[preferences->profile()]->OnGalleryWatchDropped( |
497 owner.extension_id, owner.gallery_id); | 497 owner.extension_id, owner.gallery_id); |
498 } else { | 498 } else { |
499 ++it; | 499 ++it; |
500 } | 500 } |
501 } | 501 } |
502 } | 502 } |
OLD | NEW |