| 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/metadata_database.h" | 5 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <stack> | 8 #include <stack> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 981 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 992 // - There is no active tracker that has the same |parent| and |title|. | 992 // - There is no active tracker that has the same |parent| and |title|. |
| 993 if (!tracker->active() && CanActivateTracker(*tracker)) | 993 if (!tracker->active() && CanActivateTracker(*tracker)) |
| 994 MakeTrackerActive(tracker->tracker_id(), batch.get()); | 994 MakeTrackerActive(tracker->tracker_id(), batch.get()); |
| 995 if (tracker->dirty() && !ShouldKeepDirty(*tracker)) | 995 if (tracker->dirty() && !ShouldKeepDirty(*tracker)) |
| 996 ClearDirty(tracker, NULL); | 996 ClearDirty(tracker, NULL); |
| 997 PutFileTrackerToBatch(*tracker, batch.get()); | 997 PutFileTrackerToBatch(*tracker, batch.get()); |
| 998 | 998 |
| 999 WriteToDatabase(batch.Pass(), callback); | 999 WriteToDatabase(batch.Pass(), callback); |
| 1000 } | 1000 } |
| 1001 | 1001 |
| 1002 MetadataDatabase::ActivationStatus MetadataDatabase::TryNoSideEffectActivation( | 1002 MetadataDatabase::ActivationStatus MetadataDatabase::TryActivateTracker( |
| 1003 int64 parent_tracker_id, | 1003 int64 parent_tracker_id, |
| 1004 const std::string& file_id, | 1004 const std::string& file_id, |
| 1005 const SyncStatusCallback& callback) { | 1005 const SyncStatusCallback& callback) { |
| 1006 DCHECK(ContainsKey(tracker_by_id_, parent_tracker_id)); | 1006 DCHECK(ContainsKey(tracker_by_id_, parent_tracker_id)); |
| 1007 DCHECK(ContainsKey(file_by_id_, file_id)); | 1007 DCHECK(ContainsKey(file_by_id_, file_id)); |
| 1008 | 1008 |
| 1009 FileMetadata file; | 1009 FileMetadata file; |
| 1010 if (!FindFileByFileID(file_id, &file)) { | 1010 if (!FindFileByFileID(file_id, &file)) { |
| 1011 NOTREACHED(); | 1011 NOTREACHED(); |
| 1012 RunSoon(FROM_HERE, base::Bind(callback, SYNC_STATUS_FAILED)); | 1012 RunSoon(FROM_HERE, base::Bind(callback, SYNC_STATUS_FAILED)); |
| 1013 return ACTIVATION_PENDING; | 1013 return ACTIVATION_PENDING; |
| 1014 } | 1014 } |
| 1015 std::string title = file.details().title(); | 1015 std::string title = file.details().title(); |
| 1016 DCHECK(!HasInvalidTitle(title)); | 1016 DCHECK(!HasInvalidTitle(title)); |
| 1017 | 1017 |
| 1018 TrackerSet same_file_id; | 1018 TrackerSet same_file_id; |
| 1019 FindTrackersByFileID(file_id, &same_file_id); | 1019 FindTrackersByFileID(file_id, &same_file_id); |
| 1020 | 1020 |
| 1021 // Pick up the tracker to be activated, that has: |
| 1022 // - |parent_tarcker_id| as the parent, and |
| 1023 // - |file_id| as the tracker's |file_id|. |
| 1021 FileTracker* tracker = NULL; | 1024 FileTracker* tracker = NULL; |
| 1022 for (TrackerSet::iterator itr = same_file_id.begin(); | 1025 for (TrackerSet::iterator itr = same_file_id.begin(); |
| 1023 itr != same_file_id.end(); ++itr) { | 1026 itr != same_file_id.end(); ++itr) { |
| 1024 FileTracker* candidate = *itr; | 1027 FileTracker* candidate = *itr; |
| 1025 if (candidate->parent_tracker_id() != parent_tracker_id) | 1028 if (candidate->parent_tracker_id() != parent_tracker_id) |
| 1026 continue; | 1029 continue; |
| 1027 | 1030 |
| 1028 if (candidate->has_synced_details() && | 1031 if (candidate->has_synced_details() && |
| 1029 candidate->synced_details().title() != title) | 1032 candidate->synced_details().title() != title) |
| 1030 continue; | 1033 continue; |
| 1031 tracker = candidate; | 1034 tracker = candidate; |
| 1032 } | 1035 } |
| 1033 | 1036 |
| 1034 DCHECK(tracker); | 1037 DCHECK(tracker); |
| 1035 | 1038 |
| 1039 // Check if there is another active tracker that tracks |file_id|. |
| 1040 // This can happen when the tracked file has multiple parents. |
| 1041 // If this case, report the failure to the caller. |
| 1042 if (!tracker->active() && same_file_id.has_active()) |
| 1043 return ACTIVATION_FAILED_ANOTHER_ACTIVE_TRACKER; |
| 1044 |
| 1045 scoped_ptr<leveldb::WriteBatch> batch(new leveldb::WriteBatch); |
| 1046 |
| 1036 if (!tracker->active()) { | 1047 if (!tracker->active()) { |
| 1037 if (same_file_id.has_active()) | 1048 // Check if there is another active tracker that has the same path to |
| 1038 return ACTIVATION_FAILED_ANOTHER_ACTIVE_TRACKER; | 1049 // the tracker to be activated. |
| 1039 | 1050 // Assuming the caller already overrides local file with newly added file, |
| 1051 // inactivate existing active tracker. |
| 1040 TrackerSet same_title; | 1052 TrackerSet same_title; |
| 1041 FindTrackersByParentAndTitle(parent_tracker_id, title, &same_title); | 1053 FindTrackersByParentAndTitle(parent_tracker_id, title, &same_title); |
| 1042 if (same_title.has_active()) | 1054 if (same_title.has_active()) { |
| 1043 return ACTIVATION_FAILED_SAME_PATH_TRACKER; | 1055 MakeTrackerInactive(same_title.active_tracker()->tracker_id(), |
| 1056 batch.get()); |
| 1057 } |
| 1044 } | 1058 } |
| 1045 | 1059 |
| 1046 scoped_ptr<leveldb::WriteBatch> batch(new leveldb::WriteBatch); | |
| 1047 if (!tracker->has_synced_details() || | 1060 if (!tracker->has_synced_details() || |
| 1048 tracker->synced_details().title() != title) { | 1061 tracker->synced_details().title() != title) { |
| 1049 trackers_by_parent_and_title_[parent_tracker_id] | 1062 trackers_by_parent_and_title_[parent_tracker_id] |
| 1050 [GetTrackerTitle(*tracker)].Erase(tracker); | 1063 [GetTrackerTitle(*tracker)].Erase(tracker); |
| 1051 trackers_by_parent_and_title_[parent_tracker_id][title].Insert( | 1064 trackers_by_parent_and_title_[parent_tracker_id][title].Insert( |
| 1052 tracker); | 1065 tracker); |
| 1053 } | 1066 } |
| 1054 *tracker->mutable_synced_details() = file.details(); | 1067 *tracker->mutable_synced_details() = file.details(); |
| 1055 | 1068 |
| 1056 MakeTrackerActive(tracker->tracker_id(), batch.get()); | 1069 MakeTrackerActive(tracker->tracker_id(), batch.get()); |
| (...skipping 990 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2047 MakeTrackerActive(tracker->tracker_id(), batch); | 2060 MakeTrackerActive(tracker->tracker_id(), batch); |
| 2048 ClearDirty(tracker, batch); | 2061 ClearDirty(tracker, batch); |
| 2049 return; | 2062 return; |
| 2050 } | 2063 } |
| 2051 | 2064 |
| 2052 NOTREACHED(); | 2065 NOTREACHED(); |
| 2053 } | 2066 } |
| 2054 | 2067 |
| 2055 } // namespace drive_backend | 2068 } // namespace drive_backend |
| 2056 } // namespace sync_file_system | 2069 } // namespace sync_file_system |
| OLD | NEW |