Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / drive_backend_util.cc
blobfe6b67686ce1ee183d21305aab873262f89a2eee
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) {
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_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
143 // code is coming.
144 if (error == -1)
145 return SYNC_STATUS_NETWORK_ERROR;
147 util::Log(logging::LOG_WARNING,
148 FROM_HERE,
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,
155 std::string* out) {
156 if (!StartsWithASCII(str, prefix, true)) {
157 if (out)
158 *out = str;
159 return false;
162 if (out)
163 *out = str.substr(prefix.size());
164 return true;
167 scoped_ptr<ServiceMetadata> InitializeServiceMetadata(LevelDBWrapper* db) {
168 base::ThreadRestrictions::AssertIOAllowed();
169 DCHECK(db);
171 scoped_ptr<ServiceMetadata> service_metadata;
173 std::string value;
174 leveldb::Status status = db->Get(kServiceMetadataKey, &value);
175 if (status.ok()) {
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