OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "extensions/browser/preload_check_group.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback_helpers.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "extensions/common/extension.h" | |
11 | |
12 namespace extensions { | |
13 | |
14 PreloadCheckGroup::PreloadCheckGroup() | |
15 : PreloadCheck(nullptr), weak_ptr_factory_(this) {} | |
16 | |
17 PreloadCheckGroup::~PreloadCheckGroup() {} | |
18 | |
19 void PreloadCheckGroup::AddCheck(PreloadCheck* check) { | |
20 DCHECK(running_checks_ == 0); | |
21 checks_.push_back(check); | |
22 } | |
23 | |
24 void PreloadCheckGroup::Start(ResultCallback callback) { | |
25 DCHECK(thread_checker_.CalledOnValidThread()); | |
26 DCHECK(running_checks_ == 0); | |
Devlin
2017/04/04 01:37:26
nit: DCHECK_EQ(0, running_checks_)
michaelpg
2017/04/04 21:41:29
Done.
| |
27 | |
28 callback_ = std::move(callback); | |
29 running_checks_ = checks_.size(); | |
30 for (auto* check : checks_) { | |
31 check->Start(base::Bind(&PreloadCheckGroup::OnCheckComplete, | |
32 weak_ptr_factory_.GetWeakPtr())); | |
33 // Synchronous checks may fail immediately. | |
34 if (running_checks_ == 0) | |
35 return; | |
36 } | |
37 } | |
38 | |
39 void PreloadCheckGroup::OnCheckComplete(Errors errors) { | |
40 DCHECK(thread_checker_.CalledOnValidThread()); | |
41 errors_.insert(errors.begin(), errors.end()); | |
42 running_checks_--; | |
43 MaybeInvokeCallback(); | |
44 } | |
45 | |
46 void PreloadCheckGroup::MaybeInvokeCallback() { | |
47 // Only invoke callback if all checks are complete, or if there was at least | |
48 // one failure and |stop_on_first_error_| is true. | |
49 if (running_checks_ > 0 && (errors_.empty() || !stop_on_first_error_)) | |
50 return; | |
51 | |
52 // If we are failing fast, discard any pending results. | |
53 weak_ptr_factory_.InvalidateWeakPtrs(); | |
54 running_checks_ = 0; | |
55 | |
56 DCHECK(!callback_.is_null()); | |
57 base::ResetAndReturn(&callback_).Run(errors_); | |
58 } | |
59 | |
60 } // namespace extensions | |
OLD | NEW |