Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / drive_backend_util.cc
blob27d08dff24fc8da0c2a0a9ca782eaa18765c9c13
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/sync_file_system/drive_backend/drive_backend_constants.h"
13 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h"
14 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
15 #include "chrome/browser/sync_file_system/logger.h"
16 #include "components/drive/drive_api_util.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) {
24 DCHECK(db);
25 db->Put(kDatabaseVersionKey, base::Int64ToString(version));
28 void PutServiceMetadataToDB(const ServiceMetadata& service_metadata,
29 LevelDBWrapper* db) {
30 DCHECK(db);
32 std::string value;
33 bool success = service_metadata.SerializeToString(&value);
34 DCHECK(success);
35 db->Put(kServiceMetadataKey, value);
38 void PutFileMetadataToDB(const FileMetadata& file, LevelDBWrapper* db) {
39 DCHECK(db);
41 std::string value;
42 bool success = file.SerializeToString(&value);
43 DCHECK(success);
44 db->Put(kFileMetadataKeyPrefix + file.file_id(), value);
47 void PutFileTrackerToDB(const FileTracker& tracker, LevelDBWrapper* db) {
48 DCHECK(db);
50 std::string value;
51 bool success = tracker.SerializeToString(&value);
52 DCHECK(success);
53 db->Put(kFileTrackerKeyPrefix + base::Int64ToString(tracker.tracker_id()),
54 value);
57 void PutFileMetadataDeletionToDB(const std::string& file_id,
58 LevelDBWrapper* db) {
59 DCHECK(db);
60 db->Delete(kFileMetadataKeyPrefix + file_id);
63 void PutFileTrackerDeletionToDB(int64 tracker_id, LevelDBWrapper* db) {
64 DCHECK(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)
71 return true;
73 return false;
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();
84 return std::string();
87 SyncStatusCode DriveApiErrorCodeToSyncStatusCode(
88 google_apis::DriveApiErrorCode error) {
89 // NOTE: Please update DriveFileSyncService::UpdateServiceState when you add
90 // more error code mapping.
91 switch (error) {
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_RESPONSE_TOO_LARGE:
134 case google_apis::DRIVE_OTHER_ERROR:
135 return SYNC_STATUS_FAILED;
137 case google_apis::DRIVE_NO_SPACE:
138 return SYNC_FILE_ERROR_NO_SPACE;
141 NOTREACHED();
142 return SYNC_STATUS_FAILED;
145 bool RemovePrefix(const std::string& str, const std::string& prefix,
146 std::string* out) {
147 if (!base::StartsWith(str, prefix, base::CompareCase::SENSITIVE)) {
148 if (out)
149 *out = str;
150 return false;
153 if (out)
154 *out = str.substr(prefix.size());
155 return true;
158 scoped_ptr<ServiceMetadata> InitializeServiceMetadata(LevelDBWrapper* db) {
159 base::ThreadRestrictions::AssertIOAllowed();
160 DCHECK(db);
162 scoped_ptr<ServiceMetadata> service_metadata;
164 std::string value;
165 leveldb::Status status = db->Get(kServiceMetadataKey, &value);
166 if (status.ok()) {
167 service_metadata.reset(new ServiceMetadata);
168 if (!service_metadata->ParseFromString(value))
169 service_metadata.reset();
170 } else if (status.IsNotFound()) {
171 service_metadata.reset(new ServiceMetadata);
172 service_metadata->set_next_tracker_id(1);
175 return service_metadata.Pass();
178 } // namespace drive_backend
179 } // namespace sync_file_system