blob: 32b2ed4add1f53277f5637f9370a6f49050e9766 [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;
Yuchen Liuea568d22019-03-05 21:54:0189 bool use_cma_renderer;
Sean Topping0e8ac572019-01-30 03:05:4390 };
91
Sean Toppinge91b0972018-10-25 20:59:1292 // Page state for the main frame.
93 enum class PageState {
94 IDLE, // Main frame has not started yet.
95 LOADING, // Main frame is loading resources.
96 LOADED, // Main frame is loaded, but sub-frames may still be loading.
97 CLOSED, // Page is closed and should be cleaned up.
98 DESTROYED, // The WebContents is destroyed and can no longer be used.
99 ERROR, // Main frame is in an error state.
100 };
101
102 CastWebContents() = default;
103 virtual ~CastWebContents() = default;
104
Sean Topping0e8ac572019-01-30 03:05:43105 // TODO(seantopping): Hide this, clients shouldn't use WebContents directly.
Sean Toppinge91b0972018-10-25 20:59:12106 virtual content::WebContents* web_contents() const = 0;
107 virtual PageState page_state() const = 0;
108
Sean Topping0e8ac572019-01-30 03:05:43109 // ===========================================================================
110 // Initialization and Setup
111 // ===========================================================================
112
113 // Set the delegate. SetDelegate(nullptr) can be used to stop notifications.
114 virtual void SetDelegate(Delegate* delegate) = 0;
115
116 // Add a set of features for all renderers in the WebContents. Features are
117 // configured when `CastWebContents::RenderFrameCreated` is invoked.
118 virtual void AddRendererFeatures(std::vector<RendererFeature> features) = 0;
119
120 virtual void AllowWebAndMojoWebUiBindings() = 0;
121 virtual void ClearRenderWidgetHostView() = 0;
122
123 // ===========================================================================
124 // Page Lifetime
125 // ===========================================================================
126
Sean Toppinge91b0972018-10-25 20:59:12127 // Navigates the underlying WebContents to |url|. Delegate will be notified of
128 // page progression events via OnPageStateChanged().
129 virtual void LoadUrl(const GURL& url) = 0;
130
131 // Initiate closure of the page. This invokes the appropriate unload handlers.
132 // Eventually the delegate will be notified with OnPageStopped().
133 virtual void ClosePage() = 0;
134
135 // Stop the page immediately. This will automatically invoke
136 // Delegate::OnPageStopped(error_code), allowing the delegate to delete or
137 // reload the page without waiting for page teardown, which may be handled
138 // independently.
139 virtual void Stop(int error_code) = 0;
140
Chad Duffin4aa9c042019-01-11 00:51:55141 // Used to add or remove |observer| to the ObserverList in the implementation.
142 // These functions should only be invoked by CastWebContents::Observer in a
143 // valid sequence, enforced via SequenceChecker.
144 virtual void AddObserver(Observer* observer) = 0;
145 virtual void RemoveObserver(Observer* observer) = 0;
146
Jiawei Li830fcfd2019-02-12 20:12:17147 // Used to expose CastWebContents's |binder_registry_| to Delegate.
148 // Delegate should register its mojo interface binders via this function
149 // when it is ready.
150 virtual service_manager::BinderRegistry* binder_registry() = 0;
151
152 // Used for owner to pass its |InterfaceProviderPtr|s to CastWebContents.
153 // It is owner's respoinsibility to make sure each |InterfaceProviderPtr| has
154 // distinct mojo interface set.
155 using InterfaceSet = base::flat_set<std::string>;
156 virtual void RegisterInterfaceProvider(
157 const InterfaceSet& interface_set,
158 service_manager::InterfaceProvider* interface_provider) = 0;
159
Sean Toppinge91b0972018-10-25 20:59:12160 private:
161 DISALLOW_COPY_AND_ASSIGN(CastWebContents);
162};
163
164std::ostream& operator<<(std::ostream& os, CastWebContents::PageState state);
165
166} // namespace chromecast
167
168#endif // CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_