Avi Drissman | 60039d4 | 2022-09-13 21:49:05 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Devlin Cronin | 53bc9d3 | 2022-05-07 00:23:47 | [diff] [blame] | 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_SCRIPT_RESULT_QUEUE_H_ |
| 6 | #define EXTENSIONS_BROWSER_SCRIPT_RESULT_QUEUE_H_ |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/callback.h" |
| 11 | #include "base/scoped_observation.h" |
| 12 | #include "base/values.h" |
| 13 | #include "extensions/browser/api/test/test_api_observer.h" |
| 14 | #include "extensions/browser/api/test/test_api_observer_registry.h" |
| 15 | |
| 16 | namespace extensions { |
| 17 | |
| 18 | // Intercepts results sent via chrome.test.sendScriptResult(). |
| 19 | // TODO(devlin): Add details of this class and sendScriptResult() to |
| 20 | // //extensions/docs/extension_tests.md. |
| 21 | class ScriptResultQueue : public TestApiObserver { |
| 22 | public: |
| 23 | ScriptResultQueue(); |
| 24 | ~ScriptResultQueue() override; |
| 25 | |
| 26 | // TestApiObserver: |
| 27 | void OnScriptResult(const base::Value& script_result) override; |
| 28 | |
| 29 | // Returns the next result, optionally waiting for it to come in. |
| 30 | base::Value GetNextResult(); |
| 31 | |
| 32 | const std::vector<base::Value>& results() const { return results_; } |
| 33 | |
| 34 | private: |
| 35 | // The index of the next result to return. |
| 36 | size_t next_result_index_ = 0u; |
| 37 | |
| 38 | // The collection of all script results this queue has seen. |
| 39 | std::vector<base::Value> results_; |
| 40 | |
| 41 | // Quit closure to call when waiting for a result. |
| 42 | base::OnceClosure quit_closure_; |
| 43 | |
| 44 | base::ScopedObservation<TestApiObserverRegistry, TestApiObserver> |
| 45 | test_api_observation_{this}; |
| 46 | }; |
| 47 | |
| 48 | } // namespace extensions |
| 49 | |
| 50 | #endif // EXTENSIONS_BROWSER_SCRIPT_RESULT_QUEUE_H_ |