| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "sync/test/fake_server/fake_server.h" | 5 #include "sync/test/fake_server/fake_server.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 #include "sync/test/fake_server/tombstone_entity.h" | 27 #include "sync/test/fake_server/tombstone_entity.h" |
| 28 #include "sync/test/fake_server/unique_client_entity.h" | 28 #include "sync/test/fake_server/unique_client_entity.h" |
| 29 | 29 |
| 30 using std::string; | 30 using std::string; |
| 31 using std::vector; | 31 using std::vector; |
| 32 | 32 |
| 33 using syncer::GetModelType; | 33 using syncer::GetModelType; |
| 34 using syncer::ModelType; | 34 using syncer::ModelType; |
| 35 using syncer::ModelTypeSet; | 35 using syncer::ModelTypeSet; |
| 36 | 36 |
| 37 // The default store birthday value. |
| 38 static const char kDefaultStoreBirthday[] = "1234567890"; |
| 39 |
| 40 // The default keystore key. |
| 41 static const char kDefaultKeystoreKey[] = "1111111111111111"; |
| 42 |
| 37 namespace fake_server { | 43 namespace fake_server { |
| 38 | 44 |
| 39 class FakeServerEntity; | 45 class FakeServerEntity; |
| 40 | 46 |
| 41 namespace { | 47 namespace { |
| 42 | 48 |
| 43 // The default store birthday value. | |
| 44 static const char kDefaultStoreBirthday[] = "1234567890"; | |
| 45 | |
| 46 // The default keystore key. | |
| 47 static const char kDefaultKeystoreKey[] = "1111111111111111"; | |
| 48 | |
| 49 // Properties of the bookmark bar permanent folder. | |
| 50 static const char kBookmarkBarFolderServerTag[] = "bookmark_bar"; | |
| 51 static const char kBookmarkBarFolderName[] = "Bookmark Bar"; | |
| 52 | |
| 53 // Properties of the other bookmarks permanent folder. | |
| 54 static const char kOtherBookmarksFolderServerTag[] = "other_bookmarks"; | |
| 55 static const char kOtherBookmarksFolderName[] = "Other Bookmarks"; | |
| 56 | |
| 57 // Properties of the synced bookmarks permanent folder. | |
| 58 static const char kSyncedBookmarksFolderServerTag[] = "synced_bookmarks"; | |
| 59 static const char kSyncedBookmarksFolderName[] = "Synced Bookmarks"; | |
| 60 | |
| 61 // A filter used during GetUpdates calls to determine what information to | 49 // A filter used during GetUpdates calls to determine what information to |
| 62 // send back to the client. There is a 1:1 correspondence between any given | 50 // send back to the client. There is a 1:1 correspondence between any given |
| 63 // GetUpdates call and an UpdateSieve instance. | 51 // GetUpdates call and an UpdateSieve instance. |
| 64 class UpdateSieve { | 52 class UpdateSieve { |
| 65 public: | 53 public: |
| 66 ~UpdateSieve() { } | 54 ~UpdateSieve() { } |
| 67 | 55 |
| 68 // Factory method for creating an UpdateSieve. | 56 // Factory method for creating an UpdateSieve. |
| 69 static scoped_ptr<UpdateSieve> Create( | 57 static scoped_ptr<UpdateSieve> Create( |
| 70 const sync_pb::GetUpdatesMessage& get_updates_message); | 58 const sync_pb::GetUpdatesMessage& get_updates_message); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 request_counter_(0), | 160 request_counter_(0), |
| 173 network_enabled_(true) { | 161 network_enabled_(true) { |
| 174 keystore_keys_.push_back(kDefaultKeystoreKey); | 162 keystore_keys_.push_back(kDefaultKeystoreKey); |
| 175 CHECK(CreateDefaultPermanentItems()); | 163 CHECK(CreateDefaultPermanentItems()); |
| 176 } | 164 } |
| 177 | 165 |
| 178 FakeServer::~FakeServer() { | 166 FakeServer::~FakeServer() { |
| 179 STLDeleteContainerPairSecondPointers(entities_.begin(), entities_.end()); | 167 STLDeleteContainerPairSecondPointers(entities_.begin(), entities_.end()); |
| 180 } | 168 } |
| 181 | 169 |
| 182 bool FakeServer::CreatePermanentBookmarkFolder(const std::string& server_tag, | 170 bool FakeServer::CreatePermanentBookmarkFolder(const char* server_tag, |
| 183 const std::string& name) { | 171 const char* name) { |
| 184 FakeServerEntity* entity = | 172 FakeServerEntity* entity = |
| 185 PermanentEntity::Create(syncer::BOOKMARKS, server_tag, name, | 173 PermanentEntity::Create(syncer::BOOKMARKS, server_tag, name, |
| 186 ModelTypeToRootTag(syncer::BOOKMARKS)); | 174 ModelTypeToRootTag(syncer::BOOKMARKS)); |
| 187 if (entity == NULL) | 175 if (entity == NULL) |
| 188 return false; | 176 return false; |
| 189 | 177 |
| 190 SaveEntity(entity); | 178 SaveEntity(entity); |
| 191 return true; | 179 return true; |
| 192 } | 180 } |
| 193 | 181 |
| 194 bool FakeServer::CreateDefaultPermanentItems() { | 182 bool FakeServer::CreateDefaultPermanentItems() { |
| 195 ModelTypeSet all_types = syncer::ProtocolTypes(); | 183 ModelTypeSet all_types = syncer::ProtocolTypes(); |
| 196 for (ModelTypeSet::Iterator it = all_types.First(); it.Good(); it.Inc()) { | 184 for (ModelTypeSet::Iterator it = all_types.First(); it.Good(); it.Inc()) { |
| 197 ModelType model_type = it.Get(); | 185 ModelType model_type = it.Get(); |
| 198 FakeServerEntity* top_level_entity = | 186 FakeServerEntity* top_level_entity = |
| 199 PermanentEntity::CreateTopLevel(model_type); | 187 PermanentEntity::CreateTopLevel(model_type); |
| 200 if (top_level_entity == NULL) { | 188 if (top_level_entity == NULL) { |
| 201 return false; | 189 return false; |
| 202 } | 190 } |
| 203 SaveEntity(top_level_entity); | 191 SaveEntity(top_level_entity); |
| 204 | 192 |
| 205 if (model_type == syncer::BOOKMARKS) { | 193 if (model_type == syncer::BOOKMARKS) { |
| 206 if (!CreatePermanentBookmarkFolder(kBookmarkBarFolderServerTag, | 194 if (!CreatePermanentBookmarkFolder("bookmark_bar", "Bookmark Bar")) |
| 207 kBookmarkBarFolderName)) | |
| 208 return false; | 195 return false; |
| 209 if (!CreatePermanentBookmarkFolder(kOtherBookmarksFolderServerTag, | 196 if (!CreatePermanentBookmarkFolder("other_bookmarks", "Other Bookmarks")) |
| 210 kOtherBookmarksFolderName)) | |
| 211 return false; | 197 return false; |
| 212 } | 198 } |
| 213 } | 199 } |
| 214 | 200 |
| 215 return true; | 201 return true; |
| 216 } | 202 } |
| 217 | 203 |
| 204 bool FakeServer::CreateMobileBookmarksPermanentItem() { |
| 205 // This folder is called "Synced Bookmarks" by sync and is renamed |
| 206 // "Mobile Bookmarks" by the mobile client UIs. |
| 207 FakeServerEntity* mobile_bookmarks_entity = |
| 208 PermanentEntity::Create(syncer::BOOKMARKS, |
| 209 "synced_bookmarks", |
| 210 "Synced Bookmarks", |
| 211 ModelTypeToRootTag(syncer::BOOKMARKS)); |
| 212 if (mobile_bookmarks_entity == NULL) { |
| 213 return false; |
| 214 } |
| 215 SaveEntity(mobile_bookmarks_entity); |
| 216 return true; |
| 217 } |
| 218 |
| 218 void FakeServer::SaveEntity(FakeServerEntity* entity) { | 219 void FakeServer::SaveEntity(FakeServerEntity* entity) { |
| 219 delete entities_[entity->GetId()]; | 220 delete entities_[entity->GetId()]; |
| 220 entity->SetVersion(++version_); | 221 entity->SetVersion(++version_); |
| 221 entities_[entity->GetId()] = entity; | 222 entities_[entity->GetId()] = entity; |
| 222 } | 223 } |
| 223 | 224 |
| 224 void FakeServer::HandleCommand(const string& request, | 225 void FakeServer::HandleCommand(const string& request, |
| 225 const HandleCommandCallback& callback) { | 226 const HandleCommandCallback& callback) { |
| 226 if (!network_enabled_) { | 227 if (!network_enabled_) { |
| 227 callback.Run(net::ERR_FAILED, net::ERR_FAILED, string()); | 228 callback.Run(net::ERR_FAILED, net::ERR_FAILED, string()); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 | 284 |
| 284 bool FakeServer::HandleGetUpdatesRequest( | 285 bool FakeServer::HandleGetUpdatesRequest( |
| 285 const sync_pb::GetUpdatesMessage& get_updates, | 286 const sync_pb::GetUpdatesMessage& get_updates, |
| 286 sync_pb::GetUpdatesResponse* response) { | 287 sync_pb::GetUpdatesResponse* response) { |
| 287 // TODO(pvalenzuela): Implement batching instead of sending all information | 288 // TODO(pvalenzuela): Implement batching instead of sending all information |
| 288 // at once. | 289 // at once. |
| 289 response->set_changes_remaining(0); | 290 response->set_changes_remaining(0); |
| 290 | 291 |
| 291 scoped_ptr<UpdateSieve> sieve = UpdateSieve::Create(get_updates); | 292 scoped_ptr<UpdateSieve> sieve = UpdateSieve::Create(get_updates); |
| 292 | 293 |
| 293 // This folder is called "Synced Bookmarks" by sync and is renamed | |
| 294 // "Mobile Bookmarks" by the mobile client UIs. | |
| 295 if (get_updates.create_mobile_bookmarks_folder() && | 294 if (get_updates.create_mobile_bookmarks_folder() && |
| 296 !CreatePermanentBookmarkFolder(kSyncedBookmarksFolderServerTag, | 295 !CreateMobileBookmarksPermanentItem()) { |
| 297 kSyncedBookmarksFolderName)) { | |
| 298 return false; | 296 return false; |
| 299 } | 297 } |
| 300 | 298 |
| 301 bool send_encryption_keys_based_on_nigori = false; | 299 bool send_encryption_keys_based_on_nigori = false; |
| 302 int64 max_response_version = 0; | 300 int64 max_response_version = 0; |
| 303 for (EntityMap::iterator it = entities_.begin(); it != entities_.end(); | 301 for (EntityMap::iterator it = entities_.begin(); it != entities_.end(); |
| 304 ++it) { | 302 ++it) { |
| 305 FakeServerEntity* entity = it->second; | 303 FakeServerEntity* entity = it->second; |
| 306 if (sieve->ClientWantsItem(entity)) { | 304 if (sieve->ClientWantsItem(entity)) { |
| 307 sync_pb::SyncEntity* response_entity = response->add_entries(); | 305 sync_pb::SyncEntity* response_entity = response->add_entries(); |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 } | 594 } |
| 597 | 595 |
| 598 void FakeServer::EnableNetwork() { | 596 void FakeServer::EnableNetwork() { |
| 599 network_enabled_ = true; | 597 network_enabled_ = true; |
| 600 } | 598 } |
| 601 | 599 |
| 602 void FakeServer::DisableNetwork() { | 600 void FakeServer::DisableNetwork() { |
| 603 network_enabled_ = false; | 601 network_enabled_ = false; |
| 604 } | 602 } |
| 605 | 603 |
| 606 std::string FakeServer::GetBookmarkBarFolderId() const { | |
| 607 for (EntityMap::const_iterator it = entities_.begin(); it != entities_.end(); | |
| 608 ++it) { | |
| 609 FakeServerEntity* entity = it->second; | |
| 610 if (entity->GetName() == kBookmarkBarFolderName && | |
| 611 entity->IsFolder() && | |
| 612 entity->GetModelType() == syncer::BOOKMARKS) { | |
| 613 return entity->GetId(); | |
| 614 } | |
| 615 } | |
| 616 NOTREACHED() << "Bookmark Bar entity not found."; | |
| 617 return ""; | |
| 618 } | |
| 619 | |
| 620 } // namespace fake_server | 604 } // namespace fake_server |
| OLD | NEW |