blob: 40d750c5b552e37bb1ba84d7259a31b45522fbf9 [file] [log] [blame]
[email protected]e12c2d02012-10-24 08:19:031// 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
3// found in the LICENSE file.
4
lukasza76b4a982015-08-08 00:36:395#ifndef COMPONENTS_DRIVE_FILE_CHANGE_H_
6#define COMPONENTS_DRIVE_FILE_CHANGE_H_
[email protected]e12c2d02012-10-24 08:19:037
avibc5337b2015-12-25 23:16:338#include <stddef.h>
9
[email protected]741a1772014-07-01 18:40:2910#include <deque>
11#include <map>
12#include <string>
[email protected]e12c2d02012-10-24 08:19:0313
[email protected]57999812013-02-24 05:40:5214#include "base/files/file_path.h"
[email protected]e12c2d02012-10-24 08:19:0315
16namespace drive {
[email protected]741a1772014-07-01 18:40:2917class ResourceEntry;
[email protected]e12c2d02012-10-24 08:19:0318
[email protected]e12c2d02012-10-24 08:19:0319class FileChange {
20 public:
[email protected]741a1772014-07-01 18:40:2921 enum FileType {
lukasza66fa07f2015-06-12 18:36:4422 FILE_TYPE_NO_INFO,
[email protected]741a1772014-07-01 18:40:2923 FILE_TYPE_FILE,
24 FILE_TYPE_DIRECTORY,
[email protected]e12c2d02012-10-24 08:19:0325 };
26
[email protected]741a1772014-07-01 18:40:2927 enum ChangeType {
lukasza66fa07f2015-06-12 18:36:4428 CHANGE_TYPE_ADD_OR_UPDATE,
29 CHANGE_TYPE_DELETE,
[email protected]741a1772014-07-01 18:40:2930 };
31
32 class Change {
33 public:
34 Change(ChangeType change, FileType file_type);
35
lukasza66fa07f2015-06-12 18:36:4436 bool IsAddOrUpdate() const { return change_ == CHANGE_TYPE_ADD_OR_UPDATE; }
37 bool IsDelete() const { return change_ == CHANGE_TYPE_DELETE; }
[email protected]741a1772014-07-01 18:40:2938
39 bool IsFile() const { return file_type_ == FILE_TYPE_FILE; }
40 bool IsDirectory() const { return file_type_ == FILE_TYPE_DIRECTORY; }
41 bool IsTypeUnknown() const { return !IsFile() && !IsDirectory(); }
42
43 ChangeType change() const { return change_; }
44 FileType file_type() const { return file_type_; }
45
46 std::string DebugString() const;
47
48 bool operator==(const Change& that) const {
49 return change() == that.change() && file_type() == that.file_type();
50 }
51
52 private:
53 ChangeType change_;
54 FileType file_type_;
55 };
56
57 class ChangeList {
58 public:
59 typedef std::deque<Change> List;
60
61 ChangeList();
vmpstrb6449d512016-02-25 23:55:4062 ChangeList(const ChangeList& other);
[email protected]741a1772014-07-01 18:40:2963 ~ChangeList();
64
65 // Updates the list with the |new_change|.
66 void Update(const Change& new_change);
67
68 size_t size() const { return list_.size(); }
69 bool empty() const { return list_.empty(); }
70 void clear() { list_.clear(); }
71 const List& list() const { return list_; }
72 const Change& front() const { return list_.front(); }
73 const Change& back() const { return list_.back(); }
74
75 ChangeList PopAndGetNewList() const;
76
77 std::string DebugString() const;
78
79 private:
80 List list_;
81 };
82
83 public:
84 typedef std::map<base::FilePath, FileChange::ChangeList> Map;
85
86 FileChange();
[email protected]e12c2d02012-10-24 08:19:0387 ~FileChange();
88
[email protected]741a1772014-07-01 18:40:2989 void Update(const base::FilePath file_path,
90 const FileChange::Change& new_change);
91 void Update(const base::FilePath file_path,
92 const FileChange::ChangeList& list);
93 void Update(const base::FilePath file_path,
94 FileType type,
95 FileChange::ChangeType change);
96 void Update(const base::FilePath file_path,
97 const ResourceEntry& entry,
98 FileChange::ChangeType change);
99 void Apply(const FileChange& new_changed_files);
[email protected]e12c2d02012-10-24 08:19:03100
[email protected]741a1772014-07-01 18:40:29101 const Map& map() const { return map_; }
102
103 size_t size() const { return map_.size(); }
104 bool empty() const { return map_.empty(); }
105 void ClearForTest() { map_.clear(); }
106 size_t CountDirectory(const base::FilePath& directory_path) const;
107 size_t count(const base::FilePath& file_path) const {
108 return map_.count(file_path);
[email protected]e12c2d02012-10-24 08:19:03109 }
110
[email protected]741a1772014-07-01 18:40:29111 std::string DebugString() const;
[email protected]e12c2d02012-10-24 08:19:03112
113 private:
[email protected]741a1772014-07-01 18:40:29114 Map map_;
[email protected]e12c2d02012-10-24 08:19:03115};
116
117} // namespace drive
118
lukasza76b4a982015-08-08 00:36:39119#endif // COMPONENTS_DRIVE_FILE_CHANGE_H_