| 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..70a6006365b33193af3f5e277fd05dd5a67c9571
|
| --- /dev/null
|
| +++ b/Source/modules/screen_orientation/ScreenOrientation.cpp
|
| @@ -0,0 +1,180 @@
|
| +// 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_allowedOrientation(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_allowedOrientation))
|
| + return false;
|
| +
|
| + Page* page = this->page();
|
| + if (!page)
|
| + return false;
|
| +
|
| + ScreenOrientationClient* client = ScreenOrientationController::clientFrom(page);
|
| + if (!client)
|
| + return false;
|
| +
|
| + m_allowedOrientation &= orientations;
|
| + client->lockOrientation(orientations);
|
| + return true;
|
| +}
|
| +
|
| +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_allowedOrientation = OrientationAny;
|
| + client->unlockOrientation();
|
| +}
|
| +
|
| +void ScreenOrientation::orientationChanged()
|
| +{
|
| + m_screen->dispatchEvent(Event::create(EventTypeNames::orientationchange));
|
| +}
|
| +
|
| +} // namespace WebCore
|
|
|