Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / google_apis / gcm / base / mcs_message.h
blob14d80ed3b9733c84bd92a7011c33daeaa3e4c049
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_
8 #include <string>
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"
15 namespace google {
16 namespace protobuf {
17 class MessageLite;
18 } // namespace protobuf
19 } // namespace google
21 namespace gcm {
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 {
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();
41 // Returns whether this message is valid or not (whether a protobuf was
42 // provided at construction time or not).
43 bool IsValid() const;
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;
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);
63 const google::protobuf::MessageLite& Get() const;
65 private:
66 friend class base::RefCountedThreadSafe<MCSMessage::Core>;
67 ~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.
76 const uint8 tag_;
77 const int size_;
79 // The refcounted core, containing the protobuf memory.
80 scoped_refptr<const Core> core_;
83 } // namespace gcm
85 #endif // GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_