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