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.
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_PROCESSOR_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_PROCESSOR_H_
12 #include "base/files/file_path.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "chrome/browser/chromeos/drive/file_errors.h"
18 namespace google_apis {
30 class ResourceMetadata;
32 // Holds information needed to fetch contents of a directory.
33 // This object is copyable.
34 class DirectoryFetchInfo {
36 DirectoryFetchInfo() : changestamp_(0) {}
37 DirectoryFetchInfo(const std::string& local_id,
38 const std::string& resource_id,
40 : local_id_(local_id),
41 resource_id_(resource_id),
42 changestamp_(changestamp) {
45 // Returns true if the object is empty.
46 bool empty() const { return local_id_.empty(); }
48 // Local ID of the directory.
49 const std::string& local_id() const { return local_id_; }
51 // Resource ID of the directory.
52 const std::string& resource_id() const { return resource_id_; }
54 // Changestamp of the directory. The changestamp is used to determine if
55 // the directory contents should be fetched.
56 int64 changestamp() const { return changestamp_; }
58 // Returns a string representation of this object.
59 std::string ToString() const;
62 const std::string local_id_;
63 const std::string resource_id_;
64 const int64 changestamp_;
67 // Class to represent a change list.
70 ChangeList(); // For tests.
71 explicit ChangeList(const google_apis::ChangeList& change_list);
72 explicit ChangeList(const google_apis::FileList& file_list);
75 const std::vector<ResourceEntry>& entries() const { return entries_; }
76 std::vector<ResourceEntry>* mutable_entries() { return &entries_; }
77 const std::vector<std::string>& parent_resource_ids() const {
78 return parent_resource_ids_;
80 std::vector<std::string>* mutable_parent_resource_ids() {
81 return &parent_resource_ids_;
83 const GURL& next_url() const { return next_url_; }
84 int64 largest_changestamp() const { return largest_changestamp_; }
86 void set_largest_changestamp(int64 largest_changestamp) {
87 largest_changestamp_ = largest_changestamp;
91 std::vector<ResourceEntry> entries_;
92 std::vector<std::string> parent_resource_ids_;
94 int64 largest_changestamp_;
96 DISALLOW_COPY_AND_ASSIGN(ChangeList);
99 // ChangeListProcessor is used to process change lists, or full resource
100 // lists from WAPI (codename for Documents List API) or Google Drive API, and
101 // updates the resource metadata stored locally.
102 class ChangeListProcessor {
104 explicit ChangeListProcessor(ResourceMetadata* resource_metadata);
105 ~ChangeListProcessor();
107 // Applies change lists or full resource lists to |resource_metadata_|.
109 // |is_delta_update| determines the type of input data to process, whether
110 // it is full resource lists (false) or change lists (true).
112 // Must be run on the same task runner as |resource_metadata_| uses.
113 FileError Apply(scoped_ptr<google_apis::AboutResource> about_resource,
114 ScopedVector<ChangeList> change_lists,
115 bool is_delta_update);
117 // The set of changed directories as a result of change list processing.
118 const std::set<base::FilePath>& changed_dirs() const { return changed_dirs_; }
120 // Adds or refreshes the child entries from |change_list| to the directory.
121 static FileError RefreshDirectory(
122 ResourceMetadata* resource_metadata,
123 const DirectoryFetchInfo& directory_fetch_info,
124 scoped_ptr<ChangeList> change_list,
125 std::vector<ResourceEntry>* out_refreshed_entries);
127 // Sets |entry|'s parent_local_id.
128 static FileError SetParentLocalIdOfEntry(
129 ResourceMetadata* resource_metadata,
130 ResourceEntry* entry,
131 const std::string& parent_resource_id);
134 typedef std::map<std::string /* resource_id */, ResourceEntry>
136 typedef std::map<std::string /* resource_id */,
137 std::string /* parent_resource_id*/> ParentResourceIdMap;
139 // Applies the pre-processed metadata from entry_map_ onto the resource
140 // metadata. |about_resource| must not be null.
141 FileError ApplyEntryMap(
143 scoped_ptr<google_apis::AboutResource> about_resource);
145 // Apply |entry| to resource_metadata_.
146 FileError ApplyEntry(const ResourceEntry& entry);
148 // Adds the directories changed by the update on |entry| to |changed_dirs_|.
149 void UpdateChangedDirs(const ResourceEntry& entry);
151 ResourceMetadata* resource_metadata_; // Not owned.
153 ResourceEntryMap entry_map_;
154 ParentResourceIdMap parent_resource_id_map_;
155 std::set<base::FilePath> changed_dirs_;
157 DISALLOW_COPY_AND_ASSIGN(ChangeListProcessor);
160 } // namespace internal
163 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_PROCESSOR_H_