OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/chromeos/system_logs/lsb_release_log_source.h" | 5 #include "chrome/browser/chromeos/system_logs/lsb_release_log_source.h" |
6 | 6 |
7 #include <string> | 7 #include "base/memory/scoped_ptr.h" |
8 | 8 #include "base/sys_info.h" |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/file_util.h" | |
12 #include "base/files/file_path.h" | |
13 #include "base/logging.h" | |
14 #include "base/strings/string_split.h" | |
15 #include "base/strings/string_util.h" | |
16 #include "content/public/browser/browser_thread.h" | |
17 | |
18 using content::BrowserThread; | |
19 | |
20 const char kInvalidLogEntry[] = "<invalid characters in log entry>"; | |
21 const char kEmptyLogEntry[] = "<no value>"; | |
22 | |
23 namespace { | |
24 | |
25 typedef std::pair<std::string, std::string> KeyValuePair; | |
26 | |
27 } // namespace | |
28 | 9 |
29 namespace chromeos { | 10 namespace chromeos { |
30 | 11 |
31 void LsbReleaseLogSource::Fetch(const SysLogsSourceCallback& callback) { | 12 void LsbReleaseLogSource::Fetch(const SysLogsSourceCallback& callback) { |
32 DCHECK(!callback.is_null()); | 13 DCHECK(!callback.is_null()); |
33 | 14 scoped_ptr<SystemLogsResponse> response(new SystemLogsResponse); |
34 SystemLogsResponse* response = new SystemLogsResponse; | 15 const base::SysInfo::LsbReleaseMap& lsb_map = |
35 BrowserThread::PostBlockingPoolTaskAndReply( | 16 base::SysInfo::GetLsbReleaseMap(); |
36 FROM_HERE, | 17 for (base::SysInfo::LsbReleaseMap::const_iterator iter = lsb_map.begin(); |
37 base::Bind(&LsbReleaseLogSource::ReadLSBRelease, response), | 18 iter != lsb_map.end(); ++iter) { |
38 base::Bind(callback, | 19 (*response)[iter->first] = iter->second; |
39 base::Owned(response))); | |
40 } | |
41 | |
42 void LsbReleaseLogSource::ReadLSBRelease(SystemLogsResponse* response) { | |
43 DCHECK(response); | |
44 | |
45 const base::FilePath lsb_release_file("/etc/lsb-release"); | |
46 std::string lsb_data; | |
47 bool read_success = base::ReadFileToString(lsb_release_file, &lsb_data); | |
48 // if we were using an internal temp file, the user does not need the | |
49 // logs to stay past the ReadFile call - delete the file | |
50 if (!read_success) { | |
51 LOG(ERROR) << "Can't access /etc/lsb-release file."; | |
52 return; | |
53 } | 20 } |
54 ParseLSBRelease(lsb_data, response); | 21 callback.Run(response.get()); |
55 } | |
56 | |
57 void LsbReleaseLogSource::ParseLSBRelease(const std::string& lsb_data, | |
58 SystemLogsResponse* response) { | |
59 std::vector<KeyValuePair> pairs; | |
60 base::SplitStringIntoKeyValuePairs(lsb_data, '=', '\n', &pairs); | |
61 for (size_t i = 0; i < pairs.size(); ++i) { | |
62 std::string key, value; | |
63 TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key); | |
64 TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value); | |
65 | |
66 if (key.empty()) | |
67 continue; | |
68 if (!IsStringUTF8(value) || !IsStringUTF8(key)) { | |
69 LOG(WARNING) << "Invalid characters in system log entry: " << key; | |
70 (*response)[key] = kInvalidLogEntry; | |
71 continue; | |
72 } | |
73 | |
74 if (value.empty()) | |
75 (*response)[key] = kEmptyLogEntry; | |
76 else | |
77 (*response)[key] = value; | |
78 } | |
79 } | 22 } |
80 | 23 |
81 } // namespace chromeos | 24 } // namespace chromeos |
82 | |
OLD | NEW |