| 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 TrafficRecorder::TrafficRecorder(unsigned int max_messages, |
| 34 unsigned int max_message_size) |
| 35 : max_messages_(max_messages), |
| 36 max_message_size_(max_message_size) { |
| 37 } |
| 38 |
| 39 TrafficRecorder::~TrafficRecorder() { |
| 40 } |
| 41 |
| 42 |
| 43 void TrafficRecorder::AddTrafficToQueue(TrafficRecord* record) { |
| 44 records_.resize(records_.size() + 1); |
| 45 std::swap(records_.back(), *record); |
| 46 |
| 47 // We might have more records than our limit. |
| 48 // Maintain the size invariant by deleting items. |
| 49 while (records_.size() > max_messages_) { |
| 50 records_.pop_front(); |
| 51 } |
| 52 } |
| 53 |
| 54 void TrafficRecorder::StoreProtoInQueue( |
| 55 const ::google::protobuf::MessageLite& msg, |
| 56 TrafficMessageType type) { |
| 57 bool truncated = false; |
| 58 std::string message; |
| 59 if (static_cast<unsigned int>(msg.ByteSize()) >= max_message_size_) { |
| 60 // TODO(lipalani): Trim the specifics to fit in size. |
| 61 truncated = true; |
| 62 } else { |
| 63 msg.SerializeToString(&message); |
| 64 } |
| 65 |
| 66 TrafficRecord record(message, type, truncated); |
| 67 AddTrafficToQueue(&record); |
| 68 } |
| 69 |
| 70 void TrafficRecorder::RecordClientToServerMessage( |
| 71 const sync_pb::ClientToServerMessage& msg) { |
| 72 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE); |
| 73 } |
| 74 |
| 75 void TrafficRecorder::RecordClientToServerResponse( |
| 76 const sync_pb::ClientToServerResponse& response) { |
| 77 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE); |
| 78 } |
| 79 |
| 80 } // namespace browser_sync |
| 81 |
| OLD | NEW |