blob: d60a174d7817aef0643f90555b4fe8a3ca8c29f1 [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 Topping6735aaf2019-03-20 19:20:1290 bool is_root_window = false;
Sean Topping0e8ac572019-01-30 03:05:4391 };
92
Sean Toppinge91b0972018-10-25 20:59:1293 // Page state for the main frame.
94 enum class PageState {
95 IDLE, // Main frame has not started yet.
96 LOADING, // Main frame is loading resources.
97 LOADED, // Main frame is loaded, but sub-frames may still be loading.
98 CLOSED, // Page is closed and should be cleaned up.
99 DESTROYED, // The WebContents is destroyed and can no longer be used.
100 ERROR, // Main frame is in an error state.
101 };
102
Sean Topping6735aaf2019-03-20 19:20:12103 static std::vector<CastWebContents*>& GetAll();
104
Sean Toppinge91b0972018-10-25 20:59:12105 CastWebContents() = default;
106 virtual ~CastWebContents() = default;
107
Sean Topping6735aaf2019-03-20 19:20:12108 // Tab identifier for the WebContents, mainly used by the tabs extension API.
109 // Tab IDs may be re-used, but no two live CastWebContents should have the
110 // same tab ID at any given time.
111 virtual int tab_id() const = 0;
112
Sean Topping0e8ac572019-01-30 03:05:43113 // TODO(seantopping): Hide this, clients shouldn't use WebContents directly.
Sean Toppinge91b0972018-10-25 20:59:12114 virtual content::WebContents* web_contents() const = 0;
115 virtual PageState page_state() const = 0;
116
Sean Topping0e8ac572019-01-30 03:05:43117 // ===========================================================================
118 // Initialization and Setup
119 // ===========================================================================
120
121 // Set the delegate. SetDelegate(nullptr) can be used to stop notifications.
122 virtual void SetDelegate(Delegate* delegate) = 0;
123
124 // Add a set of features for all renderers in the WebContents. Features are
125 // configured when `CastWebContents::RenderFrameCreated` is invoked.
126 virtual void AddRendererFeatures(std::vector<RendererFeature> features) = 0;
127
128 virtual void AllowWebAndMojoWebUiBindings() = 0;
129 virtual void ClearRenderWidgetHostView() = 0;
130
131 // ===========================================================================
132 // Page Lifetime
133 // ===========================================================================
134
Sean Toppinge91b0972018-10-25 20:59:12135 // Navigates the underlying WebContents to |url|. Delegate will be notified of
136 // page progression events via OnPageStateChanged().
137 virtual void LoadUrl(const GURL& url) = 0;
138
139 // Initiate closure of the page. This invokes the appropriate unload handlers.
140 // Eventually the delegate will be notified with OnPageStopped().
141 virtual void ClosePage() = 0;
142
143 // Stop the page immediately. This will automatically invoke
144 // Delegate::OnPageStopped(error_code), allowing the delegate to delete or
145 // reload the page without waiting for page teardown, which may be handled
146 // independently.
147 virtual void Stop(int error_code) = 0;
148
Chad Duffin4aa9c042019-01-11 00:51:55149 // Used to add or remove |observer| to the ObserverList in the implementation.
150 // These functions should only be invoked by CastWebContents::Observer in a
151 // valid sequence, enforced via SequenceChecker.
152 virtual void AddObserver(Observer* observer) = 0;
153 virtual void RemoveObserver(Observer* observer) = 0;
154
Jiawei Li830fcfd2019-02-12 20:12:17155 // Used to expose CastWebContents's |binder_registry_| to Delegate.
156 // Delegate should register its mojo interface binders via this function
157 // when it is ready.
158 virtual service_manager::BinderRegistry* binder_registry() = 0;
159
160 // Used for owner to pass its |InterfaceProviderPtr|s to CastWebContents.
161 // It is owner's respoinsibility to make sure each |InterfaceProviderPtr| has
162 // distinct mojo interface set.
163 using InterfaceSet = base::flat_set<std::string>;
164 virtual void RegisterInterfaceProvider(
165 const InterfaceSet& interface_set,
166 service_manager::InterfaceProvider* interface_provider) = 0;
167
Sean Toppinge91b0972018-10-25 20:59:12168 private:
169 DISALLOW_COPY_AND_ASSIGN(CastWebContents);
170};
171
172std::ostream& operator<<(std::ostream& os, CastWebContents::PageState state);
173
174} // namespace chromecast
175
176#endif // CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_