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.
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_
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"
20 class ProfileOAuth2TokenService
;
21 class SigninManagerBase
;
23 namespace drive
{ class DriveUploaderInterface
; }
25 namespace sync_file_system
{
26 namespace drive_backend
{
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
> {
37 // The resulting status of EnsureTitleUniqueness.
38 enum EnsureUniquenessStatus
{
43 typedef base::Callback
<void(google_apis::GDataErrorCode
,
44 EnsureUniquenessStatus status
,
45 scoped_ptr
<google_apis::ResourceEntry
> entry
)>
46 EnsureUniquenessCallback
;
48 APIUtil(Profile
* profile
, const base::FilePath
& temp_dir_path
);
51 virtual void AddObserver(APIUtilObserver
* observer
) OVERRIDE
;
52 virtual void RemoveObserver(APIUtilObserver
* observer
) OVERRIDE
;
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
);
59 // APIUtilInterface overrides.
60 virtual void GetDriveDirectoryForSyncRoot(const ResourceIdCallback
& callback
)
62 virtual void GetDriveDirectoryForOrigin(
63 const std::string
& sync_root_resource_id
,
65 const ResourceIdCallback
& callback
) OVERRIDE
;
66 virtual void GetLargestChangeStamp(const ChangeStampCallback
& callback
)
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
;
97 static std::string
GetSyncRootDirectoryName();
98 static std::string
OriginToDirectoryTitle(const GURL
& origin
);
99 static GURL
DirectoryTitleToOrigin(const std::string
& title
);
101 // DriveServiceObserver overrides.
102 virtual void OnReadyToSendRequests() OVERRIDE
;
104 // ConnectionTypeObserver overrides.
105 virtual void OnConnectionTypeChanged(
106 net::NetworkChangeNotifier::ConnectionType type
) OVERRIDE
;
109 typedef int64 UploadKey
;
110 typedef std::map
<UploadKey
, UploadFileCallback
> UploadCallbackMap
;
112 friend class APIUtilTest
;
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
);
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
);
126 void DidGetDriveRootResourceIdForGetSyncRoot(
127 const ResourceIdCallback
& callback
,
128 google_apis::GDataErrorCode error
);
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
);
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::ResourceEntry
> entry
);
142 void DidEnsureUniquenessForCreateDirectory(
143 const ResourceIdCallback
& callback
,
144 google_apis::GDataErrorCode error
,
145 EnsureUniquenessStatus status
,
146 scoped_ptr
<google_apis::ResourceEntry
> entry
);
148 void SearchByTitle(const std::string
& title
,
149 const std::string
& directory_resource_id
,
150 const ResourceListCallback
& callback
);
152 void DidGetLargestChangeStamp(
153 const ChangeStampCallback
& callback
,
154 google_apis::GDataErrorCode error
,
155 scoped_ptr
<google_apis::AboutResource
> about_resource
);
157 void DidGetDriveRootResourceIdForEnsureSyncRoot(
158 const std::string
& sync_root_resource_id
,
159 google_apis::GDataErrorCode error
);
161 void DidGetResourceList(const ResourceListCallback
& callback
,
162 google_apis::GDataErrorCode error
,
163 scoped_ptr
<google_apis::ResourceList
> resource_list
);
165 void DidGetResourceEntry(const ResourceEntryCallback
& callback
,
166 google_apis::GDataErrorCode error
,
167 scoped_ptr
<google_apis::ResourceEntry
> entry
);
169 void DidGetTemporaryFileForDownload(
170 const std::string
& resource_id
,
171 const std::string
& local_file_md5
,
172 scoped_ptr
<webkit_blob::ScopedFile
> local_file
,
173 const DownloadFileCallback
& callback
,
176 void DownloadFileInternal(const std::string
& local_file_md5
,
177 scoped_ptr
<webkit_blob::ScopedFile
> local_file
,
178 const DownloadFileCallback
& callback
,
179 google_apis::GDataErrorCode error
,
180 scoped_ptr
<google_apis::ResourceEntry
> entry
);
182 void DidDownloadFile(scoped_ptr
<google_apis::ResourceEntry
> entry
,
183 scoped_ptr
<webkit_blob::ScopedFile
> local_file
,
184 const DownloadFileCallback
& callback
,
185 google_apis::GDataErrorCode error
,
186 const base::FilePath
& downloaded_file_path
);
188 void DidUploadNewFile(const std::string
& parent_resource_id
,
189 const std::string
& title
,
190 UploadKey upload_key
,
191 google_apis::GDataErrorCode error
,
192 scoped_ptr
<google_apis::ResourceEntry
> entry
);
194 void DidEnsureUniquenessForCreateFile(
195 const std::string
& expected_resource_id
,
196 const UploadFileCallback
& callback
,
197 google_apis::GDataErrorCode error
,
198 EnsureUniquenessStatus status
,
199 scoped_ptr
<google_apis::ResourceEntry
> entry
);
201 void UploadExistingFileInternal(const std::string
& remote_file_md5
,
202 const base::FilePath
& local_file_path
,
203 const UploadFileCallback
& callback
,
204 google_apis::GDataErrorCode error
,
205 scoped_ptr
<google_apis::ResourceEntry
> entry
);
207 void DidUploadExistingFile(UploadKey upload_key
,
208 google_apis::GDataErrorCode error
,
209 scoped_ptr
<google_apis::ResourceEntry
> entry
);
211 void DeleteFileInternal(const std::string
& remote_file_md5
,
212 const GDataErrorCallback
& callback
,
213 google_apis::GDataErrorCode error
,
214 scoped_ptr
<google_apis::ResourceEntry
> entry
);
216 void DidDeleteFile(const GDataErrorCallback
& callback
,
217 google_apis::GDataErrorCode error
);
219 void EnsureTitleUniqueness(const std::string
& parent_resource_id
,
220 const std::string
& expected_title
,
221 const EnsureUniquenessCallback
& callback
);
222 void DidListEntriesToEnsureUniqueness(
223 const std::string
& parent_resource_id
,
224 const std::string
& expected_title
,
225 const EnsureUniquenessCallback
& callback
,
226 google_apis::GDataErrorCode error
,
227 scoped_ptr
<google_apis::ResourceList
> feed
);
228 void DeleteEntriesForEnsuringTitleUniqueness(
229 ScopedVector
<google_apis::ResourceEntry
> entries
,
230 const GDataErrorCallback
& callback
);
231 void DidDeleteEntriesForEnsuringTitleUniqueness(
232 ScopedVector
<google_apis::ResourceEntry
> entries
,
233 const GDataErrorCallback
& callback
,
234 google_apis::GDataErrorCode error
);
236 UploadKey
RegisterUploadCallback(const UploadFileCallback
& callback
);
237 UploadFileCallback
GetAndUnregisterUploadCallback(UploadKey key
);
238 void CancelAllUploads(google_apis::GDataErrorCode error
);
240 std::string
GetRootResourceId() const;
242 scoped_ptr
<drive::DriveServiceInterface
> drive_service_
;
243 scoped_ptr
<drive::DriveUploaderInterface
> drive_uploader_
;
245 ProfileOAuth2TokenService
* oauth_service_
;
246 SigninManagerBase
* signin_manager_
;
248 UploadCallbackMap upload_callback_map_
;
249 UploadKey upload_next_key_
;
251 base::FilePath temp_dir_path_
;
253 std::string root_resource_id_
;
255 bool has_initialized_token_
;
257 ObserverList
<APIUtilObserver
> observers_
;
259 DISALLOW_COPY_AND_ASSIGN(APIUtil
);
262 } // namespace drive_backend
263 } // namespace sync_file_system
265 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_V1_API_UTIL_H_