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 // Class to represent a change list. 32 class ChangeList { 33 public: 34 explicit ChangeList(const google_apis::ResourceList& resource_list); 35 ~ChangeList(); 36 37 const std::vector<ResourceEntry>& entries() const { return entries_; } 38 std::vector<ResourceEntry>* mutable_entries() { return &entries_; } 39 const GURL& next_url() const { return next_url_; } 40 int64 largest_changestamp() const { return largest_changestamp_; } 41 42 private: 43 std::vector<ResourceEntry> entries_; 44 GURL next_url_; 45 int64 largest_changestamp_; 46 47 DISALLOW_COPY_AND_ASSIGN(ChangeList); 48 }; 49 50 // ChangeListProcessor is used to process change lists, or full resource 51 // lists from WAPI (codename for Documents List API) or Google Drive API, and 52 // updates the resource metadata stored locally. 53 class ChangeListProcessor { 54 public: 55 typedef std::map<std::string /* resource_id */, ResourceEntry> 56 ResourceEntryMap; 57 58 // Class used to record UMA stats with ConvertToMap(). 59 class ChangeListToEntryMapUMAStats; 60 61 explicit ChangeListProcessor(ResourceMetadata* resource_metadata); 62 ~ChangeListProcessor(); 63 64 // Applies change lists or full resource lists to |resource_metadata_|. 65 // 66 // |is_delta_update| determines the type of input data to process, whether 67 // it is full resource lists (false) or change lists (true). 68 // 69 // Must be run on the same task runner as |resource_metadata_| uses. 70 // 71 // TODO(hashimoto): Report error on failures. 72 void Apply(scoped_ptr<google_apis::AboutResource> about_resource, 73 ScopedVector<ChangeList> change_lists, 74 bool is_delta_update); 75 76 // Converts change lists into a ResourceEntryMap. 77 // |uma_stats| may be NULL. 78 static void ConvertToMap(ScopedVector<ChangeList> change_lists, 79 ResourceEntryMap* entry_map, 80 ChangeListToEntryMapUMAStats* uma_stats); 81 82 // The set of changed directories as a result of change list processing. 83 const std::set<base::FilePath>& changed_dirs() const { return changed_dirs_; } 84 85 private: 86 // Applies the pre-processed metadata from entry_map_ onto the resource 87 // metadata. If this is not delta update (i.e. |is_delta_update| is false), 88 // |about_resource| must not be null. 89 void ApplyEntryMap(bool is_delta_update, 90 scoped_ptr<google_apis::AboutResource> about_resource); 91 92 // Apply the next item from entry_map_ to the file system. The async 93 // version posts to the message loop to avoid recursive stack-overflow. 94 void ApplyNextEntry(); 95 96 // Apply |entry| to resource_metadata_. 97 void ApplyEntry(const ResourceEntry& entry); 98 99 // Helper function to add |entry| to its parent. Updates changed_dirs_ 100 // as a side effect. 101 void AddEntry(const ResourceEntry& entry); 102 103 // Removes entry pointed to by |resource_id| from its parent. Updates 104 // changed_dirs_ as a side effect. 105 void RemoveEntry(const ResourceEntry& entry); 106 107 // Refreshes ResourceMetadata entry that has the same resource_id as 108 // |entry| with |entry|. Updates changed_dirs_ as a side effect. 109 void RefreshEntry(const ResourceEntry& entry); 110 111 // Updates the root directory entry. changestamp will be updated. 112 void UpdateRootEntry(int64 largest_changestamp); 113 114 ResourceMetadata* resource_metadata_; // Not owned. 115 116 ResourceEntryMap entry_map_; 117 std::set<base::FilePath> changed_dirs_; 118 119 DISALLOW_COPY_AND_ASSIGN(ChangeListProcessor); 120 }; 121 122 } // namespace internal 123 } // namespace drive 124 125 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_PROCESSOR_H_ 126