Chromium Code Reviews| Index: Source/modules/screen_orientation/ScreenOrientation.cpp |
| diff --git a/Source/modules/screen_orientation/ScreenOrientation.cpp b/Source/modules/screen_orientation/ScreenOrientation.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c0c3c3777915bc72087499aa463aec339bf15eee |
| --- /dev/null |
| +++ b/Source/modules/screen_orientation/ScreenOrientation.cpp |
| @@ -0,0 +1,191 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "ScreenOrientation.h" |
| + |
| +#include "core/frame/Frame.h" |
| +#include "core/frame/Screen.h" |
| +#include "core/page/ChromeClient.h" |
| +#include "modules/screen_orientation/ScreenOrientationClient.h" |
| +#include "modules/screen_orientation/ScreenOrientationController.h" |
| + |
| +namespace WebCore { |
| + |
| +ScreenOrientation::ScreenOrientation(Screen* screen) |
| + : DOMWindowProperty(screen->frame()) |
| + , m_screen(screen) |
| + , m_lockedOrientations(OrientationAny) |
| +{ |
| + ASSERT(screen); |
| + if (page()) |
| + ScreenOrientationController::from(page())->addObserver(this); |
| +} |
| + |
| +ScreenOrientation::~ScreenOrientation() |
| +{ |
| + if (page()) |
| + ScreenOrientationController::from(page())->removeObserver(this); |
| +} |
| + |
| +const char* ScreenOrientation::supplementName() |
| +{ |
| + return "ScreenOrientation"; |
| +} |
| + |
| +Page* ScreenOrientation::page() const |
| +{ |
| + return frame() ? frame()->page() : 0; |
| +} |
| + |
| +ScreenOrientation* ScreenOrientation::from(Screen* screen) |
| +{ |
| + ScreenOrientation* supplement = static_cast<ScreenOrientation*>(Supplement<Screen>::from(screen, supplementName())); |
| + if (!supplement) { |
| + supplement = new ScreenOrientation(screen); |
| + provideTo(screen, supplementName(), adoptPtr(supplement)); |
| + } |
| + return supplement; |
| +} |
| + |
| +ScreenOrientation::ScreenOrientationInfo* ScreenOrientation::orientationMap(unsigned& length) |
| +{ |
| + DEFINE_STATIC_LOCAL(const AtomicString, portrait, ("portrait", AtomicString::ConstructFromLiteral)); |
| + DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary", AtomicString::ConstructFromLiteral)); |
| + DEFINE_STATIC_LOCAL(const AtomicString, portraitSecondary, ("portrait-secondary", AtomicString::ConstructFromLiteral)); |
| + DEFINE_STATIC_LOCAL(const AtomicString, landscape, ("landscape", AtomicString::ConstructFromLiteral)); |
| + DEFINE_STATIC_LOCAL(const AtomicString, landscapePrimary, ("landscape-primary", AtomicString::ConstructFromLiteral)); |
| + DEFINE_STATIC_LOCAL(const AtomicString, landscapeSecondary, ("landscape-secondary", AtomicString::ConstructFromLiteral)); |
| + |
| + static ScreenOrientationInfo orientationMap[] = { |
| + { portrait, OrientationPortrait }, |
| + { portraitPrimary, OrientationPortraitPrimary }, |
| + { portraitSecondary, OrientationPortraitSecondary }, |
| + { landscape, OrientationLandscape }, |
| + { landscapePrimary, OrientationLandscapePrimary }, |
| + { landscapeSecondary, OrientationLandscapeSecondary } |
| + }; |
| + length = WTF_ARRAY_LENGTH(orientationMap); |
| + return orientationMap; |
| +} |
| + |
| +const AtomicString& ScreenOrientation::orientationToString(ScreenOrientationValue orientation) |
| +{ |
| + unsigned length = 0; |
| + ScreenOrientationInfo* orientationMap = ScreenOrientation::orientationMap(length); |
| + for (unsigned i = 0; i < length; ++i) { |
| + if (orientationMap[i].orientation == orientation) |
| + return orientationMap[i].name; |
| + } |
| + ASSERT_NOT_REACHED(); |
| + return nullAtom; |
| +} |
| + |
| +ScreenOrientationValues ScreenOrientation::stringToOrientationValues(const AtomicString& orientation) |
| +{ |
| + unsigned length = 0; |
| + ScreenOrientationInfo* orientationMap = ScreenOrientation::orientationMap(length); |
| + for (unsigned i = 0; i < length; ++i) { |
| + if (orientationMap[i].name == orientation) |
| + return orientationMap[i].orientation; |
| + } |
| + return OrientationInvalid; |
| +} |
| + |
| +const AtomicString& ScreenOrientation::orientation(Screen* screen) |
| +{ |
| + ScreenOrientation* screenOrientation = ScreenOrientation::from(screen); |
| + ASSERT(screenOrientation); |
| + |
| + Page* page = screenOrientation->page(); |
| + if (!page) |
| + return orientationToString(OrientationPortraitPrimary); |
| + |
| + ScreenOrientationController* controller = ScreenOrientationController::from(page); |
| + ASSERT(controller); |
| + |
| + return orientationToString(controller->orientation()); |
| +} |
| + |
| +bool ScreenOrientation::lockOrientation(Screen* screen, const Vector<String>& orientations) |
| +{ |
| + ScreenOrientationValues orientation = OrientationInvalid; |
| + |
| + for (unsigned i = 0; i < orientations.size(); ++i) { |
| + ScreenOrientationValues inputOrientation = stringToOrientationValues(AtomicString(orientations[i])); |
| + if (inputOrientation == OrientationInvalid) |
| + return false; |
| + orientation |= inputOrientation; |
| + } |
| + |
| + if (!orientation) |
| + return false; |
| + |
| + ScreenOrientation* screenOrientation = ScreenOrientation::from(screen); |
| + ASSERT(screenOrientation); |
| + return screenOrientation->lockOrientation(orientation); |
| +} |
| + |
| +bool ScreenOrientation::lockOrientation(Screen* screen, const AtomicString& orientationString) |
| +{ |
| + ScreenOrientationValues orientation = stringToOrientationValues(orientationString); |
| + if (!orientation) |
| + return false; |
| + |
| + ScreenOrientation* screenOrientation = ScreenOrientation::from(screen); |
| + ASSERT(screenOrientation); |
| + return screenOrientation->lockOrientation(orientation); |
| +} |
| + |
| +bool ScreenOrientation::lockOrientation(ScreenOrientationValues orientations) |
| +{ |
| + if (!(orientations & m_lockedOrientations)) |
| + 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
|
| + |
| + Page* page = this->page(); |
| + if (!page) |
| + return false; |
| + |
| + ScreenOrientationClient* client = ScreenOrientationController::clientFrom(page); |
| + if (!client) |
| + return false; |
| + |
| + bool locked = client->lockOrientation(orientations); |
| + if (locked) |
| + 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.
|
| + return locked; |
| +} |
| + |
| +void ScreenOrientation::unlockOrientation(Screen* screen) |
| +{ |
| + ScreenOrientation* screenOrientation = ScreenOrientation::from(screen); |
| + ASSERT(screenOrientation); |
| + Page* page = screenOrientation->page(); |
| + if (!page) |
| + return; |
| + |
| + ScreenOrientationClient* client = ScreenOrientationController::clientFrom(page); |
| + if (!client) |
| + return; |
| + |
| + screenOrientation->m_lockedOrientations = OrientationAny; |
| + client->unlockOrientation(); |
| +} |
| + |
| +void ScreenOrientation::orientationChanged() |
| +{ |
| +#ifndef NDEBUG |
| + Page* page = this->page(); |
| + if (!page) |
| + return; |
| + |
| + ScreenOrientationController* controller = ScreenOrientationController::from(page); |
| + ASSERT(controller); |
| + ASSERT(!m_lockedOrientations || m_lockedOrientations & controller->orientation()); |
| +#endif |
| + |
| + m_screen->dispatchEvent(Event::create(EventTypeNames::orientationchange)); |
| +} |
| + |
| +} // namespace WebCore |