Index: chrome/browser/chromeos/policy/system_log_uploader.cc |
diff --git a/chrome/browser/chromeos/policy/system_log_uploader.cc b/chrome/browser/chromeos/policy/system_log_uploader.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4576d561ffc8531ffb00f883ded180a35e69a8d5 |
--- /dev/null |
+++ b/chrome/browser/chromeos/policy/system_log_uploader.cc |
@@ -0,0 +1,213 @@ |
+// Copyright (c) 2015 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 "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/files/file_util.h" |
+#include "base/location.h" |
+#include "base/task/cancelable_task_tracker.h" |
+#include "base/task_runner_util.h" |
+#include "base/thread_task_runner_handle.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/chromeos/policy/system_log_uploader.h" |
+#include "chrome/browser/chromeos/policy/upload_job_impl.h" |
+#include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
+#include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+namespace { |
+// Determines the time between log uploads. |
+const int64 kDefaultUploadDelayMs = 12 * 60 * 60 * 1000; // 12 hours |
+ |
+// Determines the time, measured from the time of last failed upload, |
+// after which the log upload is retried. |
+const int64 kErrorUploadDelayMs = 120 * 1000; // 120 seconds |
+ |
+// The maximum number of consequent retries. |
Andrew T Wilson (Slow)
2015/07/17 15:43:58
nit: maybe use a different word than consequent th
Polina Bondarenko
2015/07/23 14:27:31
Changed to 'successive retries'.
|
+const int kMaxNumRetries = 1; |
+ |
+// The file names of the system logs to upload. |
+// Note: do not add anything to this list without checking for PII in the file. |
+const char* const kSystemLogFileNames[] = {"/var/log/bios_info.txt", |
+ "/var/log/chrome/chrome", |
+ "/var/log/eventlog.txt", |
+ "/var/log/messages", |
+ "/var/log/net.log", |
+ "/var/log/platform_info.txt", |
+ "/var/log/ui/ui.LATEST", |
+ "/var/log/update_engine.log"}; |
+ |
+// An implementation of the |SystemLogUploadJob::Delegate|. |
+class SystemLogDelegate : public policy::SystemLogUploadJob::Delegate { |
+ public: |
+ // FileReader - helper class that thread safely reads files from the disk. |
+ class FileReader : public base::RefCountedThreadSafe<FileReader> { |
+ public: |
+ base::CancelableTaskTracker::TaskId StartRead( |
+ const SystemLogDelegate::LogUploadCallback& upload_callback, |
+ base::CancelableTaskTracker* tracker); |
+ |
+ private: |
+ friend class base::RefCountedThreadSafe<FileReader>; |
+ ~FileReader() {} |
+ |
+ // Reads the system log files as binary files, stores the files as pairs |
+ // (file name, data) in the external structure to pass it to the |
+ // |upload_callback|. Called on the file thread (non-blocking). |
+ void ReadFiles(policy::SystemLogUploadJob::SystemLogs* system_logs); |
+ }; |
+ |
+ SystemLogDelegate(); |
+ ~SystemLogDelegate() override; |
+ |
+ // SystemLogUploadJob::Delegate: |
+ void LoadSystemLogs(const LogUploadCallback& upload_callback) override; |
+ |
+ scoped_ptr<policy::UploadJob> CreateUploadJob( |
+ const GURL& upload_url, |
+ policy::UploadJob::Delegate* delegate) override; |
+ |
+ private: |
+ // Used in reading log files. |
+ base::CancelableTaskTracker tracker_; |
+ scoped_refptr<FileReader> file_reader_; |
+ |
+ base::WeakPtrFactory<SystemLogDelegate> weak_ptr_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SystemLogDelegate); |
+}; |
+ |
+base::CancelableTaskTracker::TaskId SystemLogDelegate::FileReader::StartRead( |
Andrew T Wilson (Slow)
2015/07/17 15:43:57
Why do we return a TaskId here - looks like it's i
Polina Bondarenko
2015/07/23 14:27:31
Yes, removed.
|
+ const SystemLogDelegate::LogUploadCallback& upload_callback, |
+ base::CancelableTaskTracker* tracker) { |
+ // Owned by reply callback posted below. |
+ policy::SystemLogUploadJob::SystemLogs* system_logs = |
+ new policy::SystemLogUploadJob::SystemLogs(); |
+ |
+ // Run ReadFiles() in the thread that interacts with the file |
+ // system and return to the current thread. |
+ return tracker->PostTaskAndReply( |
+ content::BrowserThread::GetMessageLoopProxyForThread( |
+ content::BrowserThread::FILE).get(), |
+ FROM_HERE, base::Bind(&FileReader::ReadFiles, this, system_logs), |
+ base::Bind(upload_callback, base::Owned(system_logs))); |
+} |
+ |
+void SystemLogDelegate::FileReader::ReadFiles( |
+ policy::SystemLogUploadJob::SystemLogs* system_logs) { |
+ // Must be called on the file thread. |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); |
+ |
+ for (auto const file_path : kSystemLogFileNames) { |
+ if (!base::PathExists(base::FilePath(file_path))) |
+ continue; |
+ system_logs->push_back(std::make_pair(file_path, std::string())); |
+ if (!base::ReadFileToString(base::FilePath(file_path), |
+ &(system_logs->back().second))) { |
+ LOG(ERROR) << "Failed to read the system log file from the disk " |
+ << file_path << std::endl; |
+ } |
+ } |
+} |
+ |
+SystemLogDelegate::SystemLogDelegate() |
+ : file_reader_(new FileReader()), weak_ptr_factory_(this) { |
+} |
+ |
+SystemLogDelegate::~SystemLogDelegate() { |
+} |
+ |
+void SystemLogDelegate::LoadSystemLogs( |
+ const SystemLogDelegate::LogUploadCallback& upload_callback) { |
+ file_reader_->StartRead(upload_callback, &tracker_); |
+} |
+ |
+scoped_ptr<policy::UploadJob> SystemLogDelegate::CreateUploadJob( |
+ const GURL& upload_url, |
+ policy::UploadJob::Delegate* delegate) { |
+ chromeos::DeviceOAuth2TokenService* device_oauth2_token_service = |
+ chromeos::DeviceOAuth2TokenServiceFactory::Get(); |
+ |
+ scoped_refptr<net::URLRequestContextGetter> system_request_context = |
+ g_browser_process->system_request_context(); |
+ std::string robot_account_id = |
+ device_oauth2_token_service->GetRobotAccountId(); |
+ return scoped_ptr<policy::UploadJob>(new policy::UploadJobImpl( |
+ upload_url, robot_account_id, device_oauth2_token_service, |
+ system_request_context, delegate, |
+ make_scoped_ptr(new policy::UploadJobImpl::RandomMimeBoundaryGenerator))); |
+} |
+ |
+} // namespace |
+ |
+namespace policy { |
+ |
+SystemLogUploader::SystemLogUploader( |
+ const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
+ : retry_count_(0), |
+ upload_frequency_( |
+ base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs)), |
+ task_runner_(task_runner), |
+ weak_factory_(this) { |
+ // Immediately schedule the next system log upload (last_upload_attempt_ is |
+ // set to the start of the epoch, so this will trigger an update upload in the |
+ // immediate future). |
+ ScheduleNextSystemLogUpload(upload_frequency_); |
+} |
+ |
+SystemLogUploader::~SystemLogUploader() { |
+} |
+ |
+SystemLogUploadJob* SystemLogUploader::CreateSystemLogUploadJob( |
+ const base::Closure& succeeded_callback, |
+ const base::Closure& failed_callback) { |
+ return new SystemLogUploadJob(make_scoped_ptr(new SystemLogDelegate()), |
+ succeeded_callback, failed_callback); |
+} |
+ |
+void SystemLogUploader::OnSuccess() { |
+ // On successful log upload schedule the next log upload after |
+ // upload_frequency_ time from now. |
+ upload_job_.reset(); |
+ retry_count_ = 0; |
+ last_upload_attempt_ = base::Time::NowFromSystemTime(); |
+ |
+ ScheduleNextSystemLogUpload(upload_frequency_); |
+} |
+ |
+void SystemLogUploader::OnFailure() { |
+ // On first failure log upload try to re-upload logs after kErrorUploadDelayMs |
+ // time from now. |
+ upload_job_.reset(); |
+ last_upload_attempt_ = base::Time::NowFromSystemTime(); |
+ |
+ if (retry_count_++ < kMaxNumRetries) { |
+ ScheduleNextSystemLogUpload( |
+ base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); |
+ } else { |
+ // No more retries. |
+ retry_count_ = 0; |
+ ScheduleNextSystemLogUpload(upload_frequency_); |
+ } |
+} |
+ |
+void SystemLogUploader::StartLogUpload() { |
+ upload_job_.reset(CreateSystemLogUploadJob( |
+ Bind(&SystemLogUploader::OnSuccess, weak_factory_.GetWeakPtr()), |
+ Bind(&SystemLogUploader::OnFailure, weak_factory_.GetWeakPtr()))); |
+ upload_job_->Run(); |
+} |
+ |
+void SystemLogUploader::ScheduleNextSystemLogUpload(base::TimeDelta frequency) { |
+ // Calculate when to fire off the next update. |
+ base::TimeDelta delay = std::max( |
+ (last_upload_attempt_ + frequency) - base::Time::NowFromSystemTime(), |
+ base::TimeDelta()); |
+ task_runner_->PostDelayedTask(FROM_HERE, |
+ base::Bind(&SystemLogUploader::StartLogUpload, |
+ weak_factory_.GetWeakPtr()), |
+ delay); |
+} |
+ |
+} // namespace policy |