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

Unified Diff: Source/modules/screen_orientation/ScreenOrientation.cpp

Issue 169403006: Screen Orientation API: screen.orientation & orientationchange event (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: 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
index 4b147dd809a0f4ee807ee0cd9fb9fe5145c8ce0e..a1f653741bded7ccbb73cce37d345a7f0b2fbce5 100644
--- a/Source/modules/screen_orientation/ScreenOrientation.cpp
+++ b/Source/modules/screen_orientation/ScreenOrientation.cpp
@@ -5,15 +5,19 @@
#include "config.h"
#include "modules/screen_orientation/ScreenOrientation.h"
+#include "core/events/Event.h"
#include "core/frame/DOMWindow.h"
#include "core/frame/Frame.h"
#include "core/frame/Screen.h"
+#include "modules/screen_orientation/ScreenOrientationController.h"
namespace WebCore {
ScreenOrientation::ScreenOrientation(Screen* screen)
: DOMWindowProperty(screen->frame())
{
+ if (ScreenOrientationController* controller = ScreenOrientationController::from(page()))
+ controller->addObserver(this);
abarth-chromium 2014/02/18 01:34:27 Under what circumstances can |controller| be zero.
Inactive 2014/02/18 20:52:47 Done. No longer needed now that the controller is
}
const char* ScreenOrientation::supplementName()
@@ -34,6 +38,8 @@ ScreenOrientation* ScreenOrientation::from(Screen* screen)
ScreenOrientation::~ScreenOrientation()
{
+ if (ScreenOrientationController* controller = ScreenOrientationController::from(page()))
Inactive 2014/02/17 18:23:13 I looked at the Geolocation module and it subclass
+ controller->removeObserver(this);
abarth-chromium 2014/02/18 01:34:27 I believe the correct thing to do is to override w
}
Screen* ScreenOrientation::screen() const
@@ -45,11 +51,56 @@ Screen* ScreenOrientation::screen() const
return window->screen();
abarth-chromium 2014/02/18 01:34:27 Why not just: return m_associatedDOMWindow->scree
Inactive 2014/02/18 20:52:47 This function is no longer needed now that the con
}
-const AtomicString& ScreenOrientation::orientation(Screen* screen)
+struct ScreenOrientationInfo {
+ const AtomicString& name;
+ ScreenOrientationValues orientation;
+};
+
+static ScreenOrientationInfo* allowedOrientationsMap(unsigned& length)
abarth-chromium 2014/02/18 01:34:27 Please put free static functions at the top of the
Inactive 2014/02/18 20:52:47 Done.
{
- // FIXME: Implement.
+ DEFINE_STATIC_LOCAL(const AtomicString, portrait, ("portrait", AtomicString::ConstructFromLiteral));
DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary", AtomicString::ConstructFromLiteral));
- return portraitPrimary;
+ 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 = allowedOrientationsMap(length);
+ for (unsigned i = 0; i < length; ++i) {
+ if (orientationMap[i].orientation == orientation)
+ return orientationMap[i].name;
+ }
+ // We do no handle OrientationInvalid and OrientationAny but this is fine be cause screen.orientation
+ // should never return these and WebScreenOrientation does not define those values.
+ ASSERT_NOT_REACHED();
+ return nullAtom;
+}
+
+const AtomicString& ScreenOrientation::orientation(Screen* screen)
+{
+ ScreenOrientation* screenOrientation = ScreenOrientation::from(screen);
+ ASSERT(screenOrientation);
+
+ ScreenOrientationController* controller = ScreenOrientationController::from(screenOrientation->page());
+ if (!controller)
+ return orientationToString(OrientationPortraitPrimary);
+
+ return orientationToString(controller->orientation());
}
bool ScreenOrientation::lockOrientation(Screen* screen, const Vector<String>& orientations)
@@ -69,4 +120,17 @@ void ScreenOrientation::unlockOrientation(Screen* screen)
// FIXME: Implement.
}
+void ScreenOrientation::orientationChanged()
+{
+ ASSERT(screen());
+ screen()->dispatchEvent(Event::create(EventTypeNames::orientationchange));
+}
+
+Page* ScreenOrientation::page() const
+{
+ Frame* frame = this->frame();
+ ASSERT(frame);
+ return frame->page();
+}
+
} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698