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#include "google_apis/gcm/base/mcs_message.h"
6
7#include "base/logging.h"
8#include "google_apis/gcm/base/mcs_util.h"
9
10namespace gcm {
11
12MCSMessage::Core::Core() {}
13
14MCSMessage::Core::Core(uint8 tag,
15                       const google::protobuf::MessageLite& protobuf) {
16  scoped_ptr<google::protobuf::MessageLite> owned_protobuf(protobuf.New());
17  owned_protobuf->CheckTypeAndMergeFrom(protobuf);
18  protobuf_ = owned_protobuf.Pass();
19}
20
21MCSMessage::Core::Core(
22    uint8 tag,
23    scoped_ptr<const google::protobuf::MessageLite> protobuf) {
24  protobuf_ = protobuf.Pass();
25}
26
27MCSMessage::Core::~Core() {}
28
29const google::protobuf::MessageLite& MCSMessage::Core::Get() const {
30  return *protobuf_;
31}
32
33MCSMessage::MCSMessage() : tag_(0), size_(0) {}
34
35MCSMessage::MCSMessage(const google::protobuf::MessageLite& protobuf)
36  : tag_(GetMCSProtoTag(protobuf)),
37    size_(protobuf.ByteSize()),
38    core_(new Core(tag_, protobuf)) {
39}
40
41MCSMessage::MCSMessage(uint8 tag,
42                       const google::protobuf::MessageLite& protobuf)
43  : tag_(tag),
44    size_(protobuf.ByteSize()),
45    core_(new Core(tag_, protobuf)) {
46  DCHECK_EQ(tag, GetMCSProtoTag(protobuf));
47}
48
49MCSMessage::MCSMessage(uint8 tag,
50                       scoped_ptr<const google::protobuf::MessageLite> protobuf)
51  : tag_(tag),
52    size_(protobuf->ByteSize()),
53    core_(new Core(tag_, protobuf.Pass())) {
54  DCHECK_EQ(tag, GetMCSProtoTag(core_->Get()));
55}
56
57MCSMessage::~MCSMessage() {
58}
59
60bool MCSMessage::IsValid() const {
61  return core_.get() != NULL;
62}
63
64std::string MCSMessage::SerializeAsString() const {
65  return core_->Get().SerializeAsString();
66}
67
68const google::protobuf::MessageLite& MCSMessage::GetProtobuf() const {
69  return core_->Get();
70}
71
72scoped_ptr<google::protobuf::MessageLite> MCSMessage::CloneProtobuf() const {
73  scoped_ptr<google::protobuf::MessageLite> clone(GetProtobuf().New());
74  clone->CheckTypeAndMergeFrom(GetProtobuf());
75  return clone.Pass();
76}
77
78}  // namespace gcm
79