Chromium Code Reviews| 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/stringprintf.h" | |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "sync/protocol/proto_value_conversions.h" | 12 #include "sync/protocol/proto_value_conversions.h" |
| 12 #include "sync/protocol/sync.pb.h" | 13 #include "sync/protocol/sync.pb.h" |
| 13 #include "sync/sessions/sync_session.h" | 14 #include "sync/sessions/sync_session.h" |
| 14 | 15 |
| 15 namespace browser_sync { | 16 namespace browser_sync { |
| 16 | 17 |
| 17 TrafficRecorder::TrafficRecord::TrafficRecord(const std::string& message, | 18 TrafficRecorder::TrafficRecord::TrafficRecord(const std::string& message, |
| 18 TrafficMessageType message_type, | 19 TrafficMessageType message_type, |
| 19 bool truncated) : | 20 bool truncated) : |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 32 | 33 |
| 33 TrafficRecorder::TrafficRecorder(unsigned int max_messages, | 34 TrafficRecorder::TrafficRecorder(unsigned int max_messages, |
| 34 unsigned int max_message_size) | 35 unsigned int max_message_size) |
| 35 : max_messages_(max_messages), | 36 : max_messages_(max_messages), |
| 36 max_message_size_(max_message_size) { | 37 max_message_size_(max_message_size) { |
| 37 } | 38 } |
| 38 | 39 |
| 39 TrafficRecorder::~TrafficRecorder() { | 40 TrafficRecorder::~TrafficRecorder() { |
| 40 } | 41 } |
| 41 | 42 |
| 43 namespace { | |
| 44 const char* GetMessageTypeString(TrafficRecorder::TrafficMessageType type) { | |
| 45 switch(type) { | |
| 46 case TrafficRecorder::CLIENT_TO_SERVER_MESSAGE: | |
| 47 return "Request"; | |
| 48 case TrafficRecorder::CLIENT_TO_SERVER_RESPONSE: | |
| 49 return "Response"; | |
| 50 } | |
| 51 NOTREACHED(); | |
| 52 return ""; | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 DictionaryValue* TrafficRecorder::TrafficRecord::ToValue() const { | |
| 57 scoped_ptr<DictionaryValue> value; | |
| 58 if (truncated) { | |
| 59 value.reset(new DictionaryValue()); | |
| 60 value->SetString("message_type", | |
| 61 GetMessageTypeString(message_type)); | |
| 62 value->SetBoolean("truncated", true); | |
| 63 } else if (message_type == TrafficRecorder::CLIENT_TO_SERVER_MESSAGE) { | |
| 64 sync_pb::ClientToServerMessage message_proto; | |
| 65 if (message_proto.ParseFromString(message)) | |
| 66 value.reset( | |
| 67 ClientToServerMessageToValue(message_proto, | |
| 68 false /* include_specifics */)); | |
| 69 } else if (message_type == TrafficRecorder::CLIENT_TO_SERVER_RESPONSE) { | |
| 70 sync_pb::ClientToServerResponse message_proto; | |
| 71 if (message_proto.ParseFromString(message)) | |
| 72 value.reset( | |
| 73 ClientToServerResponseToValue(message_proto, | |
| 74 false /* include_specifics */)); | |
| 75 } else { | |
| 76 NOTREACHED(); | |
| 77 } | |
| 78 | |
| 79 return value.release(); | |
| 80 } | |
| 81 | |
| 82 | |
| 83 ListValue* TrafficRecorder::ToValue() const { | |
| 84 scoped_ptr<ListValue> value(new ListValue()); | |
| 85 std::deque<TrafficRecord>::const_iterator it; | |
| 86 for (it = records_.begin(); it != records_.end(); ++it) { | |
| 87 const TrafficRecord& record = *it; | |
| 88 value->Append(record.ToValue()); | |
| 89 } | |
| 90 | |
| 91 return value.release(); | |
| 92 } | |
| 93 | |
| 94 | |
| 42 | 95 |
| 43 void TrafficRecorder::AddTrafficToQueue(TrafficRecord* record) { | 96 void TrafficRecorder::AddTrafficToQueue(TrafficRecord* record) { |
| 44 records_.resize(records_.size() + 1); | 97 records_.resize(records_.size() + 1); |
| 45 std::swap(records_.back(), *record); | 98 std::swap(records_.back(), *record); |
| 46 | 99 |
| 47 // We might have more records than our limit. | 100 // We might have more records than our limit. |
| 48 // Maintain the size invariant by deleting items. | 101 // Maintain the size invariant by deleting items. |
| 49 while (records_.size() > max_messages_) { | 102 while (records_.size() > max_messages_) { |
| 50 records_.pop_front(); | 103 records_.pop_front(); |
| 51 } | 104 } |
| 52 } | 105 } |
| 53 | 106 |
| 54 void TrafficRecorder::StoreProtoInQueue( | 107 void TrafficRecorder::StoreProtoInQueue( |
| 55 const ::google::protobuf::MessageLite& msg, | 108 const ::google::protobuf::MessageLite& msg, |
| 56 TrafficMessageType type) { | 109 TrafficMessageType type) { |
| 57 bool truncated = false; | 110 bool truncated = false; |
| 58 std::string message; | 111 std::string message; |
| 59 if (static_cast<unsigned int>(msg.ByteSize()) >= max_message_size_) { | 112 if ((unsigned int)msg.ByteSize() >= max_message_size_) { |
|
rlarocque
2012/03/30 21:01:11
This line changed a few times throughout the revie
lipalani1
2012/03/30 23:34:05
Bad merge. It is static_cast after the latest reba
| |
| 60 // TODO(lipalani): Trim the specifics to fit in size. | 113 // TODO(lipalani): Trim the specifics to fit in size. |
| 61 truncated = true; | 114 truncated = true; |
| 62 } else { | 115 } else { |
| 63 msg.SerializeToString(&message); | 116 msg.SerializeToString(&message); |
| 64 } | 117 } |
| 65 | 118 |
| 66 TrafficRecord record(message, type, truncated); | 119 TrafficRecord record(message, type, truncated); |
| 67 AddTrafficToQueue(&record); | 120 AddTrafficToQueue(&record); |
| 68 } | 121 } |
| 69 | 122 |
| 70 void TrafficRecorder::RecordClientToServerMessage( | 123 void TrafficRecorder::RecordClientToServerMessage( |
| 71 const sync_pb::ClientToServerMessage& msg) { | 124 const sync_pb::ClientToServerMessage& msg) { |
| 72 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE); | 125 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE); |
| 73 } | 126 } |
| 74 | 127 |
| 75 void TrafficRecorder::RecordClientToServerResponse( | 128 void TrafficRecorder::RecordClientToServerResponse( |
| 76 const sync_pb::ClientToServerResponse& response) { | 129 const sync_pb::ClientToServerResponse& response) { |
| 77 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE); | 130 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE); |
| 78 } | 131 } |
| 79 | |
| 80 } // namespace browser_sync | 132 } // namespace browser_sync |
| 81 | 133 |
| OLD | NEW |