summaryrefslogtreecommitdiffstats
path: root/webkit/fileapi/syncable/local_file_sync_status.cc
blob: 4966fe658c1b8db46644618052d55412087b41ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/fileapi/syncable/local_file_sync_status.h"

#include "base/logging.h"

namespace fileapi {

LocalFileSyncStatus::LocalFileSyncStatus() {}

LocalFileSyncStatus::~LocalFileSyncStatus() {}

void LocalFileSyncStatus::StartWriting(const FileSystemURL& url) {
  DCHECK(CalledOnValidThread());
  DCHECK(!IsChildOrParentSyncing(url));
  writing_[url]++;
}

void LocalFileSyncStatus::EndWriting(const FileSystemURL& url) {
  DCHECK(CalledOnValidThread());
  int count = --writing_[url];
  if (count == 0) {
    writing_.erase(url);
    FOR_EACH_OBSERVER(Observer, observer_list_, OnSyncEnabled(url));
  }
}

void LocalFileSyncStatus::StartSyncing(const FileSystemURL& url) {
  DCHECK(CalledOnValidThread());
  DCHECK(!IsChildOrParentWriting(url));
  DCHECK(!IsChildOrParentSyncing(url));
  syncing_.insert(url);
}

void LocalFileSyncStatus::EndSyncing(const FileSystemURL& url) {
  DCHECK(CalledOnValidThread());
  syncing_.erase(url);
  FOR_EACH_OBSERVER(Observer, observer_list_, OnWriteEnabled(url));
}

bool LocalFileSyncStatus::IsWriting(const FileSystemURL& url) const {
  DCHECK(CalledOnValidThread());
  return IsChildOrParentWriting(url);
}

bool LocalFileSyncStatus::IsWritable(const FileSystemURL& url) const {
  DCHECK(CalledOnValidThread());
  return !IsChildOrParentSyncing(url);
}

bool LocalFileSyncStatus::IsSyncable(const FileSystemURL& url) const {
  DCHECK(CalledOnValidThread());
  return !IsChildOrParentSyncing(url) && !IsChildOrParentWriting(url);
}

void LocalFileSyncStatus::AddObserver(Observer* observer) {
  DCHECK(CalledOnValidThread());
  observer_list_.AddObserver(observer);
}

void LocalFileSyncStatus::RemoveObserver(Observer* observer) {
  DCHECK(CalledOnValidThread());
  observer_list_.RemoveObserver(observer);
}

bool LocalFileSyncStatus::IsChildOrParentWriting(
    const FileSystemURL& url) const {
  DCHECK(CalledOnValidThread());
  URLCountMap::const_iterator upper = writing_.upper_bound(url);
  URLCountMap::const_reverse_iterator rupper(upper);
  if (upper != writing_.end() && url.IsParent(upper->first))
    return true;
  if (rupper != writing_.rend() &&
      (rupper->first == url || rupper->first.IsParent(url)))
    return true;
  return false;
}

bool LocalFileSyncStatus::IsChildOrParentSyncing(
    const FileSystemURL& url) const {
  DCHECK(CalledOnValidThread());
  FileSystemURLSet::const_iterator upper = syncing_.upper_bound(url);
  FileSystemURLSet::const_reverse_iterator rupper(upper);
  if (upper != syncing_.end() && url.IsParent(*upper))
    return true;
  if (rupper != syncing_.rend() &&
      (*rupper == url || rupper->IsParent(url)))
    return true;
  return false;
}

}  // namespace fileapi