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