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.
5 #ifndef GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_
6 #define GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_
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"
18 } // namespace protobuf
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.
27 // Note: default copy and assign welcome.
28 class GCM_EXPORT MCSMessage
{
30 // Creates an invalid 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|.
38 scoped_ptr
<const google::protobuf::MessageLite
> protobuf
);
41 // Returns whether this message is valid or not (whether a protobuf was
42 // provided at construction time or not).
45 // Getters for serialization.
46 uint8
tag() const { return tag_
; }
47 int size() const {return size_
; }
48 std::string
SerializeAsString() const;
50 // Getter for accessing immutable probotuf fields.
51 const google::protobuf::MessageLite
& GetProtobuf() const;
53 // Getter for creating a mutated version of the protobuf.
54 scoped_ptr
<google::protobuf::MessageLite
> CloneProtobuf() const;
57 class Core
: public base::RefCountedThreadSafe
<MCSMessage::Core
> {
60 Core(uint8 tag
, const google::protobuf::MessageLite
& protobuf
);
61 Core(uint8 tag
, scoped_ptr
<const google::protobuf::MessageLite
> protobuf
);
63 const google::protobuf::MessageLite
& Get() const;
66 friend class base::RefCountedThreadSafe
<MCSMessage::Core
>;
69 // The immutable protobuf.
70 scoped_ptr
<const google::protobuf::MessageLite
> protobuf_
;
72 DISALLOW_COPY_AND_ASSIGN(Core
);
75 // These are cached separately to avoid having to recompute them.
79 // The refcounted core, containing the protobuf memory.
80 scoped_refptr
<const Core
> core_
;
85 #endif // GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_