Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/system_logs/lsbrelease_log_source.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/file_path.h" | |
| 12 #include "base/file_util.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/string_util.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 | |
| 17 using content::BrowserThread; | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // TODO(tudalex): Replace this code with a more simpler code based on a string | |
| 22 // tokenizer, this code was copied over from the SysLogsProvide class. | |
| 23 const char kMultilineQuote[] = "\"\"\""; | |
| 24 const char kNewLineChars[] = "\r\n"; | |
| 25 const char kInvalidLogEntry[] = "<invalid characters in log entry>"; | |
| 26 const char kEmptyLogEntry[] = "<no value>"; | |
| 27 | |
| 28 // Reads a key from the input string erasing the read values + delimiters read | |
| 29 // from the initial string | |
| 30 std::string ReadKey(std::string* data) { | |
| 31 size_t equal_sign = data->find("="); | |
| 32 if (equal_sign == std::string::npos) | |
| 33 return std::string(""); | |
| 34 std::string key = data->substr(0, equal_sign); | |
| 35 data->erase(0, equal_sign); | |
| 36 if (data->size() > 0) { | |
| 37 // erase the equal to sign also | |
| 38 data->erase(0, 1); | |
| 39 return key; | |
| 40 } | |
| 41 return std::string(); | |
| 42 } | |
| 43 | |
| 44 // Reads a value from the input string; erasing the read values from | |
| 45 // the initial string; detects if the value is multiline and reads | |
| 46 // accordingly | |
| 47 std::string ReadValue(std::string* data) { | |
| 48 // Trim the leading spaces and tabs. In order to use a multi-line | |
| 49 // value, you have to place the multi-line quote on the same line as | |
| 50 // the equal sign. | |
| 51 // | |
| 52 // Why not use TrimWhitespace? Consider the following input: | |
| 53 // | |
| 54 // KEY1= | |
| 55 // KEY2=VALUE | |
| 56 // | |
| 57 // If we use TrimWhitespace, we will incorrectly trim the new line | |
| 58 // and assume that KEY1's value is "KEY2=VALUE" rather than empty. | |
| 59 TrimString(*data, " \t", data); | |
| 60 | |
| 61 // If multiline value | |
| 62 if (StartsWithASCII(*data, std::string(kMultilineQuote), false)) { | |
| 63 data->erase(0, strlen(kMultilineQuote)); | |
| 64 size_t next_multi = data->find(kMultilineQuote); | |
| 65 if (next_multi == std::string::npos) { | |
| 66 // Error condition, clear data to stop further processing | |
| 67 data->erase(); | |
| 68 return std::string(); | |
| 69 } | |
| 70 std::string value = data->substr(0, next_multi); | |
| 71 data->erase(0, next_multi + 3); | |
| 72 return value; | |
| 73 } else { // single line value | |
| 74 size_t endl_pos = data->find_first_of(kNewLineChars); | |
| 75 // if we don't find a new line, we just return the rest of the data | |
| 76 std::string value = data->substr(0, endl_pos); | |
| 77 data->erase(0, endl_pos); | |
| 78 return value; | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 namespace chromeos { | |
| 85 | |
| 86 void LsbReleaseLogSource::Fetch(const SysLogsFetcherCallback& request) { | |
| 87 SystemLogsResponse* response = new SystemLogsResponse; | |
| 88 BrowserThread::PostBlockingPoolTaskAndReply(FROM_HERE, | |
| 89 base::Bind( | |
| 90 &LsbReleaseLogSource::Execute, | |
| 91 response), | |
| 92 base::Bind(request, response)); | |
| 93 } | |
| 94 | |
| 95 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
| |
| 96 FilePath lsb_release_file("/etc/lsb-release"); | |
| 97 std::string data; | |
| 98 bool read_success = file_util::ReadFileToString(lsb_release_file, | |
| 99 &data); | |
| 100 // if we were using an internal temp file, the user does not need the | |
| 101 // logs to stay past the ReadFile call - delete the file | |
| 102 if (!read_success) { | |
| 103 LOG(ERROR) << "Can't access /etc/lsb-release file."; | |
| 104 return; | |
| 105 } | |
| 106 while (data.length() > 0) { | |
| 107 std::string key = ReadKey(&data); | |
| 108 TrimWhitespaceASCII(key, TRIM_ALL, &key); | |
| 109 if (!key.empty()) { | |
| 110 std::string value = ReadValue(&data); | |
| 111 if (IsStringUTF8(value)) { | |
| 112 TrimWhitespaceASCII(value, TRIM_ALL, &value); | |
| 113 if (value.empty()) | |
| 114 (*response)[key] = kEmptyLogEntry; | |
| 115 else | |
| 116 (*response)[key] = value; | |
| 117 } else { | |
| 118 LOG(WARNING) << "Invalid characters in system log entry: " << key; | |
| 119 (*response)[key] = kInvalidLogEntry; | |
| 120 } | |
| 121 } else { | |
| 122 // no more keys, we're done | |
| 123 break; | |
| 124 } | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 } // namespace chromeos | |
| 129 | |
| OLD | NEW |