| Index: chrome/browser/chromeos/syslogs/lsbrelease_fetcher.cc
|
| diff --git a/chrome/browser/chromeos/syslogs/lsbrelease_fetcher.cc b/chrome/browser/chromeos/syslogs/lsbrelease_fetcher.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..5cb2b704beddd2a0341212e649362c944e200702
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/syslogs/lsbrelease_fetcher.cc
|
| @@ -0,0 +1,117 @@
|
| +// 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/syslogs/lsbrelease_fetcher.h"
|
| +
|
| +#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"
|
| +
|
| +
|
| +namespace chromeos {
|
| +
|
| +// TODO(tudalex): Replace this code with a more simpler code based on a string
|
| +// tokenizer
|
| +
|
| +// The following code is copied form SysLogsProvider
|
| +const char kMultilineQuote[] = "\"\"\"";
|
| +const char kNewLineChars[] = "\r\n";
|
| +const char kInvalidLogEntry[] = "<invalid characters in log entry>";
|
| +const char kEmptyLogEntry[] = "<no value>";
|
| +
|
| +// Reads a key from the input string erasing the read values + delimiters read
|
| +// from the initial string
|
| +std::string ReadKey(std::string* data) {
|
| + size_t equal_sign = data->find("=");
|
| + if (equal_sign == std::string::npos)
|
| + return std::string("");
|
| + std::string key = data->substr(0, equal_sign);
|
| + data->erase(0, equal_sign);
|
| + if (data->size() > 0) {
|
| + // erase the equal to sign also
|
| + data->erase(0,1);
|
| + return key;
|
| + }
|
| + return std::string();
|
| +}
|
| +
|
| +// Reads a value from the input string; erasing the read values from
|
| +// the initial string; detects if the value is multiline and reads
|
| +// accordingly
|
| +std::string ReadValue(std::string* data) {
|
| + // Trim the leading spaces and tabs. In order to use a multi-line
|
| + // value, you have to place the multi-line quote on the same line as
|
| + // the equal sign.
|
| + //
|
| + // Why not use TrimWhitespace? Consider the following input:
|
| + //
|
| + // KEY1=
|
| + // KEY2=VALUE
|
| + //
|
| + // If we use TrimWhitespace, we will incorrectly trim the new line
|
| + // and assume that KEY1's value is "KEY2=VALUE" rather than empty.
|
| + TrimString(*data, " \t", data);
|
| +
|
| + // If multiline value
|
| + if (StartsWithASCII(*data, std::string(kMultilineQuote), false)) {
|
| + data->erase(0, strlen(kMultilineQuote));
|
| + size_t next_multi = data->find(kMultilineQuote);
|
| + if (next_multi == std::string::npos) {
|
| + // Error condition, clear data to stop further processing
|
| + data->erase();
|
| + return std::string();
|
| + }
|
| + std::string value = data->substr(0, next_multi);
|
| + data->erase(0, next_multi + 3);
|
| + return value;
|
| + } else { // single line value
|
| + size_t endl_pos = data->find_first_of(kNewLineChars);
|
| + // if we don't find a new line, we just return the rest of the data
|
| + std::string value = data->substr(0, endl_pos);
|
| + data->erase(0, endl_pos);
|
| + return value;
|
| + }
|
| +}
|
| +
|
| +void LSBReleaseFetcher::Fetch(SysLogsFetcherCallback request) {
|
| + SysLogsResponse* response = new SysLogsResponse;
|
| + FilePath lsb_release_file("/etc/lsb-release");
|
| + std::string data;
|
| + bool read_success = file_util::ReadFileToString(lsb_release_file,
|
| + &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.";
|
| + request.Run(response);
|
| + return;
|
| + }
|
| + while (data.length() > 0) {
|
| + std::string key = ReadKey(&data);
|
| + TrimWhitespaceASCII(key, TRIM_ALL, &key);
|
| + if (!key.empty()) {
|
| + std::string value = ReadValue(&data);
|
| + if (IsStringUTF8(value)) {
|
| + TrimWhitespaceASCII(value, TRIM_ALL, &value);
|
| + if (value.empty())
|
| + (*response)[key] = kEmptyLogEntry;
|
| + else
|
| + (*response)[key] = value;
|
| + } else {
|
| + LOG(WARNING) << "Invalid characters in system log entry: " << key;
|
| + (*response)[key] = kInvalidLogEntry;
|
| + }
|
| + } else {
|
| + // no more keys, we're done
|
| + break;
|
| + }
|
| + }
|
| + request.Run(response);
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|