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 #ifndef CONTENT_TEST_PORTAL_PORTAL_INTERCEPTOR_FOR_TESTING_H_ 6 #define CONTENT_TEST_PORTAL_PORTAL_INTERCEPTOR_FOR_TESTING_H_ 7 8 #include <memory> 9 10 #include "base/callback.h" 11 #include "base/memory/ref_counted.h" 12 #include "base/memory/scoped_refptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/observer_list.h" 15 #include "base/observer_list_types.h" 16 #include "content/browser/portal/portal.h" 17 #include "mojo/public/cpp/bindings/associated_remote.h" 18 #include "mojo/public/cpp/bindings/pending_associated_receiver.h" 19 #include "third_party/blink/public/mojom/portal/portal.mojom-forward.h" 20 #include "third_party/blink/public/mojom/portal/portal.mojom-test-utils.h" 21 22 namespace content { 23 24 class RenderFrameHostImpl; 25 26 // The PortalInterceptorForTesting can be used in tests to inspect Portal IPCs. 27 // 28 // When in use, it is owned by the target Portal and replaces it in the 29 // associated binding. It will be destructed when the Portal is; a weak pointer 30 // should be used to determine whether this has happened. 31 class PortalInterceptorForTesting final 32 : public blink::mojom::PortalInterceptorForTesting { 33 public: 34 class Observer : public base::CheckedObserver { 35 public: OnPortalActivate()36 virtual void OnPortalActivate() {} OnPortalActivateResult(blink::mojom::PortalActivateResult result)37 virtual void OnPortalActivateResult( 38 blink::mojom::PortalActivateResult result) {} 39 }; 40 41 static PortalInterceptorForTesting* Create( 42 RenderFrameHostImpl* render_frame_host_impl, 43 mojo::PendingAssociatedReceiver<blink::mojom::Portal> receiver, 44 mojo::PendingAssociatedRemote<blink::mojom::PortalClient> client); 45 static PortalInterceptorForTesting* Create( 46 RenderFrameHostImpl* render_frame_host_impl, 47 content::Portal* portal); 48 static PortalInterceptorForTesting* From(content::Portal* portal); 49 50 ~PortalInterceptorForTesting() override; 51 52 // blink::mojom::PortalInterceptorForTesting 53 blink::mojom::Portal* GetForwardingInterface() override; 54 void Activate(blink::TransferableMessage data, 55 base::TimeTicks activation_time, 56 uint64_t trace_id, 57 ActivateCallback callback) override; 58 void Navigate(const GURL& url, 59 blink::mojom::ReferrerPtr referrer, 60 blink::mojom::Portal::NavigateCallback callback) override; 61 62 // If set, will be used to replace the implementation of Navigate. 63 using NavigateCallback = 64 base::RepeatingCallback<void(const GURL&, 65 blink::mojom::ReferrerPtr, 66 blink::mojom::Portal::NavigateCallback)>; SetNavigateCallback(NavigateCallback callback)67 void SetNavigateCallback(NavigateCallback callback) { 68 navigate_callback_ = std::move(callback); 69 } 70 71 // Test getters. GetPortal()72 content::Portal* GetPortal() { return portal_; } GetPortalContents()73 WebContentsImpl* GetPortalContents() { return portal_->GetPortalContents(); } 74 75 // Useful in observing the intercepted activity. GetWeakPtr()76 base::WeakPtr<PortalInterceptorForTesting> GetWeakPtr() { 77 return weak_ptr_factory_.GetWeakPtr(); 78 } AddObserver(Observer * observer)79 void AddObserver(Observer* observer) { 80 observers_->data.AddObserver(observer); 81 } RemoveObserver(Observer * observer)82 void RemoveObserver(Observer* observer) { 83 observers_->data.RemoveObserver(observer); 84 } 85 86 private: 87 PortalInterceptorForTesting(RenderFrameHostImpl* render_frame_host_impl, 88 content::Portal* portal); 89 90 const scoped_refptr<base::RefCountedData<base::ObserverList<Observer>>> 91 observers_; 92 content::Portal* portal_; // Owns this. 93 NavigateCallback navigate_callback_; 94 base::WeakPtrFactory<PortalInterceptorForTesting> weak_ptr_factory_{this}; 95 }; 96 97 } // namespace content 98 99 #endif // CONTENT_TEST_PORTAL_PORTAL_INTERCEPTOR_FOR_TESTING_H_ 100