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 #define ENUM_CASE(x) case TrafficRecorder::##x: return #x; break; | |
| 45 | |
| 46 const char* GetMessageTypeString(TrafficRecorder::TrafficMessageType type) { | |
|
rlarocque
2012/03/29 22:54:27
I'd like to take this opportunity to hide some unu
lipalani1
2012/03/29 23:58:37
Done.
| |
| 47 switch(type) { | |
| 48 ENUM_CASE(CLIENT_TO_SERVER_MESSAGE) | |
| 49 ENUM_CASE(CLIENT_TO_SERVER_RESPONSE) | |
| 50 } | |
| 51 NOTREACHED(); | |
| 52 return ""; | |
| 53 } | |
| 54 | |
| 55 #undef ENUM_CASE | |
| 56 } | |
| 57 | |
| 58 DictionaryValue* TrafficRecorder::TrafficRecord::ToValue() const { | |
| 59 scoped_ptr<DictionaryValue> value; | |
| 60 if (truncated) { | |
| 61 value.reset(new DictionaryValue()); | |
| 62 value->SetString("message_type", | |
| 63 GetMessageTypeString(message_type)); | |
| 64 value->SetBoolean("truncated", true); | |
| 65 } else if (message_type == TrafficRecorder::CLIENT_TO_SERVER_MESSAGE) { | |
| 66 sync_pb::ClientToServerMessage message_proto; | |
| 67 if (message_proto.ParseFromString(message)) | |
|
rlarocque
2012/03/30 00:20:51
By the way, do we really need to round-trip the me
akalin
2012/03/30 01:55:04
Yeah, I wanted it to be stored serialized to save
lipalani1
2012/03/30 20:35:16
We had a discussion about the best format for stor
rlarocque
2012/03/30 21:01:11
I'm not entirely convinced that this will be a sig
| |
| 68 value.reset( | |
| 69 ClientToServerMessageToValue(message_proto, | |
|
rlarocque
2012/03/29 22:58:49
Because the JavaScript isn't processing this in an
lipalani1
2012/03/29 23:58:37
JS converts this into a formatted string using JSO
rlarocque
2012/03/30 00:20:51
Right, but that dictionary could consist solely of
akalin
2012/03/30 01:55:04
I'm not sure I follow; why would serializing to a
rlarocque
2012/03/30 17:49:02
I don't mean to argue that it's necessarily better
| |
| 70 false /* include_specifics */)); | |
| 71 } else if (message_type == TrafficRecorder::CLIENT_TO_SERVER_RESPONSE) { | |
| 72 sync_pb::ClientToServerResponse message_proto; | |
| 73 if (message_proto.ParseFromString(message)) | |
| 74 value.reset( | |
| 75 ClientToServerResponseToValue(message_proto, | |
| 76 false /* include_specifics */)); | |
| 77 } else { | |
| 78 NOTREACHED(); | |
| 79 } | |
| 80 | |
| 81 return value.release(); | |
| 82 } | |
| 83 | |
| 84 | |
| 85 DictionaryValue* TrafficRecorder::ToValue() const { | |
| 86 int message_count = 0; | |
| 87 scoped_ptr<DictionaryValue> dictionary(new DictionaryValue()); | |
| 88 std::deque<TrafficRecord>::const_iterator it; | |
| 89 for (it = records_.begin(); it != records_.end(); ++it) { | |
| 90 const TrafficRecord& record = *it; | |
| 91 std::string message_id = StringPrintf("message%d", message_count); | |
|
rlarocque
2012/03/29 22:54:27
Couldn't this be an array instead? I don't see th
lipalani1
2012/03/29 23:58:37
We have to put this in a dictionary. Having it as
rlarocque
2012/03/30 00:20:51
Same idea as above. A single-attribute dictionary
akalin
2012/03/30 01:55:04
See my comment re. using ListValue for this. The
| |
| 92 dictionary->Set(message_id, record.ToValue()); | |
| 93 ++message_count; | |
| 94 } | |
| 95 | |
| 96 return dictionary.release(); | |
| 97 } | |
| 98 | |
| 99 | |
| 42 | 100 |
| 43 void TrafficRecorder::AddTrafficToQueue(TrafficRecord* record) { | 101 void TrafficRecorder::AddTrafficToQueue(TrafficRecord* record) { |
| 44 records_.resize(records_.size() + 1); | 102 records_.resize(records_.size() + 1); |
| 45 std::swap(records_.back(), *record); | 103 std::swap(records_.back(), *record); |
| 46 | 104 |
| 47 // We might have more records than our limit. | 105 // We might have more records than our limit. |
| 48 // Maintain the size invariant by deleting items. | 106 // Maintain the size invariant by deleting items. |
| 49 while (records_.size() > max_messages_) { | 107 while (records_.size() > max_messages_) { |
| 50 records_.pop_front(); | 108 records_.pop_front(); |
| 51 } | 109 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 69 | 127 |
| 70 void TrafficRecorder::RecordClientToServerMessage( | 128 void TrafficRecorder::RecordClientToServerMessage( |
| 71 const sync_pb::ClientToServerMessage& msg) { | 129 const sync_pb::ClientToServerMessage& msg) { |
| 72 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE); | 130 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE); |
| 73 } | 131 } |
| 74 | 132 |
| 75 void TrafficRecorder::RecordClientToServerResponse( | 133 void TrafficRecorder::RecordClientToServerResponse( |
| 76 const sync_pb::ClientToServerResponse& response) { | 134 const sync_pb::ClientToServerResponse& response) { |
| 77 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE); | 135 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE); |
| 78 } | 136 } |
| 79 | |
| 80 } // namespace browser_sync | 137 } // namespace browser_sync |
| 81 | 138 |
| OLD | NEW |