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 CHROME_BROWSER_UI_MEDIA_ROUTER_PRESENTATION_RECEIVER_WINDOW_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_MEDIA_ROUTER_PRESENTATION_RECEIVER_WINDOW_CONTROLLER_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "chrome/browser/media/router/providers/wired_display/wired_display_presentation_receiver.h"
14 #include "chrome/browser/profiles/profile_observer.h"
15 #include "chrome/browser/ui/media_router/presentation_receiver_window_delegate.h"
16 #include "components/media_router/browser/presentation/presentation_navigation_policy.h"
17 #include "content/public/browser/web_contents_delegate.h"
18 #include "content/public/browser/web_contents_observer.h"
19 
20 class GURL;
21 class PresentationReceiverWindow;
22 class Profile;
23 
24 namespace content {
25 class WebContents;
26 }
27 
28 namespace gfx {
29 class Rect;
30 }
31 
32 // This class controls a window which is used to render a receiver page for the
33 // Presentation API.  It handles creating the window to show the desired URL and
34 // showing it in fullscreen on the desired display.  This class should not be
35 // destroyed until |termination_callback| has been called, which may be called
36 // before Terminate() is ever called.
37 class PresentationReceiverWindowController final
38     : public PresentationReceiverWindowDelegate,
39       public content::WebContentsObserver,
40       public content::WebContentsDelegate,
41       public media_router::WiredDisplayPresentationReceiver,
42       public ProfileObserver {
43  public:
44   using TitleChangeCallback = base::RepeatingCallback<void(const std::string&)>;
45 
46   static std::unique_ptr<PresentationReceiverWindowController>
47   CreateFromOriginalProfile(Profile* profile,
48                             const gfx::Rect& bounds,
49                             base::OnceClosure termination_callback,
50                             TitleChangeCallback title_change_callback);
51 
52   ~PresentationReceiverWindowController() final;
53 
54   // WiredDisplayPresentationReceiver overrides.
55   void Start(const std::string& presentation_id,
56              const GURL& start_url) override;
57   void Terminate() override;
58   void ExitFullscreen() override;
59 
60   // PresentationReceiverWindowDelegate overrides.
61   content::WebContents* web_contents() const final;
62 
63  private:
64   friend class PresentationReceiverWindowControllerBrowserTest;
65 
66   PresentationReceiverWindowController(
67       Profile* profile,
68       const gfx::Rect& bounds,
69       base::OnceClosure termination_callback,
70       TitleChangeCallback title_change_callback);
71 
72   // ProfileObserver:
73   void OnProfileWillBeDestroyed(Profile* profile) override;
74 
75   // These methods are intended to be used by tests.
76   void CloseWindowForTest();
77   bool IsWindowActiveForTest() const;
78   bool IsWindowFullscreenForTest() const;
79   gfx::Rect GetWindowBoundsForTest() const;
80 
81   // PresentationReceiverWindowDelegate overrides.
82   void WindowClosed() final;
83 
84   // content::WebContentsObserver overrides.
85   void DidStartNavigation(content::NavigationHandle* handle) final;
86   void TitleWasSet(content::NavigationEntry* entry) final;
87 
88   // content::WebContentsDelegate overrides.
89   void NavigationStateChanged(content::WebContents* source,
90                               content::InvalidateTypes changed_flags) final;
91   void CloseContents(content::WebContents* source) final;
92   bool ShouldSuppressDialogs(content::WebContents* source) final;
93   bool ShouldFocusLocationBarByDefault(content::WebContents* source) final;
94   bool ShouldFocusPageAfterCrash() final;
95   void CanDownload(const GURL& url,
96                    const std::string& request_method,
97                    base::OnceCallback<void(bool)> callback) final;
98   bool IsWebContentsCreationOverridden(
99       content::SiteInstance* source_site_instance,
100       content::mojom::WindowContainerType window_container_type,
101       const GURL& opener_url,
102       const std::string& frame_name,
103       const GURL& target_url) override;
104 
105   // The profile used for the presentation.
106   Profile* otr_profile_;
107 
108   // WebContents for rendering the receiver page.
109   std::unique_ptr<content::WebContents> web_contents_;
110 
111   // The actual UI window for displaying the receiver page.
112   PresentationReceiverWindow* window_;
113 
114   base::OnceClosure termination_callback_;
115 
116   // Gets called with the new title whenever TitleWasSet() is called.
117   TitleChangeCallback title_change_callback_;
118 
119   media_router::PresentationNavigationPolicy navigation_policy_;
120 
121   DISALLOW_COPY_AND_ASSIGN(PresentationReceiverWindowController);
122 };
123 
124 #endif  // CHROME_BROWSER_UI_MEDIA_ROUTER_PRESENTATION_RECEIVER_WINDOW_CONTROLLER_H_
125