OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/screen_orientation/screen_orientation_provider.h" | 5 #include "content/browser/screen_orientation/screen_orientation_provider.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "content/browser/renderer_host/render_view_host_impl.h" | 8 #include "content/browser/renderer_host/render_view_host_impl.h" |
9 #include "content/browser/web_contents/web_contents_impl.h" | 9 #include "content/browser/web_contents/web_contents_impl.h" |
10 #include "content/public/browser/navigation_handle.h" | 10 #include "content/public/browser/navigation_handle.h" |
11 #include "content/public/browser/render_widget_host.h" | 11 #include "content/public/browser/render_widget_host.h" |
12 #include "content/public/browser/screen_orientation_delegate.h" | 12 #include "content/public/browser/screen_orientation_delegate.h" |
13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
14 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockO
rientationError.h" | 14 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockO
rientationError.h" |
15 | 15 |
16 #if defined(OS_ANDROID) | 16 #if defined(OS_ANDROID) |
17 #include "content/browser/screen_orientation/screen_orientation_delegate_android
.h" | 17 #include "content/browser/screen_orientation/screen_orientation_delegate_android
.h" |
18 #endif | 18 #endif |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 using device::mojom::ScreenOrientationLockResult; | 22 using device::mojom::ScreenOrientationLockResult; |
23 | 23 |
24 ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr; | 24 ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr; |
25 | 25 |
26 ScreenOrientationProvider::ScreenOrientationProvider(WebContents* web_contents) | 26 ScreenOrientationProvider::ScreenOrientationProvider(WebContents* web_contents) |
27 : WebContentsObserver(web_contents), | 27 : WebContentsObserver(web_contents), lock_applied_(false) {} |
28 lock_applied_(false), | |
29 bindings_(web_contents, this) {} | |
30 | 28 |
31 ScreenOrientationProvider::~ScreenOrientationProvider() { | 29 ScreenOrientationProvider::~ScreenOrientationProvider() { |
32 #if defined(OS_ANDROID) | 30 #if defined(OS_ANDROID) |
33 if (accurate_listener_count_ > 0) | 31 if (accurate_listener_count_ > 0) |
34 ScreenOrientationDelegateAndroid::StopAccurateListening(); | 32 ScreenOrientationDelegateAndroid::StopAccurateListening(); |
35 #endif | 33 #endif |
36 } | 34 } |
37 | 35 |
| 36 // static |
| 37 void ScreenOrientationProvider::SetDelegate( |
| 38 ScreenOrientationDelegate* delegate) { |
| 39 delegate_ = delegate; |
| 40 } |
| 41 |
| 42 // static |
| 43 void ScreenOrientationProvider::BindRequest( |
| 44 RenderFrameHost* render_frame_host, |
| 45 device::mojom::ScreenOrientationRequest request) { |
| 46 WebContents* web_contents = |
| 47 WebContents::FromRenderFrameHost(render_frame_host); |
| 48 DCHECK(web_contents); |
| 49 |
| 50 static_cast<WebContentsImpl*>(web_contents) |
| 51 ->GetScreenOrientationProvider() |
| 52 ->AddBinding(std::move(request)); |
| 53 } |
| 54 |
| 55 void ScreenOrientationProvider::AddBinding( |
| 56 device::mojom::ScreenOrientationRequest request) { |
| 57 bindings_.AddBinding(this, std::move(request)); |
| 58 } |
| 59 |
38 void ScreenOrientationProvider::LockOrientation( | 60 void ScreenOrientationProvider::LockOrientation( |
39 blink::WebScreenOrientationLockType orientation, | 61 blink::WebScreenOrientationLockType orientation, |
40 const LockOrientationCallback& callback) { | 62 const LockOrientationCallback& callback) { |
41 // Cancel any pending lock request. | 63 // Cancel any pending lock request. |
42 NotifyLockResult(ScreenOrientationLockResult:: | 64 NotifyLockResult(ScreenOrientationLockResult:: |
43 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | 65 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); |
44 // Record new pending lock request. | 66 // Record new pending lock request. |
45 pending_callback_ = callback; | 67 pending_callback_ = callback; |
| 68 pending_lock_orientation_ = orientation; |
46 | 69 |
47 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) { | 70 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) { |
48 NotifyLockResult(ScreenOrientationLockResult:: | 71 NotifyLockResult(ScreenOrientationLockResult:: |
49 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE); | 72 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE); |
50 return; | 73 return; |
51 } | 74 } |
52 | 75 |
53 if (delegate_->FullScreenRequired(web_contents())) { | 76 if (delegate_->FullScreenRequired(web_contents())) { |
54 RenderViewHostImpl* rvhi = | 77 RenderViewHostImpl* rvhi = |
55 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); | 78 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); |
56 if (!rvhi) { | 79 if (!rvhi) { |
57 NotifyLockResult(ScreenOrientationLockResult:: | 80 NotifyLockResult(ScreenOrientationLockResult:: |
58 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | 81 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); |
59 return; | 82 return; |
60 } | 83 } |
61 if (!static_cast<WebContentsImpl*>(web_contents()) | 84 if (!static_cast<WebContentsImpl*>(web_contents()) |
62 ->IsFullscreenForCurrentTab()) { | 85 ->IsFullscreenForCurrentTab()) { |
63 NotifyLockResult( | 86 NotifyLockResult( |
64 ScreenOrientationLockResult:: | 87 ScreenOrientationLockResult:: |
65 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED); | 88 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED); |
66 return; | 89 return; |
67 } | 90 } |
68 } | 91 } |
69 | 92 |
70 if (orientation == blink::WebScreenOrientationLockNatural) { | 93 if (orientation == blink::WebScreenOrientationLockNatural) { |
71 orientation = GetNaturalLockType(); | 94 orientation = GetNaturalLockType(); |
| 95 pending_lock_orientation_ = orientation; |
72 if (orientation == blink::WebScreenOrientationLockDefault) { | 96 if (orientation == blink::WebScreenOrientationLockDefault) { |
73 // We are in a broken state, let's pretend we got canceled. | 97 // We are in a broken state, let's pretend we got canceled. |
74 NotifyLockResult(ScreenOrientationLockResult:: | 98 NotifyLockResult(ScreenOrientationLockResult:: |
75 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | 99 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); |
76 return; | 100 return; |
77 } | 101 } |
78 } | 102 } |
79 | 103 |
80 lock_applied_ = true; | 104 lock_applied_ = true; |
81 delegate_->Lock(web_contents(), orientation); | 105 delegate_->Lock(web_contents(), orientation); |
82 | 106 |
83 // If the orientation we are locking to matches the current orientation, we | 107 // If the orientation we are locking to matches the current orientation, we |
84 // should succeed immediately. | 108 // should succeed immediately. |
85 if (LockMatchesCurrentOrientation(orientation)) { | 109 if (LockMatchesCurrentOrientation(orientation)) { |
86 NotifyLockResult( | 110 NotifyLockResult( |
87 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | 111 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
88 return; | 112 return; |
89 } | 113 } |
90 | |
91 pending_lock_orientation_ = orientation; | |
92 } | 114 } |
93 | 115 |
94 void ScreenOrientationProvider::UnlockOrientation() { | 116 void ScreenOrientationProvider::UnlockOrientation() { |
95 // Cancel any pending lock request. | 117 // Cancel any pending lock request. |
96 NotifyLockResult(ScreenOrientationLockResult:: | 118 NotifyLockResult(ScreenOrientationLockResult:: |
97 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | 119 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); |
98 | 120 |
99 if (!lock_applied_ || !delegate_) | 121 if (!lock_applied_ || !delegate_) |
100 return; | 122 return; |
101 | 123 |
(...skipping 25 matching lines...) Expand all Loading... |
127 | 149 |
128 if (LockMatchesCurrentOrientation(pending_lock_orientation_.value())) { | 150 if (LockMatchesCurrentOrientation(pending_lock_orientation_.value())) { |
129 DCHECK(!pending_callback_.is_null()); | 151 DCHECK(!pending_callback_.is_null()); |
130 NotifyLockResult( | 152 NotifyLockResult( |
131 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | 153 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
132 } | 154 } |
133 } | 155 } |
134 | 156 |
135 void ScreenOrientationProvider::NotifyLockResult( | 157 void ScreenOrientationProvider::NotifyLockResult( |
136 ScreenOrientationLockResult result) { | 158 ScreenOrientationLockResult result) { |
137 if (!pending_callback_.is_null()) | 159 if (!pending_callback_.is_null()) { |
138 base::ResetAndReturn(&pending_callback_).Run(result); | 160 DCHECK(pending_lock_orientation_.has_value()); |
139 | 161 base::ResetAndReturn(&pending_callback_) |
140 pending_lock_orientation_.reset(); | 162 .Run(result, pending_lock_orientation_.value()); |
141 } | 163 pending_lock_orientation_.reset(); |
142 | 164 } else { |
143 void ScreenOrientationProvider::SetDelegate( | 165 DCHECK(!pending_lock_orientation_.has_value()); |
144 ScreenOrientationDelegate* delegate) { | 166 } |
145 delegate_ = delegate; | |
146 } | 167 } |
147 | 168 |
148 void ScreenOrientationProvider::DidToggleFullscreenModeForTab( | 169 void ScreenOrientationProvider::DidToggleFullscreenModeForTab( |
149 bool entered_fullscreen, | 170 bool entered_fullscreen, |
150 bool will_cause_resize) { | 171 bool will_cause_resize) { |
151 if (!lock_applied_ || !delegate_) | 172 if (!lock_applied_ || !delegate_) |
152 return; | 173 return; |
153 | 174 |
154 // If fullscreen is not required in order to lock orientation, don't unlock | 175 // If fullscreen is not required in order to lock orientation, don't unlock |
155 // when fullscreen state changes. | 176 // when fullscreen state changes. |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 case blink::WebScreenOrientationLockDefault: | 260 case blink::WebScreenOrientationLockDefault: |
240 NOTREACHED(); | 261 NOTREACHED(); |
241 return false; | 262 return false; |
242 } | 263 } |
243 | 264 |
244 NOTREACHED(); | 265 NOTREACHED(); |
245 return false; | 266 return false; |
246 } | 267 } |
247 | 268 |
248 } // namespace content | 269 } // namespace content |
OLD | NEW |