Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / local / local_file_sync_status.cc
blob94430d3ba1a547ed6fac89dfac7d005bf76f4c37
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.
5 #include "chrome/browser/sync_file_system/local/local_file_sync_status.h"
7 #include "base/logging.h"
9 using fileapi::FileSystemURL;
10 using fileapi::FileSystemURLSet;
12 namespace sync_file_system {
14 LocalFileSyncStatus::LocalFileSyncStatus() {}
16 LocalFileSyncStatus::~LocalFileSyncStatus() {}
18 void LocalFileSyncStatus::StartWriting(const FileSystemURL& url) {
19 DCHECK(CalledOnValidThread());
20 DCHECK(!IsChildOrParentSyncing(url));
21 writing_[url]++;
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));
33 void LocalFileSyncStatus::StartSyncing(const FileSystemURL& url) {
34 DCHECK(CalledOnValidThread());
35 DCHECK(!IsChildOrParentWriting(url));
36 DCHECK(!IsChildOrParentSyncing(url));
37 syncing_.insert(url);
40 void LocalFileSyncStatus::EndSyncing(const FileSystemURL& url) {
41 DCHECK(CalledOnValidThread());
42 syncing_.erase(url);
43 FOR_EACH_OBSERVER(Observer, observer_list_, OnSyncEnabled(url));
44 FOR_EACH_OBSERVER(Observer, observer_list_, OnWriteEnabled(url));
47 bool LocalFileSyncStatus::IsWriting(const FileSystemURL& url) const {
48 DCHECK(CalledOnValidThread());
49 return IsChildOrParentWriting(url);
52 bool LocalFileSyncStatus::IsWritable(const FileSystemURL& url) const {
53 DCHECK(CalledOnValidThread());
54 return !IsChildOrParentSyncing(url);
57 bool LocalFileSyncStatus::IsSyncable(const FileSystemURL& url) const {
58 DCHECK(CalledOnValidThread());
59 return !IsChildOrParentSyncing(url) && !IsChildOrParentWriting(url);
62 void LocalFileSyncStatus::AddObserver(Observer* observer) {
63 DCHECK(CalledOnValidThread());
64 observer_list_.AddObserver(observer);
67 void LocalFileSyncStatus::RemoveObserver(Observer* observer) {
68 DCHECK(CalledOnValidThread());
69 observer_list_.RemoveObserver(observer);
72 bool LocalFileSyncStatus::IsChildOrParentWriting(
73 const FileSystemURL& url) const {
74 DCHECK(CalledOnValidThread());
75 URLCountMap::const_iterator upper = writing_.upper_bound(url);
76 URLCountMap::const_reverse_iterator rupper(upper);
77 if (upper != writing_.end() && url.IsParent(upper->first))
78 return true;
79 if (rupper != writing_.rend() &&
80 (rupper->first == url || rupper->first.IsParent(url)))
81 return true;
82 return false;
85 bool LocalFileSyncStatus::IsChildOrParentSyncing(
86 const FileSystemURL& url) const {
87 DCHECK(CalledOnValidThread());
88 FileSystemURLSet::const_iterator upper = syncing_.upper_bound(url);
89 FileSystemURLSet::const_reverse_iterator rupper(upper);
90 if (upper != syncing_.end() && url.IsParent(*upper))
91 return true;
92 if (rupper != syncing_.rend() &&
93 (*rupper == url || rupper->IsParent(url)))
94 return true;
95 return false;
98 } // namespace sync_file_system