1 // Copyright 2012 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_TRAFFIC_RECORDER_H_ 6 #define CHROME_BROWSER_SYNC_ENGINE_TRAFFIC_RECORDER_H_ 7 8 #include <deque> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/gtest_prod_util.h" 13 #include "base/time/time.h" 14 #include "base/values.h" 15 #include "sync/base/sync_export.h" 16 #include "sync/protocol/sync.pb.h" 17 18 namespace sync_pb { 19 class ClientToServerResponse; 20 class ClientToServerMessage; 21 } 22 23 namespace syncer { 24 25 class SYNC_EXPORT_PRIVATE TrafficRecorder { 26 public: 27 enum TrafficMessageType { 28 CLIENT_TO_SERVER_MESSAGE, 29 CLIENT_TO_SERVER_RESPONSE, 30 UNKNOWN_MESSAGE_TYPE 31 }; 32 33 struct SYNC_EXPORT_PRIVATE TrafficRecord { 34 // The serialized message. 35 std::string message; 36 TrafficMessageType message_type; 37 // If the message is too big to be kept in memory then it should be 38 // truncated. For now the entire message is omitted if it is too big. 39 // TODO(lipalani): Truncate the specifics to fit within size. 40 bool truncated; 41 42 TrafficRecord(const std::string& message, 43 TrafficMessageType message_type, 44 bool truncated, 45 base::Time time); 46 TrafficRecord(); 47 ~TrafficRecord(); 48 base::DictionaryValue* ToValue() const; 49 50 // Time of record creation. 51 base::Time timestamp; 52 }; 53 54 TrafficRecorder(unsigned int max_messages, unsigned int max_message_size); 55 virtual ~TrafficRecorder(); 56 57 void RecordClientToServerMessage(const sync_pb::ClientToServerMessage& msg); 58 void RecordClientToServerResponse( 59 const sync_pb::ClientToServerResponse& response); 60 base::ListValue* ToValue() const; 61 62 const std::deque<TrafficRecord>& records() { 63 return records_; 64 } 65 66 private: 67 void AddTrafficToQueue(TrafficRecord* record); 68 void StoreProtoInQueue(const ::google::protobuf::MessageLite& msg, 69 TrafficMessageType type); 70 71 // Method to get record creation time. 72 virtual base::Time GetTime(); 73 74 // Maximum number of messages stored in the queue. 75 unsigned int max_messages_; 76 77 // Maximum size of each message. 78 unsigned int max_message_size_; 79 std::deque<TrafficRecord> records_; 80 DISALLOW_COPY_AND_ASSIGN(TrafficRecorder); 81 }; 82 83 } // namespace syncer 84 85 #endif // CHROME_BROWSER_SYNC_ENGINE_TRAFFIC_RECORDER_H_ 86 87