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 const uint32 kEmailIndex
= 0;
16 const uint32 kStatusIndex
= 1;
17 const uint32 kStatusChangeTimestampIndex
= 2;
18 const uint32 kSizeWithNoMessage
= kStatusChangeTimestampIndex
+ 1;
19 const uint32 kMessageIdIndex
= 3;
20 const uint32 kSizeWithMessage
= kMessageIdIndex
+ 1;
22 const char kStatusNew
[] = "new";
23 const char kStatusAdding
[] = "adding";
24 const char kStatusMapped
[] = "mapped";
25 const char kStatusRemoving
[] = "removing";
27 std::string
StatusToString(AccountMapping::MappingStatus status
) {
29 case AccountMapping::NEW
:
31 case AccountMapping::ADDING
:
33 case AccountMapping::MAPPED
:
35 case AccountMapping::REMOVING
:
36 return kStatusRemoving
;
43 bool StringToStatus(const std::string
& status_str
,
44 AccountMapping::MappingStatus
* status
) {
45 if (status_str
.compare(kStatusAdding
) == 0)
46 *status
= AccountMapping::ADDING
;
47 else if (status_str
.compare(kStatusMapped
) == 0)
48 *status
= AccountMapping::MAPPED
;
49 else if (status_str
.compare(kStatusRemoving
) == 0)
50 *status
= AccountMapping::REMOVING
;
51 else if (status_str
.compare(kStatusNew
) == 0)
52 *status
= AccountMapping::NEW
;
61 AccountMapping::AccountMapping() : status(NEW
) {
64 AccountMapping::~AccountMapping() {
67 std::string
AccountMapping::SerializeAsString() const {
70 value
.append(kSeparator
);
71 value
.append(StatusToString(status
));
72 value
.append(kSeparator
);
73 value
.append(base::Int64ToString(status_change_timestamp
.ToInternalValue()));
74 if (!last_message_id
.empty()) {
75 value
.append(kSeparator
);
76 value
.append(last_message_id
);
82 bool AccountMapping::ParseFromString(const std::string
& value
) {
83 std::vector
<std::string
> values
;
84 Tokenize(value
, kSeparator
, &values
);
85 if (values
.size() != kSizeWithNoMessage
&&
86 values
.size() != kSizeWithMessage
) {
90 if (values
[kEmailIndex
].empty() ||
91 values
[kStatusChangeTimestampIndex
].empty() ||
92 values
[kStatusIndex
].empty()) {
96 if (values
.size() == kSizeWithMessage
&& values
[kMessageIdIndex
].empty())
99 MappingStatus temp_status
;
100 if (!StringToStatus(values
[kStatusIndex
], &temp_status
))
103 if (values
.size() == kSizeWithNoMessage
&& temp_status
== ADDING
)
106 int64 status_change_ts_internal
= 0LL;
107 if (!base::StringToInt64(values
[kStatusChangeTimestampIndex
],
108 &status_change_ts_internal
)) {
112 status_change_timestamp
=
113 base::Time::FromInternalValue(status_change_ts_internal
);
114 status
= temp_status
;
115 email
= values
[kEmailIndex
];
116 access_token
.clear();
118 if (values
.size() == kSizeWithMessage
)
119 last_message_id
= values
[kMessageIdIndex
];
121 last_message_id
.clear();