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 | #include "extensions/browser/script_result_queue.h" |
| 6 | |
| 7 | #include "base/run_loop.h" |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | |
| 10 | namespace extensions { |
| 11 | |
| 12 | ScriptResultQueue::ScriptResultQueue() { |
| 13 | test_api_observation_.Observe( |
| 14 | extensions::TestApiObserverRegistry::GetInstance()); |
| 15 | } |
| 16 | ScriptResultQueue::~ScriptResultQueue() = default; |
| 17 | |
| 18 | void ScriptResultQueue::OnScriptResult(const base::Value& script_result) { |
| 19 | results_.push_back(script_result.Clone()); |
| 20 | if (quit_closure_) |
| 21 | std::move(quit_closure_).Run(); |
| 22 | } |
| 23 | |
| 24 | base::Value ScriptResultQueue::GetNextResult() { |
| 25 | if (next_result_index_ >= results_.size()) { |
| 26 | base::RunLoop run_loop; |
| 27 | quit_closure_ = run_loop.QuitClosure(); |
| 28 | run_loop.Run(); |
| 29 | } |
| 30 | if (next_result_index_ >= results_.size()) { |
| 31 | // This can happen if the run loop times out. Handle it gracefully to avoid |
| 32 | // crashing the test runner. |
| 33 | ADD_FAILURE() << "Could not get next result at index " |
| 34 | << next_result_index_; |
| 35 | return base::Value(); |
| 36 | } |
| 37 | |
| 38 | return results_[next_result_index_++].Clone(); |
| 39 | } |
| 40 | |
| 41 | } // namespace extensions |