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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/sync_file_system/local/local_file_sync_status.h" 6 7 #include "base/logging.h" 8 9 using fileapi::FileSystemURL; 10 using fileapi::FileSystemURLSet; 11 12 namespace sync_file_system { 13 14 LocalFileSyncStatus::LocalFileSyncStatus() {} 15 16 LocalFileSyncStatus::~LocalFileSyncStatus() {} 17 18 void LocalFileSyncStatus::StartWriting(const FileSystemURL& url) { 19 DCHECK(CalledOnValidThread()); 20 DCHECK(!IsChildOrParentSyncing(url)); 21 writing_[url]++; 22 } 23 24 void LocalFileSyncStatus::EndWriting(const FileSystemURL& url) { 25 DCHECK(CalledOnValidThread()); 26 int count = --writing_[url]; 27 if (count == 0) { 28 writing_.erase(url); 29 FOR_EACH_OBSERVER(Observer, observer_list_, OnSyncEnabled(url)); 30 } 31 } 32 33 void LocalFileSyncStatus::StartSyncing(const FileSystemURL& url) { 34 DCHECK(CalledOnValidThread()); 35 DCHECK(!IsChildOrParentWriting(url)); 36 DCHECK(!IsChildOrParentSyncing(url)); 37 syncing_.insert(url); 38 } 39 40 void LocalFileSyncStatus::EndSyncing(const FileSystemURL& url) { 41 DCHECK(CalledOnValidThread()); 42 syncing_.erase(url); 43 FOR_EACH_OBSERVER(Observer, observer_list_, OnWriteEnabled(url)); 44 } 45 46 bool LocalFileSyncStatus::IsWriting(const FileSystemURL& url) const { 47 DCHECK(CalledOnValidThread()); 48 return IsChildOrParentWriting(url); 49 } 50 51 bool LocalFileSyncStatus::IsWritable(const FileSystemURL& url) const { 52 DCHECK(CalledOnValidThread()); 53 return !IsChildOrParentSyncing(url); 54 } 55 56 bool LocalFileSyncStatus::IsSyncable(const FileSystemURL& url) const { 57 DCHECK(CalledOnValidThread()); 58 return !IsChildOrParentSyncing(url) && !IsChildOrParentWriting(url); 59 } 60 61 void LocalFileSyncStatus::AddObserver(Observer* observer) { 62 DCHECK(CalledOnValidThread()); 63 observer_list_.AddObserver(observer); 64 } 65 66 void LocalFileSyncStatus::RemoveObserver(Observer* observer) { 67 DCHECK(CalledOnValidThread()); 68 observer_list_.RemoveObserver(observer); 69 } 70 71 bool LocalFileSyncStatus::IsChildOrParentWriting( 72 const FileSystemURL& url) const { 73 DCHECK(CalledOnValidThread()); 74 URLCountMap::const_iterator upper = writing_.upper_bound(url); 75 URLCountMap::const_reverse_iterator rupper(upper); 76 if (upper != writing_.end() && url.IsParent(upper->first)) 77 return true; 78 if (rupper != writing_.rend() && 79 (rupper->first == url || rupper->first.IsParent(url))) 80 return true; 81 return false; 82 } 83 84 bool LocalFileSyncStatus::IsChildOrParentSyncing( 85 const FileSystemURL& url) const { 86 DCHECK(CalledOnValidThread()); 87 FileSystemURLSet::const_iterator upper = syncing_.upper_bound(url); 88 FileSystemURLSet::const_reverse_iterator rupper(upper); 89 if (upper != syncing_.end() && url.IsParent(*upper)) 90 return true; 91 if (rupper != syncing_.rend() && 92 (*rupper == url || rupper->IsParent(url))) 93 return true; 94 return false; 95 } 96 97 } // namespace sync_file_system 98