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/callback_helpers.h" |
| 8 #include "content/public/browser/browser_thread.h" |
| 9 |
| 10 namespace extensions { |
| 11 |
| 12 PreloadCheckGroup::PreloadCheckGroup(scoped_refptr<Extension> extension, |
| 13 const std::vector<PreloadCheck*>& checks, |
| 14 bool fail_fast) |
| 15 : PreloadCheck(extension), |
| 16 fail_fast_(fail_fast), |
| 17 running_checks_(0), |
| 18 checks_(checks), |
| 19 weak_ptr_factory_(this) {} |
| 20 |
| 21 PreloadCheckGroup::~PreloadCheckGroup() {} |
| 22 |
| 23 /* |
| 24 void PreloadCheckGroup::AddCheck(PreloadCheck* check) { |
| 25 DCHECK(running_checks_ == 0); |
| 26 checks_.push_back(check); |
| 27 } |
| 28 */ |
| 29 |
| 30 void PreloadCheckGroup::Start(ResultCallback callback) { |
| 31 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 32 DCHECK(running_checks_ == 0); |
| 33 DCHECK(!callback.is_null()); |
| 34 |
| 35 callback_ = std::move(callback); |
| 36 |
| 37 running_checks_ = checks_.size(); |
| 38 for (auto* check : checks_) { |
| 39 check->Start(base::Bind(&PreloadCheckGroup::OnCheckComplete, |
| 40 weak_ptr_factory_.GetWeakPtr(), check)); |
| 41 // Synchronous checks may fail immediately. |
| 42 if (running_checks_ == 0) |
| 43 return; |
| 44 } |
| 45 } |
| 46 |
| 47 /* |
| 48 base::string16 PreloadCheckGroup::GetErrorMessage() const { |
| 49 DCHECK_NE(error, nullptr); |
| 50 if (error_.empty()) |
| 51 return false; |
| 52 |
| 53 *error = error_; |
| 54 return true; |
| 55 } |
| 56 */ |
| 57 |
| 58 void PreloadCheckGroup::OnCheckComplete(PreloadCheck* check, Errors errors) { |
| 59 if (errors.size()) { |
| 60 errors_.insert(errors.begin(), errors.end()); |
| 61 /* |
| 62 if (error_.empty()) |
| 63 error_ = check->GetErrorMessage(); |
| 64 */ |
| 65 } |
| 66 running_checks_--; |
| 67 MaybeInvokeCallback(); |
| 68 } |
| 69 |
| 70 void PreloadCheckGroup::MaybeInvokeCallback() { |
| 71 if (callback_.is_null()) |
| 72 return; |
| 73 |
| 74 // Invoke callback if all checks are complete or there was at least one |
| 75 // failure and |fail_fast_| is true. |
| 76 if (running_checks_ == 0 || (errors_.size() && fail_fast_)) { |
| 77 // If we are failing fast, discard any pending results. |
| 78 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 79 running_checks_ = 0; |
| 80 |
| 81 base::ResetAndReturn(&callback_).Run(errors_); |
| 82 } |
| 83 } |
| 84 |
| 85 } // namespace extensions |
OLD | NEW |