summaryrefslogtreecommitdiffstats
path: root/webkit/browser/fileapi/syncable/local_file_sync_status.h
blob: 0cb07ebf0118e6092cc8568ee7774740a2b5304b (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
95
// Copyright 2013 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.

#ifndef WEBKIT_BROWSER_FILEAPI_SYNCABLE_LOCAL_FILE_SYNC_STATUS_H_
#define WEBKIT_BROWSER_FILEAPI_SYNCABLE_LOCAL_FILE_SYNC_STATUS_H_

#include <map>
#include <set>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/observer_list.h"
#include "base/threading/non_thread_safe.h"
#include "webkit/browser/fileapi/file_system_url.h"

namespace fileapi {
class FileSystemURL;
}

namespace sync_file_system {

// Represents local file sync status.
// This class is supposed to run only on IO thread.
//
// This class manages two important synchronization flags: writing (counter)
// and syncing (flag).  Writing counter keeps track of which URL is in
// writing and syncing flag indicates which URL is in syncing.
//
// An entry can have multiple writers but sync is exclusive and cannot overwrap
// with any writes or syncs.
class WEBKIT_STORAGE_BROWSER_EXPORT LocalFileSyncStatus
    : public base::NonThreadSafe {
 public:
  class WEBKIT_STORAGE_BROWSER_EXPORT Observer {
   public:
    Observer() {}
    virtual ~Observer() {}
    virtual void OnSyncEnabled(const fileapi::FileSystemURL& url) = 0;
    virtual void OnWriteEnabled(const fileapi::FileSystemURL& url) = 0;
   private:
    DISALLOW_COPY_AND_ASSIGN(Observer);
  };

  LocalFileSyncStatus();
  ~LocalFileSyncStatus();

  // Increment writing counter for |url|.
  // This should not be called if the |url| is not writable.
  void StartWriting(const fileapi::FileSystemURL& url);

  // Decrement writing counter for |url|.
  void EndWriting(const fileapi::FileSystemURL& url);

  // Start syncing for |url| and disable writing.
  // This should not be called if |url| is in syncing or in writing.
  void StartSyncing(const fileapi::FileSystemURL& url);

  // Clears the syncing flag for |url| and enable writing.
  void EndSyncing(const fileapi::FileSystemURL& url);

  // Returns true if the |url| or its parent or child is in writing.
  bool IsWriting(const fileapi::FileSystemURL& url) const;

  // Returns true if the |url| is enabled for writing (i.e. not in syncing).
  bool IsWritable(const fileapi::FileSystemURL& url) const;

  // Returns true if the |url| is enabled for syncing (i.e. neither in
  // syncing nor writing).
  bool IsSyncable(const fileapi::FileSystemURL& url) const;

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

 private:
  typedef std::map<fileapi::FileSystemURL,int64,
                   fileapi::FileSystemURL::Comparator> URLCountMap;

  bool IsChildOrParentWriting(const fileapi::FileSystemURL& url) const;
  bool IsChildOrParentSyncing(const fileapi::FileSystemURL& url) const;

  // If this count is non-zero positive there're ongoing write operations.
  URLCountMap writing_;

  // If this flag is set sync process is running on the file.
  fileapi::FileSystemURLSet syncing_;

  ObserverList<Observer> observer_list_;

  DISALLOW_COPY_AND_ASSIGN(LocalFileSyncStatus);
};

}  // namespace sync_file_system

#endif  // WEBKIT_BROWSER_FILEAPI_SYNCABLE_LOCAL_FILE_SYNC_STATUS_H_