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 | |
| 8 | #include "url/gurl.h" |
| 9 | |
| 10 | namespace content { |
| 11 | class WebContents; |
| 12 | } // namespace content |
| 13 | |
| 14 | namespace chromecast { |
| 15 | |
| 16 | // Simplified WebContents wrapper class for Cast platforms. |
| 17 | class CastWebContents { |
| 18 | public: |
| 19 | class Delegate { |
| 20 | public: |
| 21 | // Advertises page state for the CastWebContents. |
| 22 | // Use CastWebContents::page_state() to get the new state. |
| 23 | virtual void OnPageStateChanged(CastWebContents* cast_web_contents) = 0; |
| 24 | |
| 25 | // Called when the page has stopped. e.g.: A 404 occurred when loading the |
| 26 | // page or if the render process for the main frame crashes. |error_code| |
| 27 | // will return a net::Error describing the failure, or net::OK if the page |
| 28 | // closed naturally. |
| 29 | // |
| 30 | // After this method, the page state will be one of the following: |
| 31 | // CLOSED: Page was closed as expected and the WebContents exists. |
| 32 | // DESTROYED: Page was closed due to deletion of WebContents. The |
| 33 | // CastWebContents instance is no longer usable and should be deleted. |
| 34 | // ERROR: Page is in an error state. It should be reloaded or deleted. |
| 35 | virtual void OnPageStopped(CastWebContents* cast_web_contents, |
| 36 | int error_code) = 0; |
| 37 | |
| 38 | protected: |
| 39 | virtual ~Delegate() {} |
| 40 | }; |
| 41 | |
| 42 | // Page state for the main frame. |
| 43 | enum class PageState { |
| 44 | IDLE, // Main frame has not started yet. |
| 45 | LOADING, // Main frame is loading resources. |
| 46 | LOADED, // Main frame is loaded, but sub-frames may still be loading. |
| 47 | CLOSED, // Page is closed and should be cleaned up. |
| 48 | DESTROYED, // The WebContents is destroyed and can no longer be used. |
| 49 | ERROR, // Main frame is in an error state. |
| 50 | }; |
| 51 | |
| 52 | CastWebContents() = default; |
| 53 | virtual ~CastWebContents() = default; |
| 54 | |
| 55 | virtual content::WebContents* web_contents() const = 0; |
| 56 | virtual PageState page_state() const = 0; |
| 57 | |
| 58 | // Navigates the underlying WebContents to |url|. Delegate will be notified of |
| 59 | // page progression events via OnPageStateChanged(). |
| 60 | virtual void LoadUrl(const GURL& url) = 0; |
| 61 | |
| 62 | // Initiate closure of the page. This invokes the appropriate unload handlers. |
| 63 | // Eventually the delegate will be notified with OnPageStopped(). |
| 64 | virtual void ClosePage() = 0; |
| 65 | |
| 66 | // Stop the page immediately. This will automatically invoke |
| 67 | // Delegate::OnPageStopped(error_code), allowing the delegate to delete or |
| 68 | // reload the page without waiting for page teardown, which may be handled |
| 69 | // independently. |
| 70 | virtual void Stop(int error_code) = 0; |
| 71 | |
| 72 | // Set the delegate. SetDelegate(nullptr) can be used to stop notifications. |
| 73 | virtual void SetDelegate(Delegate* delegate) = 0; |
| 74 | |
| 75 | private: |
| 76 | DISALLOW_COPY_AND_ASSIGN(CastWebContents); |
| 77 | }; |
| 78 | |
| 79 | std::ostream& operator<<(std::ostream& os, CastWebContents::PageState state); |
| 80 | |
| 81 | } // namespace chromecast |
| 82 | |
| 83 | #endif // CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_ |