1 // Copyright 2013 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 "chrome/browser/extensions/api/sessions/session_id.h" 6 7 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/stringprintf.h" 9 10 namespace extensions { 11 12 const char kIdSeparator = '.'; 13 14 // static Parse(const std::string & session_id)15scoped_ptr<SessionId> SessionId::Parse(const std::string& session_id) { 16 std::string session_tag; 17 18 // Populate session_tag if the |session_id| represents a foreign SessionId. 19 std::size_t separator = session_id.find(kIdSeparator); 20 if (separator != std::string::npos) { 21 session_tag = session_id.substr(0, separator); 22 } 23 24 // session_tag will be the empty string for local sessions that have only 25 // a unique integer as the identifier. 26 int id; 27 if (!base::StringToInt( 28 session_tag.empty() ? session_id : session_id.substr(separator + 1), 29 &id)) { 30 return scoped_ptr<SessionId>(); 31 } 32 return make_scoped_ptr(new SessionId(session_tag, id)); 33 } 34 SessionId(const std::string & session_tag,int id)35SessionId::SessionId(const std::string& session_tag, int id) 36 : session_tag_(session_tag), id_(id) { 37 } 38 IsForeign() const39bool SessionId::IsForeign() const { 40 return !session_tag_.empty(); 41 } 42 ToString() const43std::string SessionId::ToString() const { 44 return IsForeign() ? 45 (session_tag_ + kIdSeparator + base::StringPrintf("%d", id_)) 46 : base::StringPrintf("%d", id_); 47 } 48 49 } // namespace extensions 50