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 #include "chrome/browser/sync_file_system/drive_backend/folder_creator.h"
7 #include "chrome/browser/drive/drive_api_util.h"
8 #include "chrome/browser/drive/drive_service_interface.h"
9 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h"
10 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
11 #include "google_apis/drive/drive_api_parser.h"
12 #include "google_apis/drive/gdata_wapi_parser.h"
15 class DriveServiceInterface
;
16 class DriveUploaderInterface
;
19 namespace google_apis
{
24 namespace sync_file_system
{
25 namespace drive_backend
{
27 FolderCreator::FolderCreator(drive::DriveServiceInterface
* drive_service
,
28 MetadataDatabase
* metadata_database
,
29 const std::string
& parent_folder_id
,
30 const std::string
& title
)
31 : drive_service_(drive_service
),
32 metadata_database_(metadata_database
),
33 parent_folder_id_(parent_folder_id
),
35 weak_ptr_factory_(this) {
38 FolderCreator::~FolderCreator() {
41 void FolderCreator::Run(const FileIDCallback
& callback
) {
42 drive_service_
->AddNewDirectory(
45 base::Bind(&FolderCreator::DidCreateFolder
,
46 weak_ptr_factory_
.GetWeakPtr(), callback
));
49 void FolderCreator::DidCreateFolder(
50 const FileIDCallback
& callback
,
51 google_apis::GDataErrorCode error
,
52 scoped_ptr
<google_apis::ResourceEntry
> entry
) {
53 SyncStatusCode status
= GDataErrorCodeToSyncStatusCode(error
);
54 if (status
!= SYNC_STATUS_OK
) {
55 callback
.Run(std::string(), status
);
59 drive_service_
->SearchByTitle(
60 title_
, parent_folder_id_
,
61 base::Bind(&FolderCreator::DidListFolders
,
62 weak_ptr_factory_
.GetWeakPtr(), callback
,
63 base::Passed(ScopedVector
<google_apis::ResourceEntry
>())));
66 void FolderCreator::DidListFolders(
67 const FileIDCallback
& callback
,
68 ScopedVector
<google_apis::ResourceEntry
> candidates
,
69 google_apis::GDataErrorCode error
,
70 scoped_ptr
<google_apis::ResourceList
> resource_list
) {
71 SyncStatusCode status
= GDataErrorCodeToSyncStatusCode(error
);
72 if (status
!= SYNC_STATUS_OK
) {
73 callback
.Run(std::string(), status
);
79 callback
.Run(std::string(), SYNC_STATUS_FAILED
);
83 candidates
.reserve(candidates
.size() + resource_list
->entries().size());
84 candidates
.insert(candidates
.end(),
85 resource_list
->entries().begin(),
86 resource_list
->entries().end());
87 resource_list
->mutable_entries()->weak_clear();
90 if (resource_list
->GetNextFeedURL(&next_feed
)) {
91 drive_service_
->GetRemainingFileList(
93 base::Bind(&FolderCreator::DidListFolders
,
94 weak_ptr_factory_
.GetWeakPtr(), callback
,
95 base::Passed(&candidates
)));
99 ScopedVector
<google_apis::FileResource
> files
;
100 files
.reserve(candidates
.size());
101 for (size_t i
= 0; i
< candidates
.size(); ++i
) {
102 files
.push_back(drive::util::ConvertResourceEntryToFileResource(
103 *candidates
[i
]).release());
106 scoped_ptr
<google_apis::ResourceEntry
> oldest
=
107 GetOldestCreatedFolderResource(candidates
.Pass());
110 callback
.Run(std::string(), SYNC_FILE_ERROR_NOT_FOUND
);
114 std::string file_id
= oldest
->resource_id();
116 metadata_database_
->UpdateByFileResourceList(
117 files
.Pass(), base::Bind(&FolderCreator::DidUpdateDatabase
,
118 weak_ptr_factory_
.GetWeakPtr(),
122 void FolderCreator::DidUpdateDatabase(const std::string
& file_id
,
123 const FileIDCallback
& callback
,
124 SyncStatusCode status
) {
125 if (status
!= SYNC_STATUS_OK
) {
126 callback
.Run(std::string(), status
);
130 DCHECK(!file_id
.empty());
131 if (!metadata_database_
->FindFileByFileID(file_id
, NULL
)) {
132 callback
.Run(std::string(), SYNC_FILE_ERROR_NOT_FOUND
);
136 callback
.Run(file_id
, status
);
139 } // namespace drive_backend
140 } // namespace sync_file_system