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/frame/LocalDOMWindow.h" | 8 #include "core/events/Event.h" |
9 #include "core/frame/FrameView.h" | 9 #include "core/frame/FrameView.h" |
10 #include "core/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
11 #include "core/frame/Screen.h" | |
12 #include "core/page/Page.h" | 11 #include "core/page/Page.h" |
12 #include "modules/screen_orientation/ScreenOrientation.h" | |
13 #include "platform/LayoutTestSupport.h" | 13 #include "platform/LayoutTestSupport.h" |
14 #include "platform/PlatformScreen.h" | 14 #include "platform/PlatformScreen.h" |
15 #include "public/platform/WebScreenOrientationClient.h" | 15 #include "public/platform/WebScreenOrientationClient.h" |
16 | 16 |
17 namespace WebCore { | 17 namespace WebCore { |
18 | 18 |
19 ScreenOrientationController::~ScreenOrientationController() | 19 ScreenOrientationController::~ScreenOrientationController() |
20 { | 20 { |
21 } | 21 } |
22 | 22 |
23 void ScreenOrientationController::persistentHostHasBeenDestroyed() | 23 void ScreenOrientationController::persistentHostHasBeenDestroyed() |
24 { | 24 { |
25 // Unregister lifecycle observation once page is being torn down. | 25 // Unregister lifecycle observation once page is being torn down. |
26 observeContext(0); | 26 observeContext(0); |
27 } | 27 } |
28 | 28 |
29 void ScreenOrientationController::provideTo(LocalFrame& frame, blink::WebScreenO rientationClient* client) | 29 void ScreenOrientationController::provideTo(LocalFrame& frame, blink::WebScreenO rientationClient* client) |
30 { | 30 { |
31 ScreenOrientationController* controller = new ScreenOrientationController(fr ame, client); | 31 ScreenOrientationController* controller = new ScreenOrientationController(fr ame, client); |
32 WillBeHeapSupplement<LocalFrame>::provideTo(frame, supplementName(), adoptPt rWillBeNoop(controller)); | 32 WillBeHeapSupplement<LocalFrame>::provideTo(frame, supplementName(), adoptPt rWillBeNoop(controller)); |
33 } | 33 } |
34 | 34 |
35 ScreenOrientationController& ScreenOrientationController::from(LocalFrame& frame ) | 35 ScreenOrientationController* ScreenOrientationController::from(LocalFrame& frame ) |
36 { | 36 { |
37 return *static_cast<ScreenOrientationController*>(WillBeHeapSupplement<Local Frame>::from(frame, supplementName())); | 37 return static_cast<ScreenOrientationController*>(WillBeHeapSupplement<LocalF rame>::from(frame, supplementName())); |
38 } | 38 } |
39 | 39 |
40 ScreenOrientationController::ScreenOrientationController(LocalFrame& frame, blin k::WebScreenOrientationClient* client) | 40 ScreenOrientationController::ScreenOrientationController(LocalFrame& frame, blin k::WebScreenOrientationClient* client) |
41 : PageLifecycleObserver(frame.page()) | 41 : PageLifecycleObserver(frame.page()) |
42 , m_overrideOrientation(blink::WebScreenOrientationUndefined) | |
43 , m_client(client) | 42 , m_client(client) |
44 , m_frame(frame) | 43 , m_frame(frame) |
45 { | 44 { |
46 } | 45 } |
47 | 46 |
48 const char* ScreenOrientationController::supplementName() | 47 const char* ScreenOrientationController::supplementName() |
49 { | 48 { |
50 return "ScreenOrientationController"; | 49 return "ScreenOrientationController"; |
51 } | 50 } |
52 | 51 |
(...skipping 17 matching lines...) Expand all Loading... | |
70 case 180: | 69 case 180: |
71 return isTallDisplay ? blink::WebScreenOrientationPortraitSecondary : bl ink::WebScreenOrientationLandscapeSecondary; | 70 return isTallDisplay ? blink::WebScreenOrientationPortraitSecondary : bl ink::WebScreenOrientationLandscapeSecondary; |
72 case 270: | 71 case 270: |
73 return isTallDisplay ? blink::WebScreenOrientationLandscapeSecondary : b link::WebScreenOrientationPortraitPrimary; | 72 return isTallDisplay ? blink::WebScreenOrientationLandscapeSecondary : b link::WebScreenOrientationPortraitPrimary; |
74 default: | 73 default: |
75 ASSERT_NOT_REACHED(); | 74 ASSERT_NOT_REACHED(); |
76 return blink::WebScreenOrientationPortraitPrimary; | 75 return blink::WebScreenOrientationPortraitPrimary; |
77 } | 76 } |
78 } | 77 } |
79 | 78 |
80 void ScreenOrientationController::pageVisibilityChanged() | 79 void ScreenOrientationController::updateOrientation() |
81 { | 80 { |
82 if (page() && page()->visibilityState() == PageVisibilityStateVisible) { | 81 ASSERT(m_orientation); |
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 | 82 |
108 blink::WebScreenOrientationType orientationType = screenOrientationType(m_fr ame.view()); | 83 blink::WebScreenOrientationType orientationType = screenOrientationType(m_fr ame.view()); |
109 if (orientationType == blink::WebScreenOrientationUndefined) { | 84 if (orientationType == blink::WebScreenOrientationUndefined) { |
110 // The embedder could not provide us with an orientation, deduce it ours elves. | 85 // The embedder could not provide us with an orientation, deduce it ours elves. |
111 orientationType = computeOrientation(m_frame.view()); | 86 orientationType = computeOrientation(m_frame.view()); |
112 } | 87 } |
113 ASSERT(orientationType != blink::WebScreenOrientationUndefined); | 88 ASSERT(orientationType != blink::WebScreenOrientationUndefined); |
114 return orientationType; | 89 |
90 m_orientation->setType(orientationType); | |
91 m_orientation->setAngle(screenOrientationAngle(m_frame.view())); | |
115 } | 92 } |
116 | 93 |
117 void ScreenOrientationController::lockOrientation(blink::WebScreenOrientationLoc kType orientation, blink::WebLockOrientationCallback* callback) | 94 void ScreenOrientationController::pageVisibilityChanged() |
95 { | |
96 if (!m_orientation || !page() || page()->visibilityState() != PageVisibility StateVisible) | |
97 return; | |
98 | |
99 // The orientation type and angle are tied in a way that if the angle has | |
100 // changed, the type must have changed. | |
101 unsigned short currentAngle = screenOrientationAngle(m_frame.view()); | |
102 | |
103 // FIXME: sendOrientationChangeEvent() currently send an event all the | |
104 // children of the frame, so it should only be called on the frame on | |
105 // top of the tree. We would need the embedder to call | |
106 // sendOrientationChangeEvent on every WebFrame part of a WebView to be | |
107 // able to remove this. | |
108 if (m_frame == m_frame.localFrameRoot() && m_orientation->angle() != current Angle) | |
109 notifyOrientationChanged(); | |
110 } | |
111 | |
112 void ScreenOrientationController::notifyOrientationChanged() | |
113 { | |
114 ASSERT(RuntimeEnabledFeatures::screenOrientationEnabled()); | |
115 | |
116 if (!m_orientation || !page() || page()->visibilityState() != PageVisibility StateVisible) | |
117 return; | |
118 | |
119 updateOrientation(); | |
120 | |
121 // Keep track of the frames that need to be notified before notifying the | |
122 // current frame as it will prevent side effects from the change event | |
123 // handlers. | |
124 Vector<RefPtr<LocalFrame> > childFrames; | |
125 for (Frame* child = m_frame.tree().firstChild(); child; child = child->tree( ).nextSibling()) { | |
126 if (child->isLocalFrame()) | |
127 childFrames.append(toLocalFrame(child)); | |
128 } | |
129 | |
130 // Notify current orientation object. | |
131 m_orientation->dispatchEvent(Event::create(EventTypeNames::change)); | |
132 | |
133 // ... and child frames, if they have a ScreenOrientationController. | |
134 for (size_t i = 0; i < childFrames.size(); ++i) { | |
135 if (!ScreenOrientationController::from(*childFrames[i])) | |
sof
2014/07/09 07:02:53
Would be tidier as
ScreenOrientationControlle
mlamouri (slow - plz ping)
2014/07/09 09:17:29
Done.
| |
136 continue; | |
137 ScreenOrientationController::from(*childFrames[i])->notifyOrientationCha nged(); | |
138 } | |
139 } | |
140 | |
141 void ScreenOrientationController::setOrientation(ScreenOrientation* orientation) | |
142 { | |
143 m_orientation = orientation; | |
144 if (m_orientation) | |
145 updateOrientation(); | |
146 } | |
147 | |
148 void ScreenOrientationController::lock(blink::WebScreenOrientationLockType orien tation, blink::WebLockOrientationCallback* callback) | |
118 { | 149 { |
119 if (!m_client) { | 150 if (!m_client) { |
120 return; | 151 return; |
121 } | 152 } |
122 | 153 |
123 m_client->lockOrientation(orientation, callback); | 154 m_client->lockOrientation(orientation, callback); |
124 } | 155 } |
125 | 156 |
126 void ScreenOrientationController::unlockOrientation() | 157 void ScreenOrientationController::unlock() |
127 { | 158 { |
128 if (!m_client) { | 159 if (!m_client) { |
129 return; | 160 return; |
130 } | 161 } |
131 | 162 |
132 m_client->unlockOrientation(); | 163 m_client->unlockOrientation(); |
133 } | 164 } |
134 | 165 |
166 const LocalFrame& ScreenOrientationController::frame() const | |
167 { | |
168 return m_frame; | |
169 } | |
170 | |
171 void ScreenOrientationController::trace(Visitor* visitor) | |
172 { | |
173 visitor->trace(m_orientation); | |
174 WillBeHeapSupplement<LocalFrame>::trace(visitor); | |
175 } | |
176 | |
135 } // namespace WebCore | 177 } // namespace WebCore |
OLD | NEW |