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

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

Issue 2702033002: [ScreenOrientation] Fire pending lock request when we found it has failed.
Patch Set: 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
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"
(...skipping 16 matching lines...) Expand all
27 ScreenOrientationProvider::~ScreenOrientationProvider() = default; 27 ScreenOrientationProvider::~ScreenOrientationProvider() = default;
28 28
29 void ScreenOrientationProvider::LockOrientation( 29 void ScreenOrientationProvider::LockOrientation(
30 blink::WebScreenOrientationLockType orientation, 30 blink::WebScreenOrientationLockType orientation,
31 const LockOrientationCallback& callback) { 31 const LockOrientationCallback& callback) {
32 // Cancel any pending lock request. 32 // Cancel any pending lock request.
33 NotifyLockResult(ScreenOrientationLockResult:: 33 NotifyLockResult(ScreenOrientationLockResult::
34 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); 34 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
35 // Record new pending lock request. 35 // Record new pending lock request.
36 pending_callback_ = callback; 36 pending_callback_ = callback;
37 pending_lock_orientation_ = orientation;
37 38
38 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) { 39 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) {
39 NotifyLockResult(ScreenOrientationLockResult:: 40 NotifyLockResult(ScreenOrientationLockResult::
40 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE); 41 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE);
41 return; 42 return;
42 } 43 }
43 44
44 if (delegate_->FullScreenRequired(web_contents())) { 45 if (delegate_->FullScreenRequired(web_contents())) {
45 RenderViewHostImpl* rvhi = 46 RenderViewHostImpl* rvhi =
46 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); 47 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost());
47 if (!rvhi) { 48 if (!rvhi) {
48 NotifyLockResult(ScreenOrientationLockResult:: 49 NotifyLockResult(ScreenOrientationLockResult::
49 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); 50 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
50 return; 51 return;
51 } 52 }
52 if (!static_cast<WebContentsImpl*>(web_contents()) 53 if (!static_cast<WebContentsImpl*>(web_contents())
53 ->IsFullscreenForCurrentTab()) { 54 ->IsFullscreenForCurrentTab()) {
54 NotifyLockResult( 55 NotifyLockResult(
55 ScreenOrientationLockResult:: 56 ScreenOrientationLockResult::
56 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED); 57 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED);
57 return; 58 return;
58 } 59 }
59 } 60 }
60 61
61 if (orientation == blink::WebScreenOrientationLockNatural) { 62 if (orientation == blink::WebScreenOrientationLockNatural) {
62 orientation = GetNaturalLockType(); 63 orientation = GetNaturalLockType();
64 pending_lock_orientation_ = orientation;
63 if (orientation == blink::WebScreenOrientationLockDefault) { 65 if (orientation == blink::WebScreenOrientationLockDefault) {
64 // We are in a broken state, let's pretend we got canceled. 66 // We are in a broken state, let's pretend we got canceled.
65 NotifyLockResult(ScreenOrientationLockResult:: 67 NotifyLockResult(ScreenOrientationLockResult::
66 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); 68 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
67 return; 69 return;
68 } 70 }
69 } 71 }
70 72
71 lock_applied_ = true; 73 lock_applied_ = true;
72 delegate_->Lock(web_contents(), orientation); 74 delegate_->Lock(web_contents(), orientation);
73 75
74 // If the orientation we are locking to matches the current orientation, we 76 // If the orientation we are locking to matches the current orientation, we
75 // should succeed immediately. 77 // should succeed immediately.
76 if (LockMatchesCurrentOrientation(orientation)) { 78 if (LockMatchesCurrentOrientation(orientation)) {
77 NotifyLockResult( 79 NotifyLockResult(
78 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); 80 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);
79 return; 81 return;
80 } 82 }
81 83 // After delegate_ calls Lock() above, on Android we expect the actual
82 pending_lock_orientation_ = orientation; 84 // orientation value be returned asynchronous from OnOrientationChange() being
85 // called later, but on other platforms we do expect current orientation has
86 // changed to the value we wanted to lock to, otherwise let's pretend the lock
87 // got cancelled to resolve the promise in blink side.
88 #if !defined(OS_ANDROID)
89 else {
90 NotifyLockResult(ScreenOrientationLockResult::
91 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
92 return;
93 }
94 #endif
83 } 95 }
84 96
85 void ScreenOrientationProvider::UnlockOrientation() { 97 void ScreenOrientationProvider::UnlockOrientation() {
86 // Cancel any pending lock request. 98 // Cancel any pending lock request.
87 NotifyLockResult(ScreenOrientationLockResult:: 99 NotifyLockResult(ScreenOrientationLockResult::
88 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); 100 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
89 101
90 if (!lock_applied_ || !delegate_) 102 if (!lock_applied_ || !delegate_)
91 return; 103 return;
92 104
93 delegate_->Unlock(web_contents()); 105 delegate_->Unlock(web_contents());
94 106
95 lock_applied_ = false; 107 lock_applied_ = false;
96 } 108 }
97 109
110 #if defined(OS_ANDROID)
98 void ScreenOrientationProvider::OnOrientationChange() { 111 void ScreenOrientationProvider::OnOrientationChange() {
99 if (!pending_lock_orientation_.has_value()) 112 if (!pending_lock_orientation_.has_value())
100 return; 113 return;
101 114
115 DCHECK(!pending_callback_.is_null());
102 if (LockMatchesCurrentOrientation(pending_lock_orientation_.value())) { 116 if (LockMatchesCurrentOrientation(pending_lock_orientation_.value())) {
103 DCHECK(!pending_callback_.is_null());
104 NotifyLockResult( 117 NotifyLockResult(
105 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); 118 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);
119 } else {
120 // Screen orientation changed to a value not matching with current pending
121 // lock request, let's pretend it's cancelled to resolve the promise in
122 // blink side.
123 NotifyLockResult(ScreenOrientationLockResult::
124 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
106 } 125 }
107 } 126 }
127 #endif
108 128
109 void ScreenOrientationProvider::NotifyLockResult( 129 void ScreenOrientationProvider::NotifyLockResult(
110 ScreenOrientationLockResult result) { 130 ScreenOrientationLockResult result) {
111 if (!pending_callback_.is_null()) 131 if (!pending_callback_.is_null())
112 base::ResetAndReturn(&pending_callback_).Run(result); 132 base::ResetAndReturn(&pending_callback_).Run(result);
113 133
114 pending_lock_orientation_.reset(); 134 pending_lock_orientation_.reset();
115 } 135 }
116 136
117 void ScreenOrientationProvider::SetDelegate( 137 void ScreenOrientationProvider::SetDelegate(
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 case blink::WebScreenOrientationLockDefault: 233 case blink::WebScreenOrientationLockDefault:
214 NOTREACHED(); 234 NOTREACHED();
215 return false; 235 return false;
216 } 236 }
217 237
218 NOTREACHED(); 238 NOTREACHED();
219 return false; 239 return false;
220 } 240 }
221 241
222 } // namespace content 242 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698