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

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

Issue 2391883006: Mojo-ify implementation of screen orientation locking/unlocking. (Closed)
Patch Set: Codereview update Created 4 years, 1 month 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/public/browser/screen_orientation_provider.h" 5 #include "content/public/browser/screen_orientation_provider.h"
6 6
7 #include "base/callback_helpers.h"
8 #include "base/optional.h"
7 #include "content/browser/renderer_host/render_view_host_impl.h" 9 #include "content/browser/renderer_host/render_view_host_impl.h"
8 #include "content/browser/web_contents/web_contents_impl.h" 10 #include "content/browser/web_contents/web_contents_impl.h"
9 #include "content/public/browser/render_widget_host.h" 11 #include "content/public/browser/render_widget_host.h"
10 #include "content/public/browser/screen_orientation_delegate.h" 12 #include "content/public/browser/screen_orientation_delegate.h"
11 #include "content/public/browser/screen_orientation_dispatcher_host.h"
12 #include "content/public/browser/web_contents.h" 13 #include "content/public/browser/web_contents.h"
13 #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"
14 15
15 namespace content { 16 namespace content {
16 17
18 using ::blink::mojom::ScreenOrientationLockResult;
19
17 ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr; 20 ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr;
18 21
19 ScreenOrientationProvider::LockInformation::LockInformation(int request_id, 22 ScreenOrientationProvider::ScreenOrientationProvider(WebContents* web_contents)
20 blink::WebScreenOrientationLockType lock) 23 : WebContentsObserver(web_contents), lock_applied_(false) {}
21 : request_id(request_id),
22 lock(lock) {
23 }
24
25 ScreenOrientationProvider::ScreenOrientationProvider(
26 ScreenOrientationDispatcherHost* dispatcher_host,
27 WebContents* web_contents)
28 : WebContentsObserver(web_contents),
29 dispatcher_(dispatcher_host),
30 lock_applied_(false) {
31 }
32 24
33 ScreenOrientationProvider::~ScreenOrientationProvider() { 25 ScreenOrientationProvider::~ScreenOrientationProvider() {
34 } 26 }
35 27
36 void ScreenOrientationProvider::LockOrientation(int request_id, 28 void ScreenOrientationProvider::LockOrientation(
37 blink::WebScreenOrientationLockType lock_orientation) { 29 blink::WebScreenOrientationLockType orientation,
30 const LockOrientationCallback& callback) {
31 // ScreenOrientation should have cancelled all pending request at thie point.
32 DCHECK(on_result_callback_.is_null());
33 DCHECK(pending_lock_orientation_.has_value());
34 DCHECK(pending_lock_orientation_.value() >= 0);
35 on_result_callback_ = callback;
38 36
39 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) { 37 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) {
40 dispatcher_->NotifyLockError(request_id, 38 NotifyLockResult(ScreenOrientationLockResult::
41 blink::WebLockOrientationErrorNotAvailable); 39 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE);
42 return; 40 return;
43 } 41 }
44 42
45 if (delegate_->FullScreenRequired(web_contents())) { 43 if (delegate_->FullScreenRequired(web_contents())) {
46 RenderViewHostImpl* rvhi = 44 RenderViewHostImpl* rvhi =
47 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); 45 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost());
48 if (!rvhi) { 46 if (!rvhi) {
49 dispatcher_->NotifyLockError(request_id, 47 NotifyLockResult(ScreenOrientationLockResult::
50 blink::WebLockOrientationErrorCanceled); 48 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
51 return; 49 return;
52 } 50 }
53 if (!static_cast<WebContentsImpl*>(web_contents()) 51 if (!static_cast<WebContentsImpl*>(web_contents())
54 ->IsFullscreenForCurrentTab()) { 52 ->IsFullscreenForCurrentTab()) {
55 dispatcher_->NotifyLockError(request_id, 53 NotifyLockResult(
56 blink::WebLockOrientationErrorFullscreenRequired); 54 ScreenOrientationLockResult::
55 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED);
57 return; 56 return;
58 } 57 }
59 } 58 }
60 59
61 if (lock_orientation == blink::WebScreenOrientationLockNatural) { 60 if (orientation == blink::WebScreenOrientationLockNatural) {
62 lock_orientation = GetNaturalLockType(); 61 orientation = GetNaturalLockType();
63 if (lock_orientation == blink::WebScreenOrientationLockDefault) { 62 if (orientation == blink::WebScreenOrientationLockDefault) {
64 // We are in a broken state, let's pretend we got canceled. 63 // We are in a broken state, let's pretend we got canceled.
65 dispatcher_->NotifyLockError(request_id, 64 NotifyLockResult(ScreenOrientationLockResult::
66 blink::WebLockOrientationErrorCanceled); 65 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED);
67 return; 66 return;
68 } 67 }
69 } 68 }
70 69
71 lock_applied_ = true; 70 lock_applied_ = true;
72 delegate_->Lock(web_contents(), lock_orientation); 71 delegate_->Lock(web_contents(), orientation);
73
74 // If two calls happen close to each other some platforms will ignore the
75 // first. A successful lock will be once orientation matches the latest
76 // request.
77 pending_lock_.reset();
78 72
79 // If the orientation we are locking to matches the current orientation, we 73 // If the orientation we are locking to matches the current orientation, we
80 // should succeed immediately. 74 // should succeed immediately.
81 if (LockMatchesCurrentOrientation(lock_orientation)) { 75 if (LockMatchesCurrentOrientation(orientation)) {
82 dispatcher_->NotifyLockSuccess(request_id); 76 NotifyLockResult(
77 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);
83 return; 78 return;
84 } 79 }
85 80
86 pending_lock_.reset(new LockInformation(request_id, lock_orientation)); 81 pending_lock_orientation_ = orientation;
87 } 82 }
88 83
89 void ScreenOrientationProvider::UnlockOrientation() { 84 void ScreenOrientationProvider::UnlockOrientation() {
90 if (!lock_applied_ || !delegate_) 85 if (!lock_applied_ || !delegate_)
91 return; 86 return;
92 87
93 delegate_->Unlock(web_contents()); 88 delegate_->Unlock(web_contents());
94 89
95 lock_applied_ = false; 90 lock_applied_ = false;
96 pending_lock_.reset(); 91 pending_lock_orientation_.reset();
97 } 92 }
98 93
99 void ScreenOrientationProvider::OnOrientationChange() { 94 void ScreenOrientationProvider::OnOrientationChange() {
100 if (!pending_lock_.get()) 95 if (!pending_lock_orientation_.has_value())
101 return; 96 return;
102 97
103 if (LockMatchesCurrentOrientation(pending_lock_->lock)) { 98 if (LockMatchesCurrentOrientation(pending_lock_orientation_.value())) {
104 dispatcher_->NotifyLockSuccess(pending_lock_->request_id); 99 DCHECK(!on_result_callback_.is_null());
105 pending_lock_.reset(); 100 NotifyLockResult(
101 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);
106 } 102 }
107 } 103 }
108 104
105 void ScreenOrientationProvider::NotifyLockResult(
106 ScreenOrientationLockResult result) {
107 if (on_result_callback_.is_null())
pfeldman 2016/11/21 21:30:26 Reset this callback field before firing it?
lunalu1 2016/11/21 21:40:51 Done.
108 return;
109 base::ResetAndReturn(&on_result_callback_).Run(result);
110 pending_lock_orientation_.reset();
111 }
112
109 void ScreenOrientationProvider::SetDelegate( 113 void ScreenOrientationProvider::SetDelegate(
110 ScreenOrientationDelegate* delegate) { 114 ScreenOrientationDelegate* delegate) {
111 delegate_ = delegate; 115 delegate_ = delegate;
112 } 116 }
113 117
114 void ScreenOrientationProvider::DidToggleFullscreenModeForTab( 118 void ScreenOrientationProvider::DidToggleFullscreenModeForTab(
115 bool entered_fullscreen, bool will_cause_resize) { 119 bool entered_fullscreen, bool will_cause_resize) {
116 if (!lock_applied_ || !delegate_) 120 if (!lock_applied_ || !delegate_)
117 return; 121 return;
118 122
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 case blink::WebScreenOrientationLockDefault: 199 case blink::WebScreenOrientationLockDefault:
196 NOTREACHED(); 200 NOTREACHED();
197 return false; 201 return false;
198 } 202 }
199 203
200 NOTREACHED(); 204 NOTREACHED();
201 return false; 205 return false;
202 } 206 }
203 207
204 } // namespace content 208 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698