summaryrefslogtreecommitdiffstats
path: root/sync/engine/traffic_recorder.h
blob: 65e652e4731d425c2c17b47de4046d0f1101dfaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SYNC_ENGINE_TRAFFIC_RECORDER_H_
#define CHROME_BROWSER_SYNC_ENGINE_TRAFFIC_RECORDER_H_

#include <deque>
#include <string>

#include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "base/time.h"
#include "base/values.h"
#include "sync/base/sync_export.h"
#include "sync/protocol/sync.pb.h"

namespace sync_pb {
class ClientToServerResponse;
class ClientToServerMessage;
}

namespace syncer {

class SYNC_EXPORT_PRIVATE TrafficRecorder {
 public:
  enum TrafficMessageType {
    CLIENT_TO_SERVER_MESSAGE,
    CLIENT_TO_SERVER_RESPONSE,
    UNKNOWN_MESSAGE_TYPE
  };

  struct SYNC_EXPORT_PRIVATE TrafficRecord {
    // The serialized message.
    std::string message;
    TrafficMessageType message_type;
    // If the message is too big to be kept in memory then it should be
    // truncated. For now the entire message is omitted if it is too big.
    // TODO(lipalani): Truncate the specifics to fit within size.
    bool truncated;

    TrafficRecord(const std::string& message,
                  TrafficMessageType message_type,
                  bool truncated,
                  base::Time time);
    TrafficRecord();
    ~TrafficRecord();
    DictionaryValue* ToValue() const;

    // Time of record creation.
    base::Time timestamp;
  };

  TrafficRecorder(unsigned int max_messages, unsigned int max_message_size);
  virtual ~TrafficRecorder();

  void RecordClientToServerMessage(const sync_pb::ClientToServerMessage& msg);
  void RecordClientToServerResponse(
      const sync_pb::ClientToServerResponse& response);
  ListValue* ToValue() const;

  const std::deque<TrafficRecord>& records() {
    return records_;
  }

 private:
  void AddTrafficToQueue(TrafficRecord* record);
  void StoreProtoInQueue(const ::google::protobuf::MessageLite& msg,
                         TrafficMessageType type);

  // Method to get record creation time.
  virtual base::Time GetTime();

  // Maximum number of messages stored in the queue.
  unsigned int max_messages_;

  // Maximum size of each message.
  unsigned int max_message_size_;
  std::deque<TrafficRecord> records_;
  DISALLOW_COPY_AND_ASSIGN(TrafficRecorder);
};

}  // namespace syncer

#endif  // CHROME_BROWSER_SYNC_ENGINE_TRAFFIC_RECORDER_H_