Skip to content

Commit

Permalink
[Chromecast] Introduce CastWebContents
Browse files Browse the repository at this point in the history
Pulls common logic from CastWebView implementations into a shared base
class. The main intent of this new class is to wrap WebContents and
provide a simplified API for consumers in Cast.

Merge-With: eureka-internal/206097

Bug: internal b/77879457
Test: CQ
Change-Id: I46c952613e0dcf3631941a27160b34390386ad30
Reviewed-on: https://chromium-review.googlesource.com/c/1263420
Reviewed-by: Luke Halliwell <halliwell@chromium.org>
Commit-Queue: Sean Topping <seantopping@chromium.org>
Cr-Commit-Position: refs/heads/master@{#602851}
  • Loading branch information
Sean Topping authored and Commit Bot committed Oct 25, 2018
1 parent 9d534c3 commit e91b097
Show file tree
Hide file tree
Showing 15 changed files with 534 additions and 197 deletions.
3 changes: 3 additions & 0 deletions chromecast/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ cast_source_set("browser") {
"cast_quota_permission_context.h",
"cast_resource_dispatcher_host_delegate.cc",
"cast_resource_dispatcher_host_delegate.h",
"cast_web_contents_impl.cc",
"cast_web_contents_impl.h",
"cast_web_contents_manager.cc",
"cast_web_contents_manager.h",
"cast_web_view_default.cc",
Expand Down Expand Up @@ -334,6 +336,7 @@ cast_source_set("public") {
sources = [
"cast_content_window.cc",
"cast_content_window.h",
"cast_web_contents.h",
"cast_web_view.cc",
"cast_web_view.h",
]
Expand Down
6 changes: 0 additions & 6 deletions chromecast/browser/cast_extension_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@ void CastExtensionHost::LoadInitialURL() {
extensions::ExtensionHost::LoadInitialURL();
}

void CastExtensionHost::LoadingStateChanged(content::WebContents* source,
bool to_different_document) {
extensions::ExtensionHost::LoadingStateChanged(source, to_different_document);
delegate_->OnLoadingStateChanged(source->IsLoading());
}

void CastExtensionHost::ActivateContents(content::WebContents* contents) {
DCHECK_EQ(contents, host_contents());
contents->GetRenderViewHost()->GetWidget()->Focus();
Expand Down
2 changes: 0 additions & 2 deletions chromecast/browser/cast_extension_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class CastExtensionHost : public extensions::ExtensionHost,
void ActivateContents(content::WebContents* contents) override;
void DidStartNavigation(
content::NavigationHandle* navigation_handle) override;
void LoadingStateChanged(content::WebContents* source,
bool to_different_document) override;
bool DidAddMessageToConsole(content::WebContents* source,
int32_t level,
const base::string16& message,
Expand Down
83 changes: 83 additions & 0 deletions chromecast/browser/cast_web_contents.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_
#define CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_

#include "url/gurl.h"

namespace content {
class WebContents;
} // namespace content

namespace chromecast {

// Simplified WebContents wrapper class for Cast platforms.
class CastWebContents {
public:
class Delegate {
public:
// Advertises page state for the CastWebContents.
// Use CastWebContents::page_state() to get the new state.
virtual void OnPageStateChanged(CastWebContents* cast_web_contents) = 0;

// Called when the page has stopped. e.g.: A 404 occurred when loading the
// page or if the render process for the main frame crashes. |error_code|
// will return a net::Error describing the failure, or net::OK if the page
// closed naturally.
//
// After this method, the page state will be one of the following:
// CLOSED: Page was closed as expected and the WebContents exists.
// DESTROYED: Page was closed due to deletion of WebContents. The
// CastWebContents instance is no longer usable and should be deleted.
// ERROR: Page is in an error state. It should be reloaded or deleted.
virtual void OnPageStopped(CastWebContents* cast_web_contents,
int error_code) = 0;

protected:
virtual ~Delegate() {}
};

// Page state for the main frame.
enum class PageState {
IDLE, // Main frame has not started yet.
LOADING, // Main frame is loading resources.
LOADED, // Main frame is loaded, but sub-frames may still be loading.
CLOSED, // Page is closed and should be cleaned up.
DESTROYED, // The WebContents is destroyed and can no longer be used.
ERROR, // Main frame is in an error state.
};

CastWebContents() = default;
virtual ~CastWebContents() = default;

virtual content::WebContents* web_contents() const = 0;
virtual PageState page_state() const = 0;

// Navigates the underlying WebContents to |url|. Delegate will be notified of
// page progression events via OnPageStateChanged().
virtual void LoadUrl(const GURL& url) = 0;

// Initiate closure of the page. This invokes the appropriate unload handlers.
// Eventually the delegate will be notified with OnPageStopped().
virtual void ClosePage() = 0;

// Stop the page immediately. This will automatically invoke
// Delegate::OnPageStopped(error_code), allowing the delegate to delete or
// reload the page without waiting for page teardown, which may be handled
// independently.
virtual void Stop(int error_code) = 0;

// Set the delegate. SetDelegate(nullptr) can be used to stop notifications.
virtual void SetDelegate(Delegate* delegate) = 0;

private:
DISALLOW_COPY_AND_ASSIGN(CastWebContents);
};

std::ostream& operator<<(std::ostream& os, CastWebContents::PageState state);

} // namespace chromecast

#endif // CHROMECAST_BROWSER_CAST_WEB_CONTENTS_H_
Loading

0 comments on commit e91b097

Please sign in to comment.