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
5#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CHANGE_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CHANGE_H_
7
8#include <deque>
9#include <map>
10#include <string>
11
12#include "base/basictypes.h"
13#include "base/files/file_path.h"
14#include "storage/browser/fileapi/file_system_url.h"
15
16namespace drive {
17class ResourceEntry;
18
19class FileChange {
20 public:
21  enum FileType {
22    FILE_TYPE_UNKNOWN,
23    FILE_TYPE_FILE,
24    FILE_TYPE_DIRECTORY,
25  };
26
27  enum ChangeType {
28    ADD_OR_UPDATE,
29    DELETE,
30  };
31
32  class Change {
33   public:
34    Change(ChangeType change, FileType file_type);
35
36    bool IsAddOrUpdate() const { return change_ == ADD_OR_UPDATE; }
37    bool IsDelete() const { return change_ == DELETE; }
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();
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();
86  ~FileChange();
87
88  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);
99
100  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);
108  }
109
110  std::string DebugString() const;
111
112 private:
113  Map map_;
114};
115
116}  // namespace drive
117
118#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CHANGE_H_
119