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"
14 class DriveServiceInterface
;
15 class DriveUploaderInterface
;
18 namespace sync_file_system
{
19 namespace drive_backend
{
21 FolderCreator::FolderCreator(drive::DriveServiceInterface
* drive_service
,
22 MetadataDatabase
* metadata_database
,
23 const std::string
& parent_folder_id
,
24 const std::string
& title
)
25 : drive_service_(drive_service
),
26 metadata_database_(metadata_database
),
27 parent_folder_id_(parent_folder_id
),
29 weak_ptr_factory_(this) {
32 FolderCreator::~FolderCreator() {
35 void FolderCreator::Run(const FileIDCallback
& callback
) {
36 drive_service_
->AddNewDirectory(
37 parent_folder_id_
, title_
, drive::AddNewDirectoryOptions(),
38 base::Bind(&FolderCreator::DidCreateFolder
,
39 weak_ptr_factory_
.GetWeakPtr(), callback
));
42 void FolderCreator::DidCreateFolder(
43 const FileIDCallback
& callback
,
44 google_apis::DriveApiErrorCode error
,
45 scoped_ptr
<google_apis::FileResource
> entry
) {
46 SyncStatusCode status
= DriveApiErrorCodeToSyncStatusCode(error
);
47 if (status
!= SYNC_STATUS_OK
) {
48 callback
.Run(std::string(), status
);
52 drive_service_
->SearchByTitle(
53 title_
, parent_folder_id_
,
54 base::Bind(&FolderCreator::DidListFolders
,
55 weak_ptr_factory_
.GetWeakPtr(), callback
,
56 base::Passed(ScopedVector
<google_apis::FileResource
>())));
59 void FolderCreator::DidListFolders(
60 const FileIDCallback
& callback
,
61 ScopedVector
<google_apis::FileResource
> candidates
,
62 google_apis::DriveApiErrorCode error
,
63 scoped_ptr
<google_apis::FileList
> file_list
) {
64 SyncStatusCode status
= DriveApiErrorCodeToSyncStatusCode(error
);
65 if (status
!= SYNC_STATUS_OK
) {
66 callback
.Run(std::string(), status
);
72 callback
.Run(std::string(), SYNC_STATUS_FAILED
);
76 candidates
.reserve(candidates
.size() + file_list
->items().size());
77 candidates
.insert(candidates
.end(),
78 file_list
->items().begin(),
79 file_list
->items().end());
80 file_list
->mutable_items()->weak_clear();
82 if (!file_list
->next_link().is_empty()) {
83 drive_service_
->GetRemainingFileList(
84 file_list
->next_link(),
85 base::Bind(&FolderCreator::DidListFolders
,
86 weak_ptr_factory_
.GetWeakPtr(), callback
,
87 base::Passed(&candidates
)));
91 const google_apis::FileResource
* oldest
= nullptr;
92 for (size_t i
= 0; i
< candidates
.size(); ++i
) {
93 const google_apis::FileResource
& entry
= *candidates
[i
];
94 if (!entry
.IsDirectory() || entry
.labels().is_trashed())
97 if (!oldest
|| oldest
->created_date() > entry
.created_date())
102 callback
.Run(std::string(), SYNC_FILE_ERROR_NOT_FOUND
);
106 std::string file_id
= oldest
->file_id();
108 status
= metadata_database_
->UpdateByFileResourceList(candidates
.Pass());
109 if (status
!= SYNC_STATUS_OK
) {
110 callback
.Run(std::string(), status
);
114 DCHECK(!file_id
.empty());
115 if (!metadata_database_
->FindFileByFileID(file_id
, nullptr)) {
116 callback
.Run(std::string(), SYNC_FILE_ERROR_NOT_FOUND
);
120 callback
.Run(file_id
, status
);
123 } // namespace drive_backend
124 } // namespace sync_file_system