blob: 9d885b88b646a79292916f89323476dd88018cd4 [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();
62 ~ChangeList();
63
64 // Updates the list with the |new_change|.
65 void Update(const Change& new_change);
66
67 size_t size() const { return list_.size(); }
68 bool empty() const { return list_.empty(); }
69 void clear() { list_.clear(); }
70 const List& list() const { return list_; }
71 const Change& front() const { return list_.front(); }
72 const Change& back() const { return list_.back(); }
73
74 ChangeList PopAndGetNewList() const;
75
76 std::string DebugString() const;
77
78 private:
79 List list_;
80 };
81
82 public:
83 typedef std::map<base::FilePath, FileChange::ChangeList> Map;
84
85 FileChange();
[email protected]e12c2d02012-10-24 08:19:0386 ~FileChange();
87
[email protected]741a1772014-07-01 18:40:2988 void Update(const base::FilePath file_path,
89 const FileChange::Change& new_change);
90 void Update(const base::FilePath file_path,
91 const FileChange::ChangeList& list);
92 void Update(const base::FilePath file_path,
93 FileType type,
94 FileChange::ChangeType change);
95 void Update(const base::FilePath file_path,
96 const ResourceEntry& entry,
97 FileChange::ChangeType change);
98 void Apply(const FileChange& new_changed_files);
[email protected]e12c2d02012-10-24 08:19:0399
[email protected]741a1772014-07-01 18:40:29100 const Map& map() const { return map_; }
101
102 size_t size() const { return map_.size(); }
103 bool empty() const { return map_.empty(); }
104 void ClearForTest() { map_.clear(); }
105 size_t CountDirectory(const base::FilePath& directory_path) const;
106 size_t count(const base::FilePath& file_path) const {
107 return map_.count(file_path);
[email protected]e12c2d02012-10-24 08:19:03108 }
109
[email protected]741a1772014-07-01 18:40:29110 std::string DebugString() const;
[email protected]e12c2d02012-10-24 08:19:03111
112 private:
[email protected]741a1772014-07-01 18:40:29113 Map map_;
[email protected]e12c2d02012-10-24 08:19:03114};
115
116} // namespace drive
117
lukasza76b4a982015-08-08 00:36:39118#endif // COMPONENTS_DRIVE_FILE_CHANGE_H_