Home | History | Annotate | Download | only in engine
      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.
      4 
      5 #include "google_apis/gcm/engine/registration_info.h"
      6 
      7 #include "base/strings/string_util.h"
      8 
      9 namespace gcm {
     10 
     11 RegistrationInfo::RegistrationInfo() {
     12 }
     13 
     14 RegistrationInfo::~RegistrationInfo() {
     15 }
     16 
     17 std::string RegistrationInfo::SerializeAsString() const {
     18   if (sender_ids.empty() || registration_id.empty())
     19     return std::string();
     20 
     21   // Serialize as:
     22   //    sender1,sender2,...=reg_id
     23   std::string value;
     24   for (std::vector<std::string>::const_iterator iter = sender_ids.begin();
     25        iter != sender_ids.end(); ++iter) {
     26     DCHECK(!iter->empty() &&
     27            iter->find(',') == std::string::npos &&
     28            iter->find('=') == std::string::npos);
     29     if (!value.empty())
     30       value += ",";
     31     value += *iter;
     32   }
     33 
     34   DCHECK(registration_id.find('=') == std::string::npos);
     35   value += '=';
     36   value += registration_id;
     37   return value;
     38 }
     39 
     40 bool RegistrationInfo::ParseFromString(const std::string& value) {
     41   if (value.empty())
     42     return true;
     43 
     44   size_t pos = value.find('=');
     45   if (pos == std::string::npos)
     46     return false;
     47 
     48   std::string senders = value.substr(0, pos);
     49   registration_id = value.substr(pos + 1);
     50 
     51   Tokenize(senders, ",", &sender_ids);
     52 
     53   if (sender_ids.empty() || registration_id.empty()) {
     54     sender_ids.clear();
     55     registration_id.clear();
     56     return false;
     57   }
     58 
     59   return true;
     60 }
     61 
     62 }  // namespace gcm
     63