Sean Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 1 | // 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 Duffin | 4aa9c04 | 2019-01-11 00:51:55 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Jiawei Li | 830fcfd | 2019-02-12 20:12:17 | [diff] [blame] | 11 | #include "base/containers/flat_set.h" |
Chad Duffin | 4aa9c04 | 2019-01-11 00:51:55 | [diff] [blame] | 12 | #include "base/observer_list.h" |
Sean Topping | 0e8ac57 | 2019-01-30 03:05:43 | [diff] [blame] | 13 | #include "base/optional.h" |
Chad Duffin | 4aa9c04 | 2019-01-11 00:51:55 | [diff] [blame] | 14 | #include "chromecast/common/mojom/feature_manager.mojom.h" |
Jiawei Li | 830fcfd | 2019-02-12 20:12:17 | [diff] [blame] | 15 | #include "services/service_manager/public/cpp/binder_registry.h" |
| 16 | #include "services/service_manager/public/cpp/interface_provider.h" |
Sean Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 17 | #include "url/gurl.h" |
| 18 | |
| 19 | namespace content { |
| 20 | class WebContents; |
| 21 | } // namespace content |
| 22 | |
| 23 | namespace chromecast { |
| 24 | |
Sean Topping | 0e8ac57 | 2019-01-30 03:05:43 | [diff] [blame] | 25 | struct RendererFeature { |
| 26 | const std::string name; |
| 27 | base::Value value; |
| 28 | }; |
| 29 | |
Sean Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 30 | // Simplified WebContents wrapper class for Cast platforms. |
| 31 | class 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 Topping | 0e8ac57 | 2019-01-30 03:05:43 | [diff] [blame] | 52 | // 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 Duffin | 4aa9c04 | 2019-01-11 00:51:55 | [diff] [blame] | 57 | |
Sean Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 58 | protected: |
| 59 | virtual ~Delegate() {} |
| 60 | }; |
| 61 | |
Chad Duffin | 4aa9c04 | 2019-01-11 00:51:55 | [diff] [blame] | 62 | class Observer { |
| 63 | public: |
| 64 | Observer(); |
| 65 | |
| 66 | virtual void RenderFrameCreated(int render_process_id, |
| 67 | int render_frame_id) {} |
Chad Duffin | 4aa9c04 | 2019-01-11 00:51:55 | [diff] [blame] | 68 | |
| 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 Topping | 0e8ac57 | 2019-01-30 03:05:43 | [diff] [blame] | 86 | struct InitParams { |
| 87 | Delegate* delegate; |
| 88 | bool enabled_for_dev; |
Yuchen Liu | ea568d2 | 2019-03-05 21:54:01 | [diff] [blame] | 89 | bool use_cma_renderer; |
Sean Topping | 6735aaf | 2019-03-20 19:20:12 | [diff] [blame^] | 90 | bool is_root_window = false; |
Sean Topping | 0e8ac57 | 2019-01-30 03:05:43 | [diff] [blame] | 91 | }; |
| 92 | |
Sean Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 93 | // 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 Topping | 6735aaf | 2019-03-20 19:20:12 | [diff] [blame^] | 103 | static std::vector<CastWebContents*>& GetAll(); |
| 104 | |
Sean Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 105 | CastWebContents() = default; |
| 106 | virtual ~CastWebContents() = default; |
| 107 | |
Sean Topping | 6735aaf | 2019-03-20 19:20:12 | [diff] [blame^] | 108 | // 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 Topping | 0e8ac57 | 2019-01-30 03:05:43 | [diff] [blame] | 113 | // TODO(seantopping): Hide this, clients shouldn't use WebContents directly. |
Sean Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 114 | virtual content::WebContents* web_contents() const = 0; |
| 115 | virtual PageState page_state() const = 0; |
| 116 | |
Sean Topping | 0e8ac57 | 2019-01-30 03:05:43 | [diff] [blame] | 117 | // =========================================================================== |
| 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 Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 135 | // 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 Duffin | 4aa9c04 | 2019-01-11 00:51:55 | [diff] [blame] | 149 | // 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 Li | 830fcfd | 2019-02-12 20:12:17 | [diff] [blame] | 155 | // 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 Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 168 | private: |
| 169 | DISALLOW_COPY_AND_ASSIGN(CastWebContents); |
| 170 | }; |
| 171 | |
| 172 | std::ostream& operator<<(std::ostream& os, CastWebContents::PageState state); |
| 173 | |
| 174 | } // namespace chromecast |
| 175 | |
| 176 | #endif // CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_ |