1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_
6#define GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "google_apis/gcm/base/gcm_export.h"
14
15namespace google {
16namespace protobuf {
17class MessageLite;
18}  // namespace protobuf
19}  // namespace google
20
21namespace gcm {
22
23// A wrapper for MCS protobuffers that encapsulates their tag, size and data
24// in an immutable and thread-safe format. If a mutable version is desired,
25// CloneProtobuf() should use used to create a new copy of the protobuf.
26//
27// Note: default copy and assign welcome.
28class GCM_EXPORT MCSMessage {
29 public:
30  // Creates an invalid MCSMessage.
31  MCSMessage();
32  // Infers tag from |message|.
33  explicit MCSMessage(const google::protobuf::MessageLite& protobuf);
34  // |tag| must match |protobuf|'s message type.
35  MCSMessage(uint8 tag, const google::protobuf::MessageLite& protobuf);
36  // |tag| must match |protobuf|'s message type. Takes ownership of |protobuf|.
37  MCSMessage(uint8 tag,
38             scoped_ptr<const google::protobuf::MessageLite> protobuf);
39  ~MCSMessage();
40
41  // Returns whether this message is valid or not (whether a protobuf was
42  // provided at construction time or not).
43  bool IsValid() const;
44
45  // Getters for serialization.
46  uint8 tag() const { return tag_; }
47  int size() const {return size_; }
48  std::string SerializeAsString() const;
49
50  // Getter for accessing immutable probotuf fields.
51  const google::protobuf::MessageLite& GetProtobuf() const;
52
53  // Getter for creating a mutated version of the protobuf.
54  scoped_ptr<google::protobuf::MessageLite> CloneProtobuf() const;
55
56 private:
57  class Core : public base::RefCountedThreadSafe<MCSMessage::Core> {
58   public:
59    Core();
60    Core(uint8 tag, const google::protobuf::MessageLite& protobuf);
61    Core(uint8 tag, scoped_ptr<const google::protobuf::MessageLite> protobuf);
62
63    const google::protobuf::MessageLite& Get() const;
64
65   private:
66    friend class base::RefCountedThreadSafe<MCSMessage::Core>;
67    ~Core();
68
69    // The immutable protobuf.
70    scoped_ptr<const google::protobuf::MessageLite> protobuf_;
71
72    DISALLOW_COPY_AND_ASSIGN(Core);
73  };
74
75  // These are cached separately to avoid having to recompute them.
76  const uint8 tag_;
77  const int size_;
78
79  // The refcounted core, containing the protobuf memory.
80  scoped_refptr<const Core> core_;
81};
82
83}  // namespace gcm
84
85#endif  // GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_
86