blob: 89f620c21bfd585ff937ca7e1fe8ea53fafe3a5f [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
Jiawei Li830fcfd2019-02-12 20:12:1711#include "base/containers/flat_set.h"
Chad Duffin4aa9c042019-01-11 00:51:5512#include "base/observer_list.h"
Sean Topping0e8ac572019-01-30 03:05:4313#include "base/optional.h"
Chad Duffin4aa9c042019-01-11 00:51:5514#include "chromecast/common/mojom/feature_manager.mojom.h"
Jiawei Li830fcfd2019-02-12 20:12:1715#include "services/service_manager/public/cpp/binder_registry.h"
16#include "services/service_manager/public/cpp/interface_provider.h"
Sean Toppinge91b0972018-10-25 20:59:1217#include "url/gurl.h"
18
19namespace content {
20class WebContents;
21} // namespace content
22
23namespace chromecast {
24
Sean Topping0e8ac572019-01-30 03:05:4325struct RendererFeature {
26 const std::string name;
27 base::Value value;
28};
29
Sean Toppinge91b0972018-10-25 20:59:1230// Simplified WebContents wrapper class for Cast platforms.
31class CastWebContents {
32 public:
33 class Delegate {
34 public:
35 // Advertises page state for the CastWebContents.
36 // Use CastWebContents::page_state() to get the new state.
37 virtual void OnPageStateChanged(CastWebContents* cast_web_contents) = 0;
38
39 // Called when the page has stopped. e.g.: A 404 occurred when loading the
40 // page or if the render process for the main frame crashes. |error_code|
41 // will return a net::Error describing the failure, or net::OK if the page
42 // closed naturally.
43 //
44 // After this method, the page state will be one of the following:
45 // CLOSED: Page was closed as expected and the WebContents exists.
46 // DESTROYED: Page was closed due to deletion of WebContents. The
47 // CastWebContents instance is no longer usable and should be deleted.
48 // ERROR: Page is in an error state. It should be reloaded or deleted.
49 virtual void OnPageStopped(CastWebContents* cast_web_contents,
50 int error_code) = 0;
51
Sean Topping0e8ac572019-01-30 03:05:4352 // Notify that a inner WebContents was created. |inner_contents| is created
53 // in a default-initialized state with no delegate, and can be safely
54 // initialized by the delegate.
55 virtual void InnerContentsCreated(CastWebContents* inner_contents,
56 CastWebContents* outer_contents) {}
Chad Duffin4aa9c042019-01-11 00:51:5557
Sean Toppinge91b0972018-10-25 20:59:1258 protected:
59 virtual ~Delegate() {}
60 };
61
Chad Duffin4aa9c042019-01-11 00:51:5562 class Observer {
63 public:
64 Observer();
65
66 virtual void RenderFrameCreated(int render_process_id,
67 int render_frame_id) {}
Chad Duffin4aa9c042019-01-11 00:51:5568
69 // Adds |this| to the ObserverList in the implementation of
70 // |cast_web_contents|.
71 void Observe(CastWebContents* cast_web_contents);
72
73 // Removes |this| from the ObserverList in the implementation of
74 // |cast_web_contents_|. This is only invoked by CastWebContents and is used
75 // to ensure that once the observed CastWebContents object is destructed the
76 // CastWebContents::Observer does not invoke any additional function calls
77 // on it.
78 void ResetCastWebContents();
79
80 protected:
81 virtual ~Observer();
82
83 CastWebContents* cast_web_contents_;
84 };
85
Sean Topping0e8ac572019-01-30 03:05:4386 struct InitParams {
87 Delegate* delegate;
88 bool enabled_for_dev;
89 };
90
Sean Toppinge91b0972018-10-25 20:59:1291 // Page state for the main frame.
92 enum class PageState {
93 IDLE, // Main frame has not started yet.
94 LOADING, // Main frame is loading resources.
95 LOADED, // Main frame is loaded, but sub-frames may still be loading.
96 CLOSED, // Page is closed and should be cleaned up.
97 DESTROYED, // The WebContents is destroyed and can no longer be used.
98 ERROR, // Main frame is in an error state.
99 };
100
101 CastWebContents() = default;
102 virtual ~CastWebContents() = default;
103
Sean Topping0e8ac572019-01-30 03:05:43104 // TODO(seantopping): Hide this, clients shouldn't use WebContents directly.
Sean Toppinge91b0972018-10-25 20:59:12105 virtual content::WebContents* web_contents() const = 0;
106 virtual PageState page_state() const = 0;
107
Sean Topping0e8ac572019-01-30 03:05:43108 // ===========================================================================
109 // Initialization and Setup
110 // ===========================================================================
111
112 // Set the delegate. SetDelegate(nullptr) can be used to stop notifications.
113 virtual void SetDelegate(Delegate* delegate) = 0;
114
115 // Add a set of features for all renderers in the WebContents. Features are
116 // configured when `CastWebContents::RenderFrameCreated` is invoked.
117 virtual void AddRendererFeatures(std::vector<RendererFeature> features) = 0;
118
119 virtual void AllowWebAndMojoWebUiBindings() = 0;
120 virtual void ClearRenderWidgetHostView() = 0;
121
122 // ===========================================================================
123 // Page Lifetime
124 // ===========================================================================
125
Sean Toppinge91b0972018-10-25 20:59:12126 // Navigates the underlying WebContents to |url|. Delegate will be notified of
127 // page progression events via OnPageStateChanged().
128 virtual void LoadUrl(const GURL& url) = 0;
129
130 // Initiate closure of the page. This invokes the appropriate unload handlers.
131 // Eventually the delegate will be notified with OnPageStopped().
132 virtual void ClosePage() = 0;
133
134 // Stop the page immediately. This will automatically invoke
135 // Delegate::OnPageStopped(error_code), allowing the delegate to delete or
136 // reload the page without waiting for page teardown, which may be handled
137 // independently.
138 virtual void Stop(int error_code) = 0;
139
Chad Duffin4aa9c042019-01-11 00:51:55140 // Used to add or remove |observer| to the ObserverList in the implementation.
141 // These functions should only be invoked by CastWebContents::Observer in a
142 // valid sequence, enforced via SequenceChecker.
143 virtual void AddObserver(Observer* observer) = 0;
144 virtual void RemoveObserver(Observer* observer) = 0;
145
Jiawei Li830fcfd2019-02-12 20:12:17146 // Used to expose CastWebContents's |binder_registry_| to Delegate.
147 // Delegate should register its mojo interface binders via this function
148 // when it is ready.
149 virtual service_manager::BinderRegistry* binder_registry() = 0;
150
151 // Used for owner to pass its |InterfaceProviderPtr|s to CastWebContents.
152 // It is owner's respoinsibility to make sure each |InterfaceProviderPtr| has
153 // distinct mojo interface set.
154 using InterfaceSet = base::flat_set<std::string>;
155 virtual void RegisterInterfaceProvider(
156 const InterfaceSet& interface_set,
157 service_manager::InterfaceProvider* interface_provider) = 0;
158
Sean Toppinge91b0972018-10-25 20:59:12159 private:
160 DISALLOW_COPY_AND_ASSIGN(CastWebContents);
161};
162
163std::ostream& operator<<(std::ostream& os, CastWebContents::PageState state);
164
165} // namespace chromecast
166
167#endif // CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_