OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_ |
6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_ | 6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
16 #include "base/platform_file.h" | 16 #include "base/platform_file.h" |
17 #include "base/timer.h" | 17 #include "base/timer.h" |
18 #include "chrome/browser/chromeos/gdata/gdata_cache.h" | 18 #include "chrome/browser/chromeos/gdata/gdata_cache.h" |
19 #include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h" | 19 #include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h" |
20 #include "chrome/browser/chromeos/gdata/gdata_errorcode.h" | 20 #include "chrome/browser/chromeos/gdata/gdata_errorcode.h" |
21 #include "chrome/browser/chromeos/gdata/gdata_files.h" | 21 #include "chrome/browser/chromeos/gdata/gdata_files.h" |
| 22 #include "chrome/browser/chromeos/gdata/gdata_wapi_feed_loader.h" |
22 #include "chrome/browser/chromeos/gdata/gdata_wapi_feed_processor.h" | 23 #include "chrome/browser/chromeos/gdata/gdata_wapi_feed_processor.h" |
23 #include "chrome/browser/prefs/pref_change_registrar.h" | 24 #include "chrome/browser/prefs/pref_change_registrar.h" |
24 #include "content/public/browser/notification_observer.h" | 25 #include "content/public/browser/notification_observer.h" |
25 | 26 |
26 namespace base { | 27 namespace base { |
27 | 28 |
28 class SequencedTaskRunner; | 29 class SequencedTaskRunner; |
29 | 30 |
30 } // namespace base | 31 } // namespace base |
31 | 32 |
32 namespace gdata { | 33 namespace gdata { |
33 | 34 |
34 class DocumentsServiceInterface; | 35 class DocumentsServiceInterface; |
35 class DriveWebAppsRegistryInterface; | 36 class DriveWebAppsRegistryInterface; |
36 class GDataWapiFeedLoader; | 37 class GDataWapiFeedLoader; |
37 struct GetDocumentsParams; | |
38 struct GetDocumentsUiState; | |
39 struct UploadFileInfo; | 38 struct UploadFileInfo; |
40 | 39 |
41 // Callback run as a response to LoadFromServer. | |
42 // | |
43 // TODO(satorux): Move this to a new file: crbug.com/138268 | |
44 typedef base::Callback<void(GetDocumentsParams* params, | |
45 GDataFileError error)> | |
46 LoadDocumentFeedCallback; | |
47 | |
48 // GDataWapiFeedLoader is used to load feeds from WAPI (codename for | |
49 // Documents List API) and load the cached proto file. | |
50 // | |
51 // TODO(satorux): Move this to a new file: crbug.com/138268 | |
52 class GDataWapiFeedLoader { | |
53 public: | |
54 // Used to notify events from the loader. | |
55 // All events are notified on UI thread. | |
56 class Observer { | |
57 public: | |
58 // Triggered when a content of a directory has been changed. | |
59 // |directory_path| is a virtual directory path representing the | |
60 // changed directory. | |
61 virtual void OnDirectoryChanged(const FilePath& directory_path) {} | |
62 | |
63 // Triggered when a document feed is fetched. |num_accumulated_entries| | |
64 // tells the number of entries fetched so far. | |
65 virtual void OnDocumentFeedFetched(int num_accumulated_entries) {} | |
66 | |
67 // Triggered when the feed from the server is loaded. | |
68 virtual void OnFeedFromServerLoaded() {} | |
69 | |
70 protected: | |
71 virtual ~Observer() {} | |
72 }; | |
73 | |
74 GDataWapiFeedLoader( | |
75 GDataDirectoryService* directory_service, | |
76 DocumentsServiceInterface* documents_service, | |
77 DriveWebAppsRegistryInterface* webapps_registry, | |
78 GDataCache* cache, | |
79 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_); | |
80 ~GDataWapiFeedLoader(); | |
81 | |
82 // Adds and removes the observer. | |
83 void AddObserver(Observer* observer); | |
84 void RemoveObserver(Observer* observer); | |
85 | |
86 // Starts root feed load from the cache. If successful, it will try to find | |
87 // the file upon retrieval completion. In addition to that, it will | |
88 // initiate retrieval of the root feed from the server unless | |
89 // |should_load_from_server| is set to false. |should_load_from_server| is | |
90 // false only for testing. | |
91 void LoadFromCache(bool should_load_from_server, | |
92 const FilePath& search_file_path, | |
93 const FindEntryCallback& callback); | |
94 | |
95 // Starts root feed load from the server. Value of |start_changestamp| | |
96 // determines the type of feed to load - 0 means root feed, every other | |
97 // value would trigger delta feed. | |
98 // In the case of loading the root feed we use |root_feed_changestamp| as its | |
99 // initial changestamp value since it does not come with that info. | |
100 // When done |load_feed_callback| is invoked. | |
101 // |entry_found_callback| is used only when this is invoked while searching | |
102 // for file info, and is used in |load_feed_callback|. If successful, it will | |
103 // try to find the file upon retrieval completion. | |
104 // |should_fetch_multiple_feeds| is true iff don't want to stop feed loading | |
105 // after we retrieve first feed chunk. | |
106 // If invoked as a part of content search, query will be set in | |
107 // |search_query|. | |
108 // If |feed_to_load| is set, this is feed url that will be used to load feed. | |
109 void LoadFromServer( | |
110 ContentOrigin initial_origin, | |
111 int start_changestamp, | |
112 int root_feed_changestamp, | |
113 bool should_fetch_multiple_feeds, | |
114 const FilePath& search_file_path, | |
115 const std::string& search_query, | |
116 const GURL& feed_to_load, | |
117 const std::string& directory_resource_id, | |
118 const FindEntryCallback& entry_found_callback, | |
119 const LoadDocumentFeedCallback& feed_load_callback); | |
120 | |
121 // Retrieves account metadata and determines from the last change timestamp | |
122 // if the feed content loading from the server needs to be initiated. | |
123 void ReloadFromServerIfNeeded( | |
124 ContentOrigin initial_origin, | |
125 int local_changestamp, | |
126 const FilePath& search_file_path, | |
127 const FindEntryCallback& callback); | |
128 | |
129 // Updates whole directory structure feeds collected in |feed_list|. | |
130 // On success, returns PLATFORM_FILE_OK. Record file statistics as UMA | |
131 // histograms. | |
132 // | |
133 // See comments at GDataWapiFeedProcessor::ApplyFeeds() for | |
134 // |start_changestamp| and |root_feed_changestamp|. | |
135 GDataFileError UpdateFromFeed( | |
136 const std::vector<DocumentFeed*>& feed_list, | |
137 int start_changestamp, | |
138 int root_feed_changestamp); | |
139 | |
140 private: | |
141 // Callback for handling root directory refresh from the cache. | |
142 void OnProtoLoaded(LoadRootFeedParams* params); | |
143 | |
144 // Continues handling root directory refresh after the directory service | |
145 // is fully loaded. | |
146 void ContinueWithInitializedDirectoryService(LoadRootFeedParams* params, | |
147 GDataFileError error); | |
148 | |
149 // Helper callback for handling results of metadata retrieval initiated from | |
150 // ReloadFeedFromServerIfNeeded(). This method makes a decision about fetching | |
151 // the content of the root feed during the root directory refresh process. | |
152 void OnGetAccountMetadata( | |
153 ContentOrigin initial_origin, | |
154 int local_changestamp, | |
155 const FilePath& search_file_path, | |
156 const FindEntryCallback& callback, | |
157 GDataErrorCode status, | |
158 scoped_ptr<base::Value> feed_data); | |
159 | |
160 // Callback for handling feed content fetching while searching for file info. | |
161 // This callback is invoked after async feed fetch operation that was | |
162 // invoked by StartDirectoryRefresh() completes. This callback will update | |
163 // the content of the refreshed directory object and continue initially | |
164 // started FindEntryByPath() request. | |
165 void OnFeedFromServerLoaded(GetDocumentsParams* params, | |
166 GDataFileError error); | |
167 | |
168 // Callback for handling response from |GDataDocumentsService::GetDocuments|. | |
169 // Invokes |callback| when done. | |
170 void OnGetDocuments( | |
171 ContentOrigin initial_origin, | |
172 const LoadDocumentFeedCallback& callback, | |
173 GetDocumentsParams* params, | |
174 base::TimeTicks start_time, | |
175 GDataErrorCode status, | |
176 scoped_ptr<base::Value> data); | |
177 | |
178 // Save filesystem to disk. | |
179 void SaveFileSystem(); | |
180 | |
181 // Callback for handling UI updates caused by document fetching. | |
182 void OnNotifyDocumentFeedFetched( | |
183 base::WeakPtr<GetDocumentsUiState> ui_state); | |
184 | |
185 GDataDirectoryService* directory_service_; // Not owned. | |
186 DocumentsServiceInterface* documents_service_; // Not owned. | |
187 DriveWebAppsRegistryInterface* webapps_registry_; // Not owned. | |
188 GDataCache* cache_; // Not owned. | |
189 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | |
190 ObserverList<Observer> observers_; | |
191 | |
192 // Note: This should remain the last member so it'll be destroyed and | |
193 // invalidate its weak pointers before any other members are destroyed. | |
194 base::WeakPtrFactory<GDataWapiFeedLoader> weak_ptr_factory_; | |
195 DISALLOW_COPY_AND_ASSIGN(GDataWapiFeedLoader); | |
196 }; | |
197 | |
198 // The production implementation of GDataFileSystemInterface. | 40 // The production implementation of GDataFileSystemInterface. |
199 class GDataFileSystem : public GDataFileSystemInterface, | 41 class GDataFileSystem : public GDataFileSystemInterface, |
200 public GDataWapiFeedLoader::Observer, | 42 public GDataWapiFeedLoader::Observer, |
201 public content::NotificationObserver { | 43 public content::NotificationObserver { |
202 public: | 44 public: |
203 GDataFileSystem(Profile* profile, | 45 GDataFileSystem(Profile* profile, |
204 GDataCache* cache, | 46 GDataCache* cache, |
205 DocumentsServiceInterface* documents_service, | 47 DocumentsServiceInterface* documents_service, |
206 GDataUploaderInterface* uploader, | 48 GDataUploaderInterface* uploader, |
207 DriveWebAppsRegistryInterface* webapps_registry, | 49 DriveWebAppsRegistryInterface* webapps_registry, |
(...skipping 767 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
975 // invalidate the weak pointers before any other members are destroyed. | 817 // invalidate the weak pointers before any other members are destroyed. |
976 base::WeakPtrFactory<GDataFileSystem> ui_weak_ptr_factory_; | 818 base::WeakPtrFactory<GDataFileSystem> ui_weak_ptr_factory_; |
977 // Unlike other classes, we need this as we need this to redirect a task | 819 // Unlike other classes, we need this as we need this to redirect a task |
978 // from IO thread to UI thread. | 820 // from IO thread to UI thread. |
979 base::WeakPtr<GDataFileSystem> ui_weak_ptr_; | 821 base::WeakPtr<GDataFileSystem> ui_weak_ptr_; |
980 }; | 822 }; |
981 | 823 |
982 } // namespace gdata | 824 } // namespace gdata |
983 | 825 |
984 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_ | 826 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILE_SYSTEM_H_ |
OLD | NEW |