summaryrefslogtreecommitdiffstats
path: root/components/drive/file_change.h
blob: 40e3a6a55b4fdeab9fd981017f5cccc457adb69c (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 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.

#ifndef COMPONENTS_DRIVE_FILE_CHANGE_H_
#define COMPONENTS_DRIVE_FILE_CHANGE_H_

#include <deque>
#include <map>
#include <string>

#include "base/basictypes.h"
#include "base/files/file_path.h"

namespace drive {
class ResourceEntry;

class FileChange {
 public:
  enum FileType {
    FILE_TYPE_NO_INFO,
    FILE_TYPE_FILE,
    FILE_TYPE_DIRECTORY,
  };

  enum ChangeType {
    CHANGE_TYPE_ADD_OR_UPDATE,
    CHANGE_TYPE_DELETE,
  };

  class Change {
   public:
    Change(ChangeType change, FileType file_type);

    bool IsAddOrUpdate() const { return change_ == CHANGE_TYPE_ADD_OR_UPDATE; }
    bool IsDelete() const { return change_ == CHANGE_TYPE_DELETE; }

    bool IsFile() const { return file_type_ == FILE_TYPE_FILE; }
    bool IsDirectory() const { return file_type_ == FILE_TYPE_DIRECTORY; }
    bool IsTypeUnknown() const { return !IsFile() && !IsDirectory(); }

    ChangeType change() const { return change_; }
    FileType file_type() const { return file_type_; }

    std::string DebugString() const;

    bool operator==(const Change& that) const {
      return change() == that.change() && file_type() == that.file_type();
    }

   private:
    ChangeType change_;
    FileType file_type_;
  };

  class ChangeList {
   public:
    typedef std::deque<Change> List;

    ChangeList();
    ~ChangeList();

    // Updates the list with the |new_change|.
    void Update(const Change& new_change);

    size_t size() const { return list_.size(); }
    bool empty() const { return list_.empty(); }
    void clear() { list_.clear(); }
    const List& list() const { return list_; }
    const Change& front() const { return list_.front(); }
    const Change& back() const { return list_.back(); }

    ChangeList PopAndGetNewList() const;

    std::string DebugString() const;

   private:
    List list_;
  };

 public:
  typedef std::map<base::FilePath, FileChange::ChangeList> Map;

  FileChange();
  ~FileChange();

  void Update(const base::FilePath file_path,
              const FileChange::Change& new_change);
  void Update(const base::FilePath file_path,
              const FileChange::ChangeList& list);
  void Update(const base::FilePath file_path,
              FileType type,
              FileChange::ChangeType change);
  void Update(const base::FilePath file_path,
              const ResourceEntry& entry,
              FileChange::ChangeType change);
  void Apply(const FileChange& new_changed_files);

  const Map& map() const { return map_; }

  size_t size() const { return map_.size(); }
  bool empty() const { return map_.empty(); }
  void ClearForTest() { map_.clear(); }
  size_t CountDirectory(const base::FilePath& directory_path) const;
  size_t count(const base::FilePath& file_path) const {
    return map_.count(file_path);
  }

  std::string DebugString() const;

 private:
  Map map_;
};

}  // namespace drive

#endif  // COMPONENTS_DRIVE_FILE_CHANGE_H_