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/drive_backend_util.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/threading/thread_restrictions.h"
12 #include "chrome/browser/drive/drive_api_util.h"
13 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.h"
14 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h"
15 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
16 #include "chrome/browser/sync_file_system/logger.h"
17 #include "google_apis/drive/drive_api_parser.h"
18 #include "third_party/leveldatabase/src/include/leveldb/status.h"
20 namespace sync_file_system
{
21 namespace drive_backend
{
23 void PutVersionToDB(int64 version
, LevelDBWrapper
* db
) {
25 db
->Put(kDatabaseVersionKey
, base::Int64ToString(version
));
28 void PutServiceMetadataToDB(const ServiceMetadata
& service_metadata
,
33 bool success
= service_metadata
.SerializeToString(&value
);
35 db
->Put(kServiceMetadataKey
, value
);
38 void PutFileMetadataToDB(const FileMetadata
& file
, LevelDBWrapper
* db
) {
42 bool success
= file
.SerializeToString(&value
);
44 db
->Put(kFileMetadataKeyPrefix
+ file
.file_id(), value
);
47 void PutFileTrackerToDB(const FileTracker
& tracker
, LevelDBWrapper
* db
) {
51 bool success
= tracker
.SerializeToString(&value
);
53 db
->Put(kFileTrackerKeyPrefix
+ base::Int64ToString(tracker
.tracker_id()),
57 void PutFileMetadataDeletionToDB(const std::string
& file_id
,
60 db
->Delete(kFileMetadataKeyPrefix
+ file_id
);
63 void PutFileTrackerDeletionToDB(int64 tracker_id
, LevelDBWrapper
* db
) {
65 db
->Delete(kFileTrackerKeyPrefix
+ base::Int64ToString(tracker_id
));
68 bool HasFileAsParent(const FileDetails
& details
, const std::string
& file_id
) {
69 for (int i
= 0; i
< details
.parent_folder_ids_size(); ++i
) {
70 if (details
.parent_folder_ids(i
) == file_id
)
76 bool IsAppRoot(const FileTracker
& tracker
) {
77 return tracker
.tracker_kind() == TRACKER_KIND_APP_ROOT
||
78 tracker
.tracker_kind() == TRACKER_KIND_DISABLED_APP_ROOT
;
81 std::string
GetTrackerTitle(const FileTracker
& tracker
) {
82 if (tracker
.has_synced_details())
83 return tracker
.synced_details().title();
87 SyncStatusCode
DriveApiErrorCodeToSyncStatusCode(
88 google_apis::DriveApiErrorCode error
) {
89 // NOTE: Please update DriveFileSyncService::UpdateServiceState when you add
90 // more error code mapping.
92 case google_apis::HTTP_SUCCESS
:
93 case google_apis::HTTP_CREATED
:
94 case google_apis::HTTP_NO_CONTENT
:
95 case google_apis::HTTP_FOUND
:
96 return SYNC_STATUS_OK
;
98 case google_apis::HTTP_NOT_MODIFIED
:
99 return SYNC_STATUS_NOT_MODIFIED
;
101 case google_apis::HTTP_CONFLICT
:
102 case google_apis::HTTP_PRECONDITION
:
103 return SYNC_STATUS_HAS_CONFLICT
;
105 case google_apis::HTTP_UNAUTHORIZED
:
106 return SYNC_STATUS_AUTHENTICATION_FAILED
;
108 case google_apis::DRIVE_NO_CONNECTION
:
109 return SYNC_STATUS_NETWORK_ERROR
;
111 case google_apis::HTTP_INTERNAL_SERVER_ERROR
:
112 case google_apis::HTTP_BAD_GATEWAY
:
113 case google_apis::HTTP_SERVICE_UNAVAILABLE
:
114 case google_apis::DRIVE_CANCELLED
:
115 case google_apis::DRIVE_NOT_READY
:
116 return SYNC_STATUS_SERVICE_TEMPORARILY_UNAVAILABLE
;
118 case google_apis::HTTP_NOT_FOUND
:
119 case google_apis::HTTP_GONE
:
120 return SYNC_FILE_ERROR_NOT_FOUND
;
122 case google_apis::DRIVE_FILE_ERROR
:
123 return SYNC_FILE_ERROR_FAILED
;
125 case google_apis::HTTP_FORBIDDEN
:
126 return SYNC_STATUS_ACCESS_FORBIDDEN
;
128 case google_apis::HTTP_RESUME_INCOMPLETE
:
129 case google_apis::HTTP_BAD_REQUEST
:
130 case google_apis::HTTP_LENGTH_REQUIRED
:
131 case google_apis::HTTP_NOT_IMPLEMENTED
:
132 case google_apis::DRIVE_PARSE_ERROR
:
133 case google_apis::DRIVE_OTHER_ERROR
:
134 return SYNC_STATUS_FAILED
;
136 case google_apis::DRIVE_NO_SPACE
:
137 return SYNC_FILE_ERROR_NO_SPACE
;
140 // There's a case where DriveService layer returns DriveApiErrorCode==-1
141 // when network is unavailable. (http://crbug.com/223042)
142 // TODO(kinuko,nhiroki): We should identify from where this undefined error
145 return SYNC_STATUS_NETWORK_ERROR
;
147 util::Log(logging::LOG_WARNING
,
149 "Got unexpected error: %d",
150 static_cast<int>(error
));
151 return SYNC_STATUS_FAILED
;
154 bool RemovePrefix(const std::string
& str
, const std::string
& prefix
,
156 if (!StartsWithASCII(str
, prefix
, true)) {
163 *out
= str
.substr(prefix
.size());
167 scoped_ptr
<ServiceMetadata
> InitializeServiceMetadata(LevelDBWrapper
* db
) {
168 base::ThreadRestrictions::AssertIOAllowed();
171 scoped_ptr
<ServiceMetadata
> service_metadata
;
174 leveldb::Status status
= db
->Get(kServiceMetadataKey
, &value
);
176 service_metadata
.reset(new ServiceMetadata
);
177 if (!service_metadata
->ParseFromString(value
))
178 service_metadata
.reset();
179 } else if (status
.IsNotFound()) {
180 service_metadata
.reset(new ServiceMetadata
);
181 service_metadata
->set_next_tracker_id(1);
184 return service_metadata
.Pass();
187 } // namespace drive_backend
188 } // namespace sync_file_system