Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / drive_backend_util.cc
blobd41d3110559900dde17af08352b3c133449e32d7
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/strings/string_number_conversions.h"
9 #include "base/strings/string_util.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.h"
12 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h"
13 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
14 #include "chrome/browser/sync_file_system/logger.h"
15 #include "components/drive/drive_api_util.h"
16 #include "google_apis/drive/drive_api_parser.h"
17 #include "third_party/leveldatabase/src/include/leveldb/status.h"
19 namespace sync_file_system {
20 namespace drive_backend {
22 void PutVersionToDB(int64 version, LevelDBWrapper* db) {
23 DCHECK(db);
24 db->Put(kDatabaseVersionKey, base::Int64ToString(version));
27 void PutServiceMetadataToDB(const ServiceMetadata& service_metadata,
28 LevelDBWrapper* db) {
29 DCHECK(db);
31 std::string value;
32 bool success = service_metadata.SerializeToString(&value);
33 DCHECK(success);
34 db->Put(kServiceMetadataKey, value);
37 void PutFileMetadataToDB(const FileMetadata& file, LevelDBWrapper* db) {
38 DCHECK(db);
40 std::string value;
41 bool success = file.SerializeToString(&value);
42 DCHECK(success);
43 db->Put(kFileMetadataKeyPrefix + file.file_id(), value);
46 void PutFileTrackerToDB(const FileTracker& tracker, LevelDBWrapper* db) {
47 DCHECK(db);
49 std::string value;
50 bool success = tracker.SerializeToString(&value);
51 DCHECK(success);
52 db->Put(kFileTrackerKeyPrefix + base::Int64ToString(tracker.tracker_id()),
53 value);
56 void PutFileMetadataDeletionToDB(const std::string& file_id,
57 LevelDBWrapper* db) {
58 DCHECK(db);
59 db->Delete(kFileMetadataKeyPrefix + file_id);
62 void PutFileTrackerDeletionToDB(int64 tracker_id, LevelDBWrapper* db) {
63 DCHECK(db);
64 db->Delete(kFileTrackerKeyPrefix + base::Int64ToString(tracker_id));
67 bool HasFileAsParent(const FileDetails& details, const std::string& file_id) {
68 for (int i = 0; i < details.parent_folder_ids_size(); ++i) {
69 if (details.parent_folder_ids(i) == file_id)
70 return true;
72 return false;
75 bool IsAppRoot(const FileTracker& tracker) {
76 return tracker.tracker_kind() == TRACKER_KIND_APP_ROOT ||
77 tracker.tracker_kind() == TRACKER_KIND_DISABLED_APP_ROOT;
80 std::string GetTrackerTitle(const FileTracker& tracker) {
81 if (tracker.has_synced_details())
82 return tracker.synced_details().title();
83 return std::string();
86 SyncStatusCode DriveApiErrorCodeToSyncStatusCode(
87 google_apis::DriveApiErrorCode error) {
88 // NOTE: Please update DriveFileSyncService::UpdateServiceState when you add
89 // more error code mapping.
90 switch (error) {
91 case google_apis::HTTP_SUCCESS:
92 case google_apis::HTTP_CREATED:
93 case google_apis::HTTP_NO_CONTENT:
94 case google_apis::HTTP_FOUND:
95 return SYNC_STATUS_OK;
97 case google_apis::HTTP_NOT_MODIFIED:
98 return SYNC_STATUS_NOT_MODIFIED;
100 case google_apis::HTTP_CONFLICT:
101 case google_apis::HTTP_PRECONDITION:
102 return SYNC_STATUS_HAS_CONFLICT;
104 case google_apis::HTTP_UNAUTHORIZED:
105 return SYNC_STATUS_AUTHENTICATION_FAILED;
107 case google_apis::DRIVE_NO_CONNECTION:
108 return SYNC_STATUS_NETWORK_ERROR;
110 case google_apis::HTTP_INTERNAL_SERVER_ERROR:
111 case google_apis::HTTP_BAD_GATEWAY:
112 case google_apis::HTTP_SERVICE_UNAVAILABLE:
113 case google_apis::DRIVE_CANCELLED:
114 case google_apis::DRIVE_NOT_READY:
115 return SYNC_STATUS_SERVICE_TEMPORARILY_UNAVAILABLE;
117 case google_apis::HTTP_NOT_FOUND:
118 case google_apis::HTTP_GONE:
119 return SYNC_FILE_ERROR_NOT_FOUND;
121 case google_apis::DRIVE_FILE_ERROR:
122 return SYNC_FILE_ERROR_FAILED;
124 case google_apis::HTTP_FORBIDDEN:
125 return SYNC_STATUS_ACCESS_FORBIDDEN;
127 case google_apis::HTTP_RESUME_INCOMPLETE:
128 case google_apis::HTTP_BAD_REQUEST:
129 case google_apis::HTTP_LENGTH_REQUIRED:
130 case google_apis::HTTP_NOT_IMPLEMENTED:
131 case google_apis::DRIVE_PARSE_ERROR:
132 case google_apis::DRIVE_RESPONSE_TOO_LARGE:
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 NOTREACHED();
141 return SYNC_STATUS_FAILED;
144 bool RemovePrefix(const std::string& str, const std::string& prefix,
145 std::string* out) {
146 if (!base::StartsWith(str, prefix, base::CompareCase::SENSITIVE)) {
147 if (out)
148 *out = str;
149 return false;
152 if (out)
153 *out = str.substr(prefix.size());
154 return true;
157 scoped_ptr<ServiceMetadata> InitializeServiceMetadata(LevelDBWrapper* db) {
158 base::ThreadRestrictions::AssertIOAllowed();
159 DCHECK(db);
161 scoped_ptr<ServiceMetadata> service_metadata;
163 std::string value;
164 leveldb::Status status = db->Get(kServiceMetadataKey, &value);
165 if (status.ok()) {
166 service_metadata.reset(new ServiceMetadata);
167 if (!service_metadata->ParseFromString(value))
168 service_metadata.reset();
169 } else if (status.IsNotFound()) {
170 service_metadata.reset(new ServiceMetadata);
171 service_metadata->set_next_tracker_id(1);
174 return service_metadata.Pass();
177 } // namespace drive_backend
178 } // namespace sync_file_system