Chromium Code Reviews| Index: chrome/browser/chromeos/system_logs/lsbrelease_log_source.cc |
| diff --git a/chrome/browser/chromeos/system_logs/lsbrelease_log_source.cc b/chrome/browser/chromeos/system_logs/lsbrelease_log_source.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cd1bbcb26393aa5cbb097457b52571fc61146ac5 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/system_logs/lsbrelease_log_source.cc |
| @@ -0,0 +1,80 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/system_logs/lsbrelease_log_source.h" |
| + |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/file_path.h" |
| +#include "base/file_util.h" |
| +#include "base/logging.h" |
| +#include "base/string_util.h" |
| +#include "base/string_split.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace { |
| + |
| +const char kInvalidLogEntry[] = "<invalid characters in log entry>"; |
| +const char kEmptyLogEntry[] = "<no value>"; |
| + |
| +typedef std::pair<std::string, std::string> KeyValuePair; |
| + |
| +} // namespace |
| + |
| +namespace chromeos { |
| + |
| +void LsbReleaseLogSource::Fetch(const SysLogsFetcherCallback& callback) { |
| + DCHECK(!callback.is_null()); |
| + |
| + SystemLogsResponse* response = new SystemLogsResponse; |
| + BrowserThread::PostBlockingPoolTaskAndReply( |
| + FROM_HERE, |
| + base::Bind(&ReadLSBRelease, response), |
|
satorux1
2012/08/16 10:50:57
this should be &LsbReleaseLogSource::ReadLSBReleas
tudalex(Chromium)
2012/08/16 22:33:03
Done.
|
| + base::Bind(callback, |
| + base::Owned(response))); |
| +} |
| + |
| +void LsbReleaseLogSource::ReadLSBRelease(SystemLogsResponse* response) { |
| + DCHECK(response); |
| + |
| + const FilePath lsb_release_file("/etc/lsb-release"); |
| + std::string lsb_data; |
| + bool read_success = file_util::ReadFileToString(lsb_release_file, &lsb_data); |
| + // if we were using an internal temp file, the user does not need the |
| + // logs to stay past the ReadFile call - delete the file |
| + if (!read_success) { |
| + LOG(ERROR) << "Can't access /etc/lsb-release file."; |
| + return; |
| + } |
| + ParseLSBRelease(lsb_data, response); |
| +} |
| + |
| +void LsbReleaseLogSource::ParseLSBRelease(const std::string& lsb_data, |
| + SystemLogsResponse* response) { |
| + std::vector<KeyValuePair> pairs; |
| + base::SplitStringIntoKeyValuePairs(lsb_data, '=', '\n', &pairs); |
| + for (size_t i = 0; i < pairs.size(); ++i) { |
| + std::string key, value; |
| + TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key); |
| + TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value); |
| + if (!key.empty()) { |
|
satorux1
2012/08/16 10:50:57
you might want to do
if (key.empty())
continue;
tudalex(Chromium)
2012/08/16 22:33:03
Done.
|
| + if (IsStringUTF8(value) && IsStringUTF8(key)) { |
|
satorux1
2012/08/16 10:50:57
Likewise,
if (!IsStringUTF8(value) || !IsStringUT
tudalex(Chromium)
2012/08/16 22:33:03
Done.
|
| + if (value.empty()) |
| + (*response)[key] = kEmptyLogEntry; |
| + else |
| + (*response)[key] = value; |
| + } else { |
| + LOG(WARNING) << "Invalid characters in system log entry: " << key; |
| + (*response)[key] = kInvalidLogEntry; |
| + } |
| + } |
| + } |
| +} |
| + |
| +} // namespace chromeos |
| + |