michaelpg | 6a4874f | 2017-04-13 20:41:33 | [diff] [blame] | 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 | #ifndef EXTENSIONS_BROWSER_PRELOAD_CHECK_GROUP_H_ |
| 6 | #define EXTENSIONS_BROWSER_PRELOAD_CHECK_GROUP_H_ |
| 7 | |
| 8 | #include <vector> |
| 9 | |
michaelpg | 6a4874f | 2017-04-13 20:41:33 | [diff] [blame] | 10 | #include "base/memory/weak_ptr.h" |
| 11 | #include "base/threading/thread_checker.h" |
| 12 | #include "extensions/browser/preload_check.h" |
| 13 | |
| 14 | namespace extensions { |
| 15 | |
| 16 | // PreloadCheckGroup runs a collection of other PreloadChecks and reports their |
| 17 | // collective status once they have all finished. To stop the remaining checks |
| 18 | // upon hitting the first error, use set_stop_on_first_error(). |
| 19 | class PreloadCheckGroup : public PreloadCheck { |
| 20 | public: |
| 21 | PreloadCheckGroup(); |
Peter Boström | 951cf77e | 2021-09-22 00:02:59 | [diff] [blame] | 22 | |
| 23 | PreloadCheckGroup(const PreloadCheckGroup&) = delete; |
| 24 | PreloadCheckGroup& operator=(const PreloadCheckGroup&) = delete; |
| 25 | |
michaelpg | 6a4874f | 2017-04-13 20:41:33 | [diff] [blame] | 26 | ~PreloadCheckGroup() override; |
| 27 | |
| 28 | // Adds a check to run. Not owned. Must be called before Start(). |
| 29 | void AddCheck(PreloadCheck* check); |
| 30 | |
| 31 | // PreloadCheck: |
| 32 | void Start(ResultCallback callback) override; |
| 33 | |
| 34 | void set_stop_on_first_error(bool value) { stop_on_first_error_ = value; } |
| 35 | |
| 36 | private: |
| 37 | // Saves any errors and may invoke the callback. |
Istiaque Ahmed | 400c83a | 2017-10-11 02:39:35 | [diff] [blame] | 38 | virtual void OnCheckComplete(const Errors& errors); |
michaelpg | 6a4874f | 2017-04-13 20:41:33 | [diff] [blame] | 39 | |
| 40 | // Invokes the callback if the checks are considered finished. |
| 41 | void MaybeInvokeCallback(); |
| 42 | |
| 43 | base::ThreadChecker thread_checker_; |
| 44 | |
| 45 | // If true, the callback is invoked early when the first check fails, |
| 46 | // stopping the remaining checks. |
| 47 | bool stop_on_first_error_ = false; |
| 48 | |
| 49 | // Checks to run. Not owned. |
| 50 | std::vector<PreloadCheck*> checks_; |
| 51 | |
| 52 | ResultCallback callback_; |
| 53 | int running_checks_ = 0; |
| 54 | Errors errors_; |
| 55 | |
Jeremy Roman | 9fc2de6 | 2019-07-12 14:15:03 | [diff] [blame] | 56 | base::WeakPtrFactory<PreloadCheckGroup> weak_ptr_factory_{this}; |
michaelpg | 6a4874f | 2017-04-13 20:41:33 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | } // namespace extensions |
| 60 | |
| 61 | #endif // EXTENSIONS_BROWSER_PRELOAD_CHECK_GROUP_H_ |