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

Side by Side Diff: chrome/browser/chromeos/syslogs/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/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 using content::BrowserThread;
18
19 namespace {
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 // Gets the info from the /etc/lsb-release and parses it
84 void Execute(const chromeos::SysLogsFetcherCallback& request) {
85 chromeos::SysLogsResponse* response = new chromeos::SysLogsResponse;
86 FilePath lsb_release_file("/etc/lsb-release");
87 std::string data;
88 bool read_success = file_util::ReadFileToString(lsb_release_file,
89 &data);
90 // if we were using an internal temp file, the user does not need the
91 // logs to stay past the ReadFile call - delete the file
92 if (!read_success)
93 {
satorux1 2012/08/09 21:15:07 move { to the previous line.
tudalex(Chromium) 2012/08/10 00:30:31 Done.
94 LOG(ERROR)<<"Can't access /etc/lsb-release file.";
95 request.Run(response);
96 return;
97 }
98 while (data.length() > 0) {
99 std::string key = ReadKey(&data);
100 TrimWhitespaceASCII(key, TRIM_ALL, &key);
101 if (!key.empty()) {
102 std::string value = ReadValue(&data);
103 if (IsStringUTF8(value)) {
104 TrimWhitespaceASCII(value, TRIM_ALL, &value);
105 if (value.empty())
106 (*response)[key] = kEmptyLogEntry;
107 else
108 (*response)[key] = value;
109 } else {
110 LOG(WARNING) << "Invalid characters in system log entry: " << key;
111 (*response)[key] = kInvalidLogEntry;
112 }
113 } else {
114 // no more keys, we're done
115 break;
116 }
117 }
118 BrowserThread::PostTask(content::BrowserThread::UI,
satorux1 2012/08/09 21:15:07 please don't do this. please use PostTaskAndReply
tudalex(Chromium) 2012/08/10 00:30:31 Done.
119 FROM_HERE,
120 base::Bind(request, response));
121 }
122
123 } // namespace
124
125 namespace chromeos {
126
127
128 void LSBReleaseFetcher::Fetch(const SysLogsFetcherCallback& request) {
129 BrowserThread::PostBlockingPoolTask(FROM_HERE,
130 base::Bind(&Execute,
131 request));
132
133 }
134
135 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698