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

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 locked-no-orientation-change-event.html layout test 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 , m_screen(screen)
19 , m_lockedOrientations(OrientationAny)
20 {
21 ASSERT(screen);
22 if (page())
23 ScreenOrientationController::from(page())->addObserver(this);
24 }
25
26 ScreenOrientation::~ScreenOrientation()
27 {
28 if (page())
29 ScreenOrientationController::from(page())->removeObserver(this);
30 }
31
32 const char* ScreenOrientation::supplementName()
33 {
34 return "ScreenOrientation";
35 }
36
37 Page* ScreenOrientation::page() const
38 {
39 return frame() ? frame()->page() : 0;
40 }
41
42 ScreenOrientation* ScreenOrientation::from(Screen* screen)
43 {
44 ScreenOrientation* supplement = static_cast<ScreenOrientation*>(Supplement<S creen>::from(screen, supplementName()));
45 if (!supplement) {
46 supplement = new ScreenOrientation(screen);
47 provideTo(screen, supplementName(), adoptPtr(supplement));
48 }
49 return supplement;
50 }
51
52 ScreenOrientation::ScreenOrientationInfo* ScreenOrientation::orientationMap(unsi gned& length)
53 {
54 DEFINE_STATIC_LOCAL(const AtomicString, portrait, ("portrait", AtomicString: :ConstructFromLiteral));
55 DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary" , AtomicString::ConstructFromLiteral));
56 DEFINE_STATIC_LOCAL(const AtomicString, portraitSecondary, ("portrait-second ary", AtomicString::ConstructFromLiteral));
57 DEFINE_STATIC_LOCAL(const AtomicString, landscape, ("landscape", AtomicStrin g::ConstructFromLiteral));
58 DEFINE_STATIC_LOCAL(const AtomicString, landscapePrimary, ("landscape-primar y", AtomicString::ConstructFromLiteral));
59 DEFINE_STATIC_LOCAL(const AtomicString, landscapeSecondary, ("landscape-seco ndary", AtomicString::ConstructFromLiteral));
60
61 static ScreenOrientationInfo orientationMap[] = {
62 { portrait, OrientationPortrait },
63 { portraitPrimary, OrientationPortraitPrimary },
64 { portraitSecondary, OrientationPortraitSecondary },
65 { landscape, OrientationLandscape },
66 { landscapePrimary, OrientationLandscapePrimary },
67 { landscapeSecondary, OrientationLandscapeSecondary }
68 };
69 length = WTF_ARRAY_LENGTH(orientationMap);
70 return orientationMap;
71 }
72
73 const AtomicString& ScreenOrientation::orientationToString(ScreenOrientationValu e orientation)
74 {
75 unsigned length = 0;
76 ScreenOrientationInfo* orientationMap = ScreenOrientation::orientationMap(le ngth);
77 for (unsigned i = 0; i < length; ++i) {
78 if (orientationMap[i].orientation == orientation)
79 return orientationMap[i].name;
80 }
81 ASSERT_NOT_REACHED();
82 return nullAtom;
83 }
84
85 ScreenOrientationValues ScreenOrientation::stringToOrientationValues(const Atomi cString& orientation)
86 {
87 unsigned length = 0;
88 ScreenOrientationInfo* orientationMap = ScreenOrientation::orientationMap(le ngth);
89 for (unsigned i = 0; i < length; ++i) {
90 if (orientationMap[i].name == orientation)
91 return orientationMap[i].orientation;
92 }
93 return OrientationInvalid;
94 }
95
96 const AtomicString& ScreenOrientation::orientation(Screen* screen)
97 {
98 ScreenOrientation* screenOrientation = ScreenOrientation::from(screen);
99 ASSERT(screenOrientation);
100
101 Page* page = screenOrientation->page();
102 if (!page)
103 return orientationToString(OrientationPortraitPrimary);
104
105 ScreenOrientationController* controller = ScreenOrientationController::from( page);
106 ASSERT(controller);
107
108 return orientationToString(controller->orientation());
109 }
110
111 bool ScreenOrientation::lockOrientation(Screen* screen, const Vector<String>& or ientations)
112 {
113 ScreenOrientationValues orientation = OrientationInvalid;
114
115 for (unsigned i = 0; i < orientations.size(); ++i) {
116 ScreenOrientationValues inputOrientation = stringToOrientationValues(Ato micString(orientations[i]));
117 if (inputOrientation == OrientationInvalid)
118 return false;
119 orientation |= inputOrientation;
120 }
121
122 if (!orientation)
123 return false;
124
125 ScreenOrientation* screenOrientation = ScreenOrientation::from(screen);
126 ASSERT(screenOrientation);
127 return screenOrientation->lockOrientation(orientation);
128 }
129
130 bool ScreenOrientation::lockOrientation(Screen* screen, const AtomicString& orie ntationString)
131 {
132 ScreenOrientationValues orientation = stringToOrientationValues(orientationS tring);
133 if (!orientation)
134 return false;
135
136 ScreenOrientation* screenOrientation = ScreenOrientation::from(screen);
137 ASSERT(screenOrientation);
138 return screenOrientation->lockOrientation(orientation);
139 }
140
141 bool ScreenOrientation::lockOrientation(ScreenOrientationValues orientations)
142 {
143 if (!(orientations & m_lockedOrientations))
144 return false;
mlamouri (slow - plz ping) 2014/02/10 18:20:57 What are you trying to do here? It sounds like you
Inactive 2014/02/10 22:25:24 This was removed in a later iteration. This part o
145
146 Page* page = this->page();
147 if (!page)
148 return false;
149
150 ScreenOrientationClient* client = ScreenOrientationController::clientFrom(pa ge);
151 if (!client)
152 return false;
153
154 bool locked = client->lockOrientation(orientations);
155 if (locked)
156 m_lockedOrientations &= orientations;
mlamouri (slow - plz ping) 2014/02/10 18:20:57 That doesn't sound needed.
Inactive 2014/02/10 22:25:24 Right, should be a simple assignment. Done.
157 return locked;
158 }
159
160 void ScreenOrientation::unlockOrientation(Screen* screen)
161 {
162 ScreenOrientation* screenOrientation = ScreenOrientation::from(screen);
163 ASSERT(screenOrientation);
164 Page* page = screenOrientation->page();
165 if (!page)
166 return;
167
168 ScreenOrientationClient* client = ScreenOrientationController::clientFrom(pa ge);
169 if (!client)
170 return;
171
172 screenOrientation->m_lockedOrientations = OrientationAny;
173 client->unlockOrientation();
174 }
175
176 void ScreenOrientation::orientationChanged()
177 {
178 #ifndef NDEBUG
179 Page* page = this->page();
180 if (!page)
181 return;
182
183 ScreenOrientationController* controller = ScreenOrientationController::from( page);
184 ASSERT(controller);
185 ASSERT(!m_lockedOrientations || m_lockedOrientations & controller->orientati on());
186 #endif
187
188 m_screen->dispatchEvent(Event::create(EventTypeNames::orientationchange));
189 }
190
191 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698