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