summaryrefslogtreecommitdiffstats
path: root/content/public/browser/screen_orientation_provider.cc
blob: b575f0942f1b5f6e977d9f6f660fea4c20f126b8 (plain)
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
// Copyright 2014 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 "content/public/browser/screen_orientation_provider.h"

#include "content/browser/renderer_host/render_view_host_delegate.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/screen_orientation_delegate.h"
#include "content/public/browser/screen_orientation_dispatcher_host.h"
#include "content/public/browser/web_contents.h"
#include "third_party/WebKit/public/platform/WebScreenInfo.h"
#include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockOrientationError.h"

namespace content {

ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr;

ScreenOrientationProvider::LockInformation::LockInformation(int request_id,
    blink::WebScreenOrientationLockType lock)
    : request_id(request_id),
      lock(lock) {
}

ScreenOrientationProvider::ScreenOrientationProvider(
    ScreenOrientationDispatcherHost* dispatcher_host,
    WebContents* web_contents)
    : WebContentsObserver(web_contents),
      dispatcher_(dispatcher_host),
      lock_applied_(false) {
}

ScreenOrientationProvider::~ScreenOrientationProvider() {
}

void ScreenOrientationProvider::LockOrientation(int request_id,
    blink::WebScreenOrientationLockType lock_orientation) {

  if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) {
    dispatcher_->NotifyLockError(request_id,
                                 blink::WebLockOrientationErrorNotAvailable);
    return;
  }

  if (delegate_->FullScreenRequired(web_contents())) {
    RenderViewHost* rvh = web_contents()->GetRenderViewHost();
    if (!rvh) {
      dispatcher_->NotifyLockError(request_id,
                                   blink::WebLockOrientationErrorCanceled);
      return;
    }
    RenderViewHostDelegate* rvhd = rvh->GetDelegate();
    if (!rvhd) {
      dispatcher_->NotifyLockError(request_id,
                                   blink::WebLockOrientationErrorCanceled);
      return;
    }
    if (!rvhd->IsFullscreenForCurrentTab()) {
      dispatcher_->NotifyLockError(request_id,
          blink::WebLockOrientationErrorFullScreenRequired);
      return;
    }
  }

  if (lock_orientation == blink::WebScreenOrientationLockNatural) {
    lock_orientation = GetNaturalLockType();
    if (lock_orientation == blink::WebScreenOrientationLockDefault) {
      // We are in a broken state, let's pretend we got canceled.
      dispatcher_->NotifyLockError(request_id,
                                   blink::WebLockOrientationErrorCanceled);
      return;
    }
  }

  lock_applied_ = true;
  delegate_->Lock(web_contents(), lock_orientation);

  // If two calls happen close to each other some platforms will ignore the
  // first. A successful lock will be once orientation matches the latest
  // request.
  pending_lock_.reset();

  // If the orientation we are locking to matches the current orientation, we
  // should succeed immediately.
  if (LockMatchesCurrentOrientation(lock_orientation)) {
    dispatcher_->NotifyLockSuccess(request_id);
    return;
  }

  pending_lock_.reset(new LockInformation(request_id, lock_orientation));
}

void ScreenOrientationProvider::UnlockOrientation() {
  if (!lock_applied_ || !delegate_)
    return;

  delegate_->Unlock(web_contents());

  lock_applied_ = false;
  pending_lock_.reset();
}

void ScreenOrientationProvider::OnOrientationChange() {
  if (!pending_lock_.get())
    return;

  if (LockMatchesCurrentOrientation(pending_lock_->lock)) {
    dispatcher_->NotifyLockSuccess(pending_lock_->request_id);
    pending_lock_.reset();
  }
}

void ScreenOrientationProvider::SetDelegate(
    ScreenOrientationDelegate* delegate) {
  delegate_ = delegate;
}

void ScreenOrientationProvider::DidToggleFullscreenModeForTab(
    bool entered_fullscreen) {
  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();
}

blink::WebScreenOrientationLockType
    ScreenOrientationProvider::GetNaturalLockType() const {
  RenderWidgetHost* rwh = web_contents()->GetRenderViewHost();
  if (!rwh)
    return blink::WebScreenOrientationLockDefault;

  blink::WebScreenInfo screen_info;
  rwh->GetWebScreenInfo(&screen_info);

  switch (screen_info.orientationType) {
    case blink::WebScreenOrientationPortraitPrimary:
    case blink::WebScreenOrientationPortraitSecondary:
      if (screen_info.orientationAngle == 0 ||
          screen_info.orientationAngle == 180) {
        return blink::WebScreenOrientationLockPortraitPrimary;
      }
      return blink::WebScreenOrientationLockLandscapePrimary;
    case blink::WebScreenOrientationLandscapePrimary:
    case blink::WebScreenOrientationLandscapeSecondary:
      if (screen_info.orientationAngle == 0 ||
          screen_info.orientationAngle == 180) {
        return blink::WebScreenOrientationLockLandscapePrimary;
      }
      return blink::WebScreenOrientationLockPortraitPrimary;
    case blink::WebScreenOrientationUndefined:
      NOTREACHED();
      return blink::WebScreenOrientationLockDefault;
  }

  NOTREACHED();
  return blink::WebScreenOrientationLockDefault;
}

bool ScreenOrientationProvider::LockMatchesCurrentOrientation(
    blink::WebScreenOrientationLockType lock) {
  RenderWidgetHost* rwh = web_contents()->GetRenderViewHost();
  if (!rwh)
    return false;

  blink::WebScreenInfo screen_info;
  rwh->GetWebScreenInfo(&screen_info);

  switch (lock) {
    case blink::WebScreenOrientationLockPortraitPrimary:
      return screen_info.orientationType ==
          blink::WebScreenOrientationPortraitPrimary;
    case blink::WebScreenOrientationLockPortraitSecondary:
      return screen_info.orientationType ==
          blink::WebScreenOrientationPortraitSecondary;
    case blink::WebScreenOrientationLockLandscapePrimary:
      return screen_info.orientationType ==
          blink::WebScreenOrientationLandscapePrimary;
    case blink::WebScreenOrientationLockLandscapeSecondary:
      return screen_info.orientationType ==
          blink::WebScreenOrientationLandscapeSecondary;
    case blink::WebScreenOrientationLockLandscape:
      return screen_info.orientationType ==
          blink::WebScreenOrientationLandscapePrimary ||
          screen_info.orientationType ==
          blink::WebScreenOrientationLandscapeSecondary;
    case blink::WebScreenOrientationLockPortrait:
      return screen_info.orientationType ==
          blink::WebScreenOrientationPortraitPrimary ||
          screen_info.orientationType ==
          blink::WebScreenOrientationPortraitSecondary;
    case blink::WebScreenOrientationLockAny:
      return true;
    case blink::WebScreenOrientationLockNatural:
    case blink::WebScreenOrientationLockDefault:
      NOTREACHED();
      return false;
  }

  NOTREACHED();
  return false;
}

} // namespace content