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

Unified Diff: chrome/browser/chromeos/system_logs/single_log_source.cc

Issue 2844163005: Add SingleLogSource to system_logs sources (Closed)
Patch Set: Remove TODO, already done Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/system_logs/single_log_source.cc
diff --git a/chrome/browser/chromeos/system_logs/single_log_source.cc b/chrome/browser/chromeos/system_logs/single_log_source.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f1e460f0335f1d4b281a39f860811f6fd88c9bfa
--- /dev/null
+++ b/chrome/browser/chromeos/system_logs/single_log_source.cc
@@ -0,0 +1,101 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/system_logs/single_log_source.h"
+
+#include "base/bind.h"
+#include "base/files/file_path.h"
+#include "base/task_scheduler/post_task.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace system_logs {
+
+namespace {
+
+// Defines a pair: log source name and log filename.
+struct LogSource {
+ const char* name;
+ const char* filename;
+};
+
+// Whitelist of log sources that SingleLogSource is permitted to access.
+const LogSource kLogFileSources[] = {
+ {"messages", "/var/log/messages"},
+ {"ui_latest", "/var/log/ui/ui.LATEST"},
+};
+
+// Given a valid log source name from |kLogFileSources|, returns the
+// corresponding log filename. If the source name is not in the whitelist,
+// returns an empty filename.
+std::string GetLogFileSourceFilename(const std::string& source_name) {
afakhry 2017/05/06 01:12:13 This can now be a simple switch statement if we us
Simon Que 2017/05/06 14:19:42 Done.
+ for (const LogSource& source : kLogFileSources) {
+ if (source.name == source_name)
+ return source.filename;
+ }
+ return "";
+}
+
+} // namespace
+
+SingleLogSource::SingleLogSource() : SystemLogsSource(""), num_bytes_read_(0) {}
afakhry 2017/05/06 01:12:13 Please remove this default constructor. It seems u
Simon Que 2017/05/06 14:19:42 Done.
+
+SingleLogSource::SingleLogSource(const std::string& source_name)
+ : SystemLogsSource(source_name), num_bytes_read_(0) {}
+
+SingleLogSource::~SingleLogSource() {}
+
+void SingleLogSource::Fetch(const SysLogsSourceCallback& callback) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ DCHECK(!callback.is_null());
+
+ SystemLogsResponse* response = new SystemLogsResponse;
+ base::PostTaskWithTraitsAndReply(
+ FROM_HERE,
+ base::TaskTraits().MayBlock().WithPriority(
+ base::TaskPriority::BACKGROUND),
+ base::Bind(&SingleLogSource::ReadFile, base::Unretained(this), response),
+ base::Bind(callback, base::Owned(response)));
+}
+
+void SingleLogSource::ReadFile(SystemLogsResponse* result) {
+ // Attempt to open the file if it was not previously opened.
+ if (!file_.IsValid()) {
+ file_.Initialize(base::FilePath(GetLogFileSourceFilename(source_name())),
+ base::File::FLAG_OPEN | base::File::FLAG_READ);
+ if (!file_.IsValid())
+ return;
+ }
+
+ // Check for file size reset.
+ size_t length = file_.GetLength();
afakhry 2017/05/06 01:12:13 Nit: const here and everywhere below please.
Simon Que 2017/05/06 14:19:42 Done.
+ if (length < num_bytes_read_) {
+ num_bytes_read_ = 0;
+ file_.Seek(base::File::FROM_BEGIN, 0);
+ }
+
+ // Read from file until end.
+ size_t size_to_read = length - num_bytes_read_;
+ std::vector<char> read_result(size_to_read);
+ size_t size_read = file_.ReadAtCurrentPos(read_result.data(), size_to_read);
+ read_result.resize(size_read);
+
+ // Create a string from the read results.
+ std::string result_string;
afakhry 2017/05/06 01:12:13 Why don't you read into the string right away and
Simon Que 2017/05/06 14:19:43 ReadAtCurrentPos() writes to a char*, but std::str
afakhry 2017/05/08 19:20:47 You can use &result_string[0]. C++17 should provid
afakhry 2017/05/09 23:46:18 I see you missed this comment. Please take a look.
+
+ // The reader may only read complete lines.
+ if (!read_result.empty() && read_result.back() == '\n') {
+ result_string.reserve(read_result.size());
+ result_string.assign(read_result.begin(), read_result.end());
+ num_bytes_read_ += size_read;
+ } else {
+ // If an incomplete line was read, reset the file read offset to before the
+ // most recent read.
+ file_.Seek(base::File::FROM_CURRENT, -size_read);
+ }
+
+ // Pass it back to the callback.
+ (*result)[source_name()] = anonymizer_.Anonymize(result_string);
afakhry 2017/05/06 01:12:13 result->emplace(source_name_, anonymizer_.Anonymiz
Simon Que 2017/05/06 14:19:43 Done.
+}
+
+} // namespace system_logs

Powered by Google App Engine
This is Rietveld 408576698