1 // Copyright 2013 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 5 #include <sstream> 6 7 #include "base/logging.h" 8 #include "base/strings/stringprintf.h" 9 #include "chrome/browser/sync_file_system/file_change.h" 10 11 namespace sync_file_system { 12 13 FileChange::FileChange( 14 ChangeType change, 15 SyncFileType file_type) 16 : change_(change), 17 file_type_(file_type) {} 18 19 std::string FileChange::DebugString() const { 20 const char* change_string = NULL; 21 switch (change()) { 22 case FILE_CHANGE_ADD_OR_UPDATE: 23 change_string = "ADD_OR_UPDATE"; 24 break; 25 case FILE_CHANGE_DELETE: 26 change_string = "DELETE"; 27 break; 28 } 29 const char* type_string = "UNKNOWN"; 30 switch (file_type()) { 31 case SYNC_FILE_TYPE_FILE: 32 type_string = "FILE"; 33 break; 34 case SYNC_FILE_TYPE_DIRECTORY: 35 type_string = "DIRECTORY"; 36 break; 37 case SYNC_FILE_TYPE_UNKNOWN: 38 type_string = "UNKNOWN"; 39 break; 40 } 41 return base::StringPrintf("%s:%s", change_string, type_string); 42 } 43 44 FileChangeList::FileChangeList() {} 45 FileChangeList::~FileChangeList() {} 46 47 void FileChangeList::Update(const FileChange& new_change) { 48 if (list_.empty()) { 49 list_.push_back(new_change); 50 return; 51 } 52 53 FileChange& last = list_.back(); 54 if (last.IsFile() != new_change.IsFile()) { 55 list_.push_back(new_change); 56 return; 57 } 58 59 if (last.change() == new_change.change()) 60 return; 61 62 // ADD + DELETE on directory -> revert 63 if (!last.IsFile() && last.IsAddOrUpdate() && new_change.IsDelete()) { 64 list_.pop_back(); 65 return; 66 } 67 68 // DELETE + ADD/UPDATE -> ADD/UPDATE 69 // ADD/UPDATE + DELETE -> DELETE 70 last = new_change; 71 } 72 73 FileChangeList FileChangeList::PopAndGetNewList() const { 74 FileChangeList changes; 75 changes.list_ = this->list_; 76 changes.list_.pop_front(); 77 return changes; 78 } 79 80 std::string FileChangeList::DebugString() const { 81 std::ostringstream ss; 82 ss << "{ "; 83 for (size_t i = 0; i < list_.size(); ++i) 84 ss << list_[i].DebugString() << ", "; 85 ss << "}"; 86 return ss.str(); 87 } 88 89 } // namespace sync_file_system 90