| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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 #include "sync/engine/traffic_recorder.h" |
| 6 |
| 7 #include "base/json/json_writer.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/values.h" |
| 11 #include "sync/protocol/proto_value_conversions.h" |
| 12 #include "sync/protocol/sync.pb.h" |
| 13 #include "sync/sessions/sync_session.h" |
| 14 |
| 15 namespace browser_sync { |
| 16 |
| 17 TrafficRecorder::TrafficRecord::TrafficRecord(const std::string& message, |
| 18 TrafficMessageType message_type, |
| 19 bool truncated) : |
| 20 message(message), |
| 21 message_type(message_type), |
| 22 truncated(truncated) { |
| 23 } |
| 24 |
| 25 TrafficRecorder::TrafficRecord::TrafficRecord() |
| 26 : message_type(UNKNOWN_MESSAGE_TYPE), |
| 27 truncated(false) { |
| 28 } |
| 29 |
| 30 TrafficRecorder::TrafficRecord::~TrafficRecord() { |
| 31 } |
| 32 |
| 33 const unsigned int kMaxMessages = 10; |
| 34 const unsigned int kMaxMessageSize = 5 * 1024; |
| 35 |
| 36 TrafficRecorder::TrafficRecorder() { |
| 37 } |
| 38 |
| 39 TrafficRecorder::~TrafficRecorder() { |
| 40 } |
| 41 |
| 42 |
| 43 void TrafficRecorder::AddTrafficToQueue(const TrafficRecord& record) { |
| 44 records_.push_back(record); |
| 45 |
| 46 // We might have more records than our limit. |
| 47 // Maintain the size invariant by deleting items. |
| 48 while (records_.size() > browser_sync::kMaxMessages) { |
| 49 records_.pop_front(); |
| 50 } |
| 51 } |
| 52 |
| 53 void TrafficRecorder::StoreProtoInQueue( |
| 54 const ::google::protobuf::MessageLite& msg, |
| 55 TrafficMessageType type) { |
| 56 bool truncated = false; |
| 57 std::string message; |
| 58 if (msg.ByteSize() >= browser_sync::kMaxMessageSize) { |
| 59 // TODO(lipalani): Trim the specifics to fit in size. |
| 60 truncated = true; |
| 61 } else { |
| 62 msg.SerializeToString(&message); |
| 63 } |
| 64 |
| 65 TrafficRecord record(message, type, truncated); |
| 66 AddTrafficToQueue(record); |
| 67 } |
| 68 |
| 69 void TrafficRecorder::RecordClientToServerMessage( |
| 70 const sync_pb::ClientToServerMessage& msg) { |
| 71 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE); |
| 72 } |
| 73 |
| 74 void TrafficRecorder::RecordClientToServerResponse( |
| 75 const sync_pb::ClientToServerResponse& response) { |
| 76 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE); |
| 77 } |
| 78 |
| 79 } // namespace browser_sync |
| 80 |
| OLD | NEW |