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 "chrome/test/chromedriver/chrome/browser_info.h" | 5 #include "chrome/test/chromedriver/chrome/browser_info.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
30 | 30 |
31 Status ParseBrowserInfo(const std::string& data, BrowserInfo* browser_info) { | 31 Status ParseBrowserInfo(const std::string& data, BrowserInfo* browser_info) { |
32 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); | 32 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); |
33 if (!value.get()) | 33 if (!value.get()) |
34 return Status(kUnknownError, "version info not in JSON"); | 34 return Status(kUnknownError, "version info not in JSON"); |
35 | 35 |
36 base::DictionaryValue* dict; | 36 base::DictionaryValue* dict; |
37 if (!value->GetAsDictionary(&dict)) | 37 if (!value->GetAsDictionary(&dict)) |
38 return Status(kUnknownError, "version info not a dictionary"); | 38 return Status(kUnknownError, "version info not a dictionary"); |
39 | 39 |
40 std::string browser; | 40 bool is_android = dict->HasKey("Android-Package"); |
41 if (!dict->GetString("Browser", &browser)) { | 41 std::string browser_string; |
42 return Status(kUnknownError, | 42 if (!dict->GetString("Browser", &browser_string)) |
43 "version info doesn't include string 'Browser'"); | 43 return Status(kUnknownError, "version doesn't include 'Browser'"); |
44 } | 44 |
45 Status status = ParseBrowserString(is_android, browser_string, browser_info); | |
46 if (status.IsError()) | |
47 return status; | |
45 | 48 |
46 std::string blink_version; | 49 std::string blink_version; |
47 if (!dict->GetString("WebKit-Version", &blink_version)) { | 50 if (!dict->GetString("WebKit-Version", &blink_version)) |
48 return Status(kUnknownError, | 51 return Status(kUnknownError, "version doesn't include 'WebKit-Version'"); |
49 "version info doesn't include string 'WebKit-Version'"); | |
50 } | |
51 | |
52 Status status = ParseBrowserString(browser, &browser_info->browser_name, | |
53 &browser_info->browser_version, &browser_info->build_no); | |
54 | |
55 if (status.IsError()) | |
56 return status; | |
57 | 52 |
58 return ParseBlinkVersionString(blink_version, &browser_info->blink_revision); | 53 return ParseBlinkVersionString(blink_version, &browser_info->blink_revision); |
59 } | 54 } |
60 | 55 |
61 Status ParseBrowserString(const std::string& browser_string, | 56 Status ParseBrowserString(bool is_android, |
62 std::string* browser_name, | 57 const std::string& browser_string, |
63 std::string* browser_version, | 58 BrowserInfo* browser_info) { |
64 int* build_no) { | |
65 if (browser_string.empty()) { | 59 if (browser_string.empty()) { |
66 *browser_name = "content shell"; | 60 browser_info->browser_name = "content shell"; |
67 return Status(kOk); | |
68 } | |
69 | |
70 if (browser_string.find("Version/") == 0u) { | |
71 *browser_name = "webview"; | |
72 return Status(kOk); | 61 return Status(kOk); |
73 } | 62 } |
74 | 63 |
75 std::string prefix = "Chrome/"; | 64 std::string prefix = "Chrome/"; |
65 int build_no; | |
76 if (browser_string.find(prefix) == 0u) { | 66 if (browser_string.find(prefix) == 0u) { |
77 *browser_name = "chrome"; | 67 std::string version = browser_string.substr(prefix.length()); |
78 *browser_version = browser_string.substr(prefix.length()); | 68 std::vector<std::string> version_parts; |
69 base::SplitString(version, '.', &version_parts); | |
79 | 70 |
80 std::vector<std::string> version_parts; | |
81 base::SplitString(*browser_version, '.', &version_parts); | |
82 if (version_parts.size() != 4 || | 71 if (version_parts.size() != 4 || |
83 !base::StringToInt(version_parts[2], build_no)) { | 72 !base::StringToInt(version_parts[2], &build_no)) { |
84 return Status(kUnknownError, | 73 return Status(kUnknownError, "unrecognized Chrome version: " + version); |
85 "unrecognized Chrome version: " + *browser_version); | |
86 } | 74 } |
87 | 75 |
76 if (build_no != 0) { | |
stgao
2014/12/05 22:10:54
Would it be possible that |build_no| is un-initial
samuong
2014/12/05 22:17:57
The call to StringToInt() should return false if i
| |
77 browser_info->browser_name = "chrome"; | |
78 browser_info->browser_version = browser_string.substr(prefix.length()); | |
79 browser_info->build_no = build_no; | |
80 return Status(kOk); | |
81 } | |
82 } | |
83 | |
84 if (browser_string.find("Version/") == 0u || (is_android && build_no == 0)) { | |
stgao
2014/12/05 22:10:54
Maybe add some comment so that we know which condi
samuong
2014/12/05 22:17:57
Done.
| |
85 browser_info->browser_name = "webview"; | |
88 return Status(kOk); | 86 return Status(kOk); |
89 } | 87 } |
90 | 88 |
91 return Status(kUnknownError, | 89 return Status(kUnknownError, |
92 "unrecognized Chrome version: " + browser_string); | 90 "unrecognized Chrome version: " + browser_string); |
93 } | 91 } |
94 | 92 |
95 Status ParseBlinkVersionString(const std::string& blink_version, | 93 Status ParseBlinkVersionString(const std::string& blink_version, |
96 int* blink_revision) { | 94 int* blink_revision) { |
97 size_t before = blink_version.find('@'); | 95 size_t before = blink_version.find('@'); |
(...skipping 15 matching lines...) Expand all Loading... | |
113 return Status(kOk); | 111 return Status(kOk); |
114 } | 112 } |
115 | 113 |
116 bool IsGitHash(const std::string& revision) { | 114 bool IsGitHash(const std::string& revision) { |
117 const int kShortGitHashLength = 7; | 115 const int kShortGitHashLength = 7; |
118 const int kFullGitHashLength = 40; | 116 const int kFullGitHashLength = 40; |
119 return kShortGitHashLength <= revision.size() | 117 return kShortGitHashLength <= revision.size() |
120 && revision.size() <= kFullGitHashLength | 118 && revision.size() <= kFullGitHashLength |
121 && base::ContainsOnlyChars(revision, "0123456789abcdefABCDEF"); | 119 && base::ContainsOnlyChars(revision, "0123456789abcdefABCDEF"); |
122 } | 120 } |
OLD | NEW |