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

Unified 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 side-by-side diff with in-line comments
Download patch
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

Powered by Google App Engine
This is Rietveld 408576698