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/ScreenOrientation.h" | 6 #include "modules/screen_orientation/ScreenOrientation.h" |
7 | 7 |
8 #include "core/frame/DOMWindow.h" | 8 #include "core/frame/DOMWindow.h" |
9 #include "core/frame/Frame.h" | 9 #include "core/frame/Frame.h" |
10 #include "core/frame/Screen.h" | 10 #include "core/frame/Screen.h" |
11 #include "modules/screen_orientation/ScreenOrientationController.h" | 11 #include "modules/screen_orientation/ScreenOrientationController.h" |
12 #include "public/platform/WebScreenOrientation.h" | 12 #include "public/platform/Platform.h" |
13 | 13 |
14 namespace WebCore { | 14 namespace WebCore { |
15 | 15 |
| 16 static const unsigned WebScreenOrientationDefault = 0; |
| 17 |
16 struct ScreenOrientationInfo { | 18 struct ScreenOrientationInfo { |
17 const AtomicString& name; | 19 const AtomicString& name; |
18 blink::WebScreenOrientation orientation; | 20 blink::WebScreenOrientation orientation; |
19 }; | 21 }; |
20 | 22 |
21 static ScreenOrientationInfo* orientationsMap(unsigned& length) | 23 static ScreenOrientationInfo* orientationsMap(unsigned& length) |
22 { | 24 { |
23 DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary"
, AtomicString::ConstructFromLiteral)); | 25 DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary"
, AtomicString::ConstructFromLiteral)); |
24 DEFINE_STATIC_LOCAL(const AtomicString, portraitSecondary, ("portrait-second
ary", AtomicString::ConstructFromLiteral)); | 26 DEFINE_STATIC_LOCAL(const AtomicString, portraitSecondary, ("portrait-second
ary", AtomicString::ConstructFromLiteral)); |
25 DEFINE_STATIC_LOCAL(const AtomicString, landscapePrimary, ("landscape-primar
y", AtomicString::ConstructFromLiteral)); | 27 DEFINE_STATIC_LOCAL(const AtomicString, landscapePrimary, ("landscape-primar
y", AtomicString::ConstructFromLiteral)); |
(...skipping 16 matching lines...) Expand all Loading... |
42 for (unsigned i = 0; i < length; ++i) { | 44 for (unsigned i = 0; i < length; ++i) { |
43 if (orientationMap[i].orientation == orientation) | 45 if (orientationMap[i].orientation == orientation) |
44 return orientationMap[i].name; | 46 return orientationMap[i].name; |
45 } | 47 } |
46 // We do no handle OrientationInvalid and OrientationAny but this is fine be
cause screen.orientation | 48 // We do no handle OrientationInvalid and OrientationAny but this is fine be
cause screen.orientation |
47 // should never return these and WebScreenOrientation does not define those
values. | 49 // should never return these and WebScreenOrientation does not define those
values. |
48 ASSERT_NOT_REACHED(); | 50 ASSERT_NOT_REACHED(); |
49 return nullAtom; | 51 return nullAtom; |
50 } | 52 } |
51 | 53 |
| 54 static blink::WebScreenOrientations stringToOrientations(const AtomicString& ori
entationString) |
| 55 { |
| 56 DEFINE_STATIC_LOCAL(const AtomicString, portrait, ("portrait", AtomicString:
:ConstructFromLiteral)); |
| 57 DEFINE_STATIC_LOCAL(const AtomicString, landscape, ("landscape", AtomicStrin
g::ConstructFromLiteral)); |
| 58 |
| 59 if (orientationString == portrait) |
| 60 return blink::WebScreenOrientationPortraitPrimary | blink::WebScreenOrie
ntationPortraitSecondary; |
| 61 if (orientationString == landscape) |
| 62 return blink::WebScreenOrientationLandscapePrimary | blink::WebScreenOri
entationLandscapeSecondary; |
| 63 |
| 64 unsigned length = 0; |
| 65 ScreenOrientationInfo* orientationMap = orientationsMap(length); |
| 66 for (unsigned i = 0; i < length; ++i) { |
| 67 if (orientationMap[i].name == orientationString) |
| 68 return orientationMap[i].orientation; |
| 69 } |
| 70 return 0; |
| 71 } |
| 72 |
52 ScreenOrientation::ScreenOrientation(Screen& screen) | 73 ScreenOrientation::ScreenOrientation(Screen& screen) |
53 : DOMWindowProperty(screen.frame()) | 74 : DOMWindowProperty(screen.frame()) |
| 75 , m_orientationLockTimer(this, &ScreenOrientation::orientationLockTimerFired
) |
| 76 , m_lockedOrientations(WebScreenOrientationDefault) |
54 { | 77 { |
55 } | 78 } |
56 | 79 |
| 80 void ScreenOrientation::lockOrientationAsync(blink::WebScreenOrientations orient
ations) |
| 81 { |
| 82 if (m_lockedOrientations == orientations) |
| 83 return; |
| 84 m_lockedOrientations = orientations; |
| 85 if (!m_orientationLockTimer.isActive()) |
| 86 m_orientationLockTimer.startOneShot(0); |
| 87 } |
| 88 |
| 89 void ScreenOrientation::orientationLockTimerFired(Timer<ScreenOrientation>*) |
| 90 { |
| 91 if (m_lockedOrientations == WebScreenOrientationDefault) |
| 92 blink::Platform::current()->unlockOrientation(); |
| 93 else |
| 94 blink::Platform::current()->lockOrientation(m_lockedOrientations); |
| 95 } |
| 96 |
57 const char* ScreenOrientation::supplementName() | 97 const char* ScreenOrientation::supplementName() |
58 { | 98 { |
59 return "ScreenOrientation"; | 99 return "ScreenOrientation"; |
60 } | 100 } |
61 | 101 |
62 Document& ScreenOrientation::document() const | 102 Document& ScreenOrientation::document() const |
63 { | 103 { |
64 ASSERT(m_associatedDOMWindow); | 104 ASSERT(m_associatedDOMWindow); |
65 ASSERT(m_associatedDOMWindow->document()); | 105 ASSERT(m_associatedDOMWindow->document()); |
66 return *m_associatedDOMWindow->document(); | 106 return *m_associatedDOMWindow->document(); |
(...skipping 13 matching lines...) Expand all Loading... |
80 { | 120 { |
81 } | 121 } |
82 | 122 |
83 const AtomicString& ScreenOrientation::orientation(Screen& screen) | 123 const AtomicString& ScreenOrientation::orientation(Screen& screen) |
84 { | 124 { |
85 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); | 125 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); |
86 ScreenOrientationController& controller = ScreenOrientationController::from(
screenOrientation.document()); | 126 ScreenOrientationController& controller = ScreenOrientationController::from(
screenOrientation.document()); |
87 return orientationToString(controller.orientation()); | 127 return orientationToString(controller.orientation()); |
88 } | 128 } |
89 | 129 |
90 bool ScreenOrientation::lockOrientation(Screen& screen, const Vector<String>& or
ientations) | 130 bool ScreenOrientation::lockOrientation(Screen& screen, const Vector<String>& or
ientationsVector) |
91 { | 131 { |
92 // FIXME: Implement. | 132 blink::WebScreenOrientations orientations = 0; |
93 return false; | 133 for (size_t i = 0; i < orientationsVector.size(); ++i) { |
| 134 blink::WebScreenOrientations currentOrientation = stringToOrientations(A
tomicString(orientationsVector[i])); |
| 135 if (!currentOrientation) |
| 136 return false; |
| 137 orientations |= currentOrientation; |
| 138 } |
| 139 if (!orientations) |
| 140 return false; |
| 141 ScreenOrientation::from(screen).lockOrientationAsync(orientations); |
| 142 return true; |
94 } | 143 } |
95 | 144 |
96 bool ScreenOrientation::lockOrientation(Screen& screen, const AtomicString& orie
ntation) | 145 bool ScreenOrientation::lockOrientation(Screen& screen, const AtomicString& orie
ntationString) |
97 { | 146 { |
98 // FIXME: Implement. | 147 blink::WebScreenOrientations orientations = stringToOrientations(orientation
String); |
99 return false; | 148 if (!orientations) |
| 149 return false; |
| 150 ScreenOrientation::from(screen).lockOrientationAsync(orientations); |
| 151 return true; |
100 } | 152 } |
101 | 153 |
102 void ScreenOrientation::unlockOrientation(Screen& screen) | 154 void ScreenOrientation::unlockOrientation(Screen& screen) |
103 { | 155 { |
104 // FIXME: Implement. | 156 ScreenOrientation::from(screen).lockOrientationAsync(WebScreenOrientationDef
ault); |
105 } | 157 } |
106 | 158 |
107 } // namespace WebCore | 159 } // namespace WebCore |
OLD | NEW |