Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: content/public/browser/screen_orientation_provider.cc

Issue 2682643002: [ScreenOrientation] Hide ScreenOrientationProvider inside WebContentsImpl. (Closed)
Patch Set: Rebase only Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/public/browser/screen_orientation_provider.h"
6
7 #include "base/callback_helpers.h"
8 #include "content/browser/renderer_host/render_view_host_impl.h"
9 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/public/browser/navigation_handle.h"
11 #include "content/public/browser/render_widget_host.h"
12 #include "content/public/browser/screen_orientation_delegate.h"
13 #include "content/public/browser/web_contents.h"
14 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockO rientationError.h"
15
16 namespace content {
17
18 using device::mojom::ScreenOrientationLockResult;
19
20 ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr;
21
22 ScreenOrientationProvider::ScreenOrientationProvider(WebContents* web_contents)
23 : WebContentsObserver(web_contents),
24 lock_applied_(false),
25 bindings_(web_contents, this) {}
26
27 ScreenOrientationProvider::~ScreenOrientationProvider() {
28 }
29
30 void ScreenOrientationProvider::LockOrientation(
31 blink::WebScreenOrientationLockType orientation,
32 const LockOrientationCallback& callback) {
33 // Cancel any pending lock request.
34 NotifyLockResult(ScreenOrientationLockResult::
35 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
36 // Record new pending lock request.
37 pending_callback_ = callback;
38
39 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) {
40 NotifyLockResult(ScreenOrientationLockResult::
41 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE);
42 return;
43 }
44
45 if (delegate_->FullScreenRequired(web_contents())) {
46 RenderViewHostImpl* rvhi =
47 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost());
48 if (!rvhi) {
49 NotifyLockResult(ScreenOrientationLockResult::
50 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
51 return;
52 }
53 if (!static_cast<WebContentsImpl*>(web_contents())
54 ->IsFullscreenForCurrentTab()) {
55 NotifyLockResult(
56 ScreenOrientationLockResult::
57 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED);
58 return;
59 }
60 }
61
62 if (orientation == blink::WebScreenOrientationLockNatural) {
63 orientation = GetNaturalLockType();
64 if (orientation == blink::WebScreenOrientationLockDefault) {
65 // We are in a broken state, let's pretend we got canceled.
66 NotifyLockResult(ScreenOrientationLockResult::
67 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
68 return;
69 }
70 }
71
72 lock_applied_ = true;
73 delegate_->Lock(web_contents(), orientation);
74
75 // If the orientation we are locking to matches the current orientation, we
76 // should succeed immediately.
77 if (LockMatchesCurrentOrientation(orientation)) {
78 NotifyLockResult(
79 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);
80 return;
81 }
82
83 pending_lock_orientation_ = orientation;
84 }
85
86 void ScreenOrientationProvider::UnlockOrientation() {
87 // Cancel any pending lock request.
88 NotifyLockResult(ScreenOrientationLockResult::
89 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
90
91 if (!lock_applied_ || !delegate_)
92 return;
93
94 delegate_->Unlock(web_contents());
95
96 lock_applied_ = false;
97 }
98
99 void ScreenOrientationProvider::OnOrientationChange() {
100 if (!pending_lock_orientation_.has_value())
101 return;
102
103 if (LockMatchesCurrentOrientation(pending_lock_orientation_.value())) {
104 DCHECK(!pending_callback_.is_null());
105 NotifyLockResult(
106 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);
107 }
108 }
109
110 void ScreenOrientationProvider::NotifyLockResult(
111 ScreenOrientationLockResult result) {
112 if (!pending_callback_.is_null())
113 base::ResetAndReturn(&pending_callback_).Run(result);
114
115 pending_lock_orientation_.reset();
116 }
117
118 void ScreenOrientationProvider::SetDelegate(
119 ScreenOrientationDelegate* delegate) {
120 delegate_ = delegate;
121 }
122
123 void ScreenOrientationProvider::DidToggleFullscreenModeForTab(
124 bool entered_fullscreen, bool will_cause_resize) {
125 if (!lock_applied_ || !delegate_)
126 return;
127
128 // If fullscreen is not required in order to lock orientation, don't unlock
129 // when fullscreen state changes.
130 if (!delegate_->FullScreenRequired(web_contents()))
131 return;
132
133 DCHECK(!entered_fullscreen);
134 UnlockOrientation();
135 }
136
137 void ScreenOrientationProvider::DidFinishNavigation(
138 NavigationHandle* navigation_handle) {
139 if (!navigation_handle->IsInMainFrame() ||
140 !navigation_handle->HasCommitted() || navigation_handle->IsSamePage()) {
141 return;
142 }
143 UnlockOrientation();
144 }
145
146 blink::WebScreenOrientationLockType
147 ScreenOrientationProvider::GetNaturalLockType() const {
148 RenderWidgetHost* rwh = web_contents()->GetRenderViewHost()->GetWidget();
149 if (!rwh)
150 return blink::WebScreenOrientationLockDefault;
151
152 ScreenInfo screen_info;
153 rwh->GetScreenInfo(&screen_info);
154
155 switch (screen_info.orientation_type) {
156 case SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY:
157 case SCREEN_ORIENTATION_VALUES_PORTRAIT_SECONDARY:
158 if (screen_info.orientation_angle == 0 ||
159 screen_info.orientation_angle == 180) {
160 return blink::WebScreenOrientationLockPortraitPrimary;
161 }
162 return blink::WebScreenOrientationLockLandscapePrimary;
163 case SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY:
164 case SCREEN_ORIENTATION_VALUES_LANDSCAPE_SECONDARY:
165 if (screen_info.orientation_angle == 0 ||
166 screen_info.orientation_angle == 180) {
167 return blink::WebScreenOrientationLockLandscapePrimary;
168 }
169 return blink::WebScreenOrientationLockPortraitPrimary;
170 default:
171 break;
172 }
173
174 NOTREACHED();
175 return blink::WebScreenOrientationLockDefault;
176 }
177
178 bool ScreenOrientationProvider::LockMatchesCurrentOrientation(
179 blink::WebScreenOrientationLockType lock) {
180 RenderWidgetHost* rwh = web_contents()->GetRenderViewHost()->GetWidget();
181 if (!rwh)
182 return false;
183
184 ScreenInfo screen_info;
185 rwh->GetScreenInfo(&screen_info);
186
187 switch (lock) {
188 case blink::WebScreenOrientationLockPortraitPrimary:
189 return screen_info.orientation_type ==
190 SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY;
191 case blink::WebScreenOrientationLockPortraitSecondary:
192 return screen_info.orientation_type ==
193 SCREEN_ORIENTATION_VALUES_PORTRAIT_SECONDARY;
194 case blink::WebScreenOrientationLockLandscapePrimary:
195 return screen_info.orientation_type ==
196 SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY;
197 case blink::WebScreenOrientationLockLandscapeSecondary:
198 return screen_info.orientation_type ==
199 SCREEN_ORIENTATION_VALUES_LANDSCAPE_SECONDARY;
200 case blink::WebScreenOrientationLockLandscape:
201 return screen_info.orientation_type ==
202 SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY ||
203 screen_info.orientation_type ==
204 SCREEN_ORIENTATION_VALUES_LANDSCAPE_SECONDARY;
205 case blink::WebScreenOrientationLockPortrait:
206 return screen_info.orientation_type ==
207 SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY ||
208 screen_info.orientation_type ==
209 SCREEN_ORIENTATION_VALUES_PORTRAIT_SECONDARY;
210 case blink::WebScreenOrientationLockAny:
211 return true;
212 case blink::WebScreenOrientationLockNatural:
213 case blink::WebScreenOrientationLockDefault:
214 NOTREACHED();
215 return false;
216 }
217
218 NOTREACHED();
219 return false;
220 }
221
222 } // namespace content
OLDNEW
« no previous file with comments | « content/public/browser/screen_orientation_provider.h ('k') | content/public/browser/web_contents.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698