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

Side by Side Diff: handler/crash_report_upload_thread.cc

Issue 1526563003: Create WorkerThread, an abstraction to perform some work on an interval. (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Address coments Created 4 years, 12 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
« no previous file with comments | « handler/crash_report_upload_thread.h ('k') | util/thread/worker_thread.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Crashpad Authors. All rights reserved. 1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
(...skipping 15 matching lines...) Expand all
26 #include "build/build_config.h" 26 #include "build/build_config.h"
27 #include "client/settings.h" 27 #include "client/settings.h"
28 #include "snapshot/minidump/process_snapshot_minidump.h" 28 #include "snapshot/minidump/process_snapshot_minidump.h"
29 #include "snapshot/module_snapshot.h" 29 #include "snapshot/module_snapshot.h"
30 #include "util/file/file_reader.h" 30 #include "util/file/file_reader.h"
31 #include "util/misc/uuid.h" 31 #include "util/misc/uuid.h"
32 #include "util/net/http_body.h" 32 #include "util/net/http_body.h"
33 #include "util/net/http_multipart_builder.h" 33 #include "util/net/http_multipart_builder.h"
34 #include "util/net/http_transport.h" 34 #include "util/net/http_transport.h"
35 #include "util/stdlib/map_insert.h" 35 #include "util/stdlib/map_insert.h"
36 #include "util/thread/thread.h"
37 36
38 namespace crashpad { 37 namespace crashpad {
39 38
40 namespace { 39 namespace {
41 40
42 void InsertOrReplaceMapEntry(std::map<std::string, std::string>* map, 41 void InsertOrReplaceMapEntry(std::map<std::string, std::string>* map,
43 const std::string& key, 42 const std::string& key,
44 const std::string& value) { 43 const std::string& value) {
45 std::string old_value; 44 std::string old_value;
46 if (!MapInsertOrReplace(map, key, value, &old_value)) { 45 if (!MapInsertOrReplace(map, key, value, &old_value)) {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 129
131 private: 130 private:
132 CrashReportDatabase* database_; // weak 131 CrashReportDatabase* database_; // weak
133 const CrashReportDatabase::Report* report_; // weak 132 const CrashReportDatabase::Report* report_; // weak
134 133
135 DISALLOW_COPY_AND_ASSIGN(CallRecordUploadAttempt); 134 DISALLOW_COPY_AND_ASSIGN(CallRecordUploadAttempt);
136 }; 135 };
137 136
138 } // namespace 137 } // namespace
139 138
140 namespace internal {
141
142 class CrashReportUploadHelperThread final : public Thread {
143 public:
144 explicit CrashReportUploadHelperThread(CrashReportUploadThread* self)
145 : self_(self) {}
146 ~CrashReportUploadHelperThread() override {}
147
148 void ThreadMain() override {
149 self_->ThreadMain();
150 }
151
152 private:
153 CrashReportUploadThread* self_;
154
155 DISALLOW_COPY_AND_ASSIGN(CrashReportUploadHelperThread);
156 };
157
158 } // namespace internal
159
160 CrashReportUploadThread::CrashReportUploadThread(CrashReportDatabase* database, 139 CrashReportUploadThread::CrashReportUploadThread(CrashReportDatabase* database,
161 const std::string& url) 140 const std::string& url)
162 : url_(url), 141 : url_(url),
163 database_(database), 142 database_(database),
164 semaphore_(0), 143 // Check for pending reports every 15 minutes, even in the absence of a
165 thread_(), 144 // signal from the handler thread. This allows for failed uploads to be
166 running_(false) { 145 // retried periodically, and for pending reports written by other
146 // processes to be recognized.
147 thread_(15 * 60, this) {
167 } 148 }
168 149
169 CrashReportUploadThread::~CrashReportUploadThread() { 150 CrashReportUploadThread::~CrashReportUploadThread() {
170 DCHECK(!running_);
171 DCHECK(!thread_);
172 } 151 }
173 152
174 void CrashReportUploadThread::Start() { 153 void CrashReportUploadThread::Start() {
175 DCHECK(!running_); 154 thread_.Start(0);
176 DCHECK(!thread_);
177
178 running_ = true;
179 thread_.reset(new internal::CrashReportUploadHelperThread(this));
180 thread_->Start();
181 } 155 }
182 156
183 void CrashReportUploadThread::Stop() { 157 void CrashReportUploadThread::Stop() {
184 DCHECK(running_); 158 thread_.Stop();
185 DCHECK(thread_);
186
187 if (!running_) {
188 return;
189 }
190
191 running_ = false;
192 semaphore_.Signal();
193
194 thread_->Join();
195 thread_.reset();
196 } 159 }
197 160
198 void CrashReportUploadThread::ReportPending() { 161 void CrashReportUploadThread::ReportPending() {
199 semaphore_.Signal(); 162 thread_.DoWorkNow();
200 }
201
202 void CrashReportUploadThread::ThreadMain() {
203 while (running_) {
204 ProcessPendingReports();
205
206 // Check for pending reports every 15 minutes, even in the absence of a
207 // signal from the handler thread. This allows for failed uploads to be
208 // retried periodically, and for pending reports written by other processes
209 // to be recognized.
210 semaphore_.TimedWait(15 * 60);
211 }
212 } 163 }
213 164
214 void CrashReportUploadThread::ProcessPendingReports() { 165 void CrashReportUploadThread::ProcessPendingReports() {
215 std::vector<CrashReportDatabase::Report> reports; 166 std::vector<CrashReportDatabase::Report> reports;
216 if (database_->GetPendingReports(&reports) != CrashReportDatabase::kNoError) { 167 if (database_->GetPendingReports(&reports) != CrashReportDatabase::kNoError) {
217 // The database is sick. It might be prudent to stop trying to poke it from 168 // The database is sick. It might be prudent to stop trying to poke it from
218 // this thread by abandoning the thread altogether. On the other hand, if 169 // this thread by abandoning the thread altogether. On the other hand, if
219 // the problem is transient, it might be possible to talk to it again on the 170 // the problem is transient, it might be possible to talk to it again on the
220 // next pass. For now, take the latter approach. 171 // next pass. For now, take the latter approach.
221 return; 172 return;
222 } 173 }
223 174
224 for (const CrashReportDatabase::Report& report : reports) { 175 for (const CrashReportDatabase::Report& report : reports) {
225 ProcessPendingReport(report); 176 ProcessPendingReport(report);
226 177
227 // Respect Stop() being called after at least one attempt to process a 178 // Respect Stop() being called after at least one attempt to process a
228 // report. 179 // report.
229 if (!running_) { 180 if (!thread_.is_running()) {
230 return; 181 return;
231 } 182 }
232 } 183 }
233 } 184 }
234 185
235 void CrashReportUploadThread::ProcessPendingReport( 186 void CrashReportUploadThread::ProcessPendingReport(
236 const CrashReportDatabase::Report& report) { 187 const CrashReportDatabase::Report& report) {
237 Settings* const settings = database_->GetSettings(); 188 Settings* const settings = database_->GetSettings();
238 189
239 bool uploads_enabled; 190 bool uploads_enabled;
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 // TODO(mark): The timeout should be configurable by the client. 321 // TODO(mark): The timeout should be configurable by the client.
371 http_transport->SetTimeout(60.0); // 1 minute. 322 http_transport->SetTimeout(60.0); // 1 minute.
372 323
373 if (!http_transport->ExecuteSynchronously(response_body)) { 324 if (!http_transport->ExecuteSynchronously(response_body)) {
374 return UploadResult::kRetry; 325 return UploadResult::kRetry;
375 } 326 }
376 327
377 return UploadResult::kSuccess; 328 return UploadResult::kSuccess;
378 } 329 }
379 330
331 void CrashReportUploadThread::DoWork(const WorkerThread* thread) {
332 ProcessPendingReports();
333 }
334
380 } // namespace crashpad 335 } // namespace crashpad
OLDNEW
« no previous file with comments | « handler/crash_report_upload_thread.h ('k') | util/thread/worker_thread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698