blob: a7ec41bc183896d9281c0e2e20407d1edc70ca6d [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 <map>
11#include <string>
[email protected]e12c2d02012-10-24 08:19:0312
Brett Wilson45d6f2d2017-08-23 17:01:2413#include "base/containers/circular_deque.h"
[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);
Stuart Langleyfd57fd62018-06-19 03:45:1935 Change(ChangeType change,
36 FileType file_type,
37 const std::string& team_drive_id);
[email protected]741a1772014-07-01 18:40:2938
lukasza66fa07f2015-06-12 18:36:4439 bool IsAddOrUpdate() const { return change_ == CHANGE_TYPE_ADD_OR_UPDATE; }
40 bool IsDelete() const { return change_ == CHANGE_TYPE_DELETE; }
[email protected]741a1772014-07-01 18:40:2941
42 bool IsFile() const { return file_type_ == FILE_TYPE_FILE; }
43 bool IsDirectory() const { return file_type_ == FILE_TYPE_DIRECTORY; }
44 bool IsTypeUnknown() const { return !IsFile() && !IsDirectory(); }
45
46 ChangeType change() const { return change_; }
47 FileType file_type() const { return file_type_; }
Stuart Langleyfd57fd62018-06-19 03:45:1948 const std::string& team_drive_id() const { return team_drive_id_; }
[email protected]741a1772014-07-01 18:40:2949
50 std::string DebugString() const;
51
52 bool operator==(const Change& that) const {
Stuart Langleyfd57fd62018-06-19 03:45:1953 return change() == that.change() && file_type() == that.file_type() &&
54 team_drive_id() == that.team_drive_id();
[email protected]741a1772014-07-01 18:40:2955 }
56
57 private:
58 ChangeType change_;
59 FileType file_type_;
Stuart Langleyfd57fd62018-06-19 03:45:1960 // The team drive id, will be empty if the change is not a team drive root.
61 std::string team_drive_id_;
[email protected]741a1772014-07-01 18:40:2962 };
63
64 class ChangeList {
65 public:
Brett Wilson45d6f2d2017-08-23 17:01:2466 typedef base::circular_deque<Change> List;
[email protected]741a1772014-07-01 18:40:2967
68 ChangeList();
vmpstrb6449d512016-02-25 23:55:4069 ChangeList(const ChangeList& other);
[email protected]741a1772014-07-01 18:40:2970 ~ChangeList();
71
72 // Updates the list with the |new_change|.
73 void Update(const Change& new_change);
74
75 size_t size() const { return list_.size(); }
76 bool empty() const { return list_.empty(); }
77 void clear() { list_.clear(); }
78 const List& list() const { return list_; }
79 const Change& front() const { return list_.front(); }
80 const Change& back() const { return list_.back(); }
81
82 ChangeList PopAndGetNewList() const;
83
84 std::string DebugString() const;
85
86 private:
87 List list_;
88 };
89
90 public:
91 typedef std::map<base::FilePath, FileChange::ChangeList> Map;
92
93 FileChange();
vmpstr64511012016-04-08 19:59:3494 FileChange(const FileChange& other);
[email protected]e12c2d02012-10-24 08:19:0395 ~FileChange();
96
[email protected]741a1772014-07-01 18:40:2997 void Update(const base::FilePath file_path,
98 const FileChange::Change& new_change);
99 void Update(const base::FilePath file_path,
100 const FileChange::ChangeList& list);
101 void Update(const base::FilePath file_path,
102 FileType type,
103 FileChange::ChangeType change);
104 void Update(const base::FilePath file_path,
105 const ResourceEntry& entry,
106 FileChange::ChangeType change);
107 void Apply(const FileChange& new_changed_files);
[email protected]e12c2d02012-10-24 08:19:03108
[email protected]741a1772014-07-01 18:40:29109 const Map& map() const { return map_; }
110
111 size_t size() const { return map_.size(); }
112 bool empty() const { return map_.empty(); }
113 void ClearForTest() { map_.clear(); }
114 size_t CountDirectory(const base::FilePath& directory_path) const;
115 size_t count(const base::FilePath& file_path) const {
116 return map_.count(file_path);
[email protected]e12c2d02012-10-24 08:19:03117 }
118
[email protected]741a1772014-07-01 18:40:29119 std::string DebugString() const;
[email protected]e12c2d02012-10-24 08:19:03120
121 private:
[email protected]741a1772014-07-01 18:40:29122 Map map_;
[email protected]e12c2d02012-10-24 08:19:03123};
124
125} // namespace drive
126
lukasza76b4a982015-08-08 00:36:39127#endif // COMPONENTS_DRIVE_FILE_CHANGE_H_