Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_metadata_store.h" | 5 #include "chrome/browser/sync_file_system/drive_metadata_store.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 const int64 kCurrentDatabaseVersion = 2; | 46 const int64 kCurrentDatabaseVersion = 2; |
| 47 const char kChangeStampKey[] = "CHANGE_STAMP"; | 47 const char kChangeStampKey[] = "CHANGE_STAMP"; |
| 48 const char kSyncRootDirectoryKey[] = "SYNC_ROOT_DIR"; | 48 const char kSyncRootDirectoryKey[] = "SYNC_ROOT_DIR"; |
| 49 const char kDriveMetadataKeyPrefix[] = "METADATA: "; | 49 const char kDriveMetadataKeyPrefix[] = "METADATA: "; |
| 50 const char kMetadataKeySeparator = ' '; | 50 const char kMetadataKeySeparator = ' '; |
| 51 const char kDriveBatchSyncOriginKeyPrefix[] = "BSYNC_ORIGIN: "; | 51 const char kDriveBatchSyncOriginKeyPrefix[] = "BSYNC_ORIGIN: "; |
| 52 const char kDriveIncrementalSyncOriginKeyPrefix[] = "ISYNC_ORIGIN: "; | 52 const char kDriveIncrementalSyncOriginKeyPrefix[] = "ISYNC_ORIGIN: "; |
| 53 const char kDriveDisabledOriginKeyPrefix[] = "DISABLED_ORIGIN: "; | 53 const char kDriveDisabledOriginKeyPrefix[] = "DISABLED_ORIGIN: "; |
| 54 const size_t kDriveMetadataKeyPrefixLength = arraysize(kDriveMetadataKeyPrefix); | 54 const size_t kDriveMetadataKeyPrefixLength = arraysize(kDriveMetadataKeyPrefix); |
| 55 | 55 |
| 56 const base::FilePath::CharType kV0FormatPathPrefix[] = | 56 std::string RemovePrefix(const std::string& str, const std::string& prefix) { |
|
calvinlo
2013/05/29 08:36:52
Can we have a small test for this added?
nhiroki
2013/05/30 06:15:48
I think we don't have to add test since this funct
| |
| 57 FILE_PATH_LITERAL("drive/"); | 57 if (StartsWithASCII(str, prefix, true)) |
| 58 | 58 return str.substr(prefix.size()); |
| 59 bool ParseV0FormatFileSystemURLString(const GURL& url, | 59 return str; |
| 60 GURL* origin, | |
| 61 base::FilePath* path) { | |
| 62 fileapi::FileSystemType mount_type; | |
| 63 base::FilePath virtual_path; | |
| 64 | |
| 65 if (!fileapi::FileSystemURL::ParseFileSystemSchemeURL( | |
| 66 url, origin, &mount_type, &virtual_path) || | |
| 67 mount_type != fileapi::kFileSystemTypeExternal) { | |
| 68 NOTREACHED() << "Failed to parse filesystem scheme URL"; | |
| 69 return false; | |
| 70 } | |
| 71 | |
| 72 base::FilePath::StringType prefix = | |
| 73 base::FilePath(kV0FormatPathPrefix).NormalizePathSeparators().value(); | |
| 74 if (virtual_path.value().substr(0, prefix.size()) != prefix) | |
| 75 return false; | |
| 76 | |
| 77 *path = base::FilePath(virtual_path.value().substr(prefix.size())); | |
| 78 return true; | |
| 79 } | 60 } |
| 80 | 61 |
| 81 std::string FileSystemURLToMetadataKey(const FileSystemURL& url) { | 62 std::string FileSystemURLToMetadataKey(const FileSystemURL& url) { |
| 82 return kDriveMetadataKeyPrefix + url.origin().spec() + | 63 return kDriveMetadataKeyPrefix + url.origin().spec() + |
| 83 kMetadataKeySeparator + url.path().AsUTF8Unsafe(); | 64 kMetadataKeySeparator + url.path().AsUTF8Unsafe(); |
| 84 } | 65 } |
| 85 | 66 |
| 86 void MetadataKeyToOriginAndPath(const std::string& metadata_key, | 67 void MetadataKeyToOriginAndPath(const std::string& metadata_key, |
| 87 GURL* origin, | 68 GURL* origin, |
| 88 base::FilePath* path) { | 69 base::FilePath* path) { |
| 89 std::string key_body(metadata_key.begin() + kDriveMetadataKeyPrefixLength - 1, | 70 std::string key_body(RemovePrefix(metadata_key, kDriveMetadataKeyPrefix)); |
| 90 metadata_key.end()); | |
| 91 size_t separator_position = key_body.find(kMetadataKeySeparator); | 71 size_t separator_position = key_body.find(kMetadataKeySeparator); |
| 92 *origin = GURL(key_body.substr(0, separator_position)); | 72 *origin = GURL(key_body.substr(0, separator_position)); |
| 93 *path = base::FilePath::FromUTF8Unsafe( | 73 *path = base::FilePath::FromUTF8Unsafe( |
| 94 key_body.substr(separator_position + 1)); | 74 key_body.substr(separator_position + 1)); |
| 95 } | 75 } |
| 96 | 76 |
| 97 bool UpdateResourceIdMap(ResourceIdByOrigin* map, | 77 bool UpdateResourceIdMap(ResourceIdByOrigin* map, |
| 98 OriginByResourceId* reverse_map, | 78 OriginByResourceId* reverse_map, |
| 99 const GURL& origin, | 79 const GURL& origin, |
| 100 const std::string& resource_id) { | 80 const std::string& resource_id) { |
| 101 ResourceIdByOrigin::iterator found = map->find(origin); | 81 ResourceIdByOrigin::iterator found = map->find(origin); |
| 102 if (found == map->end()) | 82 if (found == map->end()) |
| 103 return false; | 83 return false; |
| 104 reverse_map->erase(found->second); | 84 reverse_map->erase(found->second); |
| 105 reverse_map->insert(std::make_pair(resource_id, origin)); | 85 reverse_map->insert(std::make_pair(resource_id, origin)); |
| 106 | 86 |
| 107 found->second = resource_id; | 87 found->second = resource_id; |
| 108 return true; | 88 return true; |
| 109 } | 89 } |
| 110 | 90 |
| 111 std::string RemovePrefix(const std::string& str, const std::string& prefix) { | |
| 112 if (StartsWithASCII(str, prefix, true)) | |
| 113 return str.substr(prefix.size()); | |
| 114 return str; | |
| 115 } | |
| 116 | |
| 117 } // namespace | 91 } // namespace |
| 118 | 92 |
| 119 class DriveMetadataDB { | 93 class DriveMetadataDB { |
| 120 public: | 94 public: |
| 121 enum OriginSyncType { | 95 enum OriginSyncType { |
| 122 INCREMENTAL_SYNC_ORIGIN, | 96 INCREMENTAL_SYNC_ORIGIN, |
| 123 DISABLED_ORIGIN | 97 DISABLED_ORIGIN |
| 124 }; | 98 }; |
| 125 | 99 |
| 126 typedef DriveMetadataStore::MetadataMap MetadataMap; | 100 typedef DriveMetadataStore::MetadataMap MetadataMap; |
| 127 | 101 |
| 128 DriveMetadataDB(const base::FilePath& base_dir, | 102 DriveMetadataDB(const base::FilePath& base_dir, |
| 129 base::SequencedTaskRunner* task_runner); | 103 base::SequencedTaskRunner* task_runner); |
| 130 ~DriveMetadataDB(); | 104 ~DriveMetadataDB(); |
| 131 | 105 |
| 132 SyncStatusCode Initialize(bool* created); | 106 SyncStatusCode Initialize(bool* created); |
| 133 SyncStatusCode ReadContents(DriveMetadataDBContents* contents); | 107 SyncStatusCode ReadContents(DriveMetadataDBContents* contents); |
| 134 | 108 |
| 135 SyncStatusCode MigrateDatabaseIfNeeded(); | 109 SyncStatusCode MigrateDatabaseIfNeeded(); |
| 136 SyncStatusCode MigrateFromVersion0To1Database(); | |
| 137 | 110 |
| 138 SyncStatusCode SetLargestChangestamp(int64 largest_changestamp); | 111 SyncStatusCode SetLargestChangestamp(int64 largest_changestamp); |
| 139 SyncStatusCode SetSyncRootDirectory(const std::string& resource_id); | 112 SyncStatusCode SetSyncRootDirectory(const std::string& resource_id); |
| 140 SyncStatusCode GetSyncRootDirectory(std::string* resource_id); | 113 SyncStatusCode GetSyncRootDirectory(std::string* resource_id); |
| 141 SyncStatusCode SetOriginRootDirectory(const GURL& origin, | 114 SyncStatusCode SetOriginRootDirectory(const GURL& origin, |
| 142 OriginSyncType sync_type, | 115 OriginSyncType sync_type, |
| 143 const std::string& resource_id); | 116 const std::string& resource_id); |
| 144 SyncStatusCode UpdateEntry(const FileSystemURL& url, | 117 SyncStatusCode UpdateEntry(const FileSystemURL& url, |
| 145 DriveMetadata metadata); | 118 DriveMetadata metadata); |
| 146 SyncStatusCode DeleteEntry(const FileSystemURL& url); | 119 SyncStatusCode DeleteEntry(const FileSystemURL& url); |
| (...skipping 714 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 861 if (!success) | 834 if (!success) |
| 862 return SYNC_DATABASE_ERROR_FAILED; | 835 return SYNC_DATABASE_ERROR_FAILED; |
| 863 if (database_version > kCurrentDatabaseVersion) | 836 if (database_version > kCurrentDatabaseVersion) |
| 864 return SYNC_DATABASE_ERROR_FAILED; | 837 return SYNC_DATABASE_ERROR_FAILED; |
| 865 if (database_version == kCurrentDatabaseVersion) | 838 if (database_version == kCurrentDatabaseVersion) |
| 866 return SYNC_STATUS_OK; | 839 return SYNC_STATUS_OK; |
| 867 } | 840 } |
| 868 | 841 |
| 869 switch (database_version) { | 842 switch (database_version) { |
| 870 case 0: | 843 case 0: |
| 871 MigrateFromVersion0To1Database(); | 844 drive::MigrateDatabaseFromV0ToV1(db_.get()); |
| 872 // fall-through | 845 // fall-through |
| 873 case 1: | 846 case 1: |
| 874 drive::MigrateDatabaseFromV1ToV2(db_.get()); | 847 drive::MigrateDatabaseFromV1ToV2(db_.get()); |
| 875 return SYNC_STATUS_OK; | 848 return SYNC_STATUS_OK; |
| 876 } | 849 } |
| 877 return SYNC_DATABASE_ERROR_FAILED; | 850 return SYNC_DATABASE_ERROR_FAILED; |
| 878 } | 851 } |
| 879 | 852 |
| 880 SyncStatusCode DriveMetadataDB::MigrateFromVersion0To1Database() { | |
| 881 // Version 0 database format: | |
| 882 // key: "CHANGE_STAMP" | |
| 883 // value: <Largest Changestamp> | |
| 884 // | |
| 885 // key: "SYNC_ROOT_DIR" | |
| 886 // value: <Resource ID of the sync root directory> | |
| 887 // | |
| 888 // key: "METADATA: " + | |
| 889 // <FileSystemURL serialized by SerializeSyncableFileSystemURL> | |
| 890 // value: <Serialized DriveMetadata> | |
| 891 // | |
| 892 // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin> | |
| 893 // value: <Resource ID of the drive directory for the origin> | |
| 894 // | |
| 895 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin> | |
| 896 // value: <Resource ID of the drive directory for the origin> | |
| 897 // | |
| 898 // Version 1 database format (changed keys/fields are marked with '*'): | |
| 899 // * key: "VERSION" (new) | |
| 900 // * value: 1 | |
| 901 // | |
| 902 // key: "CHANGE_STAMP" | |
| 903 // value: <Largest Changestamp> | |
| 904 // | |
| 905 // key: "SYNC_ROOT_DIR" | |
| 906 // value: <Resource ID of the sync root directory> | |
| 907 // | |
| 908 // * key: "METADATA: " + <Origin and URL> (changed) | |
| 909 // * value: <Serialized DriveMetadata> | |
| 910 // | |
| 911 // key: "BSYNC_ORIGIN: " + <URL string of a batch sync origin> | |
| 912 // value: <Resource ID of the drive directory for the origin> | |
| 913 // | |
| 914 // key: "ISYNC_ORIGIN: " + <URL string of a incremental sync origin> | |
| 915 // value: <Resource ID of the drive directory for the origin> | |
| 916 // | |
| 917 // key: "DISABLED_ORIGIN: " + <URL string of a disabled origin> | |
| 918 // value: <Resource ID of the drive directory for the origin> | |
| 919 | |
| 920 leveldb::WriteBatch write_batch; | |
| 921 write_batch.Put(kDatabaseVersionKey, "1"); | |
| 922 | |
| 923 scoped_ptr<leveldb::Iterator> itr(db_->NewIterator(leveldb::ReadOptions())); | |
| 924 for (itr->Seek(kDriveMetadataKeyPrefix); itr->Valid(); itr->Next()) { | |
| 925 std::string key = itr->key().ToString(); | |
| 926 if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true)) | |
| 927 break; | |
| 928 std::string serialized_url( | |
| 929 key.begin() + kDriveMetadataKeyPrefixLength - 1, key.end()); | |
| 930 | |
| 931 GURL origin; | |
| 932 base::FilePath path; | |
| 933 bool success = ParseV0FormatFileSystemURLString( | |
| 934 GURL(serialized_url), &origin, &path); | |
| 935 DCHECK(success) << serialized_url; | |
| 936 std::string new_key = kDriveMetadataKeyPrefix + origin.spec() + | |
| 937 kMetadataKeySeparator + path.AsUTF8Unsafe(); | |
| 938 | |
| 939 write_batch.Put(new_key, itr->value()); | |
| 940 write_batch.Delete(key); | |
| 941 } | |
| 942 return LevelDBStatusToSyncStatusCode( | |
| 943 db_->Write(leveldb::WriteOptions(), &write_batch)); | |
| 944 } | |
| 945 | |
| 946 SyncStatusCode DriveMetadataDB::SetLargestChangestamp( | 853 SyncStatusCode DriveMetadataDB::SetLargestChangestamp( |
| 947 int64 largest_changestamp) { | 854 int64 largest_changestamp) { |
| 948 DCHECK(CalledOnValidThread()); | 855 DCHECK(CalledOnValidThread()); |
| 949 DCHECK(db_.get()); | 856 DCHECK(db_.get()); |
| 950 | 857 |
| 951 leveldb::Status status = db_->Put( | 858 leveldb::Status status = db_->Put( |
| 952 leveldb::WriteOptions(), | 859 leveldb::WriteOptions(), |
| 953 kChangeStampKey, base::Int64ToString(largest_changestamp)); | 860 kChangeStampKey, base::Int64ToString(largest_changestamp)); |
| 954 return LevelDBStatusToSyncStatusCode(status); | 861 return LevelDBStatusToSyncStatusCode(status); |
| 955 } | 862 } |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1150 | 1057 |
| 1151 bool result = disabled_origins->insert( | 1058 bool result = disabled_origins->insert( |
| 1152 std::make_pair(origin, origin_resource_id)).second; | 1059 std::make_pair(origin, origin_resource_id)).second; |
| 1153 DCHECK(result); | 1060 DCHECK(result); |
| 1154 } | 1061 } |
| 1155 | 1062 |
| 1156 return SYNC_STATUS_OK; | 1063 return SYNC_STATUS_OK; |
| 1157 } | 1064 } |
| 1158 | 1065 |
| 1159 } // namespace sync_file_system | 1066 } // namespace sync_file_system |
| OLD | NEW |