OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h" | 5 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/threading/thread_restrictions.h" |
11 #include "chrome/browser/drive/drive_api_util.h" | 12 #include "chrome/browser/drive/drive_api_util.h" |
12 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.
h" | 13 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.
h" |
13 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h" | 14 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h" |
14 #include "chrome/browser/sync_file_system/logger.h" | 15 #include "chrome/browser/sync_file_system/logger.h" |
15 #include "google_apis/drive/drive_api_parser.h" | 16 #include "google_apis/drive/drive_api_parser.h" |
16 #include "google_apis/drive/gdata_wapi_parser.h" | 17 #include "google_apis/drive/gdata_wapi_parser.h" |
| 18 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
17 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 19 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
18 | 20 |
19 namespace sync_file_system { | 21 namespace sync_file_system { |
20 namespace drive_backend { | 22 namespace drive_backend { |
21 | 23 |
22 void PutServiceMetadataToBatch(const ServiceMetadata& service_metadata, | 24 void PutServiceMetadataToBatch(const ServiceMetadata& service_metadata, |
23 leveldb::WriteBatch* batch) { | 25 leveldb::WriteBatch* batch) { |
24 std::string value; | 26 std::string value; |
25 bool success = service_metadata.SerializeToString(&value); | 27 bool success = service_metadata.SerializeToString(&value); |
26 DCHECK(success); | 28 DCHECK(success); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 if (out) | 156 if (out) |
155 *out = str; | 157 *out = str; |
156 return false; | 158 return false; |
157 } | 159 } |
158 | 160 |
159 if (out) | 161 if (out) |
160 *out = str.substr(prefix.size()); | 162 *out = str.substr(prefix.size()); |
161 return true; | 163 return true; |
162 } | 164 } |
163 | 165 |
| 166 scoped_ptr<ServiceMetadata> ReadServiceMetadata(leveldb::DB* db) { |
| 167 base::ThreadRestrictions::AssertIOAllowed(); |
| 168 DCHECK(db); |
| 169 |
| 170 std::string value; |
| 171 leveldb::Status status = db->Get(leveldb::ReadOptions(), |
| 172 kServiceMetadataKey, |
| 173 &value); |
| 174 if (!status.ok()) |
| 175 return scoped_ptr<ServiceMetadata>(); |
| 176 |
| 177 scoped_ptr<ServiceMetadata> service_metadata(new ServiceMetadata); |
| 178 if (!service_metadata->ParseFromString(value)) { |
| 179 util::Log(logging::LOG_WARNING, FROM_HERE, |
| 180 "Failed to parse SyncServiceMetadata"); |
| 181 return scoped_ptr<ServiceMetadata>(); |
| 182 } |
| 183 return service_metadata.Pass(); |
| 184 } |
| 185 |
| 186 scoped_ptr<ServiceMetadata> InitializeServiceMetadata( |
| 187 leveldb::DB* db, leveldb::WriteBatch* batch) { |
| 188 scoped_ptr<ServiceMetadata> service_metadata = ReadServiceMetadata(db); |
| 189 if (!service_metadata) { |
| 190 service_metadata.reset(new ServiceMetadata); |
| 191 service_metadata->set_next_tracker_id(1); |
| 192 |
| 193 std::string value; |
| 194 service_metadata->SerializeToString(&value); |
| 195 if (batch) |
| 196 batch->Put(kServiceMetadataKey, value); |
| 197 } |
| 198 return service_metadata.Pass(); |
| 199 } |
| 200 |
164 } // namespace drive_backend | 201 } // namespace drive_backend |
165 } // namespace sync_file_system | 202 } // namespace sync_file_system |
OLD | NEW |