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