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

Side by Side 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 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_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.
satorux1 2012/08/14 05:51:52 Ugh, copy-and-pasting this large amount of code is
tudalex(Chromium) 2012/08/15 01:54:33 Done.
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 // Gets the info from the /etc/lsb-release and parses it
83 void ParseLSBRelease(chromeos::SystemLogsResponse* response) {
satorux1 2012/08/14 05:51:52 Add DCHECK(response); followed by a blank line.
tudalex(Chromium) 2012/08/15 01:54:33 Done.
84 FilePath lsb_release_file("/etc/lsb-release");
satorux1 2012/08/14 05:51:52 const
tudalex(Chromium) 2012/08/15 01:54:33 Done.
85 std::string data;
86 bool read_success = file_util::ReadFileToString(lsb_release_file,
87 &data);
88 // if we were using an internal temp file, the user does not need the
89 // logs to stay past the ReadFile call - delete the file
90 if (!read_success) {
91 LOG(ERROR) << "Can't access /etc/lsb-release file.";
92 return;
93 }
94 while (data.length() > 0) {
satorux1 2012/08/14 05:51:52 I think what you want to do is something like: in
tudalex(Chromium) 2012/08/15 01:54:33 Talked to rkc@ and lsb-release actually doesn't ha
satorux1 2012/08/15 22:01:09 so while you are working on the similar code here,
tudalex(Chromium) 2012/08/16 01:08:57 The plan is that the old file is deprecated and th
95 std::string key = ReadKey(&data);
96 TrimWhitespaceASCII(key, TRIM_ALL, &key);
97 if (!key.empty()) {
98 std::string value = ReadValue(&data);
99 if (IsStringUTF8(value)) {
100 TrimWhitespaceASCII(value, TRIM_ALL, &value);
101 if (value.empty())
102 (*response)[key] = kEmptyLogEntry;
103 else
104 (*response)[key] = value;
105 } else {
106 LOG(WARNING) << "Invalid characters in system log entry: " << key;
107 (*response)[key] = kInvalidLogEntry;
108 }
109 } else {
110 // no more keys, we're done
111 break;
112 }
113 }
114 }
satorux1 2012/08/14 05:51:52 This function is complicated and can easily be uni
tudalex(Chromium) 2012/08/15 01:54:33 Done, but I had to split it into to so that I can
115
116 } // namespace
117
118 namespace chromeos {
119
120 void LsbReleaseLogSource::Fetch(const SysLogsFetcherCallback& request) {
121 SystemLogsResponse* response = new SystemLogsResponse;
122 BrowserThread::PostBlockingPoolTaskAndReply(
123 FROM_HERE,
124 base::Bind(&ParseLSBRelease, response),
125 base::Bind(request,
126 base::Owned(response)));
127 }
128
129 } // namespace chromeos
130
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698