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 {
29 class ResourceMetadata;
31 // Holds information needed to fetch contents of a directory.
32 // This object is copyable.
33 class DirectoryFetchInfo {
35 DirectoryFetchInfo() : changestamp_(0) {}
36 DirectoryFetchInfo(const std::string& local_id,
37 const std::string& resource_id,
39 : local_id_(local_id),
40 resource_id_(resource_id),
41 changestamp_(changestamp) {
44 // Returns true if the object is empty.
45 bool empty() const { return local_id_.empty(); }
47 // Local ID of the directory.
48 const std::string& local_id() const { return local_id_; }
50 // Resource ID of the directory.
51 const std::string& resource_id() const { return resource_id_; }
53 // Changestamp of the directory. The changestamp is used to determine if
54 // the directory contents should be fetched.
55 int64 changestamp() const { return changestamp_; }
57 // Returns a string representation of this object.
58 std::string ToString() const;
61 const std::string local_id_;
62 const std::string resource_id_;
63 const int64 changestamp_;
66 // Class to represent a change list.
69 ChangeList(); // For tests.
70 explicit ChangeList(const google_apis::ResourceList& resource_list);
73 const std::vector<ResourceEntry>& entries() const { return entries_; }
74 std::vector<ResourceEntry>* mutable_entries() { return &entries_; }
75 const std::vector<std::string>& parent_resource_ids() const {
76 return parent_resource_ids_;
78 std::vector<std::string>* mutable_parent_resource_ids() {
79 return &parent_resource_ids_;
81 const GURL& next_url() const { return next_url_; }
82 int64 largest_changestamp() const { return largest_changestamp_; }
84 void set_largest_changestamp(int64 largest_changestamp) {
85 largest_changestamp_ = largest_changestamp;
89 std::vector<ResourceEntry> entries_;
90 std::vector<std::string> parent_resource_ids_;
92 int64 largest_changestamp_;
94 DISALLOW_COPY_AND_ASSIGN(ChangeList);
97 // ChangeListProcessor is used to process change lists, or full resource
98 // lists from WAPI (codename for Documents List API) or Google Drive API, and
99 // updates the resource metadata stored locally.
100 class ChangeListProcessor {
102 explicit ChangeListProcessor(ResourceMetadata* resource_metadata);
103 ~ChangeListProcessor();
105 // Applies change lists or full resource lists to |resource_metadata_|.
107 // |is_delta_update| determines the type of input data to process, whether
108 // it is full resource lists (false) or change lists (true).
110 // Must be run on the same task runner as |resource_metadata_| uses.
111 FileError Apply(scoped_ptr<google_apis::AboutResource> about_resource,
112 ScopedVector<ChangeList> change_lists,
113 bool is_delta_update);
115 // The set of changed directories as a result of change list processing.
116 const std::set<base::FilePath>& changed_dirs() const { return changed_dirs_; }
118 // Adds or refreshes the child entries from |change_list| to the directory.
119 static FileError RefreshDirectory(
120 ResourceMetadata* resource_metadata,
121 const DirectoryFetchInfo& directory_fetch_info,
122 scoped_ptr<ChangeList> change_list,
123 std::vector<ResourceEntry>* out_refreshed_entries);
125 // Sets |entry|'s parent_local_id.
126 static FileError SetParentLocalIdOfEntry(
127 ResourceMetadata* resource_metadata,
128 ResourceEntry* entry,
129 const std::string& parent_resource_id);
132 typedef std::map<std::string /* resource_id */, ResourceEntry>
134 typedef std::map<std::string /* resource_id */,
135 std::string /* parent_resource_id*/> ParentResourceIdMap;
137 // Applies the pre-processed metadata from entry_map_ onto the resource
138 // metadata. |about_resource| must not be null.
139 FileError ApplyEntryMap(
141 scoped_ptr<google_apis::AboutResource> about_resource);
143 // Apply |entry| to resource_metadata_.
144 FileError ApplyEntry(const ResourceEntry& entry);
146 // Adds the directories changed by the update on |entry| to |changed_dirs_|.
147 void UpdateChangedDirs(const ResourceEntry& entry);
149 ResourceMetadata* resource_metadata_; // Not owned.
151 ResourceEntryMap entry_map_;
152 ParentResourceIdMap parent_resource_id_map_;
153 std::set<base::FilePath> changed_dirs_;
155 DISALLOW_COPY_AND_ASSIGN(ChangeListProcessor);
158 } // namespace internal
161 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_PROCESSOR_H_