BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / folder_creator.cc
blob1681184a9c14749789b3998a09e9656f54deae99
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/sync_file_system/drive_backend/drive_backend_util.h"
8 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
9 #include "components/drive/drive_api_util.h"
10 #include "components/drive/service/drive_service_interface.h"
11 #include "google_apis/drive/drive_api_parser.h"
13 namespace drive {
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),
28 title_(title),
29 weak_ptr_factory_(this) {
32 FolderCreator::~FolderCreator() {
35 void FolderCreator::Run(const FileIDCallback& callback) {
36 drive::AddNewDirectoryOptions options;
37 options.visibility = google_apis::drive::FILE_VISIBILITY_PRIVATE;
38 drive_service_->AddNewDirectory(
39 parent_folder_id_, title_, options,
40 base::Bind(&FolderCreator::DidCreateFolder,
41 weak_ptr_factory_.GetWeakPtr(), callback));
44 void FolderCreator::DidCreateFolder(
45 const FileIDCallback& callback,
46 google_apis::DriveApiErrorCode error,
47 scoped_ptr<google_apis::FileResource> entry) {
48 SyncStatusCode status = DriveApiErrorCodeToSyncStatusCode(error);
49 if (status != SYNC_STATUS_OK) {
50 callback.Run(std::string(), status);
51 return;
54 drive_service_->SearchByTitle(
55 title_, parent_folder_id_,
56 base::Bind(&FolderCreator::DidListFolders,
57 weak_ptr_factory_.GetWeakPtr(), callback,
58 base::Passed(ScopedVector<google_apis::FileResource>())));
61 void FolderCreator::DidListFolders(
62 const FileIDCallback& callback,
63 ScopedVector<google_apis::FileResource> candidates,
64 google_apis::DriveApiErrorCode error,
65 scoped_ptr<google_apis::FileList> file_list) {
66 SyncStatusCode status = DriveApiErrorCodeToSyncStatusCode(error);
67 if (status != SYNC_STATUS_OK) {
68 callback.Run(std::string(), status);
69 return;
72 if (!file_list) {
73 NOTREACHED();
74 callback.Run(std::string(), SYNC_STATUS_FAILED);
75 return;
78 candidates.reserve(candidates.size() + file_list->items().size());
79 candidates.insert(candidates.end(),
80 file_list->items().begin(),
81 file_list->items().end());
82 file_list->mutable_items()->weak_clear();
84 if (!file_list->next_link().is_empty()) {
85 drive_service_->GetRemainingFileList(
86 file_list->next_link(),
87 base::Bind(&FolderCreator::DidListFolders,
88 weak_ptr_factory_.GetWeakPtr(), callback,
89 base::Passed(&candidates)));
90 return;
93 const google_apis::FileResource* oldest = nullptr;
94 for (size_t i = 0; i < candidates.size(); ++i) {
95 const google_apis::FileResource& entry = *candidates[i];
96 if (!entry.IsDirectory() || entry.labels().is_trashed())
97 continue;
99 if (!oldest || oldest->created_date() > entry.created_date())
100 oldest = &entry;
103 if (!oldest) {
104 callback.Run(std::string(), SYNC_FILE_ERROR_NOT_FOUND);
105 return;
108 std::string file_id = oldest->file_id();
110 status = metadata_database_->UpdateByFileResourceList(candidates.Pass());
111 if (status != SYNC_STATUS_OK) {
112 callback.Run(std::string(), status);
113 return;
116 DCHECK(!file_id.empty());
117 if (!metadata_database_->FindFileByFileID(file_id, nullptr)) {
118 callback.Run(std::string(), SYNC_FILE_ERROR_NOT_FOUND);
119 return;
122 callback.Run(file_id, status);
125 } // namespace drive_backend
126 } // namespace sync_file_system