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
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/screen_orientation/screen_orientation_provider.h"
#include <utility>
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/screen_orientation_delegate.h"
#include "content/public/browser/web_contents.h"
namespace content {
using device::mojom::ScreenOrientationLockResult;
ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr;
ScreenOrientationProvider::ScreenOrientationProvider(WebContents* web_contents)
: WebContentsObserver(web_contents),
lock_applied_(false),
receivers_(web_contents, this) {}
ScreenOrientationProvider::~ScreenOrientationProvider() = default;
void ScreenOrientationProvider::BindScreenOrientation(
RenderFrameHost* rfh,
mojo::PendingAssociatedReceiver<device::mojom::ScreenOrientation>
receiver) {
receivers_.Bind(rfh, std::move(receiver));
}
bool ScreenOrientationProvider::IsOrientationLockSupported() const {
return delegate_ &&
delegate_->ScreenOrientationProviderSupported(web_contents());
}
void ScreenOrientationProvider::LockOrientation(
device::mojom::ScreenOrientationLockType orientation,
LockOrientationCallback callback) {
// Cancel any pending lock request.
NotifyLockResult(ScreenOrientationLockResult::
SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
// Record new pending lock request.
pending_callback_ = std::move(callback);
if (!IsOrientationLockSupported()) {
NotifyLockResult(ScreenOrientationLockResult::
SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE);
return;
}
if (delegate_->FullScreenRequired(web_contents())) {
RenderViewHostImpl* rvhi = static_cast<RenderViewHostImpl*>(
web_contents()->GetPrimaryMainFrame()->GetRenderViewHost());
if (!rvhi) {
NotifyLockResult(ScreenOrientationLockResult::
SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
return;
}
if (!static_cast<WebContentsImpl*>(web_contents())->IsFullscreen() &&
static_cast<WebContentsImpl*>(web_contents())->GetDisplayMode() !=
blink::mojom::DisplayMode::kFullscreen) {
NotifyLockResult(
ScreenOrientationLockResult::
SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED);
return;
}
}
if (orientation == device::mojom::ScreenOrientationLockType::NATURAL) {
orientation = GetNaturalLockType();
if (orientation == device::mojom::ScreenOrientationLockType::DEFAULT) {
// We are in a broken state, let's pretend we got canceled.
NotifyLockResult(ScreenOrientationLockResult::
SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
return;
}
}
lock_applied_ = true;
delegate_->Lock(web_contents(), orientation);
if (auto* view = web_contents()->GetRenderWidgetHostView())
static_cast<RenderWidgetHostViewBase*>(view)->LockOrientation(orientation);
// If the orientation we are locking to matches the current orientation, we
// should succeed immediately.
if (LockMatchesCurrentOrientation(orientation)) {
NotifyLockResult(
ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);
return;
}
pending_lock_orientation_ = orientation;
}
void ScreenOrientationProvider::UnlockOrientation() {
// Cancel any pending lock request.
NotifyLockResult(ScreenOrientationLockResult::
SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
if (!lock_applied_ || !delegate_)
return;
delegate_->Unlock(web_contents());
if (auto* view = web_contents()->GetRenderWidgetHostView())
static_cast<RenderWidgetHostViewBase*>(view)->UnlockOrientation();
lock_applied_ = false;
}
void ScreenOrientationProvider::OnOrientationChange() {
if (!pending_lock_orientation_.has_value())
return;
if (LockMatchesCurrentOrientation(pending_lock_orientation_.value())) {
DCHECK(!pending_callback_.is_null());
NotifyLockResult(
ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);
}
}
void ScreenOrientationProvider::NotifyLockResult(
ScreenOrientationLockResult result) {
if (!pending_callback_.is_null())
std::move(pending_callback_).Run(result);
pending_lock_orientation_.reset();
}
void ScreenOrientationProvider::SetDelegate(
ScreenOrientationDelegate* delegate) {
delegate_ = delegate;
}
ScreenOrientationDelegate* ScreenOrientationProvider::GetDelegateForTesting() {
return delegate_;
}
bool ScreenOrientationProvider::LockMatchesOrientation(
device::mojom::ScreenOrientationLockType lock,
display::mojom::ScreenOrientation orientation) {
switch (lock) {
case device::mojom::ScreenOrientationLockType::PORTRAIT_PRIMARY:
return orientation == display::mojom::ScreenOrientation::kPortraitPrimary;
case device::mojom::ScreenOrientationLockType::PORTRAIT_SECONDARY:
return orientation ==
display::mojom::ScreenOrientation::kPortraitSecondary;
case device::mojom::ScreenOrientationLockType::LANDSCAPE_PRIMARY:
return orientation ==
display::mojom::ScreenOrientation::kLandscapePrimary;
case device::mojom::ScreenOrientationLockType::LANDSCAPE_SECONDARY:
return orientation ==
display::mojom::ScreenOrientation::kLandscapeSecondary;
case device::mojom::ScreenOrientationLockType::LANDSCAPE:
return orientation ==
display::mojom::ScreenOrientation::kLandscapePrimary ||
orientation ==
display::mojom::ScreenOrientation::kLandscapeSecondary;
case device::mojom::ScreenOrientationLockType::PORTRAIT:
return orientation ==
display::mojom::ScreenOrientation::kPortraitPrimary ||
orientation ==
display::mojom::ScreenOrientation::kPortraitSecondary;
case device::mojom::ScreenOrientationLockType::ANY:
return true;
case device::mojom::ScreenOrientationLockType::NATURAL:
case device::mojom::ScreenOrientationLockType::DEFAULT:
return false;
}
return false;
}
void ScreenOrientationProvider::DidToggleFullscreenModeForTab(
bool entered_fullscreen,
bool will_cause_resize) {
if (!lock_applied_ || !delegate_)
return;
// If fullscreen is not required in order to lock orientation, don't unlock
// when fullscreen state changes.
if (!delegate_->FullScreenRequired(web_contents()))
return;
DCHECK(!entered_fullscreen);
UnlockOrientation();
}
void ScreenOrientationProvider::PrimaryPageChanged(Page& page) {
UnlockOrientation();
}
device::mojom::ScreenOrientationLockType
ScreenOrientationProvider::GetNaturalLockType() const {
RenderWidgetHost* rwh =
web_contents()->GetPrimaryMainFrame()->GetRenderViewHost()->GetWidget();
if (!rwh)
return device::mojom::ScreenOrientationLockType::DEFAULT;
display::ScreenInfo screen_info = rwh->GetScreenInfo();
switch (screen_info.orientation_type) {
case display::mojom::ScreenOrientation::kPortraitPrimary:
case display::mojom::ScreenOrientation::kPortraitSecondary:
if (screen_info.orientation_angle == 0 ||
screen_info.orientation_angle == 180) {
return device::mojom::ScreenOrientationLockType::PORTRAIT_PRIMARY;
}
return device::mojom::ScreenOrientationLockType::LANDSCAPE_PRIMARY;
case display::mojom::ScreenOrientation::kLandscapePrimary:
case display::mojom::ScreenOrientation::kLandscapeSecondary:
if (screen_info.orientation_angle == 0 ||
screen_info.orientation_angle == 180) {
return device::mojom::ScreenOrientationLockType::LANDSCAPE_PRIMARY;
}
return device::mojom::ScreenOrientationLockType::PORTRAIT_PRIMARY;
default:
break;
}
NOTREACHED();
}
bool ScreenOrientationProvider::LockMatchesCurrentOrientation(
device::mojom::ScreenOrientationLockType lock) {
RenderWidgetHost* rwh =
web_contents()->GetPrimaryMainFrame()->GetRenderViewHost()->GetWidget();
if (!rwh)
return false;
display::ScreenInfo screen_info = rwh->GetScreenInfo();
if (lock == device::mojom::ScreenOrientationLockType::NATURAL ||
lock == device::mojom::ScreenOrientationLockType::DEFAULT) {
NOTREACHED();
}
return LockMatchesOrientation(lock, screen_info.orientation_type);
}
} // namespace content
|