OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/run_loop.h" |
5 #include "chrome/browser/safe_browsing/mock_permission_report_sender.h" | 6 #include "chrome/browser/safe_browsing/mock_permission_report_sender.h" |
| 7 #include "content/public/browser/browser_thread.h" |
6 | 8 |
7 namespace safe_browsing { | 9 namespace safe_browsing { |
8 | 10 |
9 MockPermissionReportSender::MockPermissionReportSender() | 11 MockPermissionReportSender::MockPermissionReportSender() |
10 : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) { | 12 : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES), |
11 number_of_reports_ = 0; | 13 number_of_reports_(0) { |
| 14 DCHECK(quit_closure_.is_null()); |
12 } | 15 } |
13 | 16 |
14 MockPermissionReportSender::~MockPermissionReportSender() {} | 17 MockPermissionReportSender::~MockPermissionReportSender() { |
| 18 } |
15 | 19 |
16 void MockPermissionReportSender::Send(const GURL& report_uri, | 20 void MockPermissionReportSender::Send(const GURL& report_uri, |
17 const std::string& report) { | 21 const std::string& report) { |
18 latest_report_uri_ = report_uri; | 22 latest_report_uri_ = report_uri; |
19 latest_report_ = report; | 23 latest_report_ = report; |
20 number_of_reports_++; | 24 number_of_reports_++; |
| 25 |
| 26 // BrowserThreads aren't initialized in the unittest, so don't post tasks |
| 27 // to them. |
| 28 if (!content::BrowserThread::IsThreadInitialized(content::BrowserThread::UI)) |
| 29 return; |
| 30 |
| 31 content::BrowserThread::PostTask( |
| 32 content::BrowserThread::UI, FROM_HERE, |
| 33 base::Bind( |
| 34 &MockPermissionReportSender::NotifyReportSentOnUIThread, |
| 35 base::Unretained(this))); |
| 36 } |
| 37 |
| 38 void MockPermissionReportSender::WaitForReportSent() { |
| 39 base::RunLoop run_loop; |
| 40 quit_closure_ = run_loop.QuitClosure(); |
| 41 run_loop.Run(); |
| 42 } |
| 43 |
| 44 void MockPermissionReportSender::NotifyReportSentOnUIThread() { |
| 45 if (!quit_closure_.is_null()) { |
| 46 quit_closure_.Run(); |
| 47 quit_closure_.Reset(); |
| 48 } |
21 } | 49 } |
22 | 50 |
23 const GURL& MockPermissionReportSender::latest_report_uri() { | 51 const GURL& MockPermissionReportSender::latest_report_uri() { |
24 return latest_report_uri_; | 52 return latest_report_uri_; |
25 } | 53 } |
26 | 54 |
27 const std::string& MockPermissionReportSender::latest_report() { | 55 const std::string& MockPermissionReportSender::latest_report() { |
28 return latest_report_; | 56 return latest_report_; |
29 } | 57 } |
30 | 58 |
31 int MockPermissionReportSender::GetAndResetNumberOfReportsSent() { | 59 int MockPermissionReportSender::GetAndResetNumberOfReportsSent() { |
32 int new_reports = number_of_reports_; | 60 int new_reports = number_of_reports_; |
33 number_of_reports_ = 0; | 61 number_of_reports_ = 0; |
34 return new_reports; | 62 return new_reports; |
35 } | 63 } |
36 | 64 |
37 } // namespace safe_browsing | 65 } // namespace safe_browsing |
OLD | NEW |