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.
5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_REMOTE_CHANGE_HANDLER_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_REMOTE_CHANGE_HANDLER_H_
13 #include "base/files/file_path.h"
14 #include "base/stl_util.h"
15 #include "base/time/time.h"
16 #include "webkit/browser/fileapi/file_system_url.h"
17 #include "webkit/browser/fileapi/syncable/file_change.h"
19 namespace sync_file_system
{
21 // Manages pending remote file changes.
22 class RemoteChangeHandler
{
26 std::string resource_id
;
27 std::string md5_checksum
;
28 base::Time updated_time
;
29 fileapi::FileSystemURL url
;
33 RemoteChange(int64 changestamp
,
34 const std::string
& resource_id
,
35 const std::string
& md5_checksum
,
36 const base::Time
& updated_time
,
37 const fileapi::FileSystemURL
& url
,
38 const FileChange
& change
);
42 struct RemoteChangeComparator
{
43 bool operator()(const RemoteChange
& left
, const RemoteChange
& right
);
46 RemoteChangeHandler();
47 virtual ~RemoteChangeHandler();
49 // Pushes the next change into |remote_change| and returns true.
50 bool GetChange(RemoteChange
* remote_change
);
52 // Pushes the change for the |url| into |remote_change| and returns true.
53 bool GetChangeForURL(const fileapi::FileSystemURL
& url
,
54 RemoteChange
* remote_change
);
56 // Appends |remote_change| into the change queue. |remote_change| overrides an
57 // existing change for the same URL if exists.
58 void AppendChange(const RemoteChange
& remote_change
);
60 // Removes a change for the |url|.
61 bool RemoveChangeForURL(const fileapi::FileSystemURL
& url
);
63 // Removes changes for the |origin|.
64 void RemoveChangesForOrigin(const GURL
& origin
);
66 bool HasChangesForOrigin(const GURL
& origin
) const;
67 bool HasChanges() const { return !changes_
.empty(); }
68 size_t ChangesSize() const { return changes_
.size(); }
71 struct ChangeQueueItem
{
73 fileapi::FileSystemURL url
;
76 ChangeQueueItem(int64 changestamp
,
77 const fileapi::FileSystemURL
& url
);
80 struct ChangeQueueComparator
{
81 bool operator()(const ChangeQueueItem
& left
, const ChangeQueueItem
& right
);
84 typedef std::set
<ChangeQueueItem
, ChangeQueueComparator
> PendingChangeQueue
;
86 struct ChangeMapItem
{
87 RemoteChange remote_change
;
88 PendingChangeQueue::iterator position_in_queue
;
91 ChangeMapItem(const RemoteChange
& remote_change
,
92 PendingChangeQueue::iterator position_in_queue
);
96 // TODO(tzik): Consider using std::pair<base::FilePath, FileType> as the key
97 // below to support directories and custom conflict handling.
98 typedef std::map
<base::FilePath::StringType
, ChangeMapItem
> PathToChangeMap
;
99 typedef std::map
<GURL
, PathToChangeMap
> OriginToChangesMap
;
101 PendingChangeQueue changes_
;
102 OriginToChangesMap origin_to_changes_map_
;
104 DISALLOW_COPY_AND_ASSIGN(RemoteChangeHandler
);
107 } // namespace sync_file_system
109 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_REMOTE_CHANGE_HANDLER_H_