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 | |
| 11 | #include "base/observer_list.h" |
| 12 | #include "chromecast/common/mojom/feature_manager.mojom.h" |
Sean Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 13 | #include "url/gurl.h" |
| 14 | |
| 15 | namespace content { |
| 16 | class WebContents; |
| 17 | } // namespace content |
| 18 | |
| 19 | namespace chromecast { |
| 20 | |
| 21 | // Simplified WebContents wrapper class for Cast platforms. |
| 22 | class CastWebContents { |
| 23 | public: |
| 24 | class Delegate { |
| 25 | public: |
| 26 | // Advertises page state for the CastWebContents. |
| 27 | // Use CastWebContents::page_state() to get the new state. |
| 28 | virtual void OnPageStateChanged(CastWebContents* cast_web_contents) = 0; |
| 29 | |
| 30 | // Called when the page has stopped. e.g.: A 404 occurred when loading the |
| 31 | // page or if the render process for the main frame crashes. |error_code| |
| 32 | // will return a net::Error describing the failure, or net::OK if the page |
| 33 | // closed naturally. |
| 34 | // |
| 35 | // After this method, the page state will be one of the following: |
| 36 | // CLOSED: Page was closed as expected and the WebContents exists. |
| 37 | // DESTROYED: Page was closed due to deletion of WebContents. The |
| 38 | // CastWebContents instance is no longer usable and should be deleted. |
| 39 | // ERROR: Page is in an error state. It should be reloaded or deleted. |
| 40 | virtual void OnPageStopped(CastWebContents* cast_web_contents, |
| 41 | int error_code) = 0; |
| 42 | |
Chad Duffin | 4aa9c04 | 2019-01-11 00:51:55 | [diff] [blame^] | 43 | // Collects the set of delegate-specific renderer features that needs to be |
| 44 | // configured when `CastWebContents::RenderFrameCreated` is invoked. |
| 45 | virtual std::vector<chromecast::shell::mojom::FeaturePtr> |
| 46 | GetRendererFeatures(); |
| 47 | |
Sean Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 48 | protected: |
| 49 | virtual ~Delegate() {} |
| 50 | }; |
| 51 | |
Chad Duffin | 4aa9c04 | 2019-01-11 00:51:55 | [diff] [blame^] | 52 | class Observer { |
| 53 | public: |
| 54 | Observer(); |
| 55 | |
| 56 | virtual void RenderFrameCreated(int render_process_id, |
| 57 | int render_frame_id) {} |
| 58 | virtual void OnInterfaceRequestFromFrame( |
| 59 | const std::string& interface_name, |
| 60 | mojo::ScopedMessagePipeHandle* interface_pipe) {} |
| 61 | |
| 62 | // Adds |this| to the ObserverList in the implementation of |
| 63 | // |cast_web_contents|. |
| 64 | void Observe(CastWebContents* cast_web_contents); |
| 65 | |
| 66 | // Removes |this| from the ObserverList in the implementation of |
| 67 | // |cast_web_contents_|. This is only invoked by CastWebContents and is used |
| 68 | // to ensure that once the observed CastWebContents object is destructed the |
| 69 | // CastWebContents::Observer does not invoke any additional function calls |
| 70 | // on it. |
| 71 | void ResetCastWebContents(); |
| 72 | |
| 73 | protected: |
| 74 | virtual ~Observer(); |
| 75 | |
| 76 | CastWebContents* cast_web_contents_; |
| 77 | }; |
| 78 | |
Sean Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 79 | // Page state for the main frame. |
| 80 | enum class PageState { |
| 81 | IDLE, // Main frame has not started yet. |
| 82 | LOADING, // Main frame is loading resources. |
| 83 | LOADED, // Main frame is loaded, but sub-frames may still be loading. |
| 84 | CLOSED, // Page is closed and should be cleaned up. |
| 85 | DESTROYED, // The WebContents is destroyed and can no longer be used. |
| 86 | ERROR, // Main frame is in an error state. |
| 87 | }; |
| 88 | |
| 89 | CastWebContents() = default; |
| 90 | virtual ~CastWebContents() = default; |
| 91 | |
| 92 | virtual content::WebContents* web_contents() const = 0; |
| 93 | virtual PageState page_state() const = 0; |
| 94 | |
| 95 | // Navigates the underlying WebContents to |url|. Delegate will be notified of |
| 96 | // page progression events via OnPageStateChanged(). |
| 97 | virtual void LoadUrl(const GURL& url) = 0; |
| 98 | |
| 99 | // Initiate closure of the page. This invokes the appropriate unload handlers. |
| 100 | // Eventually the delegate will be notified with OnPageStopped(). |
| 101 | virtual void ClosePage() = 0; |
| 102 | |
| 103 | // Stop the page immediately. This will automatically invoke |
| 104 | // Delegate::OnPageStopped(error_code), allowing the delegate to delete or |
| 105 | // reload the page without waiting for page teardown, which may be handled |
| 106 | // independently. |
| 107 | virtual void Stop(int error_code) = 0; |
| 108 | |
| 109 | // Set the delegate. SetDelegate(nullptr) can be used to stop notifications. |
| 110 | virtual void SetDelegate(Delegate* delegate) = 0; |
| 111 | |
Chad Duffin | 4aa9c04 | 2019-01-11 00:51:55 | [diff] [blame^] | 112 | virtual void AllowWebAndMojoWebUiBindings() = 0; |
| 113 | virtual void ClearRenderWidgetHostView() = 0; |
| 114 | |
| 115 | // Used to add or remove |observer| to the ObserverList in the implementation. |
| 116 | // These functions should only be invoked by CastWebContents::Observer in a |
| 117 | // valid sequence, enforced via SequenceChecker. |
| 118 | virtual void AddObserver(Observer* observer) = 0; |
| 119 | virtual void RemoveObserver(Observer* observer) = 0; |
| 120 | |
Sean Topping | e91b097 | 2018-10-25 20:59:12 | [diff] [blame] | 121 | private: |
| 122 | DISALLOW_COPY_AND_ASSIGN(CastWebContents); |
| 123 | }; |
| 124 | |
| 125 | std::ostream& operator<<(std::ostream& os, CastWebContents::PageState state); |
| 126 | |
| 127 | } // namespace chromecast |
| 128 | |
| 129 | #endif // CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_ |