OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include "chrome/browser/extensions/api/sessions/session_id.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | |
7 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
8 #include "chrome/browser/extensions/webstore_installer.h" | |
9 #include "chrome/common/omaha_query_params/omaha_query_params.h" | |
10 #include "extensions/common/id_util.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 using base::StringPrintf; | |
14 using chrome::OmahaQueryParams; | |
15 | 9 |
16 namespace extensions { | 10 namespace extensions { |
17 | 11 |
18 // Returns true if |target| is found in |source|. | 12 const char kIdSeparator = '.'; |
19 bool Contains(const std::string& source, const std::string& target) { | 13 |
20 return source.find(target) != std::string::npos; | 14 SessionId::SessionId(const std::string& session_tag, int id) |
15 : session_tag_(session_tag), id_(id) { | |
21 } | 16 } |
22 | 17 |
23 TEST(WebstoreInstallerTest, PlatformParams) { | 18 // static |
24 std::string id = extensions::id_util::GenerateId("some random string"); | 19 scoped_ptr<SessionId> SessionId::Parse(const std::string& session_id) { |
25 GURL url = WebstoreInstaller::GetWebstoreInstallURL(id, ""); | 20 std::string session_tag; |
26 std::string query = url.query(); | |
27 EXPECT_TRUE(Contains(query,StringPrintf("os=%s", OmahaQueryParams::getOS()))); | |
28 EXPECT_TRUE(Contains(query,StringPrintf("arch=%s", | |
29 OmahaQueryParams::getArch()))); | |
30 EXPECT_TRUE(Contains(query,StringPrintf("nacl_arch=%s", | |
31 OmahaQueryParams::getNaclArch()))); | |
32 | 21 |
22 // Populate session_tag if the |session_id| represents a foreign SessionId. | |
23 std::size_t separator = session_id.find(kIdSeparator); | |
24 if (separator != std::string::npos) { | |
25 session_tag = session_id.substr(0, separator); | |
26 } | |
27 | |
28 // session_tag will be the empty string for local sessions that have only | |
29 // a unique integer as the identifier. | |
30 int id; | |
31 if (!base::StringToInt( | |
32 session_tag.empty() ? session_id : session_id.substr(separator + 1), | |
33 &id)) { | |
34 return scoped_ptr<SessionId>(); | |
35 } | |
36 return scoped_ptr<SessionId>(new SessionId(session_tag, id)); | |
not at google - send to devlin
2013/08/15 20:16:24
you can use make_scoped_ptr(new SessionId(session_
Kristen Dwan
2013/08/16 22:03:06
Done.
| |
37 } | |
38 | |
39 bool SessionId::IsForeign() const { | |
40 return !session_tag_.empty(); | |
41 } | |
42 | |
43 std::string SessionId::ToString() const { | |
44 return IsForeign() ? | |
45 (session_tag_ + kIdSeparator + base::StringPrintf("%d", id_)) | |
46 : base::StringPrintf("%d", id_); | |
33 } | 47 } |
34 | 48 |
35 } // namespace extensions | 49 } // namespace extensions |
OLD | NEW |