blob: db1c817b4682a1c029d49f1d206f5eb98b770a15 [file] [log] [blame]
Sean Toppinge91b0972018-10-25 20:59:121// Copyright 2018 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 CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_
6#define CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_
7
Chad Duffin4aa9c042019-01-11 00:51:558#include <string>
9#include <vector>
10
11#include "base/observer_list.h"
12#include "chromecast/common/mojom/feature_manager.mojom.h"
Sean Toppinge91b0972018-10-25 20:59:1213#include "url/gurl.h"
14
15namespace content {
16class WebContents;
17} // namespace content
18
19namespace chromecast {
20
21// Simplified WebContents wrapper class for Cast platforms.
22class CastWebContents {
23 public:
24 class Delegate {
25 public:
26 // Advertises page state for the CastWebContents.
27 // Use CastWebContents::page_state() to get the new state.
28 virtual void OnPageStateChanged(CastWebContents* cast_web_contents) = 0;
29
30 // Called when the page has stopped. e.g.: A 404 occurred when loading the
31 // page or if the render process for the main frame crashes. |error_code|
32 // will return a net::Error describing the failure, or net::OK if the page
33 // closed naturally.
34 //
35 // After this method, the page state will be one of the following:
36 // CLOSED: Page was closed as expected and the WebContents exists.
37 // DESTROYED: Page was closed due to deletion of WebContents. The
38 // CastWebContents instance is no longer usable and should be deleted.
39 // ERROR: Page is in an error state. It should be reloaded or deleted.
40 virtual void OnPageStopped(CastWebContents* cast_web_contents,
41 int error_code) = 0;
42
Chad Duffin4aa9c042019-01-11 00:51:5543 // Collects the set of delegate-specific renderer features that needs to be
44 // configured when `CastWebContents::RenderFrameCreated` is invoked.
45 virtual std::vector<chromecast::shell::mojom::FeaturePtr>
46 GetRendererFeatures();
47
Sean Toppinge91b0972018-10-25 20:59:1248 protected:
49 virtual ~Delegate() {}
50 };
51
Chad Duffin4aa9c042019-01-11 00:51:5552 class Observer {
53 public:
54 Observer();
55
56 virtual void RenderFrameCreated(int render_process_id,
57 int render_frame_id) {}
58 virtual void OnInterfaceRequestFromFrame(
59 const std::string& interface_name,
60 mojo::ScopedMessagePipeHandle* interface_pipe) {}
61
62 // Adds |this| to the ObserverList in the implementation of
63 // |cast_web_contents|.
64 void Observe(CastWebContents* cast_web_contents);
65
66 // Removes |this| from the ObserverList in the implementation of
67 // |cast_web_contents_|. This is only invoked by CastWebContents and is used
68 // to ensure that once the observed CastWebContents object is destructed the
69 // CastWebContents::Observer does not invoke any additional function calls
70 // on it.
71 void ResetCastWebContents();
72
73 protected:
74 virtual ~Observer();
75
76 CastWebContents* cast_web_contents_;
77 };
78
Sean Toppinge91b0972018-10-25 20:59:1279 // Page state for the main frame.
80 enum class PageState {
81 IDLE, // Main frame has not started yet.
82 LOADING, // Main frame is loading resources.
83 LOADED, // Main frame is loaded, but sub-frames may still be loading.
84 CLOSED, // Page is closed and should be cleaned up.
85 DESTROYED, // The WebContents is destroyed and can no longer be used.
86 ERROR, // Main frame is in an error state.
87 };
88
89 CastWebContents() = default;
90 virtual ~CastWebContents() = default;
91
92 virtual content::WebContents* web_contents() const = 0;
93 virtual PageState page_state() const = 0;
94
95 // Navigates the underlying WebContents to |url|. Delegate will be notified of
96 // page progression events via OnPageStateChanged().
97 virtual void LoadUrl(const GURL& url) = 0;
98
99 // Initiate closure of the page. This invokes the appropriate unload handlers.
100 // Eventually the delegate will be notified with OnPageStopped().
101 virtual void ClosePage() = 0;
102
103 // Stop the page immediately. This will automatically invoke
104 // Delegate::OnPageStopped(error_code), allowing the delegate to delete or
105 // reload the page without waiting for page teardown, which may be handled
106 // independently.
107 virtual void Stop(int error_code) = 0;
108
109 // Set the delegate. SetDelegate(nullptr) can be used to stop notifications.
110 virtual void SetDelegate(Delegate* delegate) = 0;
111
Chad Duffin4aa9c042019-01-11 00:51:55112 virtual void AllowWebAndMojoWebUiBindings() = 0;
113 virtual void ClearRenderWidgetHostView() = 0;
114
115 // Used to add or remove |observer| to the ObserverList in the implementation.
116 // These functions should only be invoked by CastWebContents::Observer in a
117 // valid sequence, enforced via SequenceChecker.
118 virtual void AddObserver(Observer* observer) = 0;
119 virtual void RemoveObserver(Observer* observer) = 0;
120
Sean Toppinge91b0972018-10-25 20:59:12121 private:
122 DISALLOW_COPY_AND_ASSIGN(CastWebContents);
123};
124
125std::ostream& operator<<(std::ostream& os, CastWebContents::PageState state);
126
127} // namespace chromecast
128
129#endif // CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_