| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/engine/traffic_recorder.h" | 5 #include "sync/engine/traffic_recorder.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "sync/protocol/proto_value_conversions.h" | 11 #include "sync/protocol/proto_value_conversions.h" |
| 12 #include "sync/protocol/sync.pb.h" | 12 #include "sync/protocol/sync.pb.h" |
| 13 #include "sync/sessions/sync_session.h" | 13 #include "sync/sessions/sync_session.h" |
| 14 #include "sync/util/time.h" |
| 14 | 15 |
| 15 namespace syncer { | 16 namespace syncer { |
| 16 | 17 |
| 18 // Return current time in ms since Unix epoch |
| 19 base::Time TrafficRecorder::GetTime() { |
| 20 return base::Time::Now(); |
| 21 } |
| 22 |
| 17 TrafficRecorder::TrafficRecord::TrafficRecord(const std::string& message, | 23 TrafficRecorder::TrafficRecord::TrafficRecord(const std::string& message, |
| 18 TrafficMessageType message_type, | 24 TrafficMessageType message_type, |
| 19 bool truncated) : | 25 bool truncated, |
| 26 base::Time time) : |
| 20 message(message), | 27 message(message), |
| 21 message_type(message_type), | 28 message_type(message_type), |
| 22 truncated(truncated) { | 29 truncated(truncated), |
| 30 timestamp(time) { |
| 23 } | 31 } |
| 24 | 32 |
| 25 TrafficRecorder::TrafficRecord::TrafficRecord() | 33 TrafficRecorder::TrafficRecord::TrafficRecord() |
| 26 : message_type(UNKNOWN_MESSAGE_TYPE), | 34 : message_type(UNKNOWN_MESSAGE_TYPE), |
| 27 truncated(false) { | 35 truncated(false) { |
| 28 } | 36 } |
| 29 | 37 |
| 30 TrafficRecorder::TrafficRecord::~TrafficRecord() { | 38 TrafficRecorder::TrafficRecord::~TrafficRecord() { |
| 31 } | 39 } |
| 32 | 40 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 } else if (message_type == TrafficRecorder::CLIENT_TO_SERVER_RESPONSE) { | 77 } else if (message_type == TrafficRecorder::CLIENT_TO_SERVER_RESPONSE) { |
| 70 sync_pb::ClientToServerResponse message_proto; | 78 sync_pb::ClientToServerResponse message_proto; |
| 71 if (message_proto.ParseFromString(message)) | 79 if (message_proto.ParseFromString(message)) |
| 72 value.reset( | 80 value.reset( |
| 73 ClientToServerResponseToValue(message_proto, | 81 ClientToServerResponseToValue(message_proto, |
| 74 false /* include_specifics */)); | 82 false /* include_specifics */)); |
| 75 } else { | 83 } else { |
| 76 NOTREACHED(); | 84 NOTREACHED(); |
| 77 } | 85 } |
| 78 | 86 |
| 87 value->SetString("timestamp", GetTimeDebugString(timestamp)); |
| 88 |
| 79 return value.release(); | 89 return value.release(); |
| 80 } | 90 } |
| 81 | 91 |
| 82 | 92 |
| 83 ListValue* TrafficRecorder::ToValue() const { | 93 ListValue* TrafficRecorder::ToValue() const { |
| 84 scoped_ptr<ListValue> value(new ListValue()); | 94 scoped_ptr<ListValue> value(new ListValue()); |
| 85 std::deque<TrafficRecord>::const_iterator it; | 95 std::deque<TrafficRecord>::const_iterator it; |
| 86 for (it = records_.begin(); it != records_.end(); ++it) { | 96 for (it = records_.begin(); it != records_.end(); ++it) { |
| 87 const TrafficRecord& record = *it; | 97 const TrafficRecord& record = *it; |
| 88 value->Append(record.ToValue()); | 98 value->Append(record.ToValue()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 108 TrafficMessageType type) { | 118 TrafficMessageType type) { |
| 109 bool truncated = false; | 119 bool truncated = false; |
| 110 std::string message; | 120 std::string message; |
| 111 if (static_cast<unsigned int>(msg.ByteSize()) >= max_message_size_) { | 121 if (static_cast<unsigned int>(msg.ByteSize()) >= max_message_size_) { |
| 112 // TODO(lipalani): Trim the specifics to fit in size. | 122 // TODO(lipalani): Trim the specifics to fit in size. |
| 113 truncated = true; | 123 truncated = true; |
| 114 } else { | 124 } else { |
| 115 msg.SerializeToString(&message); | 125 msg.SerializeToString(&message); |
| 116 } | 126 } |
| 117 | 127 |
| 118 TrafficRecord record(message, type, truncated); | 128 TrafficRecord record(message, type, truncated, GetTime()); |
| 119 AddTrafficToQueue(&record); | 129 AddTrafficToQueue(&record); |
| 120 } | 130 } |
| 121 | 131 |
| 122 void TrafficRecorder::RecordClientToServerMessage( | 132 void TrafficRecorder::RecordClientToServerMessage( |
| 123 const sync_pb::ClientToServerMessage& msg) { | 133 const sync_pb::ClientToServerMessage& msg) { |
| 124 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE); | 134 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE); |
| 125 } | 135 } |
| 126 | 136 |
| 127 void TrafficRecorder::RecordClientToServerResponse( | 137 void TrafficRecorder::RecordClientToServerResponse( |
| 128 const sync_pb::ClientToServerResponse& response) { | 138 const sync_pb::ClientToServerResponse& response) { |
| 129 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE); | 139 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE); |
| 130 } | 140 } |
| 131 | 141 |
| 132 } // namespace syncer | 142 } // namespace syncer |
| 133 | 143 |
| OLD | NEW |