OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/drive/file_change.h" | 5 #include "chrome/browser/chromeos/drive/file_change.h" |
6 | 6 |
7 #include <sstream> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "chrome/browser/chromeos/drive/drive.pb.h" | |
12 | |
7 namespace drive { | 13 namespace drive { |
8 | 14 |
9 FileChange::FileChange(const base::FilePath& path, Type type) | 15 FileChange::Change::Change(ChangeType change, FileType file_type) |
10 : path_(path), type_(type) { | 16 : change_(change), file_type_(file_type) { |
11 } | 17 } |
12 | 18 |
19 std::string FileChange::Change::DebugString() const { | |
20 const char* change_string = NULL; | |
21 switch (change()) { | |
22 case ADD_OR_UPDATE: | |
23 change_string = "ADD_OR_UPDATE"; | |
24 break; | |
25 case DELETE: | |
26 change_string = "DELETE"; | |
27 break; | |
28 } | |
29 const char* type_string = "UNKNOWN"; | |
30 switch (file_type()) { | |
31 case FileChange::FILE_TYPE_FILE: | |
32 type_string = "FILE"; | |
33 break; | |
34 case FileChange::FILE_TYPE_DIRECTORY: | |
35 type_string = "DIRECTORY"; | |
36 break; | |
37 case FILE_TYPE_UNKNOWN: | |
38 // Keeps it as "unknown". | |
39 break; | |
40 } | |
41 return base::StringPrintf("%s:%s", change_string, type_string); | |
42 } | |
43 | |
44 FileChange::ChangeList::ChangeList() { | |
45 } | |
46 FileChange::ChangeList::~ChangeList() { | |
47 } | |
48 | |
49 void FileChange::ChangeList::Update(const Change& new_change) { | |
50 if (list_.empty()) { | |
51 list_.push_back(new_change); | |
52 return; | |
53 } | |
54 | |
55 Change& last = list_.back(); | |
56 if (last.IsFile() != new_change.IsFile()) { | |
57 list_.push_back(new_change); | |
58 return; | |
59 } | |
60 | |
61 if (last.change() == new_change.change()) | |
62 return; | |
63 | |
64 // ADD + DELETE on directory -> revert | |
65 if (!last.IsFile() && last.IsAddOrUpdate() && new_change.IsDelete()) { | |
66 list_.pop_back(); | |
67 return; | |
68 } | |
69 | |
70 // DELETE + ADD/UPDATE -> ADD/UPDATE | |
71 // ADD/UPDATE + DELETE -> DELETE | |
72 last = new_change; | |
73 } | |
74 | |
75 FileChange::ChangeList FileChange::ChangeList::PopAndGetNewList() const { | |
76 ChangeList changes; | |
77 changes.list_ = this->list_; | |
78 changes.list_.pop_front(); | |
79 return changes; | |
80 } | |
81 | |
82 std::string FileChange::ChangeList::DebugString() const { | |
83 std::ostringstream ss; | |
84 ss << "{ "; | |
85 for (size_t i = 0; i < list_.size(); ++i) | |
86 ss << list_[i].DebugString() << ", "; | |
87 ss << "}"; | |
88 return ss.str(); | |
89 } | |
90 | |
91 FileChange::FileChange() { | |
92 } | |
13 FileChange::~FileChange() {} | 93 FileChange::~FileChange() {} |
14 | 94 |
15 // static | 95 void FileChange::Update(const base::FilePath file_path, |
16 FileChangeSet FileChange::CreateSingleSet(const base::FilePath& path, | 96 const FileChange::Change& new_change) { |
17 Type type) { | 97 map_[file_path].Update(new_change); |
18 FileChangeSet result; | 98 } |
19 result.insert(FileChange(path, type)); | 99 |
20 return result; | 100 void FileChange::Update(const base::FilePath file_path, |
101 const FileChange::ChangeList& new_change) { | |
102 Map::iterator change = map_.find(file_path); | |
103 if (change == map_.end()) { | |
104 map_.insert(std::make_pair(file_path, new_change)); | |
105 return; | |
106 } | |
kinaba
2014/06/24 07:24:47
nit: maybe you can drop the above 4 lines. map_[fi
yoshiki
2014/06/24 14:09:29
Done.
| |
107 | |
108 for (ChangeList::List::const_iterator it = new_change.list().begin(); | |
109 it != new_change.list().end(); | |
110 it++) { | |
111 map_[file_path].Update(*it); | |
112 } | |
113 } | |
114 | |
115 void FileChange::Update(const base::FilePath file_path, | |
116 FileType file_type, | |
117 FileChange::ChangeType change) { | |
118 Update(file_path, FileChange::Change(change, file_type)); | |
119 } | |
120 | |
121 void FileChange::Update(const base::FilePath file_path, | |
122 const ResourceEntry& entry, | |
123 FileChange::ChangeType change) { | |
124 FileType type = | |
125 entry.deleted() ? FILE_TYPE_UNKNOWN : | |
126 entry.file_info().is_directory() ? FILE_TYPE_DIRECTORY : | |
127 FILE_TYPE_FILE; | |
128 Update(file_path, type, change); | |
129 } | |
130 | |
131 void FileChange::Apply(const FileChange& new_changed_files) { | |
132 for (Map::const_iterator it = new_changed_files.map().begin(); | |
133 it != new_changed_files.map().end(); | |
134 it++) { | |
135 Update(it->first, it->second); | |
136 } | |
137 } | |
138 | |
139 size_t FileChange::CountDirectory(const base::FilePath& directory_path) const { | |
140 size_t count = 0; | |
141 for (Map::const_iterator it = map_.begin(); it != map_.end(); it++) { | |
142 if (it->first.DirName() == directory_path) | |
143 count++; | |
144 } | |
145 return count; | |
146 } | |
147 | |
148 std::string FileChange::DebugString() const { | |
149 std::ostringstream ss; | |
150 ss << "{ "; | |
151 for (FileChange::Map::const_iterator it = map_.begin(); it != map_.end(); | |
152 it++) { | |
153 ss << it->first.value() << ": " << it->second.DebugString() << ", "; | |
154 } | |
155 ss << "}"; | |
156 return ss.str(); | |
21 } | 157 } |
22 | 158 |
23 } // namespace drive | 159 } // namespace drive |
OLD | NEW |