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

Side by Side Diff: Source/modules/screen_orientation/ScreenOrientation.cpp

Issue 219463003: Screen Orientation API: cleanup, simplify and fix implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: ch.dumez's comments applied Created 6 years, 8 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 unified diff | Download patch
OLDNEW
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 assumes that WebScreenOrientationType values are included in WebScr eenOrientationLockType.
16 #define COMPILE_ASSERT_MATCHING_ENUM(enum1, enum2) \
17 COMPILE_ASSERT(static_cast<char>(blink::enum1) == static_cast<char>(blink::e num2), mismatching_types)
18 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationPortraitPrimary, WebScreenOrien tationLockPortraitPrimary);
19 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationPortraitSecondary, WebScreenOri entationLockPortraitSecondary);
20 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationLandscapePrimary, WebScreenOrie ntationLockLandscapePrimary);
21 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationLandscapeSecondary, WebScreenOr ientationLockLandscapeSecondary);
abarth-chromium 2014/04/07 19:59:36 We usually put these in AssertMatchingEnums.cpp
mlamouri (slow - plz ping) 2014/04/13 01:59:30 I left this here because web/AssertMatchingEnums.c
13 22
14 namespace WebCore { 23 namespace WebCore {
15 24
16 static const unsigned WebScreenOrientationDefault = 0;
17
18 struct ScreenOrientationInfo { 25 struct ScreenOrientationInfo {
19 const AtomicString& name; 26 const AtomicString& name;
20 blink::WebScreenOrientation orientation; 27 unsigned char orientation;
abarth-chromium 2014/04/07 19:59:36 Why "unsigned char" and not blink::WebScreenOrient
mlamouri (slow - plz ping) 2014/04/13 01:59:30 I switched from 'unsigned char' to 'unsigned'.
21 }; 28 };
22 29
23 static ScreenOrientationInfo* orientationsMap(unsigned& length) 30 static ScreenOrientationInfo* orientationsMap(unsigned& length)
24 { 31 {
25 DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary" , AtomicString::ConstructFromLiteral)); 32 DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary" , AtomicString::ConstructFromLiteral));
26 DEFINE_STATIC_LOCAL(const AtomicString, portraitSecondary, ("portrait-second ary", AtomicString::ConstructFromLiteral)); 33 DEFINE_STATIC_LOCAL(const AtomicString, portraitSecondary, ("portrait-second ary", AtomicString::ConstructFromLiteral));
27 DEFINE_STATIC_LOCAL(const AtomicString, landscapePrimary, ("landscape-primar y", AtomicString::ConstructFromLiteral)); 34 DEFINE_STATIC_LOCAL(const AtomicString, landscapePrimary, ("landscape-primar y", AtomicString::ConstructFromLiteral));
28 DEFINE_STATIC_LOCAL(const AtomicString, landscapeSecondary, ("landscape-seco ndary", AtomicString::ConstructFromLiteral)); 35 DEFINE_STATIC_LOCAL(const AtomicString, landscapeSecondary, ("landscape-seco ndary", AtomicString::ConstructFromLiteral));
36 DEFINE_STATIC_LOCAL(const AtomicString, any, ("any", AtomicString::Construct FromLiteral));
37 DEFINE_STATIC_LOCAL(const AtomicString, portrait, ("portrait", AtomicString: :ConstructFromLiteral));
38 DEFINE_STATIC_LOCAL(const AtomicString, landscape, ("landscape", AtomicStrin g::ConstructFromLiteral));
29 39
30 static ScreenOrientationInfo orientationMap[] = { 40 static ScreenOrientationInfo orientationMap[] = {
31 { portraitPrimary, blink::WebScreenOrientationPortraitPrimary }, 41 { portraitPrimary, blink::WebScreenOrientationLockPortraitPrimary },
32 { portraitSecondary, blink::WebScreenOrientationPortraitSecondary }, 42 { portraitSecondary, blink::WebScreenOrientationLockPortraitSecondary },
33 { landscapePrimary, blink::WebScreenOrientationLandscapePrimary }, 43 { landscapePrimary, blink::WebScreenOrientationLockLandscapePrimary },
34 { landscapeSecondary, blink::WebScreenOrientationLandscapeSecondary } 44 { landscapeSecondary, blink::WebScreenOrientationLockLandscapeSecondary },
45 { any, blink::WebScreenOrientationLockAny },
46 { portrait, blink::WebScreenOrientationLockPortrait },
47 { landscape, blink::WebScreenOrientationLockLandscape }
35 }; 48 };
36 length = WTF_ARRAY_LENGTH(orientationMap); 49 length = WTF_ARRAY_LENGTH(orientationMap);
50
37 return orientationMap; 51 return orientationMap;
38 } 52 }
39 53
40 static const AtomicString& orientationToString(blink::WebScreenOrientation orien tation) 54 static const AtomicString& orientationTypeToString(blink::WebScreenOrientationTy pe orientation)
41 { 55 {
42 unsigned length = 0; 56 unsigned length = 0;
43 ScreenOrientationInfo* orientationMap = orientationsMap(length); 57 ScreenOrientationInfo* orientationMap = orientationsMap(length);
44 for (unsigned i = 0; i < length; ++i) { 58 for (unsigned i = 0; i < length; ++i) {
45 if (orientationMap[i].orientation == orientation) 59 if (static_cast<unsigned char>(orientation) == orientationMap[i].orienta tion)
46 return orientationMap[i].name; 60 return orientationMap[i].name;
47 } 61 }
48 // We do no handle OrientationInvalid and OrientationAny but this is fine be cause screen.orientation 62
49 // 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
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
OLDNEW
« no previous file with comments | « Source/modules/screen_orientation/ScreenOrientation.h ('k') | Source/modules/screen_orientation/ScreenOrientationController.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698