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.
5 #include "chrome/browser/extensions/api/sessions/session_id.h"
7 #include "base/strings/string_number_conversions.h"
11 const char kIdSeparator
= '.';
14 scoped_ptr
<SessionId
> SessionId::Parse(const std::string
& session_id
) {
15 std::string session_tag
;
17 // Populate session_tag if the |session_id| represents a foreign SessionId.
18 std::size_t separator
= session_id
.find(kIdSeparator
);
19 if (separator
!= std::string::npos
) {
20 session_tag
= session_id
.substr(0, separator
);
23 // session_tag will be the empty string for local sessions that have only
24 // a unique integer as the identifier.
26 if (!base::StringToInt(
27 session_tag
.empty() ? session_id
: session_id
.substr(separator
+ 1),
29 return scoped_ptr
<SessionId
>();
31 return make_scoped_ptr(new SessionId(session_tag
, id
));
34 SessionId::SessionId(const std::string
& session_tag
, int id
)
35 : session_tag_(session_tag
), id_(id
) {
38 bool SessionId::IsForeign() const {
39 return !session_tag_
.empty();
42 std::string
SessionId::ToString() const {
44 (session_tag_
+ kIdSeparator
+ base::IntToString(id_
))
45 : base::IntToString(id_
);
48 } // namespace extensions