Index: extensions/browser/preload_check_group.cc |
diff --git a/extensions/browser/preload_check_group.cc b/extensions/browser/preload_check_group.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d777d521f85a7cb15798374e88cc2221a4a6576c |
--- /dev/null |
+++ b/extensions/browser/preload_check_group.cc |
@@ -0,0 +1,85 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "extensions/browser/preload_check_group.h" |
+ |
+#include "base/callback_helpers.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+namespace extensions { |
+ |
+PreloadCheckGroup::PreloadCheckGroup(scoped_refptr<Extension> extension, |
+ const std::vector<PreloadCheck*>& checks, |
+ bool fail_fast) |
+ : PreloadCheck(extension), |
+ fail_fast_(fail_fast), |
+ running_checks_(0), |
+ checks_(checks), |
+ weak_ptr_factory_(this) {} |
+ |
+PreloadCheckGroup::~PreloadCheckGroup() {} |
+ |
+/* |
+void PreloadCheckGroup::AddCheck(PreloadCheck* check) { |
+ DCHECK(running_checks_ == 0); |
+ checks_.push_back(check); |
+} |
+*/ |
+ |
+void PreloadCheckGroup::Start(ResultCallback callback) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ DCHECK(running_checks_ == 0); |
+ DCHECK(!callback.is_null()); |
+ |
+ callback_ = std::move(callback); |
+ |
+ running_checks_ = checks_.size(); |
+ for (auto* check : checks_) { |
+ check->Start(base::Bind(&PreloadCheckGroup::OnCheckComplete, |
+ weak_ptr_factory_.GetWeakPtr(), check)); |
+ // Synchronous checks may fail immediately. |
+ if (running_checks_ == 0) |
+ return; |
+ } |
+} |
+ |
+/* |
+base::string16 PreloadCheckGroup::GetErrorMessage() const { |
+ DCHECK_NE(error, nullptr); |
+ if (error_.empty()) |
+ return false; |
+ |
+ *error = error_; |
+ return true; |
+} |
+*/ |
+ |
+void PreloadCheckGroup::OnCheckComplete(PreloadCheck* check, Errors errors) { |
+ if (errors.size()) { |
+ errors_.insert(errors.begin(), errors.end()); |
+ /* |
+ if (error_.empty()) |
+ error_ = check->GetErrorMessage(); |
+ */ |
+ } |
+ running_checks_--; |
+ MaybeInvokeCallback(); |
+} |
+ |
+void PreloadCheckGroup::MaybeInvokeCallback() { |
+ if (callback_.is_null()) |
+ return; |
+ |
+ // Invoke callback if all checks are complete or there was at least one |
+ // failure and |fail_fast_| is true. |
+ if (running_checks_ == 0 || (errors_.size() && fail_fast_)) { |
+ // If we are failing fast, discard any pending results. |
+ weak_ptr_factory_.InvalidateWeakPtrs(); |
+ running_checks_ = 0; |
+ |
+ base::ResetAndReturn(&callback_).Run(errors_); |
+ } |
+} |
+ |
+} // namespace extensions |