1 // Copyright 2014 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 #include "google_apis/gcm/engine/account_mapping.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/string_util.h"
14 const char kSeparator
[] = "&";
15 uint32 kEmailIndex
= 0;
16 uint32 kMappingChangeTimestampIndex
= 1;
17 uint32 kMessageTypeIndex
= 2;
18 uint32 kMessageIdIndex
= 3;
19 uint32 kSizeWithNoMessage
= kMessageTypeIndex
+ 1;
20 uint32 kSizeWithMessage
= kMessageIdIndex
+ 1;
22 const char kMessageTypeNoneString
[] = "none";
23 const char kMessageTypeAddString
[] = "add";
24 const char kMessageTypeRemoveString
[] = "remove";
26 std::string
MessageTypeToString(AccountMapping::MessageType type
) {
28 case AccountMapping::MSG_NONE
:
29 return kMessageTypeNoneString
;
30 case AccountMapping::MSG_ADD
:
31 return kMessageTypeAddString
;
32 case AccountMapping::MSG_REMOVE
:
33 return kMessageTypeRemoveString
;
40 bool StringToMessageType(const std::string
& type_str
,
41 AccountMapping::MessageType
* type
) {
42 if (type_str
.compare(kMessageTypeAddString
) == 0)
43 *type
= AccountMapping::MSG_ADD
;
44 else if (type_str
.compare(kMessageTypeRemoveString
) == 0)
45 *type
= AccountMapping::MSG_REMOVE
;
46 else if (type_str
.compare(kMessageTypeNoneString
) == 0)
47 *type
= AccountMapping::MSG_NONE
;
56 AccountMapping::AccountMapping() {
59 AccountMapping::~AccountMapping() {
62 std::string
AccountMapping::SerializeAsString() const {
65 value
.append(kSeparator
);
66 value
.append(base::Int64ToString(status_change_timestamp
.ToInternalValue()));
67 value
.append(kSeparator
);
68 value
.append(MessageTypeToString(last_message_type
));
69 if (last_message_type
!= MSG_NONE
) {
70 value
.append(kSeparator
);
71 value
.append(last_message_id
);
77 bool AccountMapping::ParseFromString(const std::string
& value
) {
78 std::vector
<std::string
> values
;
79 Tokenize(value
, kSeparator
, &values
);
80 if (values
.size() != kSizeWithNoMessage
&&
81 values
.size() != kSizeWithMessage
) {
85 if (values
[kEmailIndex
].empty() ||
86 values
[kMappingChangeTimestampIndex
].empty() ||
87 values
[kMessageTypeIndex
].empty()) {
91 if (values
.size() == kSizeWithMessage
&& values
[kMessageIdIndex
].empty()) {
95 MessageType message_type
;
96 if (!StringToMessageType(values
[kMessageTypeIndex
], &message_type
))
99 if ((message_type
== MSG_NONE
&& values
.size() == kSizeWithMessage
) ||
100 (message_type
!= MSG_NONE
&& values
.size() != kSizeWithMessage
)) {
104 last_message_type
= message_type
;
106 int64 status_change_ts_internal
= 0LL;
107 if (!base::StringToInt64(values
[kMappingChangeTimestampIndex
],
108 &status_change_ts_internal
)) {
112 if (status_change_ts_internal
== 0LL)
114 else if (last_message_type
== MSG_REMOVE
)
119 if (values
.size() == kSizeWithMessage
)
120 last_message_id
= values
[kMessageIdIndex
];
122 last_message_id
.clear();
124 email
= values
[kEmailIndex
];
125 status_change_timestamp
=
126 base::Time::FromInternalValue(status_change_ts_internal
);
127 access_token
.clear();