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/public/browser/screen_orientation_provider.h" | 5 #include "content/public/browser/screen_orientation_provider.h" |
6 | 6 |
7 #include "content/browser/renderer_host/render_view_host_impl.h" | 7 #include "content/browser/renderer_host/render_view_host_impl.h" |
8 #include "content/browser/web_contents/web_contents_impl.h" | 8 #include "content/browser/web_contents/web_contents_impl.h" |
9 #include "content/public/browser/render_widget_host.h" | 9 #include "content/public/browser/render_widget_host.h" |
10 #include "content/public/browser/screen_orientation_delegate.h" | 10 #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" | 11 #include "content/public/browser/web_contents.h" |
13 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockO rientationError.h" | 12 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockO rientationError.h" |
14 | 13 |
15 namespace content { | 14 namespace content { |
16 | 15 |
16 using ::blink::mojom::ScreenOrientationLockResult; | |
17 | |
17 ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr; | 18 ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr; |
18 | 19 |
19 ScreenOrientationProvider::LockInformation::LockInformation(int request_id, | 20 ScreenOrientationProvider::ScreenOrientationProvider(WebContents* web_contents) |
20 blink::WebScreenOrientationLockType lock) | |
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), | 21 : WebContentsObserver(web_contents), |
29 dispatcher_(dispatcher_host), | 22 lock_applied_(false), |
30 lock_applied_(false) { | 23 pending_lock_orientation_(blink::WebScreenOrientationLockDefault) {} |
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 // Cancel any pending requests. | |
32 NotifyLockResult(ScreenOrientationLockResult:: | |
blundell
2016/10/25 16:09:26
There shouldn't be any pending request at this poi
lunalu1
2016/11/03 23:15:25
Done.
| |
33 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | |
34 on_result_callback_ = callback; | |
38 | 35 |
39 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) { | 36 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) { |
40 dispatcher_->NotifyLockError(request_id, | 37 NotifyLockResult(ScreenOrientationLockResult:: |
41 blink::WebLockOrientationErrorNotAvailable); | 38 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE); |
42 return; | 39 return; |
43 } | 40 } |
44 | 41 |
45 if (delegate_->FullScreenRequired(web_contents())) { | 42 if (delegate_->FullScreenRequired(web_contents())) { |
46 RenderViewHostImpl* rvhi = | 43 RenderViewHostImpl* rvhi = |
47 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); | 44 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); |
48 if (!rvhi) { | 45 if (!rvhi) { |
49 dispatcher_->NotifyLockError(request_id, | 46 NotifyLockResult(ScreenOrientationLockResult:: |
50 blink::WebLockOrientationErrorCanceled); | 47 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); |
51 return; | 48 return; |
52 } | 49 } |
53 if (!static_cast<WebContentsImpl*>(web_contents()) | 50 if (!static_cast<WebContentsImpl*>(web_contents()) |
54 ->IsFullscreenForCurrentTab()) { | 51 ->IsFullscreenForCurrentTab()) { |
55 dispatcher_->NotifyLockError(request_id, | 52 NotifyLockResult( |
56 blink::WebLockOrientationErrorFullscreenRequired); | 53 ScreenOrientationLockResult:: |
54 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED); | |
57 return; | 55 return; |
58 } | 56 } |
59 } | 57 } |
60 | 58 |
61 if (lock_orientation == blink::WebScreenOrientationLockNatural) { | 59 if (orientation == blink::WebScreenOrientationLockNatural) { |
62 lock_orientation = GetNaturalLockType(); | 60 orientation = GetNaturalLockType(); |
63 if (lock_orientation == blink::WebScreenOrientationLockDefault) { | 61 if (orientation == blink::WebScreenOrientationLockDefault) { |
64 // We are in a broken state, let's pretend we got canceled. | 62 // We are in a broken state, let's pretend we got canceled. |
65 dispatcher_->NotifyLockError(request_id, | 63 NotifyLockResult(ScreenOrientationLockResult:: |
66 blink::WebLockOrientationErrorCanceled); | 64 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); |
67 return; | 65 return; |
68 } | 66 } |
69 } | 67 } |
70 | 68 |
71 lock_applied_ = true; | 69 lock_applied_ = true; |
72 delegate_->Lock(web_contents(), lock_orientation); | 70 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 | 71 |
79 // If the orientation we are locking to matches the current orientation, we | 72 // If the orientation we are locking to matches the current orientation, we |
80 // should succeed immediately. | 73 // should succeed immediately. |
81 if (LockMatchesCurrentOrientation(lock_orientation)) { | 74 if (LockMatchesCurrentOrientation(orientation)) { |
82 dispatcher_->NotifyLockSuccess(request_id); | 75 NotifyLockResult( |
76 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | |
83 return; | 77 return; |
84 } | 78 } |
85 | 79 |
86 pending_lock_.reset(new LockInformation(request_id, lock_orientation)); | 80 pending_lock_orientation_ = orientation; |
87 } | 81 } |
88 | 82 |
89 void ScreenOrientationProvider::UnlockOrientation() { | 83 void ScreenOrientationProvider::UnlockOrientation() { |
90 if (!lock_applied_ || !delegate_) | 84 if (!lock_applied_ || !delegate_) |
91 return; | 85 return; |
92 | 86 |
93 delegate_->Unlock(web_contents()); | 87 delegate_->Unlock(web_contents()); |
94 | 88 |
95 lock_applied_ = false; | 89 lock_applied_ = false; |
96 pending_lock_.reset(); | |
blundell
2016/10/25 16:09:26
Should you reset pending_lock_orientation_ to Defa
lunalu1
2016/11/03 23:15:25
Done.
| |
97 } | 90 } |
98 | 91 |
99 void ScreenOrientationProvider::OnOrientationChange() { | 92 void ScreenOrientationProvider::OnOrientationChange() { |
100 if (!pending_lock_.get()) | 93 if (pending_lock_orientation_ == blink::WebScreenOrientationLockDefault) |
101 return; | 94 return; |
102 | 95 |
103 if (LockMatchesCurrentOrientation(pending_lock_->lock)) { | 96 if (LockMatchesCurrentOrientation(pending_lock_orientation_)) { |
104 dispatcher_->NotifyLockSuccess(pending_lock_->request_id); | 97 DCHECK(!on_result_callback_.is_null()); |
105 pending_lock_.reset(); | 98 NotifyLockResult( |
99 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | |
106 } | 100 } |
107 } | 101 } |
108 | 102 |
103 void ScreenOrientationProvider::NotifyLockResult( | |
104 ScreenOrientationLockResult result) { | |
105 if (on_result_callback_.is_null()) | |
106 return; | |
107 on_result_callback_.Run(result); | |
108 pending_lock_orientation_ = blink::WebScreenOrientationLockDefault; | |
109 on_result_callback_ = LockOrientationCallback(); | |
110 } | |
111 | |
109 void ScreenOrientationProvider::SetDelegate( | 112 void ScreenOrientationProvider::SetDelegate( |
110 ScreenOrientationDelegate* delegate) { | 113 ScreenOrientationDelegate* delegate) { |
111 delegate_ = delegate; | 114 delegate_ = delegate; |
112 } | 115 } |
113 | 116 |
114 void ScreenOrientationProvider::DidToggleFullscreenModeForTab( | 117 void ScreenOrientationProvider::DidToggleFullscreenModeForTab( |
115 bool entered_fullscreen, bool will_cause_resize) { | 118 bool entered_fullscreen, bool will_cause_resize) { |
116 if (!lock_applied_ || !delegate_) | 119 if (!lock_applied_ || !delegate_) |
117 return; | 120 return; |
118 | 121 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 case blink::WebScreenOrientationLockDefault: | 198 case blink::WebScreenOrientationLockDefault: |
196 NOTREACHED(); | 199 NOTREACHED(); |
197 return false; | 200 return false; |
198 } | 201 } |
199 | 202 |
200 NOTREACHED(); | 203 NOTREACHED(); |
201 return false; | 204 return false; |
202 } | 205 } |
203 | 206 |
204 } // namespace content | 207 } // namespace content |
OLD | NEW |