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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/web_view/ash_web_view_impl.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/public/cpp/window_properties.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/ash/new_window/chrome_new_window_client.h"
#include "content/public/browser/focused_node_details.h"
#include "content/public/browser/host_zoom_map.h"
#include "content/public/browser/media_session.h"
#include "content/public/browser/page.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "third_party/blink/public/common/renderer_preferences/renderer_preferences.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h"
#include "ui/aura/window.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/views/controls/webview/web_contents_set_background_color.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
namespace {
void FixZoomLevelToOne(content::RenderFrameHost* render_frame_host) {
content::HostZoomMap* zoom_map =
content::HostZoomMap::Get(render_frame_host->GetSiteInstance());
zoom_map->SetTemporaryZoomLevel(render_frame_host->GetGlobalId(), 1.0);
}
} // namespace
AshWebViewImpl::AshWebViewImpl(const InitParams& params) : params_(params) {
Profile* profile = ProfileManager::GetActiveUserProfile();
InitWebContents(profile);
InitLayout(profile);
}
AshWebViewImpl::~AshWebViewImpl() {
Observe(nullptr);
web_contents_->SetDelegate(nullptr);
}
gfx::NativeView AshWebViewImpl::GetNativeView() {
return web_contents_->GetNativeView();
}
void AshWebViewImpl::ChildPreferredSizeChanged(views::View* child) {
DCHECK_EQ(web_view_, child);
SetPreferredSize(web_view_->GetPreferredSize());
}
void AshWebViewImpl::Layout(PassKey) {
web_view_->SetBoundsRect(GetContentsBounds());
}
void AshWebViewImpl::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void AshWebViewImpl::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
bool AshWebViewImpl::GoBack() {
if (web_contents_->GetController().CanGoBack()) {
web_contents_->GetController().GoBack();
return true;
}
return false;
}
void AshWebViewImpl::Navigate(const GURL& url) {
content::NavigationController::LoadURLParams params(url);
web_contents_->GetController().LoadURLWithParams(params);
}
const GURL& AshWebViewImpl::GetVisibleURL() {
return web_contents_->GetVisibleURL();
}
bool AshWebViewImpl::IsErrorDocument() {
return web_contents_->GetPrimaryMainFrame()->IsErrorDocument();
}
views::View* AshWebViewImpl::GetInitiallyFocusedView() {
return web_view_;
}
void AshWebViewImpl::SetCornerRadii(const gfx::RoundedCornersF& corner_radii) {
web_view_->holder()->SetCornerRadii(corner_radii);
}
const base::UnguessableToken& AshWebViewImpl::GetMediaSessionRequestId() {
return content::MediaSession::GetRequestIdFromWebContents(
web_contents_.get());
}
void AshWebViewImpl::AddedToWidget() {
UpdateMinimizeOnBackProperty();
AshWebView::AddedToWidget();
// Apply rounded corners. This can't be done earlier since it
// requires `web_view_->holder()->native_view()` to be initialized.
if (params_.rounded_corners.has_value()) {
web_view_->holder()->SetCornerRadii(params_.rounded_corners.value());
}
}
bool AshWebViewImpl::IsWebContentsCreationOverridden(
content::RenderFrameHost* opener,
content::SiteInstance* source_site_instance,
content::mojom::WindowContainerType window_container_type,
const GURL& opener_url,
const std::string& frame_name,
const GURL& target_url) {
if (params_.suppress_navigation) {
NotifyDidSuppressNavigation(target_url,
WindowOpenDisposition::NEW_FOREGROUND_TAB,
/*from_user_gesture=*/true);
return true;
}
return content::WebContentsDelegate::IsWebContentsCreationOverridden(
opener, source_site_instance, window_container_type, opener_url,
frame_name, target_url);
}
content::WebContents* AshWebViewImpl::OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params,
base::OnceCallback<void(content::NavigationHandle&)>
navigation_handle_callback) {
if (params_.suppress_navigation) {
NotifyDidSuppressNavigation(params.url, params.disposition,
params.user_gesture);
return nullptr;
}
return content::WebContentsDelegate::OpenURLFromTab(
source, params, std::move(navigation_handle_callback));
}
void AshWebViewImpl::ActivateContents(content::WebContents* contents) {
// In cases where the widget is not activatable, an `activation_url` may be
// provided to show instead.
// This is currently used for Focus Mode YTM for when the user clicks on the
// media controls view. Since the media window is a custom hidden window, a
// separately provided URL tab (navigating to `activation_url`) is shown when
// the hidden media window is activated.
if (!GetWidget()->CanActivate() && !params_.activation_url.is_empty()) {
ChromeNewWindowClient::Get()->OpenUrl(
params_.activation_url,
ash::NewWindowDelegate::OpenUrlFrom::kUserInteraction,
ash::NewWindowDelegate::Disposition::kSwitchToTab);
return;
}
content::WebContentsDelegate::ActivateContents(contents);
}
void AshWebViewImpl::ResizeDueToAutoResize(content::WebContents* web_contents,
const gfx::Size& new_size) {
DCHECK_EQ(web_contents_.get(), web_contents);
web_view_->SetPreferredSize(new_size);
}
bool AshWebViewImpl::TakeFocus(content::WebContents* web_contents,
bool reverse) {
DCHECK_EQ(web_contents_.get(), web_contents);
auto* focus_manager = GetFocusManager();
if (focus_manager) {
focus_manager->ClearNativeFocus();
}
return false;
}
void AshWebViewImpl::NavigationStateChanged(
content::WebContents* web_contents,
content::InvalidateTypes changed_flags) {
DCHECK_EQ(web_contents_.get(), web_contents);
UpdateCanGoBack();
}
void AshWebViewImpl::RequestMediaAccessPermission(
content::WebContents* web_contents,
const content::MediaStreamRequest& request,
content::MediaResponseCallback callback) {
if (!params_.can_record_media) {
std::move(callback).Run(
blink::mojom::StreamDevicesSet(),
blink::mojom::MediaStreamRequestResult::NOT_SUPPORTED,
std::unique_ptr<content::MediaStreamUI>());
return;
}
MediaCaptureDevicesDispatcher::GetInstance()->ProcessMediaAccessRequest(
web_contents, request, std::move(callback), /*extension=*/nullptr);
}
bool AshWebViewImpl::CheckMediaAccessPermission(
content::RenderFrameHost* render_frame_host,
const url::Origin& security_origin,
blink::mojom::MediaStreamType type) {
if (!params_.can_record_media) {
return false;
}
return MediaCaptureDevicesDispatcher::GetInstance()
->CheckMediaAccessPermission(render_frame_host, security_origin, type);
}
std::string AshWebViewImpl::GetTitleForMediaControls(
content::WebContents* web_contents) {
if (!params_.source_title.empty()) {
return params_.source_title;
}
return content::WebContentsDelegate::GetTitleForMediaControls(web_contents);
}
void AshWebViewImpl::DidStopLoading() {
for (auto& observer : observers_) {
observer.DidStopLoading();
}
}
void AshWebViewImpl::OnFocusChangedInPage(
content::FocusedNodeDetails* details) {
// When navigating to the |web_contents_|, it may not focus it. Request focus
// as needed. This is a workaround to get a non-empty rect of the focused
// node. See details in b/177047240.
auto* native_view = web_contents_->GetContentNativeView();
if (native_view && !native_view->HasFocus()) {
web_contents_->Focus();
}
for (auto& observer : observers_) {
observer.DidChangeFocusedNode(details->node_bounds_in_screen);
}
}
void AshWebViewImpl::RenderFrameHostChanged(
content::RenderFrameHost* old_host,
content::RenderFrameHost* new_host) {
if (new_host != new_host->GetOutermostMainFrame()) {
return;
}
if (params_.fix_zoom_level_to_one) {
FixZoomLevelToOne(new_host);
}
}
void AshWebViewImpl::PrimaryPageChanged(content::Page& page) {
DCHECK_EQ(&page.GetMainDocument(), web_contents_->GetPrimaryMainFrame());
if (!web_contents_->GetRenderWidgetHostView()) {
return;
}
if (!params_.enable_auto_resize) {
return;
}
gfx::Size min_size(1, 1);
if (params_.min_size) {
min_size.SetToMax(params_.min_size.value());
}
gfx::Size max_size(INT_MAX, INT_MAX);
if (params_.max_size) {
max_size.SetToMin(params_.max_size.value());
}
web_contents_->GetRenderWidgetHostView()->EnableAutoResize(min_size,
max_size);
}
void AshWebViewImpl::NavigationEntriesDeleted() {
UpdateCanGoBack();
}
void AshWebViewImpl::InitWebContents(Profile* profile) {
auto web_contents_params = content::WebContents::CreateParams(
profile, content::SiteInstance::Create(profile));
web_contents_params.enable_wake_locks = params_.enable_wake_locks;
web_contents_ = content::WebContents::Create(web_contents_params);
web_contents_->SetDelegate(this);
Observe(web_contents_.get());
// Use a transparent background.
views::WebContentsSetBackgroundColor::CreateForWebContentsWithColor(
web_contents_.get(), SK_ColorTRANSPARENT);
// If requested, suppress navigation.
if (params_.suppress_navigation) {
web_contents_->GetMutableRendererPrefs()
->browser_handles_all_top_level_requests = true;
web_contents_->SyncRendererPrefs();
}
if (params_.fix_zoom_level_to_one) {
FixZoomLevelToOne(web_contents_->GetPrimaryMainFrame());
}
}
void AshWebViewImpl::InitLayout(Profile* profile) {
web_view_ = AddChildView(std::make_unique<views::WebView>(profile));
web_view_->SetID(ash::kAshWebViewChildWebViewId);
web_view_->SetWebContents(web_contents_.get());
}
void AshWebViewImpl::NotifyDidSuppressNavigation(
const GURL& url,
WindowOpenDisposition disposition,
bool from_user_gesture) {
// Note that we post notification to |observers_| as an observer may cause
// |this| to be deleted during handling of the event which is unsafe to do
// until the original navigation sequence has been completed.
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(
[](const base::WeakPtr<AshWebViewImpl>& self, GURL url,
WindowOpenDisposition disposition, bool from_user_gesture) {
if (self) {
for (auto& observer : self->observers_) {
observer.DidSuppressNavigation(url, disposition,
from_user_gesture);
// We need to check |self| to confirm that |observer| did not
// delete |this|. If |this| is deleted, we quit.
if (!self) {
return;
}
}
}
},
weak_factory_.GetWeakPtr(), url, disposition, from_user_gesture));
}
void AshWebViewImpl::UpdateCanGoBack() {
const bool can_go_back = web_contents_->GetController().CanGoBack();
if (can_go_back_ == can_go_back) {
return;
}
can_go_back_ = can_go_back;
UpdateMinimizeOnBackProperty();
for (auto& observer : observers_) {
observer.DidChangeCanGoBack(can_go_back_);
}
}
void AshWebViewImpl::UpdateMinimizeOnBackProperty() {
const bool minimize_on_back = params_.minimize_on_back_key && !can_go_back_;
views::Widget* widget = GetWidget();
if (widget) {
widget->GetNativeWindow()->SetProperty(ash::kMinimizeOnBackKey,
minimize_on_back);
}
}
BEGIN_METADATA(AshWebViewImpl)
END_METADATA
|