Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / google_apis / gcm / engine / account_mapping.cc
blob7b0bfb0e3efbc17eb1d742b9684b22ea4e0ebdd2
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"
11 namespace gcm {
13 namespace {
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) {
29 switch (status) {
30 case AccountMapping::NEW:
31 return kStatusNew;
32 case AccountMapping::ADDING:
33 return kStatusAdding;
34 case AccountMapping::MAPPED:
35 return kStatusMapped;
36 case AccountMapping::REMOVING:
37 return kStatusRemoving;
38 default:
39 NOTREACHED();
41 return std::string();
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;
54 else
55 return false;
57 return true;
60 } // namespace
62 AccountMapping::AccountMapping() : status(NEW) {
65 AccountMapping::~AccountMapping() {
68 std::string AccountMapping::SerializeAsString() const {
69 std::string value;
70 value.append(email);
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);
80 return value;
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) {
88 return false;
91 if (values[kEmailIndex].empty() ||
92 values[kStatusChangeTimestampIndex].empty() ||
93 values[kStatusIndex].empty()) {
94 return false;
97 if (values.size() == kSizeWithMessage && values[kMessageIdIndex].empty())
98 return false;
100 MappingStatus temp_status;
101 if (!StringToStatus(values[kStatusIndex], &temp_status))
102 return false;
104 if (values.size() == kSizeWithNoMessage && temp_status == ADDING)
105 return false;
107 int64 status_change_ts_internal = 0LL;
108 if (!base::StringToInt64(values[kStatusChangeTimestampIndex],
109 &status_change_ts_internal)) {
110 return false;
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];
121 else
122 last_message_id.clear();
124 return true;
127 } // namespace gcm