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