OLD | NEW |
(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_allowedOrientation(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_allowedOrientation)) |
| 144 return false; |
| 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 m_allowedOrientation &= orientations; |
| 155 client->lockOrientation(orientations); |
| 156 return true; |
| 157 } |
| 158 |
| 159 void ScreenOrientation::unlockOrientation(Screen* screen) |
| 160 { |
| 161 ScreenOrientation* screenOrientation = ScreenOrientation::from(screen); |
| 162 ASSERT(screenOrientation); |
| 163 Page* page = screenOrientation->page(); |
| 164 if (!page) |
| 165 return; |
| 166 |
| 167 ScreenOrientationClient* client = ScreenOrientationController::clientFrom(pa
ge); |
| 168 if (!client) |
| 169 return; |
| 170 |
| 171 screenOrientation->m_allowedOrientation = OrientationAny; |
| 172 client->unlockOrientation(); |
| 173 } |
| 174 |
| 175 void ScreenOrientation::orientationChanged() |
| 176 { |
| 177 m_screen->dispatchEvent(Event::create(EventTypeNames::orientationchange)); |
| 178 } |
| 179 |
| 180 } // namespace WebCore |
OLD | NEW |