Commit cafbb262 authored by Sean Topping's avatar Sean Topping Committed by Commit Bot

Revert "[Chromecast] Refactor and Simplify CastWebView"

This reverts commit 6d514087.

Reason for revert: b/149569123

Original change's description:
> [Chromecast] Refactor and Simplify CastWebView
> 
> CastWebView forces too much indirection from the underlying page/window
> functionality, so some forwarding methods have been removed in favor of
> calling the members directly. This is presently not ideal, but will be a
> great improvement once the CastWebContents and CastContentWindow are
> surfaced as mojo interfaces via CastWebService and their lifetimes can
> be tracked independently.
> 
> The CastWebService API has been simplified so that clients no longer
> need to provide a SiteInstance when creating CastWebViews. The renderer
> preloading logic, which was previously disjoint from CastWebView, has
> now been incorporated.
> 
> Merge-With: eureka-internal/331288
> 
> Bug: internal 77879457
> Test: CQ, manually verify on display assistant device.
> Change-Id: I72fc57f0669783f61076626962d47b72d93463f6
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1968257
> Reviewed-by: Yuchen Liu <yucliu@chromium.org>
> Commit-Queue: Sean Topping <seantopping@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#740143}

TBR=sanfin@chromium.org,seantopping@chromium.org,yucliu@chromium.org,lijiawei@chromium.org,mdellaquila@google.com,zxliang@google.com

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: internal 77879457
Change-Id: I9a6445b7a76e681e2f9c7e155c18058481e88684
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2065850Reviewed-by: default avatarSean Topping <seantopping@chromium.org>
Reviewed-by: default avatarYuchen Liu <yucliu@chromium.org>
Commit-Queue: Sean Topping <seantopping@chromium.org>
Cr-Commit-Position: refs/heads/master@{#742893}
parent 718a6f6f
...@@ -99,8 +99,6 @@ cast_source_set("browser") { ...@@ -99,8 +99,6 @@ cast_source_set("browser") {
"cast_web_contents_impl.h", "cast_web_contents_impl.h",
"cast_web_service.cc", "cast_web_service.cc",
"cast_web_service.h", "cast_web_service.h",
"cast_web_view_base.cc",
"cast_web_view_base.h",
"cast_web_view_default.cc", "cast_web_view_default.cc",
"cast_web_view_default.h", "cast_web_view_default.h",
"cast_web_view_factory.cc", "cast_web_view_factory.cc",
......
...@@ -79,8 +79,7 @@ CastContentWindowAura::CastContentWindowAura( ...@@ -79,8 +79,7 @@ CastContentWindowAura::CastContentWindowAura(
gesture_priority_(params.gesture_priority), gesture_priority_(params.gesture_priority),
is_touch_enabled_(params.enable_touch_input), is_touch_enabled_(params.enable_touch_input),
window_(nullptr), window_(nullptr),
has_screen_access_(false), has_screen_access_(false) {}
resize_window_when_navigation_starts_(true) {}
CastContentWindowAura::~CastContentWindowAura() { CastContentWindowAura::~CastContentWindowAura() {
CastWebContents::Observer::Observe(nullptr); CastWebContents::Observer::Observe(nullptr);
...@@ -100,7 +99,6 @@ void CastContentWindowAura::CreateWindowForWebContents( ...@@ -100,7 +99,6 @@ void CastContentWindowAura::CreateWindowForWebContents(
DCHECK(window_manager_) << "A CastWindowManager must be provided before " DCHECK(window_manager_) << "A CastWindowManager must be provided before "
<< "creating a window for WebContents."; << "creating a window for WebContents.";
CastWebContents::Observer::Observe(cast_web_contents); CastWebContents::Observer::Observe(cast_web_contents);
content::WebContentsObserver::Observe(cast_web_contents->web_contents());
window_ = cast_web_contents->web_contents()->GetNativeView(); window_ = cast_web_contents->web_contents()->GetNativeView();
if (!window_->HasObserver(this)) { if (!window_->HasObserver(this)) {
window_->AddObserver(this); window_->AddObserver(this);
...@@ -133,7 +131,6 @@ void CastContentWindowAura::GrantScreenAccess() { ...@@ -133,7 +131,6 @@ void CastContentWindowAura::GrantScreenAccess() {
void CastContentWindowAura::RevokeScreenAccess() { void CastContentWindowAura::RevokeScreenAccess() {
has_screen_access_ = false; has_screen_access_ = false;
resize_window_when_navigation_starts_ = false;
if (window_) { if (window_) {
window_->Hide(); window_->Hide();
// Because rendering a larger window may require more system resources, // Because rendering a larger window may require more system resources,
...@@ -191,23 +188,4 @@ void CastContentWindowAura::OnWindowDestroyed(aura::Window* window) { ...@@ -191,23 +188,4 @@ void CastContentWindowAura::OnWindowDestroyed(aura::Window* window) {
window_ = nullptr; window_ = nullptr;
} }
void CastContentWindowAura::DidStartNavigation(
content::NavigationHandle* navigation_handle) {
if (!resize_window_when_navigation_starts_) {
return;
}
resize_window_when_navigation_starts_ = false;
// Resize window
gfx::Size display_size =
display::Screen::GetScreen()->GetPrimaryDisplay().size();
aura::Window* content_window = web_contents()->GetNativeView();
content_window->SetBounds(
gfx::Rect(display_size.width(), display_size.height()));
}
void CastContentWindowAura::WebContentsDestroyed() {
content::WebContentsObserver::Observe(nullptr);
}
} // namespace chromecast } // namespace chromecast
...@@ -9,8 +9,6 @@ ...@@ -9,8 +9,6 @@
#include "chromecast/browser/cast_content_gesture_handler.h" #include "chromecast/browser/cast_content_gesture_handler.h"
#include "chromecast/browser/cast_content_window.h" #include "chromecast/browser/cast_content_window.h"
#include "chromecast/ui/media_control_ui.h" #include "chromecast/ui/media_control_ui.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "ui/aura/window_observer.h" #include "ui/aura/window_observer.h"
namespace aura { namespace aura {
...@@ -23,7 +21,6 @@ class TouchBlocker; ...@@ -23,7 +21,6 @@ class TouchBlocker;
class CastContentWindowAura : public CastContentWindow, class CastContentWindowAura : public CastContentWindow,
public CastWebContents::Observer, public CastWebContents::Observer,
private content::WebContentsObserver,
public aura::WindowObserver { public aura::WindowObserver {
public: public:
CastContentWindowAura(const CastContentWindow::CreateParams& params, CastContentWindowAura(const CastContentWindow::CreateParams& params,
...@@ -53,11 +50,6 @@ class CastContentWindowAura : public CastContentWindow, ...@@ -53,11 +50,6 @@ class CastContentWindowAura : public CastContentWindow,
void OnWindowDestroyed(aura::Window* window) override; void OnWindowDestroyed(aura::Window* window) override;
private: private:
// WebContentsObserver implementation:
void DidStartNavigation(
content::NavigationHandle* navigation_handle) override;
void WebContentsDestroyed() override;
CastWindowManager* const window_manager_; CastWindowManager* const window_manager_;
// Utility class for detecting and dispatching gestures to delegates. // Utility class for detecting and dispatching gestures to delegates.
...@@ -71,7 +63,6 @@ class CastContentWindowAura : public CastContentWindow, ...@@ -71,7 +63,6 @@ class CastContentWindowAura : public CastContentWindow,
aura::Window* window_; aura::Window* window_;
bool has_screen_access_; bool has_screen_access_;
bool resize_window_when_navigation_starts_;
DISALLOW_COPY_AND_ASSIGN(CastContentWindowAura); DISALLOW_COPY_AND_ASSIGN(CastContentWindowAura);
}; };
......
...@@ -295,18 +295,6 @@ class CastWebContents { ...@@ -295,18 +295,6 @@ class CastWebContents {
// page. // page.
virtual void Stop(int error_code) = 0; virtual void Stop(int error_code) = 0;
// ===========================================================================
// Visibility
// ===========================================================================
// Specify if the WebContents should be treated as visible. This triggers a
// document "visibilitychange" change event, and will paint the WebContents
// quad if |visible| is true (otherwise it will be blank). Note that this does
// *not* guarantee the page is visible on the screen, as that depends on if
// the WebContents quad is present in the screen layout and isn't obscured by
// another window.
virtual void SetWebVisibilityAndPaint(bool visible) = 0;
// =========================================================================== // ===========================================================================
// Media Management // Media Management
// =========================================================================== // ===========================================================================
......
...@@ -290,21 +290,6 @@ void CastWebContentsImpl::Stop(int error_code) { ...@@ -290,21 +290,6 @@ void CastWebContentsImpl::Stop(int error_code) {
NotifyPageState(); NotifyPageState();
} }
void CastWebContentsImpl::SetWebVisibilityAndPaint(bool visible) {
if (!web_contents_)
return;
if (visible) {
web_contents_->WasShown();
} else {
web_contents_->WasHidden();
}
if (web_contents_->GetVisibility() != content::Visibility::VISIBLE) {
// Since we are managing the visibility, we need to ensure pages are
// unfrozen in the event this occurred while in the background.
web_contents_->SetPageFrozen(false);
}
}
void CastWebContentsImpl::BlockMediaLoading(bool blocked) { void CastWebContentsImpl::BlockMediaLoading(bool blocked) {
if (media_blocker_) if (media_blocker_)
media_blocker_->BlockMediaLoading(blocked); media_blocker_->BlockMediaLoading(blocked);
......
...@@ -59,7 +59,6 @@ class CastWebContentsImpl : public CastWebContents, ...@@ -59,7 +59,6 @@ class CastWebContentsImpl : public CastWebContents,
void LoadUrl(const GURL& url) override; void LoadUrl(const GURL& url) override;
void ClosePage() override; void ClosePage() override;
void Stop(int error_code) override; void Stop(int error_code) override;
void SetWebVisibilityAndPaint(bool visible) override;
void RegisterInterfaceProvider( void RegisterInterfaceProvider(
const InterfaceSet& interface_set, const InterfaceSet& interface_set,
service_manager::InterfaceProvider* interface_provider) override; service_manager::InterfaceProvider* interface_provider) override;
......
...@@ -17,11 +17,11 @@ ...@@ -17,11 +17,11 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "chromecast/browser/cast_web_view_default.h" #include "chromecast/browser/cast_web_view_default.h"
#include "chromecast/browser/cast_web_view_factory.h" #include "chromecast/browser/cast_web_view_factory.h"
#include "chromecast/browser/lru_renderer_cache.h"
#include "chromecast/chromecast_buildflags.h" #include "chromecast/chromecast_buildflags.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/browser/gpu_utils.h" #include "content/public/browser/gpu_utils.h"
#include "content/public/browser/media_session.h" #include "content/public/browser/media_session.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "services/network/public/mojom/cookie_manager.mojom.h" #include "services/network/public/mojom/cookie_manager.mojom.h"
...@@ -46,8 +46,6 @@ CastWebService::CastWebService(content::BrowserContext* browser_context, ...@@ -46,8 +46,6 @@ CastWebService::CastWebService(content::BrowserContext* browser_context,
: browser_context_(browser_context), : browser_context_(browser_context),
web_view_factory_(web_view_factory), web_view_factory_(web_view_factory),
window_manager_(window_manager), window_manager_(window_manager),
overlay_renderer_cache_(
std::make_unique<LRURendererCache>(browser_context_, 1)),
task_runner_(base::SequencedTaskRunnerHandle::Get()), task_runner_(base::SequencedTaskRunnerHandle::Get()),
weak_factory_(this) { weak_factory_(this) {
DCHECK(browser_context_); DCHECK(browser_context_);
...@@ -60,9 +58,26 @@ CastWebService::~CastWebService() = default; ...@@ -60,9 +58,26 @@ CastWebService::~CastWebService() = default;
CastWebView::Scoped CastWebService::CreateWebView( CastWebView::Scoped CastWebService::CreateWebView(
const CastWebView::CreateParams& params, const CastWebView::CreateParams& params,
scoped_refptr<content::SiteInstance> site_instance,
const GURL& initial_url) { const GURL& initial_url) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto web_view = web_view_factory_->CreateWebView(params, this, initial_url); auto web_view = web_view_factory_->CreateWebView(
params, this, std::move(site_instance), initial_url);
CastWebView::Scoped scoped(web_view.get(), [this](CastWebView* web_view) {
OwnerDestroyed(web_view);
});
web_views_.insert(std::move(web_view));
return scoped;
}
CastWebView::Scoped CastWebService::CreateWebView(
const CastWebView::CreateParams& params,
const GURL& initial_url) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto web_view = web_view_factory_->CreateWebView(
params, this,
content::SiteInstance::CreateForURL(browser_context_, initial_url),
initial_url);
CastWebView::Scoped scoped(web_view.get(), [this](CastWebView* web_view) { CastWebView::Scoped scoped(web_view.get(), [this](CastWebView* web_view) {
OwnerDestroyed(web_view); OwnerDestroyed(web_view);
}); });
......
...@@ -28,7 +28,6 @@ namespace chromecast { ...@@ -28,7 +28,6 @@ namespace chromecast {
class CastWebViewFactory; class CastWebViewFactory;
class CastWindowManager; class CastWindowManager;
class LRURendererCache;
// This class dispenses CastWebView objects which are used to wrap WebContents // This class dispenses CastWebView objects which are used to wrap WebContents
// in cast_shell. This class temporarily takes ownership of CastWebViews when // in cast_shell. This class temporarily takes ownership of CastWebViews when
...@@ -42,17 +41,17 @@ class CastWebService { ...@@ -42,17 +41,17 @@ class CastWebService {
CastWindowManager* window_manager); CastWindowManager* window_manager);
~CastWebService(); ~CastWebService();
CastWebView::Scoped CreateWebView(
const CastWebView::CreateParams& params,
scoped_refptr<content::SiteInstance> site_instance,
const GURL& initial_url);
CastWebView::Scoped CreateWebView(const CastWebView::CreateParams& params, CastWebView::Scoped CreateWebView(const CastWebView::CreateParams& params,
const GURL& initial_url); const GURL& initial_url);
std::unique_ptr<CastContentWindow> CreateWindow( std::unique_ptr<CastContentWindow> CreateWindow(
const CastContentWindow::CreateParams& params); const CastContentWindow::CreateParams& params);
content::BrowserContext* browser_context() { return browser_context_; }
LRURendererCache* overlay_renderer_cache() {
return overlay_renderer_cache_.get();
}
void FlushDomLocalStorage(); void FlushDomLocalStorage();
// |callback| is called when data deletion is done or at least the deletion // |callback| is called when data deletion is done or at least the deletion
...@@ -69,8 +68,6 @@ class CastWebService { ...@@ -69,8 +68,6 @@ class CastWebService {
CastWindowManager* const window_manager_; CastWindowManager* const window_manager_;
base::flat_set<std::unique_ptr<CastWebView>> web_views_; base::flat_set<std::unique_ptr<CastWebView>> web_views_;
const std::unique_ptr<LRURendererCache> overlay_renderer_cache_;
const scoped_refptr<base::SequencedTaskRunner> task_runner_; const scoped_refptr<base::SequencedTaskRunner> task_runner_;
base::WeakPtr<CastWebService> weak_ptr_; base::WeakPtr<CastWebService> weak_ptr_;
......
...@@ -13,10 +13,31 @@ CastWebView::Delegate::RunBluetoothChooser( ...@@ -13,10 +13,31 @@ CastWebView::Delegate::RunBluetoothChooser(
return nullptr; return nullptr;
} }
CastWebView::CreateParams::CreateParams() = default; CastWebView::CastWebView(const CreateParams& create_params)
: delegate_(create_params.delegate),
shutdown_delay_(create_params.shutdown_delay) {}
CastWebView::CreateParams::CreateParams(const CreateParams& other) = default; CastWebView::~CastWebView() {
for (Observer& observer : observer_list_) {
observer.OnPageDestroyed(this);
}
}
void CastWebView::ForceClose() {
shutdown_delay_ = base::TimeDelta();
ClosePage();
}
void CastWebView::AddObserver(CastWebView::Observer* observer) {
observer_list_.AddObserver(observer);
}
void CastWebView::RemoveObserver(CastWebView::Observer* observer) {
observer_list_.RemoveObserver(observer);
}
CastWebView::CreateParams::CreateParams() = default;
CastWebView::CreateParams::CreateParams(const CreateParams& other) = default;
CastWebView::CreateParams::~CreateParams() = default; CastWebView::CreateParams::~CreateParams() = default;
} // namespace chromecast } // namespace chromecast
...@@ -9,8 +9,10 @@ ...@@ -9,8 +9,10 @@
#include <string> #include <string>
#include "base/macros.h" #include "base/macros.h"
#include "base/observer_list.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "base/values.h"
#include "chromecast/browser/cast_content_window.h" #include "chromecast/browser/cast_content_window.h"
#include "chromecast/browser/cast_web_contents.h" #include "chromecast/browser/cast_web_contents.h"
#include "chromecast/ui/mojom/ui_service.mojom.h" #include "chromecast/ui/mojom/ui_service.mojom.h"
...@@ -20,8 +22,6 @@ ...@@ -20,8 +22,6 @@
namespace chromecast { namespace chromecast {
class CastWebService;
// A simplified interface for loading and displaying WebContents in cast_shell. // A simplified interface for loading and displaying WebContents in cast_shell.
class CastWebView { class CastWebView {
public: public:
...@@ -55,14 +55,6 @@ class CastWebView { ...@@ -55,14 +55,6 @@ class CastWebView {
using Scoped = using Scoped =
std::unique_ptr<CastWebView, std::function<void(CastWebView*)>>; std::unique_ptr<CastWebView, std::function<void(CastWebView*)>>;
enum class RendererPool {
// Don't use a renderer pool for prelaunching. This means launching the
// render process eagerly is un-restricted and will always succeed.
NONE,
// Pool for overlay apps, which allows up to one pre-cached site.
OVERLAY,
};
// The parameters used to create a CastWebView instance. Passed to // The parameters used to create a CastWebView instance. Passed to
// CastWebService::CreateWebView(). // CastWebService::CreateWebView().
struct CreateParams { struct CreateParams {
...@@ -79,8 +71,6 @@ class CastWebView { ...@@ -79,8 +71,6 @@ class CastWebView {
// Parameters for creating the content window for this CastWebView. // Parameters for creating the content window for this CastWebView.
CastContentWindow::CreateParams window_params; CastContentWindow::CreateParams window_params;
CastWebService* web_service = nullptr;
// Identifies the activity that is hosted by this CastWebView. // Identifies the activity that is hosted by this CastWebView.
std::string activity_id = ""; std::string activity_id = "";
...@@ -105,19 +95,13 @@ class CastWebView { ...@@ -105,19 +95,13 @@ class CastWebView {
// immediately and synchronously. // immediately and synchronously.
base::TimeDelta shutdown_delay = base::TimeDelta(); base::TimeDelta shutdown_delay = base::TimeDelta();
// Pool for pre-launched renderers.
RendererPool renderer_pool = RendererPool::NONE;
// Eagerly pre-launches a render process for |prelaunch_url| if it is valid.
GURL prelaunch_url;
CreateParams(); CreateParams();
CreateParams(const CreateParams& other); CreateParams(const CreateParams& other);
~CreateParams(); ~CreateParams();
}; };
CastWebView() = default; explicit CastWebView(const CreateParams& create_params);
virtual ~CastWebView() = default; virtual ~CastWebView();
virtual CastContentWindow* window() const = 0; virtual CastContentWindow* window() const = 0;
...@@ -125,10 +109,21 @@ class CastWebView { ...@@ -125,10 +109,21 @@ class CastWebView {
virtual CastWebContents* cast_web_contents() = 0; virtual CastWebContents* cast_web_contents() = 0;
virtual base::TimeDelta shutdown_delay() const = 0; base::TimeDelta shutdown_delay() const { return shutdown_delay_; }
// Navigates to |url|. The loaded page will be preloaded if MakeVisible has
// not been called on the object.
virtual void LoadUrl(GURL url) = 0;
// Begins the close process for this page (ie. triggering document.onunload).
// A consumer of the class can be notified when the process has been finished
// via Delegate::OnPageStopped(). The page will be torn down after
// |CreateParams::shutdown_delay| has elapsed, or immediately if the browser
// is shutting down.
virtual void ClosePage() = 0;
// Closes the page immediately, ignoring |CreateParams::shutdown_delay|. // Closes the page immediately, ignoring |CreateParams::shutdown_delay|.
virtual void ForceClose() = 0; void ForceClose();
// Adds the page to the window manager and makes it visible to the user if // Adds the page to the window manager and makes it visible to the user if
// |is_visible| is true. |z_order| determines how this window is layered in // |is_visible| is true. |z_order| determines how this window is layered in
...@@ -136,11 +131,26 @@ class CastWebView { ...@@ -136,11 +131,26 @@ class CastWebView {
virtual void InitializeWindow(mojom::ZOrder z_order, virtual void InitializeWindow(mojom::ZOrder z_order,
VisibilityPriority initial_priority) = 0; VisibilityPriority initial_priority) = 0;
// Allows the page to be shown on the screen. The page cannot be shown on the
// screen until this is called.
virtual void GrantScreenAccess() = 0;
// Prevents the page from being shown on the screen until GrantScreenAccess()
// is called.
virtual void RevokeScreenAccess() = 0;
// Observer interface: // Observer interface:
virtual void AddObserver(Observer* observer) = 0; void AddObserver(Observer* observer);
virtual void RemoveObserver(Observer* observer) = 0; void RemoveObserver(Observer* observer);
protected:
base::WeakPtr<Delegate> delegate_;
private: private:
base::TimeDelta shutdown_delay_;
base::ObserverList<Observer>::Unchecked observer_list_;
DISALLOW_COPY_AND_ASSIGN(CastWebView); DISALLOW_COPY_AND_ASSIGN(CastWebView);
}; };
......
// Copyright 2019 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.
#include "chromecast/browser/cast_web_view_base.h"
#include "chromecast/browser/cast_web_service.h"
#include "chromecast/browser/lru_renderer_cache.h"
#include "chromecast/browser/renderer_prelauncher.h"
#include "content/public/browser/site_instance.h"
namespace chromecast {
CastWebViewBase::CastWebViewBase(const CreateParams& create_params,
CastWebService* web_service)
: delegate_(create_params.delegate),
web_service_(web_service),
shutdown_delay_(create_params.shutdown_delay),
renderer_pool_(create_params.renderer_pool),
prelaunch_url_(create_params.prelaunch_url),
renderer_prelauncher_(nullptr),
site_instance_(nullptr) {
DCHECK(web_service_);
if (prelaunch_url_.is_valid()) {
if (renderer_pool_ == RendererPool::OVERLAY) {
renderer_prelauncher_ =
web_service_->overlay_renderer_cache()->TakeRendererPrelauncher(
prelaunch_url_);
} else {
renderer_prelauncher_ = std::make_unique<RendererPrelauncher>(
web_service_->browser_context(), prelaunch_url_);
}
}
if (renderer_prelauncher_) {
renderer_prelauncher_->Prelaunch();
site_instance_ = renderer_prelauncher_->site_instance();
}
}
CastWebViewBase::~CastWebViewBase() {
if (renderer_prelauncher_ && prelaunch_url_.is_valid() &&
renderer_pool_ == RendererPool::OVERLAY) {
web_service_->overlay_renderer_cache()->ReleaseRendererPrelauncher(
prelaunch_url_);
}
for (Observer& observer : observer_list_) {
observer.OnPageDestroyed(this);
}
}
void CastWebViewBase::ForceClose() {
shutdown_delay_ = base::TimeDelta();
cast_web_contents()->ClosePage();
}
void CastWebViewBase::AddObserver(CastWebViewBase::Observer* observer) {
observer_list_.AddObserver(observer);
}
void CastWebViewBase::RemoveObserver(CastWebViewBase::Observer* observer) {
observer_list_.RemoveObserver(observer);
}
base::TimeDelta CastWebViewBase::shutdown_delay() const {
return shutdown_delay_;
}
} // namespace chromecast
// Copyright 2019 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_VIEW_BASE_H_
#define CHROMECAST_BROWSER_CAST_WEB_VIEW_BASE_H_
#include <cstdint>
#include <memory>
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "chromecast/browser/cast_web_view.h"
#include "url/gurl.h"
namespace content {
class SiteInstance;
} // namespace content
namespace chromecast {
class CastWebService;
class RendererPrelauncher;
// Common logic for CastWebView implementations.
class CastWebViewBase : public CastWebView {
public:
CastWebViewBase(const CreateParams& create_params,
CastWebService* web_service);
CastWebViewBase(const CastWebViewBase&) = delete;
CastWebViewBase& operator=(const CastWebViewBase&) = delete;
~CastWebViewBase() override;
scoped_refptr<content::SiteInstance> site_instance() const {
return site_instance_;
}
// CastWebView implementation (partial):
void ForceClose() override;
void AddObserver(Observer* observer) override;
void RemoveObserver(Observer* observer) override;
base::TimeDelta shutdown_delay() const override;
protected:
base::WeakPtr<Delegate> delegate_;
CastWebService* const web_service_;
private:
base::TimeDelta shutdown_delay_;
const RendererPool renderer_pool_;
const GURL prelaunch_url_;
std::unique_ptr<RendererPrelauncher> renderer_prelauncher_;
scoped_refptr<content::SiteInstance> site_instance_;
base::ObserverList<Observer>::Unchecked observer_list_;
};
} // namespace chromecast
#endif // CHROMECAST_BROWSER_CAST_WEB_VIEW_BASE_H_
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "chromecast/chromecast_buildflags.h" #include "chromecast/chromecast_buildflags.h"
#include "content/public/browser/media_capture_devices.h" #include "content/public/browser/media_capture_devices.h"
#include "content/public/browser/media_session.h" #include "content/public/browser/media_session.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h" #include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h" #include "content/public/browser/render_widget_host.h"
...@@ -53,23 +54,27 @@ CastWebViewDefault::CastWebViewDefault( ...@@ -53,23 +54,27 @@ CastWebViewDefault::CastWebViewDefault(
const CreateParams& params, const CreateParams& params,
CastWebService* web_service, CastWebService* web_service,
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
scoped_refptr<content::SiteInstance> site_instance,
std::unique_ptr<CastContentWindow> cast_content_window) std::unique_ptr<CastContentWindow> cast_content_window)
: CastWebViewBase(params, web_service), : CastWebView(params),
web_service_(web_service), web_service_(web_service),
browser_context_(browser_context), browser_context_(browser_context),
site_instance_(std::move(site_instance)),
activity_id_(params.activity_id), activity_id_(params.activity_id),
session_id_(params.window_params.session_id), session_id_(params.window_params.session_id),
sdk_version_(params.sdk_version), sdk_version_(params.sdk_version),
allow_media_access_(params.allow_media_access), allow_media_access_(params.allow_media_access),
log_prefix_(params.log_prefix), log_prefix_(params.log_prefix),
web_contents_(CreateWebContents(browser_context_, site_instance())), web_contents_(CreateWebContents(browser_context_, site_instance_)),
cast_web_contents_(web_contents_.get(), params.web_contents_params), cast_web_contents_(web_contents_.get(), params.web_contents_params),
window_(cast_content_window window_(cast_content_window
? std::move(cast_content_window) ? std::move(cast_content_window)
: web_service->CreateWindow(params.window_params)) { : web_service->CreateWindow(params.window_params)),
resize_window_when_navigation_starts_(true) {
DCHECK(web_service_); DCHECK(web_service_);
DCHECK(browser_context_); DCHECK(browser_context_);
DCHECK(window_); DCHECK(window_);
content::WebContentsObserver::Observe(web_contents_.get());
web_contents_->SetDelegate(this); web_contents_->SetDelegate(this);
#if defined(USE_AURA) #if defined(USE_AURA)
...@@ -99,6 +104,15 @@ CastWebContents* CastWebViewDefault::cast_web_contents() { ...@@ -99,6 +104,15 @@ CastWebContents* CastWebViewDefault::cast_web_contents() {
return &cast_web_contents_; return &cast_web_contents_;
} }
void CastWebViewDefault::LoadUrl(GURL url) {
cast_web_contents_.LoadUrl(url);
}
void CastWebViewDefault::ClosePage() {
content::WebContentsObserver::Observe(nullptr);
cast_web_contents_.ClosePage();
}
void CastWebViewDefault::CloseContents(content::WebContents* source) { void CastWebViewDefault::CloseContents(content::WebContents* source) {
DCHECK_EQ(source, web_contents_.get()); DCHECK_EQ(source, web_contents_.get());
window_.reset(); // Window destructor requires live web_contents on Android. window_.reset(); // Window destructor requires live web_contents on Android.
...@@ -116,6 +130,19 @@ void CastWebViewDefault::InitializeWindow(mojom::ZOrder z_order, ...@@ -116,6 +130,19 @@ void CastWebViewDefault::InitializeWindow(mojom::ZOrder z_order,
web_contents_->Focus(); web_contents_->Focus();
} }
void CastWebViewDefault::GrantScreenAccess() {
if (!window_)
return;
window_->GrantScreenAccess();
}
void CastWebViewDefault::RevokeScreenAccess() {
resize_window_when_navigation_starts_ = false;
if (!window_)
return;
window_->RevokeScreenAccess();
}
content::WebContents* CastWebViewDefault::OpenURLFromTab( content::WebContents* CastWebViewDefault::OpenURLFromTab(
content::WebContents* source, content::WebContents* source,
const content::OpenURLParams& params) { const content::OpenURLParams& params) {
...@@ -254,4 +281,21 @@ bool CastWebViewDefault::ShouldAllowRunningInsecureContent( ...@@ -254,4 +281,21 @@ bool CastWebViewDefault::ShouldAllowRunningInsecureContent(
return allowed_per_prefs; return allowed_per_prefs;
} }
void CastWebViewDefault::DidStartNavigation(
content::NavigationHandle* navigation_handle) {
if (!resize_window_when_navigation_starts_) {
return;
}
resize_window_when_navigation_starts_ = false;
#if defined(USE_AURA)
// Resize window
gfx::Size display_size =
display::Screen::GetScreen()->GetPrimaryDisplay().size();
aura::Window* content_window = web_contents()->GetNativeView();
content_window->SetBounds(
gfx::Rect(display_size.width(), display_size.height()));
#endif
}
} // namespace chromecast } // namespace chromecast
...@@ -13,9 +13,10 @@ ...@@ -13,9 +13,10 @@
#include "build/build_config.h" #include "build/build_config.h"
#include "chromecast/browser/cast_content_window.h" #include "chromecast/browser/cast_content_window.h"
#include "chromecast/browser/cast_web_contents_impl.h" #include "chromecast/browser/cast_web_contents_impl.h"
#include "chromecast/browser/cast_web_view_base.h" #include "chromecast/browser/cast_web_view.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h" #include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
namespace content { namespace content {
class BrowserContext; class BrowserContext;
...@@ -27,8 +28,9 @@ namespace chromecast { ...@@ -27,8 +28,9 @@ namespace chromecast {
class CastWebService; class CastWebService;
// A simplified interface for loading and displaying WebContents in cast_shell. // A simplified interface for loading and displaying WebContents in cast_shell.
class CastWebViewDefault : public CastWebViewBase, class CastWebViewDefault : public CastWebView,
private content::WebContentsDelegate { content::WebContentsObserver,
content::WebContentsDelegate {
public: public:
// |web_service| and |browser_context| should outlive this object. If // |web_service| and |browser_context| should outlive this object. If
// |cast_content_window| is not provided, an instance will be constructed from // |cast_content_window| is not provided, an instance will be constructed from
...@@ -37,6 +39,7 @@ class CastWebViewDefault : public CastWebViewBase, ...@@ -37,6 +39,7 @@ class CastWebViewDefault : public CastWebViewBase,
const CreateParams& params, const CreateParams& params,
CastWebService* web_service, CastWebService* web_service,
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
scoped_refptr<content::SiteInstance> site_instance,
std::unique_ptr<CastContentWindow> cast_content_window = nullptr); std::unique_ptr<CastContentWindow> cast_content_window = nullptr);
~CastWebViewDefault() override; ~CastWebViewDefault() override;
...@@ -44,10 +47,18 @@ class CastWebViewDefault : public CastWebViewBase, ...@@ -44,10 +47,18 @@ class CastWebViewDefault : public CastWebViewBase,
CastContentWindow* window() const override; CastContentWindow* window() const override;
content::WebContents* web_contents() const override; content::WebContents* web_contents() const override;
CastWebContents* cast_web_contents() override; CastWebContents* cast_web_contents() override;
void LoadUrl(GURL url) override;
void ClosePage() override;
void InitializeWindow(mojom::ZOrder z_order, void InitializeWindow(mojom::ZOrder z_order,
VisibilityPriority initial_priority) override; VisibilityPriority initial_priority) override;
void GrantScreenAccess() override;
void RevokeScreenAccess() override;
private: private:
// WebContentsObserver implementation:
void DidStartNavigation(
content::NavigationHandle* navigation_handle) override;
// WebContentsDelegate implementation: // WebContentsDelegate implementation:
content::WebContents* OpenURLFromTab( content::WebContents* OpenURLFromTab(
content::WebContents* source, content::WebContents* source,
...@@ -87,6 +98,7 @@ class CastWebViewDefault : public CastWebViewBase, ...@@ -87,6 +98,7 @@ class CastWebViewDefault : public CastWebViewBase,
std::unique_ptr<content::WebContents> web_contents_; std::unique_ptr<content::WebContents> web_contents_;
CastWebContentsImpl cast_web_contents_; CastWebContentsImpl cast_web_contents_;
std::unique_ptr<CastContentWindow> window_; std::unique_ptr<CastContentWindow> window_;
bool resize_window_when_navigation_starts_;
DISALLOW_COPY_AND_ASSIGN(CastWebViewDefault); DISALLOW_COPY_AND_ASSIGN(CastWebViewDefault);
}; };
......
...@@ -23,10 +23,11 @@ void CastWebViewFactory::OnPageDestroyed(CastWebView* web_view) { ...@@ -23,10 +23,11 @@ void CastWebViewFactory::OnPageDestroyed(CastWebView* web_view) {
std::unique_ptr<CastWebView> CastWebViewFactory::CreateWebView( std::unique_ptr<CastWebView> CastWebViewFactory::CreateWebView(
const CastWebView::CreateParams& params, const CastWebView::CreateParams& params,
CastWebService* web_service, CastWebService* web_service,
scoped_refptr<content::SiteInstance> site_instance,
const GURL& initial_url) { const GURL& initial_url) {
std::unique_ptr<CastWebView> webview; std::unique_ptr<CastWebView> webview;
webview = std::make_unique<CastWebViewDefault>(params, web_service, webview = std::make_unique<CastWebViewDefault>(
browser_context_); params, web_service, browser_context_, site_instance);
if (webview) { if (webview) {
webview->AddObserver(this); webview->AddObserver(this);
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
namespace content { namespace content {
class BrowserContext; class BrowserContext;
class SiteInstance;
} // namespace content } // namespace content
namespace chromecast { namespace chromecast {
...@@ -30,6 +31,7 @@ class CastWebViewFactory : public CastWebView::Observer { ...@@ -30,6 +31,7 @@ class CastWebViewFactory : public CastWebView::Observer {
virtual std::unique_ptr<CastWebView> CreateWebView( virtual std::unique_ptr<CastWebView> CreateWebView(
const CastWebView::CreateParams& params, const CastWebView::CreateParams& params,
CastWebService* web_service, CastWebService* web_service,
scoped_refptr<content::SiteInstance> site_instance,
const GURL& initial_url); const GURL& initial_url);
content::BrowserContext* browser_context() const { return browser_context_; } content::BrowserContext* browser_context() const { return browser_context_; }
......
...@@ -72,9 +72,10 @@ void CastServiceSimple::StartInternal() { ...@@ -72,9 +72,10 @@ void CastServiceSimple::StartInternal() {
params.web_contents_params.enabled_for_dev = true; params.web_contents_params.enabled_for_dev = true;
params.window_params.delegate = weak_factory_.GetWeakPtr(); params.window_params.delegate = weak_factory_.GetWeakPtr();
cast_web_view_ = cast_web_view_ =
web_service_->CreateWebView(params, GURL() /* initial_url */); web_service_->CreateWebView(params, nullptr, /* site_instance */
cast_web_view_->cast_web_contents()->LoadUrl(startup_url_); GURL() /* initial_url */);
cast_web_view_->window()->GrantScreenAccess(); cast_web_view_->LoadUrl(startup_url_);
cast_web_view_->GrantScreenAccess();
cast_web_view_->InitializeWindow( cast_web_view_->InitializeWindow(
::chromecast::mojom::ZOrder::APP, ::chromecast::mojom::ZOrder::APP,
chromecast::VisibilityPriority::STICKY_ACTIVITY); chromecast::VisibilityPriority::STICKY_ACTIVITY);
...@@ -82,7 +83,7 @@ void CastServiceSimple::StartInternal() { ...@@ -82,7 +83,7 @@ void CastServiceSimple::StartInternal() {
void CastServiceSimple::StopInternal() { void CastServiceSimple::StopInternal() {
if (cast_web_view_) { if (cast_web_view_) {
cast_web_view_->cast_web_contents()->ClosePage(); cast_web_view_->ClosePage();
} }
cast_web_view_.reset(); cast_web_view_.reset();
} }
......
...@@ -64,7 +64,8 @@ content::WebContents* CastBrowserTest::CreateWebView() { ...@@ -64,7 +64,8 @@ content::WebContents* CastBrowserTest::CreateWebView() {
params.web_contents_params.enabled_for_dev = true; params.web_contents_params.enabled_for_dev = true;
params.window_params.delegate = weak_factory_.GetWeakPtr(); params.window_params.delegate = weak_factory_.GetWeakPtr();
cast_web_view_ = cast_web_view_ =
web_service_->CreateWebView(params, GURL() /* initial_url */); web_service_->CreateWebView(params, nullptr, /* site_instance */
GURL() /* initial_url */);
return cast_web_view_->web_contents(); return cast_web_view_->web_contents();
} }
...@@ -76,7 +77,7 @@ content::WebContents* CastBrowserTest::NavigateToURL(const GURL& url) { ...@@ -76,7 +77,7 @@ content::WebContents* CastBrowserTest::NavigateToURL(const GURL& url) {
content::WaitForLoadStop(web_contents); content::WaitForLoadStop(web_contents);
content::TestNavigationObserver same_tab_observer(web_contents, 1); content::TestNavigationObserver same_tab_observer(web_contents, 1);
cast_web_view_->cast_web_contents()->LoadUrl(url); cast_web_view_->LoadUrl(url);
same_tab_observer.Wait(); same_tab_observer.Wait();
......
...@@ -35,8 +35,6 @@ class CastBrowserTest : public content::BrowserTestBase, ...@@ -35,8 +35,6 @@ class CastBrowserTest : public content::BrowserTestBase,
CastBrowserTest(); CastBrowserTest();
~CastBrowserTest() override; ~CastBrowserTest() override;
CastWebView* cast_web_view() const { return cast_web_view_.get(); }
// content::BrowserTestBase implementation: // content::BrowserTestBase implementation:
void SetUp() final; void SetUp() final;
void SetUpCommandLine(base::CommandLine* command_line) override; void SetUpCommandLine(base::CommandLine* command_line) override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment