blob: ade7c6a58261c753e7e54a9d01aac6d2feba9c4f [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
Sean Topping077ba6d82021-11-02 22:48:4211#include "base/callback_forward.h"
Jiawei Li830fcfd2019-02-12 20:12:1712#include "base/containers/flat_set.h"
Chad Duffin4aa9c042019-01-11 00:51:5513#include "base/observer_list.h"
Sean Topping6c9e3932021-08-23 21:25:3314#include "base/observer_list_types.h"
Zhaoxin2fe094a2020-02-11 23:35:4515#include "base/process/process.h"
Jiawei Li3446cb72021-06-07 14:11:5616#include "chromecast/bindings/public/mojom/api_bindings.mojom.h"
Sean Topping6c9e3932021-08-23 21:25:3317#include "chromecast/browser/cast_web_contents_observer.h"
Sean Topping92d0465b2021-07-16 01:11:3318#include "chromecast/browser/mojom/cast_web_contents.mojom.h"
19#include "chromecast/browser/web_types.h"
Chad Duffin4aa9c042019-01-11 00:51:5520#include "chromecast/common/mojom/feature_manager.mojom.h"
Sean Topping0436b462021-10-12 21:38:5621#include "chromecast/mojo/interface_bundle.h"
Chih-Hsuan Kuo7b4fb102020-03-04 08:21:4922#include "content/public/common/media_playback_renderer_type.mojom.h"
Ken Rockot48c7b502020-11-20 23:33:5923#include "mojo/public/cpp/bindings/generic_pending_receiver.h"
Sean Topping92d0465b2021-07-16 01:11:3324#include "mojo/public/cpp/bindings/pending_receiver.h"
Sean Topping077ba6d82021-11-02 22:48:4225#include "mojo/public/cpp/bindings/receiver.h"
Sean Toppinge0c5a95c2021-11-12 23:13:4926#include "mojo/public/cpp/bindings/receiver_set.h"
Sean Topping92d0465b2021-07-16 01:11:3327#include "mojo/public/cpp/bindings/remote_set.h"
Jiawei Li830fcfd2019-02-12 20:12:1728#include "services/service_manager/public/cpp/interface_provider.h"
Anton Bikineev3f85f032021-05-15 17:58:2429#include "third_party/abseil-cpp/absl/types/optional.h"
Chris Hamilton825f2ed2020-01-30 21:46:4130#include "third_party/blink/public/common/messaging/web_message_port.h"
Sean Toppingf0a1d572019-06-17 23:03:4431#include "ui/gfx/geometry/rect.h"
Sean Toppinge91b0972018-10-25 20:59:1232#include "url/gurl.h"
33
34namespace content {
Ryan Keane0d0493d2021-05-25 16:59:5535class NavigationHandle;
Sean Toppinge91b0972018-10-25 20:59:1236class WebContents;
37} // namespace content
38
Junbo Ke7f8623a2022-01-21 17:57:0539namespace url_rewrite {
40class UrlRequestRewriteRulesManager;
41} // namespace url_rewrite
42
Sean Toppinge91b0972018-10-25 20:59:1243namespace chromecast {
44
Sean Topping0e8ac572019-01-30 03:05:4345struct RendererFeature {
46 const std::string name;
47 base::Value value;
48};
49
Sean Toppinge91b0972018-10-25 20:59:1250// Simplified WebContents wrapper class for Cast platforms.
Sean Topping7414eed2019-04-10 23:04:2151//
52// Proper usage of content::WebContents relies on understanding the meaning
53// behind various WebContentsObserver methods, and then translating those
54// signals into some concrete state. CastWebContents does *not* own the
55// underlying WebContents (usually whatever class implements
56// content::WebContentsDelegate is the actual owner).
57//
58// =============================================================================
59// Lifetime
60// =============================================================================
61// CastWebContents *must* be created before WebContents begins loading any
62// content. Once content begins loading (via CWC::LoadUrl() or one of the
63// WebContents navigation APIs), CastWebContents will calculate its state based
64// on the status of the WebContents' *main* RenderFrame. Events from sub-frames
65// (e.g. iframes) are ignored, since we expect the web app to take care of
66// sub-frame errors.
67//
68// We consider the CastWebContents to be in a LOADED state when the content of
Jiawei Li042aaca2021-03-30 00:15:5369// the main frame is fully loaded and running (all resources fetched,
70// redirection finished, JS is running). Iframes might still be loading in this
71// case, but in general we consider the page to be in a presentable state at
72// this stage, so it is appropriate to display the WebContents to the user.
Sean Topping7414eed2019-04-10 23:04:2173//
74// During or after the page is loaded, there are multiple error conditions that
75// can occur. The following events will cause the page to enter an ERROR state:
76//
77// 1. If the main frame is served an HTTP error page (such as a 404 page), then
78// it means the desired content wasn't loaded.
79//
80// 2. If the main frame fails to load, such as when the browser blocked the URL
81// request, we treat this as an error.
82//
83// 3. The RenderProcess for the main frame could crash, so the page is not in a
84// usable state.
85//
86// The CastWebContents user can respond to these errors in a few ways: The
87// content can be reloaded, or the entire page activity can be cancelled. If we
88// totally cancel the activity, we prefer to notify the user with an error
89// screen or visible/audible error message. Otherwise, a silent retry is
90// preferred.
91//
92// CastWebContents can be used to close the underlying WebContents gracefully
93// via CWC::Close(). This initiates web page tear-down logic so that the web
94// app has a chance to perform its own finalization logic in JS. Next, we call
95// WebContents::ClosePage(), which defers the page closure logic to the
96// content::WebContentsDelegate. Usually, it will run its own finalization
97// logic and then destroy the WebContents. CastWebContents will be notified of
98// the WebContents destruction and enter the DESTROYED state. In the event
99// the page isn't destroyed, the page will enter the CLOSED state automatically
100// after a timeout. CastWebContents users should not try to reload the page, as
101// page closure is intentional.
102//
103// The web app may decide to close itself (such as via "window.close()" in JS).
104// This is similar to initiating the close flow via CWC::Close(), with the end
105// result being the same. We consider this an intentional closure, and should
106// not attempt to reload the page.
107//
108// Once CastWebContents is in the DESTROYED state, it is not really usable
109// anymore; most of the methods will simply no-op, and no more observer signals
110// will be emitted.
111//
112// CastWebContents can be deleted at any time, *except* during Observer
113// notifications. If the owner wants to destroy CastWebContents as a result of
114// an Observer event, it should post a task to destroy CastWebContents.
Sean Topping92d0465b2021-07-16 01:11:33115class CastWebContents : public mojom::CastWebContents {
Sean Toppinge91b0972018-10-25 20:59:12116 public:
Sean Topping6c9e3932021-08-23 21:25:33117 // Synchronous in-process observer for CastWebContents. Observers must not
118 // destroy CastWebContents in any of the methods below.
119 class Observer : public base::CheckedObserver {
Sean Toppingf0a1d572019-06-17 23:03:44120 public:
121 Observer();
122
Sean Topping92d0465b2021-07-16 01:11:33123 // Adds |this| to the CastWebContents observer list. Observe(nullptr) will
124 // remove |this| from the observer list of the current CastWebContents being
125 // observed.
126 void Observe(CastWebContents* cast_web_contents);
127
Ryan Keane0d0493d2021-05-25 16:59:55128 // Called when the navigation is ready to be committed in the WebContents'
129 // main frame.
130 virtual void MainFrameReadyToCommitNavigation(
131 content::NavigationHandle* navigation_handle) {}
132
Vigen Issahhanjan77d798c22021-11-30 23:36:11133 // Notify that an inner WebContents was created. |inner_contents| is created
134 // in a default-initialized state with no delegate, and can be safely
135 // initialized by the delegate.
136 virtual void InnerContentsCreated(CastWebContents* inner_contents,
137 CastWebContents* outer_contents) {}
138
Sean Topping6c9e3932021-08-23 21:25:33139 // Sets |cast_web_contents_| to |nullptr| but does not remove the Observer
140 // from the ObserverList. Called for each Observer during CastWebContents
141 // destruction; we don't use Observe(nullptr) since it would mutate the
142 // ObserverList during iteration.
Chad Duffin4aa9c042019-01-11 00:51:55143 void ResetCastWebContents();
144
145 protected:
Sean Topping92d0465b2021-07-16 01:11:33146 ~Observer() override;
Chad Duffin4aa9c042019-01-11 00:51:55147
148 CastWebContents* cast_web_contents_;
Sean Toppinge91b0972018-10-25 20:59:12149 };
150
Sean Topping6735aaf2019-03-20 19:20:12151 static std::vector<CastWebContents*>& GetAll();
152
Jiawei Lia64a082b2019-12-06 21:17:26153 // Returns the CastWebContents that wraps the content::WebContents, or nullptr
154 // if the CastWebContents does not exist.
155 static CastWebContents* FromWebContents(content::WebContents* web_contents);
156
Sean Topping92d0465b2021-07-16 01:11:33157 CastWebContents();
Peter Boströmae0ffa02021-10-05 17:08:33158
159 CastWebContents(const CastWebContents&) = delete;
160 CastWebContents& operator=(const CastWebContents&) = delete;
161
Sean Topping92d0465b2021-07-16 01:11:33162 ~CastWebContents() override;
Sean Toppinge91b0972018-10-25 20:59:12163
Sean Topping6735aaf2019-03-20 19:20:12164 // Tab identifier for the WebContents, mainly used by the tabs extension API.
165 // Tab IDs may be re-used, but no two live CastWebContents should have the
166 // same tab ID at any given time.
167 virtual int tab_id() const = 0;
168
Randy Rossic6bb3ab2020-08-11 18:02:52169 // An identifier for the WebContents, mainly used by platform views service.
170 // IDs may be re-used but are unique among all live CastWebContents.
171 virtual int id() const = 0;
172
Sean Topping0e8ac572019-01-30 03:05:43173 // TODO(seantopping): Hide this, clients shouldn't use WebContents directly.
Sean Toppinge91b0972018-10-25 20:59:12174 virtual content::WebContents* web_contents() const = 0;
175 virtual PageState page_state() const = 0;
Junbo Ked10a1eb2022-01-25 02:28:35176 virtual url_rewrite::UrlRequestRewriteRulesManager*
177 url_rewrite_rules_manager() = 0;
Sean Toppinge91b0972018-10-25 20:59:12178
Sean Topping92d0465b2021-07-16 01:11:33179 // mojom::CastWebContents implementation:
Sean Topping6c9e3932021-08-23 21:25:33180 void SetAppProperties(const std::string& app_id,
181 const std::string& session_id,
182 bool is_audio_app,
Guohui Dengedbf8032021-09-29 21:34:19183 const GURL& app_web_url,
184 bool enforce_feature_permissions,
185 const std::vector<int32_t>& feature_permissions,
186 const std::vector<std::string>&
187 additional_feature_permission_origins) override = 0;
Sean Topping077ba6d82021-11-02 22:48:42188 void SetGroupInfo(const std::string& session_id,
189 bool is_multizone_launch) override = 0;
Sean Topping6c9e3932021-08-23 21:25:33190 void AddRendererFeatures(base::Value features) override = 0;
191 void SetInterfacesForRenderer(mojo::PendingRemote<mojom::RemoteInterfaces>
192 remote_interfaces) override = 0;
Junbo Ke7f8623a2022-01-21 17:57:05193 void SetUrlRewriteRules(
194 url_rewrite::mojom::UrlRequestRewriteRulesPtr rules) override = 0;
Sean Topping92d0465b2021-07-16 01:11:33195 void LoadUrl(const GURL& url) override = 0;
196 void ClosePage() override = 0;
197 void SetWebVisibilityAndPaint(bool visible) override = 0;
198 void BlockMediaLoading(bool blocked) override = 0;
199 void BlockMediaStarting(bool blocked) override = 0;
200 void EnableBackgroundVideoPlayback(bool enabled) override = 0;
Sean Topping6c9e3932021-08-23 21:25:33201 void ConnectToBindingsService(
202 mojo::PendingRemote<mojom::ApiBindings> api_bindings_remote) override = 0;
Sean Topping92d0465b2021-07-16 01:11:33203 void SetEnabledForRemoteDebugging(bool enabled) override = 0;
204 void AddObserver(
205 mojo::PendingRemote<mojom::CastWebContentsObserver> observer) override;
Sean Topping6c9e3932021-08-23 21:25:33206 void GetMainFramePid(GetMainFramePidCallback cb) override = 0;
Sean Topping92d0465b2021-07-16 01:11:33207
Sean Topping0e8ac572019-01-30 03:05:43208 // ===========================================================================
Sean Topping0e8ac572019-01-30 03:05:43209 // Page Lifetime
210 // ===========================================================================
211
Sean Toppinge91b0972018-10-25 20:59:12212 // Stop the page immediately. This will automatically invoke
213 // Delegate::OnPageStopped(error_code), allowing the delegate to delete or
Sean Topping7414eed2019-04-10 23:04:21214 // reload the page without waiting for the WebContents owner to tear down the
215 // page.
Sean Toppinge91b0972018-10-25 20:59:12216 virtual void Stop(int error_code) = 0;
217
Sean Topping8a343862019-05-09 22:19:14218 // ===========================================================================
Jiawei Lidf9ad18a2019-07-07 05:31:02219 // Page Communication
220 // ===========================================================================
221
Jiawei Li3446cb72021-06-07 14:11:56222 // Executes a UTF-8 encoded |script| for every subsequent page load where
223 // the frame's URL has an origin reflected in |origins|. The script is
224 // executed early, prior to the execution of the document's scripts.
225 //
226 // Scripts are identified by a client-managed |id|. Any
227 // script previously injected using the same |id| will be replaced.
228 //
229 // The order in which multiple bindings are executed is the same as the
230 // order in which the bindings were added. If a script is added which
231 // clobbers an existing script of the same |id|, the previous script's
232 // precedence in the injection order will be preserved.
233 // |script| and |id| must be non-empty string.
234 virtual void AddBeforeLoadJavaScript(uint64_t id,
235 base::StringPiece script) = 0;
236
Jiawei Libba76072019-07-29 23:00:25237 // Posts a message to the frame's onMessage handler.
238 //
239 // `target_origin` restricts message delivery to the specified origin.
240 // If `target_origin` is "*", then the message will be sent to the
241 // document regardless of its origin.
242 // See html.spec.whatwg.org/multipage/web-messaging.html sect. 9.4.3
243 // for more details on how the target origin policy is applied.
244 // Should be called on UI thread.
Chris Hamilton825f2ed2020-01-30 21:46:41245 virtual void PostMessageToMainFrame(
246 const std::string& target_origin,
247 const std::string& data,
248 std::vector<blink::WebMessagePort> ports) = 0;
Jiawei Libba76072019-07-29 23:00:25249
Jiawei Li5283ad42020-03-27 21:26:05250 // Executes a string of JavaScript in the main frame's context.
251 // This is no-op if the main frame is not available.
252 // Pass in a callback to receive a result when it is available.
253 // If there is no need to receive the result, pass in a
254 // default-constructed callback. If provided, the callback
255 // will be invoked on the UI thread.
256 virtual void ExecuteJavaScript(
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58257 const std::u16string& javascript,
Jiawei Li5283ad42020-03-27 21:26:05258 base::OnceCallback<void(base::Value)> callback) = 0;
259
Jiawei Lidf9ad18a2019-07-07 05:31:02260 // ===========================================================================
Sean Topping8a343862019-05-09 22:19:14261 // Utility Methods
262 // ===========================================================================
263
Ken Rockot48c7b502020-11-20 23:33:59264 // Asks the CastWebContents to bind an interface receiver using either its
265 // registry or any registered InterfaceProvider.
266 virtual bool TryBindReceiver(mojo::GenericPendingReceiver& receiver) = 0;
267
Sean Topping0436b462021-10-12 21:38:56268 // Locally-registered interfaces which are exposed to render frames.
269 virtual InterfaceBundle* local_interfaces() = 0;
270
Jiawei Lia64a082b2019-12-06 21:17:26271 // Returns true if WebSQL database is configured enabled for this
272 // CastWebContents.
273 virtual bool is_websql_enabled() = 0;
274
275 // Returns true if mixer audio is enabled.
276 virtual bool is_mixer_audio_enabled() = 0;
277
Sean Toppinge0c5a95c2021-11-12 23:13:49278 // Binds an owning receiver for remote control of CastWebContents. When the
279 // CastWebContents is managed by CastWebService, its lifetime is scoped to the
280 // duration of the connection. Only one owner can be bound at a time.
281 void BindOwnerReceiver(
282 mojo::PendingReceiver<mojom::CastWebContents> receiver);
283
284 // Binds a non-owning receiver for CastWebContents. This can be called by
285 // multiple clients.
286 void BindSharedReceiver(
287 mojo::PendingReceiver<mojom::CastWebContents> receiver);
Sean Topping92d0465b2021-07-16 01:11:33288
Sean Topping077ba6d82021-11-02 22:48:42289 // |cb| is called when |receiver_| is disconnected. This allows the web
290 // service to destroy CastWebContents which are owned via a remote handle.
291 void SetDisconnectCallback(base::OnceClosure cb);
292
Sean Topping92d0465b2021-07-16 01:11:33293 protected:
Sean Toppinge0c5a95c2021-11-12 23:13:49294 mojo::Receiver<mojom::CastWebContents> owner_receiver_{this};
295 mojo::ReceiverSet<mojom::CastWebContents> shared_receivers_;
Sean Topping92d0465b2021-07-16 01:11:33296 mojo::RemoteSet<mojom::CastWebContentsObserver> observers_;
Sean Topping6c9e3932021-08-23 21:25:33297 base::ObserverList<Observer> sync_observers_;
Sean Topping92d0465b2021-07-16 01:11:33298
Sean Toppinge91b0972018-10-25 20:59:12299 private:
Sean Topping92d0465b2021-07-16 01:11:33300 friend class Observer;
301
302 // These functions should only be invoked by CastWebContents::Observer in a
303 // valid sequence, enforced via SequenceChecker.
Sean Topping6c9e3932021-08-23 21:25:33304 void AddObserver(Observer* observer);
305 void RemoveObserver(Observer* observer);
Sean Topping077ba6d82021-11-02 22:48:42306
307 void OnDisconnect();
308
309 base::OnceClosure disconnect_cb_;
Sean Toppinge91b0972018-10-25 20:59:12310};
311
Sean Toppinge91b0972018-10-25 20:59:12312} // namespace chromecast
313
314#endif // CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_