9bbf27dd337b04b2fa5822102db8a95453d76358
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / change_list_processor.h
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.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_PROCESSOR_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_PROCESSOR_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
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"
16 #include "url/gurl.h"
17
18 namespace google_apis {
19 class AboutResource;
20 class ChangeList;
21 class FileList;
22 }  // google_apis
23
24 namespace drive {
25
26 class ResourceEntry;
27
28 namespace internal {
29
30 class ResourceMetadata;
31
32 // Holds information needed to fetch contents of a directory.
33 // This object is copyable.
34 class DirectoryFetchInfo {
35  public:
36   DirectoryFetchInfo() : changestamp_(0) {}
37   DirectoryFetchInfo(const std::string& local_id,
38                      const std::string& resource_id,
39                      int64 changestamp)
40       : local_id_(local_id),
41         resource_id_(resource_id),
42         changestamp_(changestamp) {
43   }
44
45   // Returns true if the object is empty.
46   bool empty() const { return local_id_.empty(); }
47
48   // Local ID of the directory.
49   const std::string& local_id() const { return local_id_; }
50
51   // Resource ID of the directory.
52   const std::string& resource_id() const { return resource_id_; }
53
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_; }
57
58   // Returns a string representation of this object.
59   std::string ToString() const;
60
61  private:
62   const std::string local_id_;
63   const std::string resource_id_;
64   const int64 changestamp_;
65 };
66
67 // Class to represent a change list.
68 class ChangeList {
69  public:
70   ChangeList();  // For tests.
71   explicit ChangeList(const google_apis::ChangeList& change_list);
72   explicit ChangeList(const google_apis::FileList& file_list);
73   ~ChangeList();
74
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_;
79   }
80   std::vector<std::string>* mutable_parent_resource_ids() {
81     return &parent_resource_ids_;
82   }
83   const GURL& next_url() const { return next_url_; }
84   int64 largest_changestamp() const { return largest_changestamp_; }
85
86   void set_largest_changestamp(int64 largest_changestamp) {
87     largest_changestamp_ = largest_changestamp;
88   }
89
90  private:
91   std::vector<ResourceEntry> entries_;
92   std::vector<std::string> parent_resource_ids_;
93   GURL next_url_;
94   int64 largest_changestamp_;
95
96   DISALLOW_COPY_AND_ASSIGN(ChangeList);
97 };
98
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 {
103  public:
104   explicit ChangeListProcessor(ResourceMetadata* resource_metadata);
105   ~ChangeListProcessor();
106
107   // Applies change lists or full resource lists to |resource_metadata_|.
108   //
109   // |is_delta_update| determines the type of input data to process, whether
110   // it is full resource lists (false) or change lists (true).
111   //
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);
116
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_; }
119
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);
126
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);
132
133  private:
134   typedef std::map<std::string /* resource_id */, ResourceEntry>
135       ResourceEntryMap;
136   typedef std::map<std::string /* resource_id */,
137                    std::string /* parent_resource_id*/> ParentResourceIdMap;
138
139   // Applies the pre-processed metadata from entry_map_ onto the resource
140   // metadata. |about_resource| must not be null.
141   FileError ApplyEntryMap(
142       int64 changestamp,
143       scoped_ptr<google_apis::AboutResource> about_resource);
144
145   // Apply |entry| to resource_metadata_.
146   FileError ApplyEntry(const ResourceEntry& entry);
147
148   // Adds the directories changed by the update on |entry| to |changed_dirs_|.
149   void UpdateChangedDirs(const ResourceEntry& entry);
150
151   ResourceMetadata* resource_metadata_;  // Not owned.
152
153   ResourceEntryMap entry_map_;
154   ParentResourceIdMap parent_resource_id_map_;
155   std::set<base::FilePath> changed_dirs_;
156
157   DISALLOW_COPY_AND_ASSIGN(ChangeListProcessor);
158 };
159
160 }  // namespace internal
161 }  // namespace drive
162
163 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_PROCESSOR_H_