Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / drive_backend_v1 / api_util.h
1 // Copyright 2013 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_SYNC_FILE_SYSTEM_DRIVE_BACKEND_V1_API_UTIL_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_V1_API_UTIL_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "chrome/browser/drive/drive_service_interface.h"
14 #include "chrome/browser/sync_file_system/drive_backend_v1/api_util_interface.h"
15 #include "net/base/network_change_notifier.h"
16 #include "webkit/common/blob/scoped_file.h"
17
18 class GURL;
19 class Profile;
20 class ProfileOAuth2TokenService;
21 class SigninManagerBase;
22
23 namespace drive { class DriveUploaderInterface; }
24
25 namespace sync_file_system {
26 namespace drive_backend {
27
28 // This class is responsible for talking to the Drive service to get and put
29 // Drive directories, files and metadata.
30 // This class is owned by DriveFileSyncService.
31 class APIUtil : public APIUtilInterface,
32                 public drive::DriveServiceObserver,
33                 public net::NetworkChangeNotifier::ConnectionTypeObserver,
34                 public base::NonThreadSafe,
35                 public base::SupportsWeakPtr<APIUtil> {
36  public:
37   // The resulting status of EnsureTitleUniqueness.
38   enum EnsureUniquenessStatus {
39     NO_DUPLICATES_FOUND,
40     RESOLVED_DUPLICATES,
41   };
42
43   typedef base::Callback<void(google_apis::GDataErrorCode,
44                               EnsureUniquenessStatus status,
45                               scoped_ptr<google_apis::ResourceEntry> entry)>
46       EnsureUniquenessCallback;
47
48   APIUtil(Profile* profile, const base::FilePath& temp_dir_path);
49   virtual ~APIUtil();
50
51   virtual void AddObserver(APIUtilObserver* observer) OVERRIDE;
52   virtual void RemoveObserver(APIUtilObserver* observer) OVERRIDE;
53
54   static scoped_ptr<APIUtil> CreateForTesting(
55       const base::FilePath& temp_dir_path,
56       scoped_ptr<drive::DriveServiceInterface> drive_service,
57       scoped_ptr<drive::DriveUploaderInterface> drive_uploader);
58
59   // APIUtilInterface overrides.
60   virtual void GetDriveDirectoryForSyncRoot(const ResourceIdCallback& callback)
61       OVERRIDE;
62   virtual void GetDriveDirectoryForOrigin(
63       const std::string& sync_root_resource_id,
64       const GURL& origin,
65       const ResourceIdCallback& callback) OVERRIDE;
66   virtual void GetLargestChangeStamp(const ChangeStampCallback& callback)
67       OVERRIDE;
68   virtual void GetResourceEntry(const std::string& resource_id,
69                                 const ResourceEntryCallback& callback) OVERRIDE;
70   virtual void ListFiles(const std::string& directory_resource_id,
71                          const ResourceListCallback& callback) OVERRIDE;
72   virtual void ListChanges(int64 start_changestamp,
73                            const ResourceListCallback& callback) OVERRIDE;
74   virtual void ContinueListing(const GURL& next_link,
75                                const ResourceListCallback& callback) OVERRIDE;
76   virtual void DownloadFile(const std::string& resource_id,
77                             const std::string& local_file_md5,
78                             const DownloadFileCallback& callback) OVERRIDE;
79   virtual void UploadNewFile(const std::string& directory_resource_id,
80                              const base::FilePath& local_file_path,
81                              const std::string& title,
82                              const UploadFileCallback& callback) OVERRIDE;
83   virtual void UploadExistingFile(const std::string& resource_id,
84                                   const std::string& remote_file_md5,
85                                   const base::FilePath& local_file_path,
86                                   const UploadFileCallback& callback) OVERRIDE;
87   virtual void CreateDirectory(const std::string& parent_resource_id,
88                                const std::string& title,
89                                const ResourceIdCallback& callback) OVERRIDE;
90   virtual bool IsAuthenticated() const OVERRIDE;
91   virtual void DeleteFile(const std::string& resource_id,
92                           const std::string& remote_file_md5,
93                           const GDataErrorCallback& callback) OVERRIDE;
94   virtual void EnsureSyncRootIsNotInMyDrive(
95       const std::string& sync_root_resource_id) OVERRIDE;
96
97   static std::string GetSyncRootDirectoryName();
98   static std::string OriginToDirectoryTitle(const GURL& origin);
99   static GURL DirectoryTitleToOrigin(const std::string& title);
100
101   // DriveServiceObserver overrides.
102   virtual void OnReadyToSendRequests() OVERRIDE;
103
104   // ConnectionTypeObserver overrides.
105   virtual void OnConnectionTypeChanged(
106       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
107
108  private:
109   typedef int64 UploadKey;
110   typedef std::map<UploadKey, UploadFileCallback> UploadCallbackMap;
111
112   friend class APIUtilTest;
113
114   // Constructor for test use.
115   APIUtil(const base::FilePath& temp_dir_path,
116           scoped_ptr<drive::DriveServiceInterface> drive_service,
117           scoped_ptr<drive::DriveUploaderInterface> drive_uploader,
118           const std::string& account_id);
119
120   void GetDriveRootResourceId(const GDataErrorCallback& callback);
121   void DidGetDriveRootResourceId(
122       const GDataErrorCallback& callback,
123       google_apis::GDataErrorCode error,
124       scoped_ptr<google_apis::AboutResource> about_resource);
125
126   void DidGetDriveRootResourceIdForGetSyncRoot(
127       const ResourceIdCallback& callback,
128       google_apis::GDataErrorCode error);
129
130   void DidGetDirectory(const std::string& parent_resource_id,
131                        const std::string& directory_name,
132                        const ResourceIdCallback& callback,
133                        google_apis::GDataErrorCode error,
134                        scoped_ptr<google_apis::ResourceList> feed);
135
136   void DidCreateDirectory(const std::string& parent_resource_id,
137                           const std::string& title,
138                           const ResourceIdCallback& callback,
139                           google_apis::GDataErrorCode error,
140                           scoped_ptr<google_apis::FileResource> entry);
141
142   void DidEnsureUniquenessForCreateDirectory(
143       const ResourceIdCallback& callback,
144       google_apis::GDataErrorCode error,
145       EnsureUniquenessStatus status,
146       scoped_ptr<google_apis::ResourceEntry> entry);
147
148   void SearchByTitle(const std::string& title,
149                      const std::string& directory_resource_id,
150                      const ResourceListCallback& callback);
151
152   void DidGetLargestChangeStamp(
153       const ChangeStampCallback& callback,
154       google_apis::GDataErrorCode error,
155       scoped_ptr<google_apis::AboutResource> about_resource);
156
157   void DidGetDriveRootResourceIdForEnsureSyncRoot(
158       const std::string& sync_root_resource_id,
159       google_apis::GDataErrorCode error);
160
161   void DidGetFileList(const ResourceListCallback& callback,
162                       google_apis::GDataErrorCode error,
163                       scoped_ptr<google_apis::FileList> file_list);
164
165   void DidGetChangeList(const ResourceListCallback& callback,
166                         google_apis::GDataErrorCode error,
167                         scoped_ptr<google_apis::ChangeList> change_list);
168
169   void DidGetFileResource(const ResourceEntryCallback& callback,
170                           google_apis::GDataErrorCode error,
171                           scoped_ptr<google_apis::FileResource> entry);
172
173   void DidGetTemporaryFileForDownload(
174       const std::string& resource_id,
175       const std::string& local_file_md5,
176       scoped_ptr<webkit_blob::ScopedFile> local_file,
177       const DownloadFileCallback& callback,
178       bool success);
179
180   void DownloadFileInternal(const std::string& local_file_md5,
181                             scoped_ptr<webkit_blob::ScopedFile> local_file,
182                             const DownloadFileCallback& callback,
183                             google_apis::GDataErrorCode error,
184                             scoped_ptr<google_apis::ResourceEntry> entry);
185
186   void DidDownloadFile(scoped_ptr<google_apis::ResourceEntry> entry,
187                        scoped_ptr<webkit_blob::ScopedFile> local_file,
188                        const DownloadFileCallback& callback,
189                        google_apis::GDataErrorCode error,
190                        const base::FilePath& downloaded_file_path);
191
192   void DidUploadNewFile(const std::string& parent_resource_id,
193                         const std::string& title,
194                         UploadKey upload_key,
195                         google_apis::GDataErrorCode error,
196                         scoped_ptr<google_apis::ResourceEntry> entry);
197
198   void DidEnsureUniquenessForCreateFile(
199       const std::string& expected_resource_id,
200       const UploadFileCallback& callback,
201       google_apis::GDataErrorCode error,
202       EnsureUniquenessStatus status,
203       scoped_ptr<google_apis::ResourceEntry> entry);
204
205   void UploadExistingFileInternal(const std::string& remote_file_md5,
206                                   const base::FilePath& local_file_path,
207                                   const UploadFileCallback& callback,
208                                   google_apis::GDataErrorCode error,
209                                   scoped_ptr<google_apis::ResourceEntry> entry);
210
211   void DidUploadExistingFile(UploadKey upload_key,
212                              google_apis::GDataErrorCode error,
213                              scoped_ptr<google_apis::ResourceEntry> entry);
214
215   void DeleteFileInternal(const std::string& remote_file_md5,
216                           const GDataErrorCallback& callback,
217                           google_apis::GDataErrorCode error,
218                           scoped_ptr<google_apis::ResourceEntry> entry);
219
220   void DidDeleteFile(const GDataErrorCallback& callback,
221                      google_apis::GDataErrorCode error);
222
223   void EnsureTitleUniqueness(const std::string& parent_resource_id,
224                              const std::string& expected_title,
225                              const EnsureUniquenessCallback& callback);
226   void DidListEntriesToEnsureUniqueness(
227       const std::string& parent_resource_id,
228       const std::string& expected_title,
229       const EnsureUniquenessCallback& callback,
230       google_apis::GDataErrorCode error,
231       scoped_ptr<google_apis::ResourceList> feed);
232   void DeleteEntriesForEnsuringTitleUniqueness(
233       ScopedVector<google_apis::ResourceEntry> entries,
234       const GDataErrorCallback& callback);
235   void DidDeleteEntriesForEnsuringTitleUniqueness(
236       ScopedVector<google_apis::ResourceEntry> entries,
237       const GDataErrorCallback& callback,
238       google_apis::GDataErrorCode error);
239
240   UploadKey RegisterUploadCallback(const UploadFileCallback& callback);
241   UploadFileCallback GetAndUnregisterUploadCallback(UploadKey key);
242   void CancelAllUploads(google_apis::GDataErrorCode error);
243
244   std::string GetRootResourceId() const;
245
246   scoped_ptr<drive::DriveServiceInterface> drive_service_;
247   scoped_ptr<drive::DriveUploaderInterface> drive_uploader_;
248
249   ProfileOAuth2TokenService* oauth_service_;
250   SigninManagerBase* signin_manager_;
251
252   UploadCallbackMap upload_callback_map_;
253   UploadKey upload_next_key_;
254
255   base::FilePath temp_dir_path_;
256
257   std::string root_resource_id_;
258
259   bool has_initialized_token_;
260
261   ObserverList<APIUtilObserver> observers_;
262
263   DISALLOW_COPY_AND_ASSIGN(APIUtil);
264 };
265
266 }  // namespace drive_backend
267 }  // namespace sync_file_system
268
269 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_V1_API_UTIL_H_