[email protected] | e12c2d0 | 2012-10-24 08:19:03 | [diff] [blame] | 1 | // 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 | |
lukasza | 76b4a98 | 2015-08-08 00:36:39 | [diff] [blame] | 5 | #ifndef COMPONENTS_DRIVE_FILE_CHANGE_H_ |
| 6 | #define COMPONENTS_DRIVE_FILE_CHANGE_H_ |
[email protected] | e12c2d0 | 2012-10-24 08:19:03 | [diff] [blame] | 7 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
[email protected] | 741a177 | 2014-07-01 18:40:29 | [diff] [blame] | 10 | #include <deque> |
| 11 | #include <map> |
| 12 | #include <string> |
[email protected] | e12c2d0 | 2012-10-24 08:19:03 | [diff] [blame] | 13 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
[email protected] | e12c2d0 | 2012-10-24 08:19:03 | [diff] [blame] | 15 | |
| 16 | namespace drive { |
[email protected] | 741a177 | 2014-07-01 18:40:29 | [diff] [blame] | 17 | class ResourceEntry; |
[email protected] | e12c2d0 | 2012-10-24 08:19:03 | [diff] [blame] | 18 | |
[email protected] | e12c2d0 | 2012-10-24 08:19:03 | [diff] [blame] | 19 | class FileChange { |
| 20 | public: |
[email protected] | 741a177 | 2014-07-01 18:40:29 | [diff] [blame] | 21 | enum FileType { |
lukasza | 66fa07f | 2015-06-12 18:36:44 | [diff] [blame] | 22 | FILE_TYPE_NO_INFO, |
[email protected] | 741a177 | 2014-07-01 18:40:29 | [diff] [blame] | 23 | FILE_TYPE_FILE, |
| 24 | FILE_TYPE_DIRECTORY, |
[email protected] | e12c2d0 | 2012-10-24 08:19:03 | [diff] [blame] | 25 | }; |
| 26 | |
[email protected] | 741a177 | 2014-07-01 18:40:29 | [diff] [blame] | 27 | enum ChangeType { |
lukasza | 66fa07f | 2015-06-12 18:36:44 | [diff] [blame] | 28 | CHANGE_TYPE_ADD_OR_UPDATE, |
| 29 | CHANGE_TYPE_DELETE, |
[email protected] | 741a177 | 2014-07-01 18:40:29 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | class Change { |
| 33 | public: |
| 34 | Change(ChangeType change, FileType file_type); |
| 35 | |
lukasza | 66fa07f | 2015-06-12 18:36:44 | [diff] [blame] | 36 | bool IsAddOrUpdate() const { return change_ == CHANGE_TYPE_ADD_OR_UPDATE; } |
| 37 | bool IsDelete() const { return change_ == CHANGE_TYPE_DELETE; } |
[email protected] | 741a177 | 2014-07-01 18:40:29 | [diff] [blame] | 38 | |
| 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(); |
vmpstr | b6449d51 | 2016-02-25 23:55:40 | [diff] [blame^] | 62 | ChangeList(const ChangeList& other); |
[email protected] | 741a177 | 2014-07-01 18:40:29 | [diff] [blame] | 63 | ~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] | e12c2d0 | 2012-10-24 08:19:03 | [diff] [blame] | 87 | ~FileChange(); |
| 88 | |
[email protected] | 741a177 | 2014-07-01 18:40:29 | [diff] [blame] | 89 | 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] | e12c2d0 | 2012-10-24 08:19:03 | [diff] [blame] | 100 | |
[email protected] | 741a177 | 2014-07-01 18:40:29 | [diff] [blame] | 101 | 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] | e12c2d0 | 2012-10-24 08:19:03 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | 741a177 | 2014-07-01 18:40:29 | [diff] [blame] | 111 | std::string DebugString() const; |
[email protected] | e12c2d0 | 2012-10-24 08:19:03 | [diff] [blame] | 112 | |
| 113 | private: |
[email protected] | 741a177 | 2014-07-01 18:40:29 | [diff] [blame] | 114 | Map map_; |
[email protected] | e12c2d0 | 2012-10-24 08:19:03 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } // namespace drive |
| 118 | |
lukasza | 76b4a98 | 2015-08-08 00:36:39 | [diff] [blame] | 119 | #endif // COMPONENTS_DRIVE_FILE_CHANGE_H_ |