Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "modules/screen_orientation/ScreenOrientation.h" | 6 #include "modules/screen_orientation/ScreenOrientation.h" |
| 7 | 7 |
| 8 #include "core/frame/DOMWindow.h" | 8 #include "core/frame/DOMWindow.h" |
| 9 #include "core/frame/LocalFrame.h" | 9 #include "core/frame/LocalFrame.h" |
| 10 #include "core/frame/Screen.h" | 10 #include "core/frame/Screen.h" |
| 11 #include "modules/screen_orientation/ScreenOrientationController.h" | 11 #include "modules/screen_orientation/ScreenOrientationController.h" |
| 12 #include "public/platform/Platform.h" | 12 #include "public/platform/Platform.h" |
| 13 #include "public/platform/WebScreenOrientationType.h" | |
| 14 | |
| 15 // This code assume that WebScreenOrientationType values are included in WebScre enOrientationLockType. | |
| 16 COMPILE_ASSERT((char)blink::WebScreenOrientationPortraitPrimary == (char)blink:: WebScreenOrientationLockPortraitPrimary, mismatching_types); | |
|
Inactive
2014/04/01 13:20:44
We prefer C++ casting instead of C casting (here s
mlamouri (slow - plz ping)
2014/04/01 16:30:21
Done.
| |
| 17 COMPILE_ASSERT((char)blink::WebScreenOrientationPortraitSecondary == (char)blink ::WebScreenOrientationLockPortraitSecondary, mismatching_types); | |
| 18 COMPILE_ASSERT((char)blink::WebScreenOrientationLandscapePrimary == (char)blink: :WebScreenOrientationLockLandscapePrimary, mismatching_types); | |
| 19 COMPILE_ASSERT((char)blink::WebScreenOrientationLandscapeSecondary == (char)blin k::WebScreenOrientationLockLandscapeSecondary, mismatching_types); | |
| 13 | 20 |
| 14 namespace WebCore { | 21 namespace WebCore { |
| 15 | 22 |
| 16 static const unsigned WebScreenOrientationDefault = 0; | |
| 17 | |
| 18 struct ScreenOrientationInfo { | 23 struct ScreenOrientationInfo { |
| 19 const AtomicString& name; | 24 const AtomicString& name; |
| 20 blink::WebScreenOrientation orientation; | 25 char orientation; |
|
Inactive
2014/04/01 13:20:44
If you don't want to switch to the enum type, shou
mlamouri (slow - plz ping)
2014/04/01 16:30:21
Done.
| |
| 21 }; | 26 }; |
| 22 | 27 |
| 23 static ScreenOrientationInfo* orientationsMap(unsigned& length) | 28 static ScreenOrientationInfo* orientationsMap(unsigned& length) |
| 24 { | 29 { |
| 25 DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary" , AtomicString::ConstructFromLiteral)); | 30 DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary" , AtomicString::ConstructFromLiteral)); |
| 26 DEFINE_STATIC_LOCAL(const AtomicString, portraitSecondary, ("portrait-second ary", AtomicString::ConstructFromLiteral)); | 31 DEFINE_STATIC_LOCAL(const AtomicString, portraitSecondary, ("portrait-second ary", AtomicString::ConstructFromLiteral)); |
| 27 DEFINE_STATIC_LOCAL(const AtomicString, landscapePrimary, ("landscape-primar y", AtomicString::ConstructFromLiteral)); | 32 DEFINE_STATIC_LOCAL(const AtomicString, landscapePrimary, ("landscape-primar y", AtomicString::ConstructFromLiteral)); |
| 28 DEFINE_STATIC_LOCAL(const AtomicString, landscapeSecondary, ("landscape-seco ndary", AtomicString::ConstructFromLiteral)); | 33 DEFINE_STATIC_LOCAL(const AtomicString, landscapeSecondary, ("landscape-seco ndary", AtomicString::ConstructFromLiteral)); |
| 34 DEFINE_STATIC_LOCAL(const AtomicString, any, ("any", AtomicString::Construct FromLiteral)); | |
| 35 DEFINE_STATIC_LOCAL(const AtomicString, portrait, ("portrait", AtomicString: :ConstructFromLiteral)); | |
| 36 DEFINE_STATIC_LOCAL(const AtomicString, landscape, ("landscape", AtomicStrin g::ConstructFromLiteral)); | |
| 29 | 37 |
| 30 static ScreenOrientationInfo orientationMap[] = { | 38 static ScreenOrientationInfo orientationMap[] = { |
| 31 { portraitPrimary, blink::WebScreenOrientationPortraitPrimary }, | 39 { portraitPrimary, blink::WebScreenOrientationLockPortraitPrimary }, |
| 32 { portraitSecondary, blink::WebScreenOrientationPortraitSecondary }, | 40 { portraitSecondary, blink::WebScreenOrientationLockPortraitSecondary }, |
| 33 { landscapePrimary, blink::WebScreenOrientationLandscapePrimary }, | 41 { landscapePrimary, blink::WebScreenOrientationLockLandscapePrimary }, |
| 34 { landscapeSecondary, blink::WebScreenOrientationLandscapeSecondary } | 42 { landscapeSecondary, blink::WebScreenOrientationLockLandscapeSecondary }, |
| 43 { any, blink::WebScreenOrientationLockAny }, | |
| 44 { portrait, blink::WebScreenOrientationLockPortrait }, | |
| 45 { landscape, blink::WebScreenOrientationLockLandscape } | |
| 35 }; | 46 }; |
| 36 length = WTF_ARRAY_LENGTH(orientationMap); | 47 length = WTF_ARRAY_LENGTH(orientationMap); |
| 48 | |
| 37 return orientationMap; | 49 return orientationMap; |
| 38 } | 50 } |
| 39 | 51 |
| 40 static const AtomicString& orientationToString(blink::WebScreenOrientation orien tation) | 52 static const AtomicString& orientationTypeToString(blink::WebScreenOrientationTy pe orientation) |
| 41 { | 53 { |
| 42 unsigned length = 0; | 54 unsigned length = 0; |
| 43 ScreenOrientationInfo* orientationMap = orientationsMap(length); | 55 ScreenOrientationInfo* orientationMap = orientationsMap(length); |
| 44 for (unsigned i = 0; i < length; ++i) { | 56 for (unsigned i = 0; i < length; ++i) { |
| 45 if (orientationMap[i].orientation == orientation) | 57 if (static_cast<blink::WebScreenOrientationType>(orientationMap[i].orien tation) == orientation) |
|
Inactive
2014/04/01 13:20:44
I still think we should add some kind of assertion
mlamouri (slow - plz ping)
2014/04/01 16:30:21
Are you more comfortable with |orientation| being
| |
| 46 return orientationMap[i].name; | 58 return orientationMap[i].name; |
| 47 } | 59 } |
| 60 | |
| 48 // We do no handle OrientationInvalid and OrientationAny but this is fine be cause screen.orientation | 61 // We do no handle OrientationInvalid and OrientationAny but this is fine be cause screen.orientation |
|
Inactive
2014/04/01 13:20:44
This comment seems outdated now.
mlamouri (slow - plz ping)
2014/04/01 16:30:21
Removed. The assert should be self explanatory.
| |
| 49 // should never return these and WebScreenOrientation does not define those values. | 62 // should never return these and WebScreenOrientation does not define those values. |
| 50 ASSERT_NOT_REACHED(); | 63 ASSERT_NOT_REACHED(); |
| 51 return nullAtom; | 64 return nullAtom; |
| 52 } | 65 } |
| 53 | 66 |
| 54 static blink::WebScreenOrientations stringToOrientations(const AtomicString& ori entationString) | 67 static blink::WebScreenOrientationLockType stringToOrientationLock(const AtomicS tring& orientationLockString) |
| 55 { | 68 { |
| 56 DEFINE_STATIC_LOCAL(const AtomicString, any, ("any", AtomicString::Construct FromLiteral)); | |
| 57 DEFINE_STATIC_LOCAL(const AtomicString, portrait, ("portrait", AtomicString: :ConstructFromLiteral)); | |
| 58 DEFINE_STATIC_LOCAL(const AtomicString, landscape, ("landscape", AtomicStrin g::ConstructFromLiteral)); | |
| 59 | |
| 60 if (orientationString == any) { | |
| 61 return blink::WebScreenOrientationPortraitPrimary | blink::WebScreenOrie ntationPortraitSecondary | | |
| 62 blink::WebScreenOrientationLandscapePrimary | blink::WebScreenOrient ationLandscapeSecondary; | |
| 63 } | |
| 64 if (orientationString == portrait) | |
| 65 return blink::WebScreenOrientationPortraitPrimary | blink::WebScreenOrie ntationPortraitSecondary; | |
| 66 if (orientationString == landscape) | |
| 67 return blink::WebScreenOrientationLandscapePrimary | blink::WebScreenOri entationLandscapeSecondary; | |
| 68 | |
| 69 unsigned length = 0; | 69 unsigned length = 0; |
| 70 ScreenOrientationInfo* orientationMap = orientationsMap(length); | 70 ScreenOrientationInfo* orientationMap = orientationsMap(length); |
| 71 for (unsigned i = 0; i < length; ++i) { | 71 for (unsigned i = 0; i < length; ++i) { |
| 72 if (orientationMap[i].name == orientationString) | 72 if (orientationMap[i].name == orientationLockString) |
| 73 return orientationMap[i].orientation; | 73 return static_cast<blink::WebScreenOrientationLockType>(orientationM ap[i].orientation); |
| 74 } | 74 } |
| 75 return 0; | 75 |
| 76 ASSERT_NOT_REACHED(); | |
| 77 return blink::WebScreenOrientationLockDefault; | |
| 76 } | 78 } |
| 77 | 79 |
| 78 ScreenOrientation::ScreenOrientation(Screen& screen) | 80 ScreenOrientation::ScreenOrientation(Screen& screen) |
| 79 : DOMWindowProperty(screen.frame()) | 81 : DOMWindowProperty(screen.frame()) |
| 80 , m_orientationLockTimer(this, &ScreenOrientation::orientationLockTimerFired ) | 82 , m_orientationLockTimer(this, &ScreenOrientation::orientationLockTimerFired ) |
| 81 , m_lockedOrientations(WebScreenOrientationDefault) | 83 , m_prospectiveLock(blink::WebScreenOrientationLockDefault) |
| 82 { | 84 { |
| 83 } | 85 } |
| 84 | 86 |
| 85 void ScreenOrientation::lockOrientationAsync(blink::WebScreenOrientations orient ations) | 87 void ScreenOrientation::lockOrientationAsync(blink::WebScreenOrientationLockType orientation) |
| 86 { | 88 { |
| 87 if (m_lockedOrientations == orientations) | 89 if (m_orientationLockTimer.isActive()) |
| 88 return; | 90 m_orientationLockTimer.stop(); |
| 89 m_lockedOrientations = orientations; | 91 |
| 90 if (!m_orientationLockTimer.isActive()) | 92 m_prospectiveLock = orientation; |
| 91 m_orientationLockTimer.startOneShot(0, FROM_HERE); | 93 m_orientationLockTimer.startOneShot(0, FROM_HERE); |
| 92 } | 94 } |
| 93 | 95 |
| 94 void ScreenOrientation::orientationLockTimerFired(Timer<ScreenOrientation>*) | 96 void ScreenOrientation::orientationLockTimerFired(Timer<ScreenOrientation>*) |
| 95 { | 97 { |
| 96 if (m_lockedOrientations == WebScreenOrientationDefault) | 98 if (m_prospectiveLock == blink::WebScreenOrientationLockDefault) |
| 97 blink::Platform::current()->unlockOrientation(); | 99 blink::Platform::current()->unlockOrientation(); |
| 98 else | 100 else |
| 99 blink::Platform::current()->lockOrientation(m_lockedOrientations); | 101 blink::Platform::current()->lockOrientation(m_prospectiveLock); |
| 100 } | 102 } |
| 101 | 103 |
| 102 const char* ScreenOrientation::supplementName() | 104 const char* ScreenOrientation::supplementName() |
| 103 { | 105 { |
| 104 return "ScreenOrientation"; | 106 return "ScreenOrientation"; |
| 105 } | 107 } |
| 106 | 108 |
| 107 Document* ScreenOrientation::document() const | 109 Document* ScreenOrientation::document() const |
| 108 { | 110 { |
| 109 if (!m_associatedDOMWindow || !m_associatedDOMWindow->isCurrentlyDisplayedIn Frame()) | 111 if (!m_associatedDOMWindow || !m_associatedDOMWindow->isCurrentlyDisplayedIn Frame()) |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 124 | 126 |
| 125 ScreenOrientation::~ScreenOrientation() | 127 ScreenOrientation::~ScreenOrientation() |
| 126 { | 128 { |
| 127 } | 129 } |
| 128 | 130 |
| 129 const AtomicString& ScreenOrientation::orientation(Screen& screen) | 131 const AtomicString& ScreenOrientation::orientation(Screen& screen) |
| 130 { | 132 { |
| 131 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); | 133 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); |
| 132 if (!screenOrientation.document()) { | 134 if (!screenOrientation.document()) { |
| 133 // FIXME: we should try to return a better guess, like the latest known value. | 135 // FIXME: we should try to return a better guess, like the latest known value. |
| 134 return orientationToString(blink::WebScreenOrientationPortraitPrimary); | 136 return orientationTypeToString(blink::WebScreenOrientationPortraitPrimar y); |
| 135 } | 137 } |
| 136 ScreenOrientationController& controller = ScreenOrientationController::from( *screenOrientation.document()); | 138 ScreenOrientationController& controller = ScreenOrientationController::from( *screenOrientation.document()); |
| 137 return orientationToString(controller.orientation()); | 139 return orientationTypeToString(controller.orientation()); |
| 138 } | 140 } |
| 139 | 141 |
| 140 bool ScreenOrientation::lockOrientation(Screen& screen, const AtomicString& orie ntationString) | 142 bool ScreenOrientation::lockOrientation(Screen& screen, const AtomicString& lock String) |
| 141 { | 143 { |
| 142 blink::WebScreenOrientations orientations = stringToOrientations(orientation String); | 144 ScreenOrientation::from(screen).lockOrientationAsync(stringToOrientationLock (lockString)); |
| 143 if (!orientations) | |
| 144 return false; | |
| 145 ScreenOrientation::from(screen).lockOrientationAsync(orientations); | |
| 146 return true; | 145 return true; |
| 147 } | 146 } |
| 148 | 147 |
| 149 void ScreenOrientation::unlockOrientation(Screen& screen) | 148 void ScreenOrientation::unlockOrientation(Screen& screen) |
| 150 { | 149 { |
| 151 ScreenOrientation::from(screen).lockOrientationAsync(WebScreenOrientationDef ault); | 150 ScreenOrientation::from(screen).lockOrientationAsync(blink::WebScreenOrienta tionLockDefault); |
| 152 } | 151 } |
| 153 | 152 |
| 154 } // namespace WebCore | 153 } // namespace WebCore |
| OLD | NEW |