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

Side by Side Diff: chrome/browser/chromeos/system_logs/single_log_source.cc

Issue 2939543002: Refactor SingleLogSource for better test coverage (Closed)
Patch Set: Created 3 years, 6 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
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/system_logs/single_log_source.h" 5 #include "chrome/browser/chromeos/system_logs/single_log_source.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/task_scheduler/post_task.h" 9 #include "base/task_scheduler/post_task.h"
10 #include "content/public/browser/browser_thread.h" 10 #include "content/public/browser/browser_thread.h"
11 11
12 namespace system_logs { 12 namespace system_logs {
13 13
14 namespace { 14 namespace {
15 15
16 // Converts a logs source type to the corresponding filename. In the future, if 16 const char kDefaultSystemLogDirPath[] = "/var/log";
17 // non-file source types are added, this function should return an empty string. 17
18 std::string GetLogFileSourceFilename(SingleLogSource::SupportedSource source) { 18 // Converts a logs source type to the corresponding file path, relative to the
19 // base system log directory path. In the future, if non-file source types are
20 // added, this function should return an empty file path.
21 base::FilePath GetLogFileSourceRelativeFilePath(
22 SingleLogSource::SupportedSource source) {
19 switch (source) { 23 switch (source) {
20 case SingleLogSource::SupportedSource::kMessages: 24 case SingleLogSource::SupportedSource::kMessages:
21 return "/var/log/messages"; 25 return base::FilePath("messages");
22 case SingleLogSource::SupportedSource::kUiLatest: 26 case SingleLogSource::SupportedSource::kUiLatest:
23 return "/var/log/ui/ui.LATEST"; 27 return base::FilePath("ui/ui.LATEST");
24 } 28 }
25 NOTREACHED(); 29 NOTREACHED();
26 return ""; 30 return base::FilePath();
27 } 31 }
28 32
29 } // namespace 33 } // namespace
30 34
31 SingleLogSource::SingleLogSource(SupportedSource source) 35 SingleLogSource::SingleLogSource(SupportedSource source)
32 : SystemLogsSource(GetLogFileSourceFilename(source)), 36 : SystemLogsSource(GetLogFileSourceRelativeFilePath(source).value()),
37 log_file_dir_path_(kDefaultSystemLogDirPath),
33 num_bytes_read_(0), 38 num_bytes_read_(0),
34 weak_ptr_factory_(this) {} 39 weak_ptr_factory_(this) {}
35 40
36 SingleLogSource::~SingleLogSource() {} 41 SingleLogSource::~SingleLogSource() {}
37 42
38 void SingleLogSource::Fetch(const SysLogsSourceCallback& callback) { 43 void SingleLogSource::Fetch(const SysLogsSourceCallback& callback) {
39 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 44 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
40 DCHECK(!callback.is_null()); 45 DCHECK(!callback.is_null());
41 46
42 SystemLogsResponse* response = new SystemLogsResponse; 47 SystemLogsResponse* response = new SystemLogsResponse;
43 base::PostTaskWithTraitsAndReply( 48 base::PostTaskWithTraitsAndReply(
44 FROM_HERE, 49 FROM_HERE,
45 base::TaskTraits(base::MayBlock(), base::TaskPriority::BACKGROUND), 50 base::TaskTraits(base::MayBlock(), base::TaskPriority::BACKGROUND),
46 base::Bind(&SingleLogSource::ReadFile, weak_ptr_factory_.GetWeakPtr(), 51 base::Bind(&SingleLogSource::ReadFile, weak_ptr_factory_.GetWeakPtr(),
47 response), 52 response),
48 base::Bind(callback, base::Owned(response))); 53 base::Bind(callback, base::Owned(response)));
49 } 54 }
50 55
51 void SingleLogSource::ReadFile(SystemLogsResponse* result) { 56 void SingleLogSource::ReadFile(SystemLogsResponse* result) {
52 // Attempt to open the file if it was not previously opened. 57 // Attempt to open the file if it was not previously opened.
53 if (!file_.IsValid()) { 58 if (!file_.IsValid()) {
54 file_.Initialize(base::FilePath(source_name()), 59 file_.Initialize(base::FilePath(log_file_dir_path_).Append(source_name()),
55 base::File::FLAG_OPEN | base::File::FLAG_READ); 60 base::File::FLAG_OPEN | base::File::FLAG_READ);
56 if (!file_.IsValid()) 61 if (!file_.IsValid())
57 return; 62 return;
58 } 63 }
59 64
60 // Check for file size reset. 65 // Check for file size reset.
61 const size_t length = file_.GetLength(); 66 const size_t length = file_.GetLength();
62 if (length < num_bytes_read_) { 67 if (length < num_bytes_read_) {
63 num_bytes_read_ = 0; 68 num_bytes_read_ = 0;
64 file_.Seek(base::File::FROM_BEGIN, 0); 69 file_.Seek(base::File::FROM_BEGIN, 0);
(...skipping 16 matching lines...) Expand all
81 return; 86 return;
82 } 87 }
83 88
84 num_bytes_read_ += size_read; 89 num_bytes_read_ += size_read;
85 90
86 // Pass it back to the callback. 91 // Pass it back to the callback.
87 result->emplace(source_name(), anonymizer_.Anonymize(result_string)); 92 result->emplace(source_name(), anonymizer_.Anonymize(result_string));
88 } 93 }
89 94
90 } // namespace system_logs 95 } // namespace system_logs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698