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 "config.h" | 5 #include "config.h" |
6 #include "modules/screen_orientation/ScreenOrientationController.h" | 6 #include "modules/screen_orientation/ScreenOrientationController.h" |
7 | 7 |
| 8 #include "core/events/Event.h" |
8 #include "core/frame/LocalDOMWindow.h" | 9 #include "core/frame/LocalDOMWindow.h" |
9 #include "core/frame/FrameView.h" | 10 #include "core/frame/FrameView.h" |
10 #include "core/frame/LocalFrame.h" | 11 #include "core/frame/LocalFrame.h" |
11 #include "core/frame/Screen.h" | 12 #include "core/frame/Screen.h" |
12 #include "core/page/Page.h" | 13 #include "core/page/Page.h" |
| 14 #include "modules/screen_orientation/OrientationInformation.h" |
13 #include "platform/LayoutTestSupport.h" | 15 #include "platform/LayoutTestSupport.h" |
14 #include "platform/PlatformScreen.h" | 16 #include "platform/PlatformScreen.h" |
15 #include "public/platform/WebScreenOrientationClient.h" | 17 #include "public/platform/WebScreenOrientationClient.h" |
16 | 18 |
| 19 |
17 namespace WebCore { | 20 namespace WebCore { |
18 | 21 |
19 ScreenOrientationController::~ScreenOrientationController() | 22 ScreenOrientationController::~ScreenOrientationController() |
20 { | 23 { |
21 } | 24 } |
22 | 25 |
23 void ScreenOrientationController::persistentHostHasBeenDestroyed() | 26 void ScreenOrientationController::persistentHostHasBeenDestroyed() |
24 { | 27 { |
25 // Unregister lifecycle observation once page is being torn down. | 28 // Unregister lifecycle observation once page is being torn down. |
26 observeContext(0); | 29 observeContext(0); |
27 } | 30 } |
28 | 31 |
29 void ScreenOrientationController::provideTo(LocalFrame& frame, blink::WebScreenO
rientationClient* client) | 32 void ScreenOrientationController::provideTo(LocalFrame& frame, blink::WebScreenO
rientationClient* client) |
30 { | 33 { |
31 ScreenOrientationController* controller = new ScreenOrientationController(fr
ame, client); | 34 ScreenOrientationController* controller = new ScreenOrientationController(fr
ame, client); |
32 WillBeHeapSupplement<LocalFrame>::provideTo(frame, supplementName(), adoptPt
rWillBeNoop(controller)); | 35 WillBeHeapSupplement<LocalFrame>::provideTo(frame, supplementName(), adoptPt
rWillBeNoop(controller)); |
33 } | 36 } |
34 | 37 |
35 ScreenOrientationController& ScreenOrientationController::from(LocalFrame& frame
) | 38 ScreenOrientationController& ScreenOrientationController::from(LocalFrame& frame
) |
36 { | 39 { |
37 return *static_cast<ScreenOrientationController*>(WillBeHeapSupplement<Local
Frame>::from(frame, supplementName())); | 40 return *static_cast<ScreenOrientationController*>(WillBeHeapSupplement<Local
Frame>::from(frame, supplementName())); |
38 } | 41 } |
39 | 42 |
40 ScreenOrientationController::ScreenOrientationController(LocalFrame& frame, blin
k::WebScreenOrientationClient* client) | 43 ScreenOrientationController::ScreenOrientationController(LocalFrame& frame, blin
k::WebScreenOrientationClient* client) |
41 : PageLifecycleObserver(frame.page()) | 44 : PageLifecycleObserver(frame.page()) |
42 , m_overrideOrientation(blink::WebScreenOrientationUndefined) | 45 , m_orientation(new OrientationInformation) |
43 , m_client(client) | 46 , m_client(client) |
44 , m_frame(frame) | 47 , m_frame(frame) |
45 { | 48 { |
46 } | 49 } |
47 | 50 |
48 const char* ScreenOrientationController::supplementName() | 51 const char* ScreenOrientationController::supplementName() |
49 { | 52 { |
50 return "ScreenOrientationController"; | 53 return "ScreenOrientationController"; |
51 } | 54 } |
52 | 55 |
(...skipping 17 matching lines...) Expand all Loading... |
70 case 180: | 73 case 180: |
71 return isTallDisplay ? blink::WebScreenOrientationPortraitSecondary : bl
ink::WebScreenOrientationLandscapeSecondary; | 74 return isTallDisplay ? blink::WebScreenOrientationPortraitSecondary : bl
ink::WebScreenOrientationLandscapeSecondary; |
72 case 270: | 75 case 270: |
73 return isTallDisplay ? blink::WebScreenOrientationLandscapeSecondary : b
link::WebScreenOrientationPortraitPrimary; | 76 return isTallDisplay ? blink::WebScreenOrientationLandscapeSecondary : b
link::WebScreenOrientationPortraitPrimary; |
74 default: | 77 default: |
75 ASSERT_NOT_REACHED(); | 78 ASSERT_NOT_REACHED(); |
76 return blink::WebScreenOrientationPortraitPrimary; | 79 return blink::WebScreenOrientationPortraitPrimary; |
77 } | 80 } |
78 } | 81 } |
79 | 82 |
80 void ScreenOrientationController::pageVisibilityChanged() | 83 void ScreenOrientationController::updateOrientation() |
81 { | 84 { |
82 if (page() && page()->visibilityState() == PageVisibilityStateVisible) { | |
83 blink::WebScreenOrientationType oldOrientation = m_overrideOrientation; | |
84 m_overrideOrientation = blink::WebScreenOrientationUndefined; | |
85 // FIXME: sendOrientationChangeEvent() currently send an event all the | |
86 // children of the frame, so it should only be called on the frame on | |
87 // top of the tree. We would need the embedder to call | |
88 // sendOrientationChangeEvent on every WebFrame part of a WebView to be | |
89 // able to remove this. | |
90 if (m_frame == m_frame.localFrameRoot() && oldOrientation != orientation
()) | |
91 m_frame.sendOrientationChangeEvent(); | |
92 } else if (m_overrideOrientation == blink::WebScreenOrientationUndefined) { | |
93 // The page is no longer visible, store the last know screen orientation | |
94 // so that we keep returning this orientation until the page becomes | |
95 // visible again. | |
96 m_overrideOrientation = orientation(); | |
97 } | |
98 } | |
99 | |
100 blink::WebScreenOrientationType ScreenOrientationController::orientation() const | |
101 { | |
102 if (m_overrideOrientation != blink::WebScreenOrientationUndefined) { | |
103 // The page is not visible, keep returning the last known screen orienta
tion. | |
104 ASSERT(!page() || page()->visibilityState() != PageVisibilityStateVisibl
e); | |
105 return m_overrideOrientation; | |
106 } | |
107 | |
108 blink::WebScreenOrientationType orientationType = screenOrientationType(m_fr
ame.view()); | 85 blink::WebScreenOrientationType orientationType = screenOrientationType(m_fr
ame.view()); |
109 if (orientationType == blink::WebScreenOrientationUndefined) { | 86 if (orientationType == blink::WebScreenOrientationUndefined) { |
110 // The embedder could not provide us with an orientation, deduce it ours
elves. | 87 // The embedder could not provide us with an orientation, deduce it ours
elves. |
111 orientationType = computeOrientation(m_frame.view()); | 88 orientationType = computeOrientation(m_frame.view()); |
112 } | 89 } |
113 ASSERT(orientationType != blink::WebScreenOrientationUndefined); | 90 ASSERT(orientationType != blink::WebScreenOrientationUndefined); |
114 return orientationType; | 91 |
| 92 m_orientation->setType(orientationType); |
| 93 m_orientation->setAngle(screenOrientationAngle(m_frame.view())); |
| 94 } |
| 95 |
| 96 void ScreenOrientationController::pageVisibilityChanged() |
| 97 { |
| 98 if (!page() || page()->visibilityState() != PageVisibilityStateVisible) |
| 99 return; |
| 100 |
| 101 OrientationInformation previousOrientation = *m_orientation; |
| 102 updateOrientation(); |
| 103 |
| 104 // FIXME: sendOrientationChangeEvent() currently send an event all the |
| 105 // children of the frame, so it should only be called on the frame on |
| 106 // top of the tree. We would need the embedder to call |
| 107 // sendOrientationChangeEvent on every WebFrame part of a WebView to be |
| 108 // able to remove this. |
| 109 if (m_frame == m_frame.localFrameRoot() && previousOrientation != *m_orienta
tion) |
| 110 ScreenOrientationController::from(m_frame).notifyOrientationChanged(); |
| 111 } |
| 112 |
| 113 void ScreenOrientationController::notifyOrientationChanged() |
| 114 { |
| 115 if (!RuntimeEnabledFeatures::orientationEventEnabled() && !RuntimeEnabledFea
tures::screenOrientationEnabled()) |
| 116 return; |
| 117 |
| 118 if (!page() || page()->visibilityState() != PageVisibilityStateVisible) |
| 119 return; |
| 120 |
| 121 updateOrientation(); |
| 122 |
| 123 LocalDOMWindow* window = m_frame.domWindow(); |
| 124 if (!window) |
| 125 return; |
| 126 window->dispatchEvent(Event::create(EventTypeNames::orientationchange)); |
| 127 |
| 128 // Notify subframes. |
| 129 Vector<RefPtr<LocalFrame> > childFrames; |
| 130 for (Frame* child = m_frame.tree().firstChild(); child; child = child->tree(
).nextSibling()) { |
| 131 if (child->isLocalFrame()) |
| 132 childFrames.append(toLocalFrame(child)); |
| 133 } |
| 134 |
| 135 for (size_t i = 0; i < childFrames.size(); ++i) |
| 136 ScreenOrientationController::from(*childFrames[i]).notifyOrientationChan
ged(); |
| 137 } |
| 138 |
| 139 OrientationInformation* ScreenOrientationController::orientation() |
| 140 { |
| 141 if (!m_orientation->initialized()) |
| 142 updateOrientation(); |
| 143 |
| 144 return m_orientation.get(); |
115 } | 145 } |
116 | 146 |
117 void ScreenOrientationController::lockOrientation(blink::WebScreenOrientationLoc
kType orientation, blink::WebLockOrientationCallback* callback) | 147 void ScreenOrientationController::lockOrientation(blink::WebScreenOrientationLoc
kType orientation, blink::WebLockOrientationCallback* callback) |
118 { | 148 { |
119 if (!m_client) { | 149 if (!m_client) { |
120 return; | 150 return; |
121 } | 151 } |
122 | 152 |
123 m_client->lockOrientation(orientation, callback); | 153 m_client->lockOrientation(orientation, callback); |
124 } | 154 } |
125 | 155 |
126 void ScreenOrientationController::unlockOrientation() | 156 void ScreenOrientationController::unlockOrientation() |
127 { | 157 { |
128 if (!m_client) { | 158 if (!m_client) { |
129 return; | 159 return; |
130 } | 160 } |
131 | 161 |
132 m_client->unlockOrientation(); | 162 m_client->unlockOrientation(); |
133 } | 163 } |
134 | 164 |
| 165 void ScreenOrientationController::trace(Visitor* visitor) |
| 166 { |
| 167 WillBeHeapSupplement<LocalFrame>::trace(visitor); |
| 168 visitor->trace(m_orientation); |
| 169 } |
| 170 |
135 } // namespace WebCore | 171 } // namespace WebCore |
OLD | NEW |