Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5880)

Unified Diff: chrome/browser/chromeos/system_logs/lsbrelease_log_source.cc

Issue 10827130: Refactoring the SysInfoProvider. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..d291aa630029561312f714397b24b2683efc646d
--- /dev/null
+++ b/chrome/browser/chromeos/system_logs/lsbrelease_log_source.cc
@@ -0,0 +1,129 @@
+// 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 "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.
+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;
+ }
+}
+
+} // namespace
+
+namespace chromeos {
+
+void LsbReleaseLogSource::Fetch(const SysLogsFetcherCallback& request) {
+ SystemLogsResponse* response = new SystemLogsResponse;
+ BrowserThread::PostBlockingPoolTaskAndReply(FROM_HERE,
+ base::Bind(
+ &LsbReleaseLogSource::Execute,
+ response),
+ base::Bind(request, response));
+}
+
+void LsbReleaseLogSource::Execute(SystemLogsResponse* response) {
satorux1 2012/08/13 18:29:03 move this to anonymous namespace?
tudalex(Chromium) 2012/08/14 03:24:00 Same as CommandLineLogSource::Execute. Moved it an
+ 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.";
+ 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;
+ }
+ }
+}
+
+} // namespace chromeos
+

Powered by Google App Engine
This is Rietveld 408576698