summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync_file_system/remote_change_handler.cc
blob: 28557981ee7d4a00fde34b5600d7f11da2533636 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// 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.

#include "chrome/browser/sync_file_system/remote_change_handler.h"

#include "base/logging.h"
#include "webkit/common/fileapi/file_system_util.h"

namespace sync_file_system {

RemoteChangeHandler::ChangeQueueItem::ChangeQueueItem()
    : changestamp(0), sync_type(REMOTE_SYNC_TYPE_INCREMENTAL) {}

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

bool RemoteChangeHandler::ChangeQueueComparator::operator()(
    const ChangeQueueItem& left,
    const ChangeQueueItem& right) {
  if (left.changestamp != right.changestamp)
    return left.changestamp < right.changestamp;
  if (left.sync_type != right.sync_type)
    return left.sync_type < right.sync_type;
  return fileapi::FileSystemURL::Comparator()(left.url, right.url);
}

RemoteChangeHandler::RemoteChange::RemoteChange()
    : changestamp(0),
      sync_type(REMOTE_SYNC_TYPE_INCREMENTAL),
      change(FileChange::FILE_CHANGE_ADD_OR_UPDATE, SYNC_FILE_TYPE_UNKNOWN) {}

RemoteChangeHandler::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)
    : changestamp(changestamp),
      resource_id(resource_id),
      md5_checksum(md5_checksum),
      updated_time(updated_time),
      sync_type(sync_type),
      url(url),
      change(change) {}

RemoteChangeHandler::RemoteChange::~RemoteChange() {}

bool RemoteChangeHandler::RemoteChangeComparator::operator()(
    const RemoteChange& left,
    const RemoteChange& right) {
  // This should return true if |right| has higher priority than |left|.
  // Smaller changestamps have higher priorities (i.e. need to be processed
  // earlier).
  if (left.changestamp != right.changestamp)
    return left.changestamp > right.changestamp;
  return false;
}

RemoteChangeHandler::RemoteChangeHandler() {}

RemoteChangeHandler::~RemoteChangeHandler() {}

bool RemoteChangeHandler::GetChange(RemoteChange* remote_change) {
  const fileapi::FileSystemURL& url = changes_.begin()->url;
  const base::FilePath::StringType& normalized_path =
      fileapi::VirtualPath::GetNormalizedFilePath(url.path());
  DCHECK(ContainsKey(origin_to_changes_map_, url.origin()));
  PathToChangeMap* path_to_change = &origin_to_changes_map_[url.origin()];
  DCHECK(ContainsKey(*path_to_change, normalized_path));
  *remote_change = (*path_to_change)[normalized_path].remote_change;
  return true;
}

bool RemoteChangeHandler::GetChangeForURL(
    const fileapi::FileSystemURL& url,
    RemoteChange* remote_change) {
  OriginToChangesMap::iterator found_origin =
      origin_to_changes_map_.find(url.origin());
  if (found_origin == origin_to_changes_map_.end())
    return false;

  PathToChangeMap* path_to_change = &found_origin->second;
  PathToChangeMap::iterator found_change = path_to_change->find(
      fileapi::VirtualPath::GetNormalizedFilePath(url.path()));
  if (found_change == path_to_change->end())
    return false;

  *remote_change = found_change->second.remote_change;
  return true;
}

void RemoteChangeHandler::AppendChange(const RemoteChange& remote_change) {
  base::FilePath::StringType normalized_path =
      fileapi::VirtualPath::GetNormalizedFilePath(remote_change.url.path());

  PathToChangeMap* path_to_change =
      &origin_to_changes_map_[remote_change.url.origin()];
  PathToChangeMap::iterator found = path_to_change->find(normalized_path);

  // Remove an existing change for |remote_change.url.path()|.
  if (found != path_to_change->end())
    changes_.erase(found->second.position_in_queue);

  std::pair<PendingChangeQueue::iterator, bool> inserted_to_queue =
      changes_.insert(ChangeQueueItem(remote_change.changestamp,
                                      remote_change.sync_type,
                                      remote_change.url));
  DCHECK(inserted_to_queue.second);

  (*path_to_change)[normalized_path] =
      ChangeMapItem(remote_change, inserted_to_queue.first);
}

bool RemoteChangeHandler::RemoveChangeForURL(
    const fileapi::FileSystemURL& url) {
  OriginToChangesMap::iterator found_origin =
      origin_to_changes_map_.find(url.origin());
  if (found_origin == origin_to_changes_map_.end())
    return false;

  PathToChangeMap* path_to_change = &found_origin->second;
  PathToChangeMap::iterator found_change = path_to_change->find(
      fileapi::VirtualPath::GetNormalizedFilePath(url.path()));
  if (found_change == path_to_change->end())
    return false;

  changes_.erase(found_change->second.position_in_queue);
  path_to_change->erase(found_change);
  if (path_to_change->empty())
    origin_to_changes_map_.erase(found_origin);
  return true;
}

void RemoteChangeHandler::RemoveChangesForOrigin(const GURL& origin) {
  OriginToChangesMap::iterator found = origin_to_changes_map_.find(origin);
  if (found == origin_to_changes_map_.end())
    return;
  for (PathToChangeMap::iterator itr = found->second.begin();
       itr != found->second.end(); ++itr)
    changes_.erase(itr->second.position_in_queue);
  origin_to_changes_map_.erase(found);
}

bool RemoteChangeHandler::HasChangesForOrigin(const GURL& origin) const {
  return ContainsKey(origin_to_changes_map_, origin);
}

RemoteChangeHandler::ChangeMapItem::ChangeMapItem() {}

RemoteChangeHandler::ChangeMapItem::ChangeMapItem(
    const RemoteChange& remote_change,
    PendingChangeQueue::iterator position_in_queue)
  : remote_change(remote_change),
    position_in_queue(position_in_queue) {}

RemoteChangeHandler::ChangeMapItem::~ChangeMapItem() {}

}  // namespace sync_file_system