1 // Copyright 2019 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 "content/test/portal/portal_activated_observer.h" 6 7 #include "base/auto_reset.h" 8 #include "base/run_loop.h" 9 #include "content/browser/web_contents/web_contents_impl.h" 10 11 namespace content { 12 PortalActivatedObserver(Portal * portal)13PortalActivatedObserver::PortalActivatedObserver(Portal* portal) 14 : interceptor_(PortalInterceptorForTesting::From(portal)->GetWeakPtr()) { 15 interceptor_->AddObserver(this); 16 } 17 ~PortalActivatedObserver()18PortalActivatedObserver::~PortalActivatedObserver() { 19 if (auto* interceptor = interceptor_.get()) 20 interceptor->RemoveObserver(this); 21 } 22 WaitForActivate()23void PortalActivatedObserver::WaitForActivate() { 24 if (has_activated_) 25 return; 26 27 base::RunLoop run_loop; 28 base::AutoReset<base::RunLoop*> auto_reset(&run_loop_, &run_loop); 29 run_loop.Run(); 30 31 DCHECK(has_activated_); 32 } 33 34 blink::mojom::PortalActivateResult WaitForActivateResult()35PortalActivatedObserver::WaitForActivateResult() { 36 WaitForActivate(); 37 if (result_) 38 return *result_; 39 40 base::RunLoop run_loop; 41 base::AutoReset<base::RunLoop*> auto_reset(&run_loop_, &run_loop); 42 run_loop.Run(); 43 44 DCHECK(result_); 45 return *result_; 46 } 47 OnPortalActivate()48void PortalActivatedObserver::OnPortalActivate() { 49 DCHECK(!has_activated_) 50 << "PortalActivatedObserver can't handle overlapping activations."; 51 has_activated_ = true; 52 53 if (run_loop_) 54 run_loop_->Quit(); 55 } 56 OnPortalActivateResult(blink::mojom::PortalActivateResult result)57void PortalActivatedObserver::OnPortalActivateResult( 58 blink::mojom::PortalActivateResult result) { 59 DCHECK(has_activated_) << "PortalActivatedObserver should observe the whole " 60 "activation; this may be a race."; 61 DCHECK(!result_) 62 << "PortalActivatedObserver can't handle overlapping activations."; 63 result_ = result; 64 65 if (run_loop_) 66 run_loop_->Quit(); 67 68 if (auto* interceptor = interceptor_.get()) 69 interceptor->RemoveObserver(this); 70 } 71 72 } // namespace content 73