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_split.h"
9 #include "base/strings/string_util.h"
15 const char kSeparator
[] = "&";
16 const uint32 kEmailIndex
= 0;
17 const uint32 kStatusIndex
= 1;
18 const uint32 kStatusChangeTimestampIndex
= 2;
19 const uint32 kSizeWithNoMessage
= kStatusChangeTimestampIndex
+ 1;
20 const uint32 kMessageIdIndex
= 3;
21 const uint32 kSizeWithMessage
= kMessageIdIndex
+ 1;
23 const char kStatusNew
[] = "new";
24 const char kStatusAdding
[] = "adding";
25 const char kStatusMapped
[] = "mapped";
26 const char kStatusRemoving
[] = "removing";
28 std::string
StatusToString(AccountMapping::MappingStatus status
) {
30 case AccountMapping::NEW
:
32 case AccountMapping::ADDING
:
34 case AccountMapping::MAPPED
:
36 case AccountMapping::REMOVING
:
37 return kStatusRemoving
;
44 bool StringToStatus(const std::string
& status_str
,
45 AccountMapping::MappingStatus
* status
) {
46 if (status_str
.compare(kStatusAdding
) == 0)
47 *status
= AccountMapping::ADDING
;
48 else if (status_str
.compare(kStatusMapped
) == 0)
49 *status
= AccountMapping::MAPPED
;
50 else if (status_str
.compare(kStatusRemoving
) == 0)
51 *status
= AccountMapping::REMOVING
;
52 else if (status_str
.compare(kStatusNew
) == 0)
53 *status
= AccountMapping::NEW
;
62 AccountMapping::AccountMapping() : status(NEW
) {
65 AccountMapping::~AccountMapping() {
68 std::string
AccountMapping::SerializeAsString() const {
71 value
.append(kSeparator
);
72 value
.append(StatusToString(status
));
73 value
.append(kSeparator
);
74 value
.append(base::Int64ToString(status_change_timestamp
.ToInternalValue()));
75 if (!last_message_id
.empty()) {
76 value
.append(kSeparator
);
77 value
.append(last_message_id
);
83 bool AccountMapping::ParseFromString(const std::string
& value
) {
84 std::vector
<std::string
> values
= base::SplitString(
85 value
, kSeparator
, base::KEEP_WHITESPACE
, base::SPLIT_WANT_NONEMPTY
);
86 if (values
.size() != kSizeWithNoMessage
&&
87 values
.size() != kSizeWithMessage
) {
91 if (values
[kEmailIndex
].empty() ||
92 values
[kStatusChangeTimestampIndex
].empty() ||
93 values
[kStatusIndex
].empty()) {
97 if (values
.size() == kSizeWithMessage
&& values
[kMessageIdIndex
].empty())
100 MappingStatus temp_status
;
101 if (!StringToStatus(values
[kStatusIndex
], &temp_status
))
104 if (values
.size() == kSizeWithNoMessage
&& temp_status
== ADDING
)
107 int64 status_change_ts_internal
= 0LL;
108 if (!base::StringToInt64(values
[kStatusChangeTimestampIndex
],
109 &status_change_ts_internal
)) {
113 status_change_timestamp
=
114 base::Time::FromInternalValue(status_change_ts_internal
);
115 status
= temp_status
;
116 email
= values
[kEmailIndex
];
117 access_token
.clear();
119 if (values
.size() == kSizeWithMessage
)
120 last_message_id
= values
[kMessageIdIndex
];
122 last_message_id
.clear();