Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / drive / file_change.cc
blob0bdd7bc7947d1bbe2237b02c36f1d90cf2544600
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.
5 #include "components/drive/file_change.h"
7 #include <sstream>
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11 #include "components/drive/drive.pb.h"
13 namespace drive {
15 FileChange::Change::Change(ChangeType change, FileType file_type)
16 : change_(change), file_type_(file_type) {
19 std::string FileChange::Change::DebugString() const {
20 const char* change_string = NULL;
21 switch (change()) {
22 case CHANGE_TYPE_ADD_OR_UPDATE:
23 change_string = "ADD_OR_UPDATE";
24 break;
25 case CHANGE_TYPE_DELETE:
26 change_string = "DELETE";
27 break;
29 const char* type_string = "NO_INFO";
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_NO_INFO:
38 // Keeps it as "no_info".
39 break;
41 return base::StringPrintf("%s:%s", change_string, type_string);
44 FileChange::ChangeList::ChangeList() {
46 FileChange::ChangeList::~ChangeList() {
49 void FileChange::ChangeList::Update(const Change& new_change) {
50 if (list_.empty()) {
51 list_.push_back(new_change);
52 return;
55 Change& last = list_.back();
56 if (last.IsFile() != new_change.IsFile()) {
57 list_.push_back(new_change);
58 return;
61 if (last.change() == new_change.change())
62 return;
64 // ADD + DELETE on directory -> revert
65 if (!last.IsFile() && last.IsAddOrUpdate() && new_change.IsDelete()) {
66 list_.pop_back();
67 return;
70 // DELETE + ADD/UPDATE -> ADD/UPDATE
71 // ADD/UPDATE + DELETE -> DELETE
72 last = new_change;
75 FileChange::ChangeList FileChange::ChangeList::PopAndGetNewList() const {
76 ChangeList changes;
77 changes.list_ = this->list_;
78 changes.list_.pop_front();
79 return changes;
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();
91 FileChange::FileChange() {
93 FileChange::~FileChange() {}
95 void FileChange::Update(const base::FilePath file_path,
96 const FileChange::Change& new_change) {
97 map_[file_path].Update(new_change);
100 void FileChange::Update(const base::FilePath file_path,
101 const FileChange::ChangeList& new_change) {
102 for (ChangeList::List::const_iterator it = new_change.list().begin();
103 it != new_change.list().end();
104 it++) {
105 map_[file_path].Update(*it);
109 void FileChange::Update(const base::FilePath file_path,
110 FileType file_type,
111 FileChange::ChangeType change) {
112 Update(file_path, FileChange::Change(change, file_type));
115 void FileChange::Update(const base::FilePath file_path,
116 const ResourceEntry& entry,
117 FileChange::ChangeType change) {
118 FileType type = !entry.has_file_info()
119 ? FILE_TYPE_NO_INFO
120 : entry.file_info().is_directory() ? FILE_TYPE_DIRECTORY
121 : FILE_TYPE_FILE;
123 Update(file_path, type, change);
126 void FileChange::Apply(const FileChange& new_changed_files) {
127 for (Map::const_iterator it = new_changed_files.map().begin();
128 it != new_changed_files.map().end();
129 it++) {
130 Update(it->first, it->second);
134 size_t FileChange::CountDirectory(const base::FilePath& directory_path) const {
135 size_t count = 0;
136 for (Map::const_iterator it = map_.begin(); it != map_.end(); it++) {
137 if (it->first.DirName() == directory_path)
138 count++;
140 return count;
143 std::string FileChange::DebugString() const {
144 std::ostringstream ss;
145 ss << "{ ";
146 for (FileChange::Map::const_iterator it = map_.begin(); it != map_.end();
147 it++) {
148 ss << it->first.value() << ": " << it->second.DebugString() << ", ";
150 ss << "}";
151 return ss.str();
154 } // namespace drive