1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/cast_receiver/browser/runtime_application_base.h"
#include <algorithm>
#include "base/notreached.h"
#include "base/task/sequenced_task_runner.h"
#include "components/cast_receiver/browser/permissions_manager_impl.h"
#include "components/media_control/browser/media_blocker.h"
#include "components/url_rewrite/browser/url_request_rewrite_rules_manager.h"
#include "content/public/browser/web_contents.h"
namespace cast_receiver {
RuntimeApplicationBase::RuntimeApplicationBase(
std::string cast_session_id,
ApplicationConfig app_config,
ApplicationClient& application_client)
: cast_session_id_(std::move(cast_session_id)),
app_config_(std::move(app_config)),
task_runner_(base::SequencedTaskRunner::GetCurrentDefault()),
application_client_(application_client) {
DCHECK(task_runner_);
}
RuntimeApplicationBase::~RuntimeApplicationBase() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(!is_application_running_);
}
void RuntimeApplicationBase::SetEmbedderApplication(
EmbedderApplication& embedder_application) {
DCHECK(!embedder_application_);
embedder_application_ = &embedder_application;
}
const std::string& RuntimeApplicationBase::GetDisplayName() const {
return config().display_name;
}
const std::string& RuntimeApplicationBase::GetAppId() const {
return config().app_id;
}
const std::string& RuntimeApplicationBase::GetCastSessionId() const {
return cast_session_id_;
}
void RuntimeApplicationBase::Load(StatusCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(embedder_application().GetWebContents());
is_application_running_ = true;
if (cached_mojom_rules_) {
// Apply cached URL rewrite rules before anything is done with the page.
SetUrlRewriteRules(std::move(cached_mojom_rules_));
}
DLOG(INFO) << "Loaded application: " << *this;
std::move(callback).Run(OkStatus());
}
void RuntimeApplicationBase::Stop(StatusCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
StopApplication(EmbedderApplication::ApplicationStopReason::kUserRequest,
net::ERR_ABORTED);
std::move(callback).Run(OkStatus());
}
ApplicationClient::ApplicationControls&
RuntimeApplicationBase::GetApplicationControls() {
DCHECK(embedder_application().GetWebContents());
return application_client_->GetApplicationControls(
*embedder_application().GetWebContents());
}
void RuntimeApplicationBase::NavigateToPage(const GURL& url) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto* window_controls = embedder_application().GetContentWindowControls();
DCHECK(window_controls);
window_controls->AddVisibilityChangeObserver(*this);
embedder_application().NavigateToPage(url);
SetWebVisibilityAndPaint(is_visible_);
}
void RuntimeApplicationBase::SetContentPermissions(
content::WebContents& web_contents) {
PermissionsManagerImpl* permissions_manager =
PermissionsManagerImpl::CreateInstance(web_contents, GetAppId());
if (config().url.has_value()) {
auto app_url_origin = url::Origin::Create(config().url.value());
if (!app_url_origin.opaque()) {
permissions_manager->AddOrigin(app_url_origin);
}
}
for (blink::PermissionType permission : config().permissions.permissions) {
permissions_manager->AddPermission(permission);
}
for (auto& origin : config().permissions.additional_origins) {
DCHECK(!origin.opaque());
permissions_manager->AddOrigin(origin);
}
}
void RuntimeApplicationBase::OnPageNavigationComplete() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DLOG(INFO) << "Page loaded: " << *this;
embedder_application().NotifyApplicationStarted();
SetWebVisibilityAndPaint(is_visible_);
SetTouchInputEnabled(is_touch_input_enabled_);
SetMediaBlocking(is_media_load_blocked_, is_media_start_blocked_);
}
void RuntimeApplicationBase::SetUrlRewriteRules(
url_rewrite::mojom::UrlRequestRewriteRulesPtr mojom_rules) {
if (!embedder_application().GetWebContents()) {
cached_mojom_rules_ = std::move(mojom_rules);
return;
}
url_rewrite::UrlRequestRewriteRulesManager&
url_request_rewrite_rules_manager =
GetApplicationControls().GetUrlRequestRewriteRulesManager();
if (!url_request_rewrite_rules_manager.OnRulesUpdated(
std::move(mojom_rules))) {
LOG(ERROR) << "URL rewrite rules update failed.";
StopApplication(EmbedderApplication::ApplicationStopReason::kRuntimeError,
net::Error::ERR_UNEXPECTED);
}
}
void RuntimeApplicationBase::SetMediaBlocking(bool load_blocked,
bool start_blocked) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
is_media_load_blocked_ = load_blocked;
is_media_start_blocked_ = start_blocked;
DLOG(INFO) << "Media state updated: is_load_blocked=" << load_blocked
<< ", is_start_blocked=" << start_blocked << ", " << *this;
if (!embedder_application().GetWebContents()) {
return;
}
media_control::MediaBlocker& media_blocker =
GetApplicationControls().GetMediaBlocker();
media_blocker.BlockMediaLoading(is_media_load_blocked_);
// TODO(crbug.com/1359584): Block media starting.
}
void RuntimeApplicationBase::SetVisibility(bool is_visible) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
is_visible_ = is_visible;
DLOG(INFO) << "Visibility updated: is_visible_=" << is_visible_ << ", "
<< *this;
auto* window_controls = embedder_application().GetContentWindowControls();
if (!window_controls) {
return;
}
if (is_visible_) {
window_controls->ShowWindow();
} else {
window_controls->HideWindow();
}
}
void RuntimeApplicationBase::SetTouchInputEnabled(bool enabled) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
is_touch_input_enabled_ = enabled;
DLOG(INFO) << "Touch input updated: is_touch_input_enabled_= "
<< is_touch_input_enabled_ << ", " << *this;
auto* window_controls = embedder_application().GetContentWindowControls();
if (!window_controls) {
return;
}
if (is_touch_input_enabled_) {
window_controls->EnableTouchInput();
} else {
window_controls->DisableTouchInput();
}
}
bool RuntimeApplicationBase::IsApplicationRunning() const {
return is_application_running_;
}
void RuntimeApplicationBase::StopApplication(
EmbedderApplication::ApplicationStopReason stop_reason,
net::Error net_error_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!is_application_running_) {
return;
}
is_application_running_ = false;
auto* web_contents = embedder_application().GetWebContents();
if (web_contents) {
web_contents->DispatchBeforeUnload(false /* auto_cancel */);
web_contents->ClosePage();
// Check if window is still available as page might have been closed before.
auto* window_controls = embedder_application().GetContentWindowControls();
if (window_controls) {
window_controls->RemoveVisibilityChangeObserver(*this);
}
}
embedder_application().NotifyApplicationStopped(stop_reason, net_error_code);
DLOG(INFO) << "Application is stopped: stop_reason=" << stop_reason << ", "
<< *this;
}
void RuntimeApplicationBase::SetWebVisibilityAndPaint(bool is_visible) {
auto* web_contents = embedder_application().GetWebContents();
if (!web_contents) {
return;
}
if (is_visible) {
web_contents->WasShown();
} else {
// NOTE: Calling WasHidden() and later WasShown() does not behave properly
// on some platforms (e.g. Linux devices using X11 platform for Ozone). In
// such cases, the WasShown() call will execute, and the browser-side code
// associated with this call will run, but it will never reach the Renderer
// process, so the LayerTreeHost will never draw the surface assocaited with
// this WebContents.
DLOG(WARNING)
<< "WebContents hidden. NOTE: Changing from hidden to visible does not "
"work in all cases, and such calls may not be respected.";
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 RuntimeApplicationBase::OnWindowShown() {
SetWebVisibilityAndPaint(true);
}
void RuntimeApplicationBase::OnWindowHidden() {
SetWebVisibilityAndPaint(false);
}
} // namespace cast_receiver
|