1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ 6 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ 7 #pragma once 8 9 #include <string> 10 11 #include "base/gtest_prod_util.h" 12 #include "chrome/browser/sync/syncable/blob.h" 13 #include "chrome/browser/sync/syncable/model_type.h" 14 15 namespace syncable { 16 class Directory; 17 class Entry; 18 class ScopedDirLookup; 19 class SyncName; 20 } // namespace syncable 21 22 namespace sync_pb { 23 class ClientToServerResponse; 24 class EntitySpecifics; 25 } // namespace sync_pb 26 27 namespace browser_sync { 28 29 namespace sessions { 30 class SyncSession; 31 } 32 33 class AuthWatcher; 34 class ClientToServerMessage; 35 class ServerConnectionManager; 36 class SyncEntity; 37 class CommitResponse_EntryResponse; 38 39 class SyncerProtoUtil { 40 public: 41 // Posts the given message and fills the buffer with the returned value. 42 // Returns true on success. Also handles store birthday verification: 43 // session->status()->syncer_stuck_ is set true if the birthday is 44 // incorrect. A false value will always be returned if birthday is bad. 45 static bool PostClientToServerMessage( 46 const ClientToServerMessage& msg, 47 sync_pb::ClientToServerResponse* response, 48 sessions::SyncSession* session); 49 50 // Compares a syncable Entry to SyncEntity, returns true iff the data is 51 // identical. 52 // 53 // TODO(sync): The places where this function is used are arguable big causes 54 // of the fragility, because there's a tendency to freak out the moment the 55 // local and server values diverge. However, this almost always indicates a 56 // sync bug somewhere earlier in the sync cycle. 57 static bool Compare(const syncable::Entry& local_entry, 58 const SyncEntity& server_entry); 59 60 // Utility methods for converting between syncable::Blobs and protobuf byte 61 // fields. 62 static void CopyProtoBytesIntoBlob(const std::string& proto_bytes, 63 syncable::Blob* blob); 64 static bool ProtoBytesEqualsBlob(const std::string& proto_bytes, 65 const syncable::Blob& blob); 66 static void CopyBlobIntoProtoBytes(const syncable::Blob& blob, 67 std::string* proto_bytes); 68 69 // Extract the name field from a sync entity. 70 static const std::string& NameFromSyncEntity( 71 const sync_pb::SyncEntity& entry); 72 73 // Extract the name field from a commit entry response. 74 static const std::string& NameFromCommitEntryResponse( 75 const CommitResponse_EntryResponse& entry); 76 77 // EntitySpecifics is used as a filter for the GetUpdates message to tell 78 // the server which datatypes to send back. This adds a datatype so that 79 // it's included in the filter. 80 static void AddToEntitySpecificDatatypesFilter(syncable::ModelType datatype, 81 sync_pb::EntitySpecifics* filter); 82 83 // Get a debug string representation of the client to server response. 84 static std::string ClientToServerResponseDebugString( 85 const sync_pb::ClientToServerResponse& response); 86 87 // Get update contents as a string. Intended for logging, and intended 88 // to have a smaller footprint than the protobuf's built-in pretty printer. 89 static std::string SyncEntityDebugString(const sync_pb::SyncEntity& entry); 90 91 // Pull the birthday from the dir and put it into the msg. 92 static void AddRequestBirthday(syncable::Directory* dir, 93 ClientToServerMessage* msg); 94 95 private: 96 SyncerProtoUtil() {} 97 98 // Helper functions for PostClientToServerMessage. 99 100 // Verifies the store birthday, alerting/resetting as appropriate if there's a 101 // mismatch. Return false if the syncer should be stuck. 102 static bool VerifyResponseBirthday(syncable::Directory* dir, 103 const sync_pb::ClientToServerResponse* response); 104 105 // Builds and sends a SyncEngineEvent to begin migration for types (specified 106 // in notification). 107 static void HandleMigrationDoneResponse( 108 const sync_pb::ClientToServerResponse* response, 109 sessions::SyncSession* session); 110 111 // Post the message using the scm, and do some processing on the returned 112 // headers. Decode the server response. 113 static bool PostAndProcessHeaders(browser_sync::ServerConnectionManager* scm, 114 sessions::SyncSession* session, 115 const ClientToServerMessage& msg, 116 sync_pb::ClientToServerResponse* response); 117 118 friend class SyncerProtoUtilTest; 119 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, AddRequestBirthday); 120 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, PostAndProcessHeaders); 121 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, VerifyResponseBirthday); 122 123 DISALLOW_COPY_AND_ASSIGN(SyncerProtoUtil); 124 }; 125 126 } // namespace browser_sync 127 128 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ 129