summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync_file_system/remote_change_handler.h
blob: 17c504a1dd0a089dece22c1602bd39e926d6e3b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_CHANGE_HANDLER_H_
#define CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_CHANGE_HANDLER_H_

#include <map>
#include <set>
#include <string>
#include <utility>

#include "base/files/file_path.h"
#include "base/stl_util.h"
#include "base/time.h"
#include "webkit/browser/fileapi/file_system_url.h"
#include "webkit/fileapi/syncable/file_change.h"

namespace sync_file_system {

// Manages pending remote file changes.
class RemoteChangeHandler {
 public:
  enum RemoteSyncType {
    // Smaller number indicates higher priority in ChangeQueue.
    REMOTE_SYNC_TYPE_FETCH = 0,
    REMOTE_SYNC_TYPE_INCREMENTAL = 1,
    REMOTE_SYNC_TYPE_BATCH = 2,
  };

  struct RemoteChange {
    int64 changestamp;
    std::string resource_id;
    std::string md5_checksum;
    base::Time updated_time;
    RemoteSyncType sync_type;
    fileapi::FileSystemURL url;
    FileChange change;

    RemoteChange();
    RemoteChange(int64 changestamp,
                 const std::string& resource_id,
                 const std::string& md5_checksum,
                 const base::Time& updated_time,
                 RemoteSyncType sync_type,
                 const fileapi::FileSystemURL& url,
                 const FileChange& change);
    ~RemoteChange();
  };

  struct RemoteChangeComparator {
    bool operator()(const RemoteChange& left, const RemoteChange& right);
  };

  RemoteChangeHandler();
  virtual ~RemoteChangeHandler();

  // Pushes the next change into |remote_change| and returns true.
  bool GetChange(RemoteChange* remote_change);

  // Pushes the change for the |url| into |remote_change| and returns true.
  bool GetChangeForURL(const fileapi::FileSystemURL& url,
                       RemoteChange* remote_change);

  // Appends |remote_change| into the change queue. |remote_change| overrides an
  // existing change for the same URL if exists.
  void AppendChange(const RemoteChange& remote_change);

  // Removes a change for the |url|.
  bool RemoveChangeForURL(const fileapi::FileSystemURL& url);

  // Removes changes for the |origin|.
  void RemoveChangesForOrigin(const GURL& origin);

  bool HasChangesForOrigin(const GURL& origin) const;
  bool HasChanges() const { return !changes_.empty(); }
  size_t ChangesSize() const { return changes_.size(); }

 private:
  struct ChangeQueueItem {
    int64 changestamp;
    RemoteSyncType sync_type;
    fileapi::FileSystemURL url;

    ChangeQueueItem();
    ChangeQueueItem(int64 changestamp,
                    RemoteSyncType sync_type,
                    const fileapi::FileSystemURL& url);
  };

  struct ChangeQueueComparator {
    bool operator()(const ChangeQueueItem& left, const ChangeQueueItem& right);
  };

  typedef std::set<ChangeQueueItem, ChangeQueueComparator> PendingChangeQueue;

  struct ChangeMapItem {
    RemoteChange remote_change;
    PendingChangeQueue::iterator position_in_queue;

    ChangeMapItem();
    ChangeMapItem(const RemoteChange& remote_change,
                  PendingChangeQueue::iterator position_in_queue);
    ~ChangeMapItem();
  };

  // TODO(tzik): Consider using std::pair<base::FilePath, FileType> as the key
  // below to support directories and custom conflict handling.
  typedef std::map<base::FilePath::StringType, ChangeMapItem> PathToChangeMap;
  typedef std::map<GURL, PathToChangeMap> OriginToChangesMap;

  PendingChangeQueue changes_;
  OriginToChangesMap origin_to_changes_map_;

  DISALLOW_COPY_AND_ASSIGN(RemoteChangeHandler);
};

}  // namespace sync_file_system

#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_REMOTE_CHANGE_HANDLER_H_