OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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/browser/screen_orientation/screen_orientation_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "content/public/browser/navigation_details.h" | |
9 #include "content/public/browser/screen_orientation_provider.h" | |
10 #include "content/public/browser/web_contents.h" | |
11 | |
12 namespace content { | |
13 | |
14 using typename ::blink::mojom::ScreenOrientationLockResult; | |
15 | |
16 ScreenOrientation::ScreenOrientation(WebContents* web_contents) | |
17 : binding_(web_contents, this), | |
18 weak_factory_(this) { | |
19 provider_.reset(new ScreenOrientationProvider(web_contents)); | |
20 } | |
21 | |
22 ScreenOrientation::~ScreenOrientation() = default; | |
23 | |
24 void ScreenOrientation::LockOrientation( | |
25 blink::WebScreenOrientationLockType orientation, | |
26 const LockOrientationCallback& callback) { | |
27 // When a request comes in while another request was already outstanding. | |
28 if (!on_result_callback_.is_null()) { | |
29 NotifyLockResult(ScreenOrientationLockResult:: | |
30 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | |
31 } | |
32 | |
33 on_result_callback_ = callback; | |
34 | |
35 if (!provider_) { | |
36 NotifyLockResult(ScreenOrientationLockResult:: | |
37 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE); | |
38 return; | |
39 } | |
40 | |
41 provider_->LockOrientation( | |
42 orientation, base::Bind(&ScreenOrientation::NotifyLockResult, | |
43 weak_factory_.GetWeakPtr())); | |
44 } | |
45 | |
46 void ScreenOrientation::UnlockOrientation() { | |
47 // Cancel any pending lock request. | |
48 NotifyLockResult(ScreenOrientationLockResult:: | |
49 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | |
50 | |
51 if (provider_) | |
52 provider_->UnlockOrientation(); | |
53 } | |
54 | |
55 void ScreenOrientation::DidNavigateMainFrame( | |
56 const LoadCommittedDetails& details, | |
57 const FrameNavigateParams& params) { | |
58 if (!provider_ || details.is_in_page) | |
59 return; | |
60 provider_->UnlockOrientation(); | |
61 } | |
62 | |
63 void ScreenOrientation::NotifyLockResult( | |
64 ScreenOrientationLockResult result) { | |
65 if (on_result_callback_.is_null()) | |
66 return; | |
67 | |
68 on_result_callback_.Run(result); | |
69 // Reset the callback. | |
blundell
2016/10/28 21:21:32
nit: I don't think this comment is necessary.
lunalu1
2016/10/31 18:02:53
Done.
| |
70 on_result_callback_ = LockOrientationCallback(); | |
71 } | |
72 | |
73 ScreenOrientationProvider* | |
74 ScreenOrientation::GetScreenOrientationProvider() { | |
75 return provider_.get(); | |
76 } | |
77 | |
78 } // namespace content | |
OLD | NEW |