OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015 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 "base/bind.h" | |
6 #include "base/bind_helpers.h" | |
7 #include "base/files/file_util.h" | |
8 #include "base/location.h" | |
9 #include "base/task/cancelable_task_tracker.h" | |
10 #include "base/task_runner_util.h" | |
11 #include "base/thread_task_runner_handle.h" | |
12 #include "chrome/browser/browser_process.h" | |
13 #include "chrome/browser/chromeos/policy/system_log_uploader.h" | |
14 #include "chrome/browser/chromeos/policy/upload_job_impl.h" | |
15 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" | |
16 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h " | |
17 #include "content/public/browser/browser_thread.h" | |
18 | |
19 namespace { | |
20 // Determines the time between log uploads. | |
21 const int64 kDefaultUploadDelayMs = 12 * 60 * 60 * 1000; // 12 hours | |
22 | |
23 // Determines the time, measured from the time of last failed upload, | |
24 // after which the log upload is retried. | |
25 const int64 kErrorUploadDelayMs = 120 * 1000; // 120 seconds | |
26 | |
27 // 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'.
| |
28 const int kMaxNumRetries = 1; | |
29 | |
30 // The file names of the system logs to upload. | |
31 // Note: do not add anything to this list without checking for PII in the file. | |
32 const char* const kSystemLogFileNames[] = {"/var/log/bios_info.txt", | |
33 "/var/log/chrome/chrome", | |
34 "/var/log/eventlog.txt", | |
35 "/var/log/messages", | |
36 "/var/log/net.log", | |
37 "/var/log/platform_info.txt", | |
38 "/var/log/ui/ui.LATEST", | |
39 "/var/log/update_engine.log"}; | |
40 | |
41 // An implementation of the |SystemLogUploadJob::Delegate|. | |
42 class SystemLogDelegate : public policy::SystemLogUploadJob::Delegate { | |
43 public: | |
44 // FileReader - helper class that thread safely reads files from the disk. | |
45 class FileReader : public base::RefCountedThreadSafe<FileReader> { | |
46 public: | |
47 base::CancelableTaskTracker::TaskId StartRead( | |
48 const SystemLogDelegate::LogUploadCallback& upload_callback, | |
49 base::CancelableTaskTracker* tracker); | |
50 | |
51 private: | |
52 friend class base::RefCountedThreadSafe<FileReader>; | |
53 ~FileReader() {} | |
54 | |
55 // Reads the system log files as binary files, stores the files as pairs | |
56 // (file name, data) in the external structure to pass it to the | |
57 // |upload_callback|. Called on the file thread (non-blocking). | |
58 void ReadFiles(policy::SystemLogUploadJob::SystemLogs* system_logs); | |
59 }; | |
60 | |
61 SystemLogDelegate(); | |
62 ~SystemLogDelegate() override; | |
63 | |
64 // SystemLogUploadJob::Delegate: | |
65 void LoadSystemLogs(const LogUploadCallback& upload_callback) override; | |
66 | |
67 scoped_ptr<policy::UploadJob> CreateUploadJob( | |
68 const GURL& upload_url, | |
69 policy::UploadJob::Delegate* delegate) override; | |
70 | |
71 private: | |
72 // Used in reading log files. | |
73 base::CancelableTaskTracker tracker_; | |
74 scoped_refptr<FileReader> file_reader_; | |
75 | |
76 base::WeakPtrFactory<SystemLogDelegate> weak_ptr_factory_; | |
77 | |
78 DISALLOW_COPY_AND_ASSIGN(SystemLogDelegate); | |
79 }; | |
80 | |
81 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.
| |
82 const SystemLogDelegate::LogUploadCallback& upload_callback, | |
83 base::CancelableTaskTracker* tracker) { | |
84 // Owned by reply callback posted below. | |
85 policy::SystemLogUploadJob::SystemLogs* system_logs = | |
86 new policy::SystemLogUploadJob::SystemLogs(); | |
87 | |
88 // Run ReadFiles() in the thread that interacts with the file | |
89 // system and return to the current thread. | |
90 return tracker->PostTaskAndReply( | |
91 content::BrowserThread::GetMessageLoopProxyForThread( | |
92 content::BrowserThread::FILE).get(), | |
93 FROM_HERE, base::Bind(&FileReader::ReadFiles, this, system_logs), | |
94 base::Bind(upload_callback, base::Owned(system_logs))); | |
95 } | |
96 | |
97 void SystemLogDelegate::FileReader::ReadFiles( | |
98 policy::SystemLogUploadJob::SystemLogs* system_logs) { | |
99 // Must be called on the file thread. | |
100 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | |
101 | |
102 for (auto const file_path : kSystemLogFileNames) { | |
103 if (!base::PathExists(base::FilePath(file_path))) | |
104 continue; | |
105 system_logs->push_back(std::make_pair(file_path, std::string())); | |
106 if (!base::ReadFileToString(base::FilePath(file_path), | |
107 &(system_logs->back().second))) { | |
108 LOG(ERROR) << "Failed to read the system log file from the disk " | |
109 << file_path << std::endl; | |
110 } | |
111 } | |
112 } | |
113 | |
114 SystemLogDelegate::SystemLogDelegate() | |
115 : file_reader_(new FileReader()), weak_ptr_factory_(this) { | |
116 } | |
117 | |
118 SystemLogDelegate::~SystemLogDelegate() { | |
119 } | |
120 | |
121 void SystemLogDelegate::LoadSystemLogs( | |
122 const SystemLogDelegate::LogUploadCallback& upload_callback) { | |
123 file_reader_->StartRead(upload_callback, &tracker_); | |
124 } | |
125 | |
126 scoped_ptr<policy::UploadJob> SystemLogDelegate::CreateUploadJob( | |
127 const GURL& upload_url, | |
128 policy::UploadJob::Delegate* delegate) { | |
129 chromeos::DeviceOAuth2TokenService* device_oauth2_token_service = | |
130 chromeos::DeviceOAuth2TokenServiceFactory::Get(); | |
131 | |
132 scoped_refptr<net::URLRequestContextGetter> system_request_context = | |
133 g_browser_process->system_request_context(); | |
134 std::string robot_account_id = | |
135 device_oauth2_token_service->GetRobotAccountId(); | |
136 return scoped_ptr<policy::UploadJob>(new policy::UploadJobImpl( | |
137 upload_url, robot_account_id, device_oauth2_token_service, | |
138 system_request_context, delegate, | |
139 make_scoped_ptr(new policy::UploadJobImpl::RandomMimeBoundaryGenerator))); | |
140 } | |
141 | |
142 } // namespace | |
143 | |
144 namespace policy { | |
145 | |
146 SystemLogUploader::SystemLogUploader( | |
147 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | |
148 : retry_count_(0), | |
149 upload_frequency_( | |
150 base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs)), | |
151 task_runner_(task_runner), | |
152 weak_factory_(this) { | |
153 // Immediately schedule the next system log upload (last_upload_attempt_ is | |
154 // set to the start of the epoch, so this will trigger an update upload in the | |
155 // immediate future). | |
156 ScheduleNextSystemLogUpload(upload_frequency_); | |
157 } | |
158 | |
159 SystemLogUploader::~SystemLogUploader() { | |
160 } | |
161 | |
162 SystemLogUploadJob* SystemLogUploader::CreateSystemLogUploadJob( | |
163 const base::Closure& succeeded_callback, | |
164 const base::Closure& failed_callback) { | |
165 return new SystemLogUploadJob(make_scoped_ptr(new SystemLogDelegate()), | |
166 succeeded_callback, failed_callback); | |
167 } | |
168 | |
169 void SystemLogUploader::OnSuccess() { | |
170 // On successful log upload schedule the next log upload after | |
171 // upload_frequency_ time from now. | |
172 upload_job_.reset(); | |
173 retry_count_ = 0; | |
174 last_upload_attempt_ = base::Time::NowFromSystemTime(); | |
175 | |
176 ScheduleNextSystemLogUpload(upload_frequency_); | |
177 } | |
178 | |
179 void SystemLogUploader::OnFailure() { | |
180 // On first failure log upload try to re-upload logs after kErrorUploadDelayMs | |
181 // time from now. | |
182 upload_job_.reset(); | |
183 last_upload_attempt_ = base::Time::NowFromSystemTime(); | |
184 | |
185 if (retry_count_++ < kMaxNumRetries) { | |
186 ScheduleNextSystemLogUpload( | |
187 base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); | |
188 } else { | |
189 // No more retries. | |
190 retry_count_ = 0; | |
191 ScheduleNextSystemLogUpload(upload_frequency_); | |
192 } | |
193 } | |
194 | |
195 void SystemLogUploader::StartLogUpload() { | |
196 upload_job_.reset(CreateSystemLogUploadJob( | |
197 Bind(&SystemLogUploader::OnSuccess, weak_factory_.GetWeakPtr()), | |
198 Bind(&SystemLogUploader::OnFailure, weak_factory_.GetWeakPtr()))); | |
199 upload_job_->Run(); | |
200 } | |
201 | |
202 void SystemLogUploader::ScheduleNextSystemLogUpload(base::TimeDelta frequency) { | |
203 // Calculate when to fire off the next update. | |
204 base::TimeDelta delay = std::max( | |
205 (last_upload_attempt_ + frequency) - base::Time::NowFromSystemTime(), | |
206 base::TimeDelta()); | |
207 task_runner_->PostDelayedTask(FROM_HERE, | |
208 base::Bind(&SystemLogUploader::StartLogUpload, | |
209 weak_factory_.GetWeakPtr()), | |
210 delay); | |
211 } | |
212 | |
213 } // namespace policy | |
OLD | NEW |