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

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

Powered by Google App Engine
This is Rietveld 408576698