Chromium Code Reviews| 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..dfefcfd8ea9bb20ba035580a29f03884caccb985 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/syslogs/lsbrelease_fetcher.cc |
| @@ -0,0 +1,135 @@ |
| +// 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 <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 "content/public/browser/browser_thread.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace { |
| +// TODO(tudalex): Replace this code with a more simpler code based on a string |
| +// tokenizer, this code was copied over from the SysLogsProvide class. |
| + |
| +// 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; |
| + } |
| +} |
| + |
| +// Gets the info from the /etc/lsb-release and parses it |
| +void Execute(const chromeos::SysLogsFetcherCallback& request) { |
| + chromeos::SysLogsResponse* response = new chromeos::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) |
| + { |
|
satorux1
2012/08/09 21:15:07
move { to the previous line.
tudalex(Chromium)
2012/08/10 00:30:31
Done.
|
| + 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; |
| + } |
| + } |
| + BrowserThread::PostTask(content::BrowserThread::UI, |
|
satorux1
2012/08/09 21:15:07
please don't do this. please use PostTaskAndReply
tudalex(Chromium)
2012/08/10 00:30:31
Done.
|
| + FROM_HERE, |
| + base::Bind(request, response)); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace chromeos { |
| + |
| + |
| +void LSBReleaseFetcher::Fetch(const SysLogsFetcherCallback& request) { |
| + BrowserThread::PostBlockingPoolTask(FROM_HERE, |
| + base::Bind(&Execute, |
| + request)); |
| + |
| +} |
| + |
| +} // namespace chromeos |