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

Side by Side Diff: Source/modules/screen_orientation/ScreenOrientation.cpp

Issue 132113006: Add initial Blink-side support for the Screen Orientation API (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Add Kenneth as OWNER Created 6 years, 10 months 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "ScreenOrientation.h"
7
8 #include "core/frame/Frame.h"
9 #include "core/frame/Screen.h"
10 #include "core/page/ChromeClient.h"
11 #include "modules/screen_orientation/ScreenOrientationClient.h"
12 #include "modules/screen_orientation/ScreenOrientationController.h"
13
14 namespace WebCore {
15
16 ScreenOrientation::ScreenOrientation(Screen* screen)
17 : DOMWindowProperty(screen->frame())
18 , PageLifecycleObserver(page())
19 , ActiveDOMObject(frame() ? frame()->document() : 0)
20 , m_screen(screen)
21 , m_orientationLockTimer(this, &ScreenOrientation::orientationLockTimerFired )
22 , m_lockedOrientations(OrientationAny)
23 {
24 ASSERT(screen);
25 ScreenOrientationController* controller = ScreenOrientationController::from( page());
26 if (controller)
27 controller->addObserver(this);
28 }
29
30 ScreenOrientation::~ScreenOrientation()
31 {
32 }
33
34 const char* ScreenOrientation::supplementName()
35 {
36 return "ScreenOrientation";
37 }
38
39 void ScreenOrientation::orientationLockTimerFired(Timer<ScreenOrientation>*)
40 {
41 ScreenOrientationClient* client = ScreenOrientationController::clientFrom(pa ge());
42 if (!client)
43 return;
44
45 client->lockOrientation(m_lockedOrientations);
46 }
47
48 void ScreenOrientation::didCommitLoad(Frame*)
49 {
50 // New page loaded, unlock screen orientation.
kenneth.r.christiansen 2014/02/12 13:42:37 so basically this should return to the default UA
51 unlockOrientation();
52 }
53
54 void ScreenOrientation::willDestroyGlobalObjectInFrame()
55 {
56 m_screen = 0;
57 DOMWindowProperty::willDestroyGlobalObjectInFrame();
58 }
59
60 void ScreenOrientation::willDetachGlobalObjectFromFrame()
61 {
62 m_screen = 0;
63 DOMWindowProperty::willDetachGlobalObjectFromFrame();
64 }
65
66 void ScreenOrientation::stop()
67 {
68 ScreenOrientationController* controller = ScreenOrientationController::from( page());
69 if (controller)
70 controller->removeObserver(this);
71 }
72
73 Page* ScreenOrientation::page() const
74 {
75 return frame() ? frame()->page() : 0;
76 }
77
78 ScreenOrientation* ScreenOrientation::from(Screen* screen)
79 {
80 ScreenOrientation* supplement = static_cast<ScreenOrientation*>(Supplement<S creen>::from(screen, supplementName()));
81 if (!supplement) {
82 supplement = new ScreenOrientation(screen);
83 supplement->suspendIfNeeded();
84 provideTo(screen, supplementName(), adoptPtr(supplement));
85 }
86 return supplement;
87 }
88
89 struct ScreenOrientationInfo {
90 const AtomicString& name;
91 ScreenOrientationValues orientation;
92 };
93
94 static ScreenOrientationInfo* allowedOrientationsMap(unsigned& length)
95 {
96 DEFINE_STATIC_LOCAL(const AtomicString, portrait, ("portrait", AtomicString: :ConstructFromLiteral));
97 DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary" , AtomicString::ConstructFromLiteral));
98 DEFINE_STATIC_LOCAL(const AtomicString, portraitSecondary, ("portrait-second ary", AtomicString::ConstructFromLiteral));
99 DEFINE_STATIC_LOCAL(const AtomicString, landscape, ("landscape", AtomicStrin g::ConstructFromLiteral));
100 DEFINE_STATIC_LOCAL(const AtomicString, landscapePrimary, ("landscape-primar y", AtomicString::ConstructFromLiteral));
101 DEFINE_STATIC_LOCAL(const AtomicString, landscapeSecondary, ("landscape-seco ndary", AtomicString::ConstructFromLiteral));
102
103 static ScreenOrientationInfo orientationMap[] = {
104 { portrait, OrientationPortrait },
105 { portraitPrimary, OrientationPortraitPrimary },
106 { portraitSecondary, OrientationPortraitSecondary },
107 { landscape, OrientationLandscape },
108 { landscapePrimary, OrientationLandscapePrimary },
109 { landscapeSecondary, OrientationLandscapeSecondary }
110 };
111 length = WTF_ARRAY_LENGTH(orientationMap);
112 return orientationMap;
113 }
114
115 const AtomicString& ScreenOrientation::orientationToString(ScreenOrientationValu e orientation)
116 {
117 unsigned length = 0;
118 ScreenOrientationInfo* orientationMap = allowedOrientationsMap(length);
119 for (unsigned i = 0; i < length; ++i) {
120 if (orientationMap[i].orientation == orientation)
121 return orientationMap[i].name;
122 }
123 ASSERT_NOT_REACHED();
124 return nullAtom;
125 }
126
127 ScreenOrientationValues ScreenOrientation::stringToOrientationValues(const Atomi cString& orientation)
128 {
129 unsigned length = 0;
130 ScreenOrientationInfo* orientationMap = allowedOrientationsMap(length);
131 for (unsigned i = 0; i < length; ++i) {
132 if (orientationMap[i].name == orientation)
133 return orientationMap[i].orientation;
134 }
135 return OrientationInvalid;
136 }
137
138 const AtomicString& ScreenOrientation::orientation(Screen* screen)
139 {
140 ScreenOrientation* screenOrientation = ScreenOrientation::from(screen);
141 ASSERT(screenOrientation);
142
143 ScreenOrientationController* controller = ScreenOrientationController::from( screenOrientation->page());
144 if (!controller)
145 return orientationToString(OrientationPortraitPrimary);
146
147 return orientationToString(controller->orientation());
148 }
149
150 bool ScreenOrientation::lockOrientation(Screen* screen, const Vector<String>& or ientationsVector)
151 {
152 ScreenOrientationValues orientations = OrientationInvalid;
153
154 for (unsigned i = 0; i < orientationsVector.size(); ++i) {
155 ScreenOrientationValues inputOrientation = stringToOrientationValues(Ato micString(orientationsVector[i]));
156 if (inputOrientation == OrientationInvalid)
157 return false;
158 orientations |= inputOrientation;
159 }
160
161 if (!orientations)
162 return false;
163
164 ScreenOrientation* screenOrientation = ScreenOrientation::from(screen);
165 ASSERT(screenOrientation);
166 screenOrientation->lockOrientationAsync(orientations);
167 return true;
168 }
169
170 bool ScreenOrientation::lockOrientation(Screen* screen, const AtomicString& orie ntationString)
171 {
172 ScreenOrientationValues orientation = stringToOrientationValues(orientationS tring);
173 if (!orientation)
174 return false;
175
176 ScreenOrientation* screenOrientation = ScreenOrientation::from(screen);
177 ASSERT(screenOrientation);
178 screenOrientation->lockOrientationAsync(orientation);
179 return true;
180 }
181
182 void ScreenOrientation::lockOrientationAsync(ScreenOrientationValues orientation s)
183 {
184 ASSERT(!(orientations & OrientationInvalid));
185
186 m_lockedOrientations = orientations;
187 if (!m_orientationLockTimer.isActive())
188 m_orientationLockTimer.startOneShot(0);
189 }
190
191 void ScreenOrientation::unlockOrientation(Screen* screen)
192 {
193 ScreenOrientation* screenOrientation = ScreenOrientation::from(screen);
194 ASSERT(screenOrientation);
195 screenOrientation->unlockOrientation();
196 }
197
198 void ScreenOrientation::unlockOrientation()
199 {
200 if (m_lockedOrientations == OrientationAny)
kenneth.r.christiansen 2014/02/12 13:42:37 This is what might be wrong depending on the defau
201 return; // Not currently locked.
202
203 m_lockedOrientations = OrientationAny;
204
205 ScreenOrientationClient* client = ScreenOrientationController::clientFrom(pa ge());
206 if (client)
207 client->unlockOrientation();
208 }
209
210 void ScreenOrientation::orientationChanged()
211 {
212 if (m_screen)
213 m_screen->dispatchEvent(Event::create(EventTypeNames::orientationchange) );
214 }
215
216 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698