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

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: 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"
13 14
14 namespace WebCore { 15 namespace WebCore {
15 16
16 static const unsigned WebScreenOrientationDefault = 0;
17
18 struct ScreenOrientationInfo { 17 struct ScreenOrientationInfo {
19 const AtomicString& name; 18 const AtomicString& name;
20 blink::WebScreenOrientation orientation; 19 char orientation;
Inactive 2014/03/31 18:52:36 Any reason we are not using a WebScreenOrientation
mlamouri (slow - plz ping) 2014/03/31 19:45:35 I could put WebScreenOrientationLockType but it wo
21 }; 20 };
22 21
23 static ScreenOrientationInfo* orientationsMap(unsigned& length) 22 static ScreenOrientationInfo* orientationsMap(unsigned& length)
24 { 23 {
25 DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary" , AtomicString::ConstructFromLiteral)); 24 DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary" , AtomicString::ConstructFromLiteral));
26 DEFINE_STATIC_LOCAL(const AtomicString, portraitSecondary, ("portrait-second ary", AtomicString::ConstructFromLiteral)); 25 DEFINE_STATIC_LOCAL(const AtomicString, portraitSecondary, ("portrait-second ary", AtomicString::ConstructFromLiteral));
27 DEFINE_STATIC_LOCAL(const AtomicString, landscapePrimary, ("landscape-primar y", AtomicString::ConstructFromLiteral)); 26 DEFINE_STATIC_LOCAL(const AtomicString, landscapePrimary, ("landscape-primar y", AtomicString::ConstructFromLiteral));
28 DEFINE_STATIC_LOCAL(const AtomicString, landscapeSecondary, ("landscape-seco ndary", AtomicString::ConstructFromLiteral)); 27 DEFINE_STATIC_LOCAL(const AtomicString, landscapeSecondary, ("landscape-seco ndary", AtomicString::ConstructFromLiteral));
28 DEFINE_STATIC_LOCAL(const AtomicString, any, ("any", AtomicString::Construct FromLiteral));
29 DEFINE_STATIC_LOCAL(const AtomicString, portrait, ("portrait", AtomicString: :ConstructFromLiteral));
30 DEFINE_STATIC_LOCAL(const AtomicString, landscape, ("landscape", AtomicStrin g::ConstructFromLiteral));
29 31
30 static ScreenOrientationInfo orientationMap[] = { 32 static ScreenOrientationInfo orientationMap[] = {
31 { portraitPrimary, blink::WebScreenOrientationPortraitPrimary }, 33 { portraitPrimary, blink::WebScreenOrientationLockPortraitPrimary },
32 { portraitSecondary, blink::WebScreenOrientationPortraitSecondary }, 34 { portraitSecondary, blink::WebScreenOrientationLockPortraitSecondary },
33 { landscapePrimary, blink::WebScreenOrientationLandscapePrimary }, 35 { landscapePrimary, blink::WebScreenOrientationLockLandscapePrimary },
34 { landscapeSecondary, blink::WebScreenOrientationLandscapeSecondary } 36 { landscapeSecondary, blink::WebScreenOrientationLockLandscapeSecondary },
37 { any, blink::WebScreenOrientationLockAny },
38 { portrait, blink::WebScreenOrientationLockPortrait },
39 { landscape, blink::WebScreenOrientationLockLandscape }
35 }; 40 };
36 length = WTF_ARRAY_LENGTH(orientationMap); 41 length = WTF_ARRAY_LENGTH(orientationMap);
42
37 return orientationMap; 43 return orientationMap;
38 } 44 }
39 45
40 static const AtomicString& orientationToString(blink::WebScreenOrientation orien tation) 46 static const AtomicString& orientationTypeToString(blink::WebScreenOrientationTy pe orientation)
41 { 47 {
42 unsigned length = 0; 48 unsigned length = 0;
43 ScreenOrientationInfo* orientationMap = orientationsMap(length); 49 ScreenOrientationInfo* orientationMap = orientationsMap(length);
44 for (unsigned i = 0; i < length; ++i) { 50 for (unsigned i = 0; i < length; ++i) {
45 if (orientationMap[i].orientation == orientation) 51 if (static_cast<blink::WebScreenOrientationType>(orientationMap[i].orien tation) == orientation)
Inactive 2014/03/31 18:52:36 So we rely on the values shared by WebScreenOrient
mlamouri (slow - plz ping) 2014/03/31 19:45:35 Argh. I meant to add compile time checks but I for
46 return orientationMap[i].name; 52 return orientationMap[i].name;
47 } 53 }
54
48 // We do no handle OrientationInvalid and OrientationAny but this is fine be cause screen.orientation 55 // We do no handle OrientationInvalid and OrientationAny but this is fine be cause screen.orientation
49 // should never return these and WebScreenOrientation does not define those values. 56 // should never return these and WebScreenOrientation does not define those values.
50 ASSERT_NOT_REACHED(); 57 ASSERT_NOT_REACHED();
51 return nullAtom; 58 return nullAtom;
52 } 59 }
53 60
54 static blink::WebScreenOrientations stringToOrientations(const AtomicString& ori entationString) 61 static blink::WebScreenOrientationLockType stringToOrientationLock(const AtomicS tring& orientationLockString)
55 { 62 {
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; 63 unsigned length = 0;
70 ScreenOrientationInfo* orientationMap = orientationsMap(length); 64 ScreenOrientationInfo* orientationMap = orientationsMap(length);
71 for (unsigned i = 0; i < length; ++i) { 65 for (unsigned i = 0; i < length; ++i) {
72 if (orientationMap[i].name == orientationString) 66 if (orientationMap[i].name == orientationLockString)
73 return orientationMap[i].orientation; 67 return static_cast<blink::WebScreenOrientationLockType>(orientationM ap[i].orientation);
74 } 68 }
75 return 0; 69
70 ASSERT_NOT_REACHED();
71 return blink::WebScreenOrientationLockDefault;
76 } 72 }
77 73
78 ScreenOrientation::ScreenOrientation(Screen& screen) 74 ScreenOrientation::ScreenOrientation(Screen& screen)
79 : DOMWindowProperty(screen.frame()) 75 : DOMWindowProperty(screen.frame())
80 , m_orientationLockTimer(this, &ScreenOrientation::orientationLockTimerFired ) 76 , m_orientationLockTimer(this, &ScreenOrientation::orientationLockTimerFired )
81 , m_lockedOrientations(WebScreenOrientationDefault) 77 , m_prospectiveLock(blink::WebScreenOrientationLockDefault)
82 { 78 {
83 } 79 }
84 80
85 void ScreenOrientation::lockOrientationAsync(blink::WebScreenOrientations orient ations) 81 void ScreenOrientation::lockOrientationAsync(blink::WebScreenOrientationLockType orientation)
86 { 82 {
87 if (m_lockedOrientations == orientations) 83 if (m_orientationLockTimer.isActive())
88 return; 84 m_orientationLockTimer.stop();
89 m_lockedOrientations = orientations; 85
90 if (!m_orientationLockTimer.isActive()) 86 m_prospectiveLock = orientation;
91 m_orientationLockTimer.startOneShot(0, FROM_HERE); 87 m_orientationLockTimer.startOneShot(0, FROM_HERE);
Inactive 2014/03/31 18:52:36 So consecutive calls to lockOrientation("portrait"
mlamouri (slow - plz ping) 2014/03/31 19:45:35 If another ScreenOrientation instance lock the scr
Inactive 2014/03/31 19:59:42 What do you mean by another ScreenOrientation inst
92 } 88 }
93 89
94 void ScreenOrientation::orientationLockTimerFired(Timer<ScreenOrientation>*) 90 void ScreenOrientation::orientationLockTimerFired(Timer<ScreenOrientation>*)
95 { 91 {
96 if (m_lockedOrientations == WebScreenOrientationDefault) 92 if (m_prospectiveLock == blink::WebScreenOrientationLockDefault)
97 blink::Platform::current()->unlockOrientation(); 93 blink::Platform::current()->unlockOrientation();
98 else 94 else
99 blink::Platform::current()->lockOrientation(m_lockedOrientations); 95 blink::Platform::current()->lockOrientation(m_prospectiveLock);
100 } 96 }
101 97
102 const char* ScreenOrientation::supplementName() 98 const char* ScreenOrientation::supplementName()
103 { 99 {
104 return "ScreenOrientation"; 100 return "ScreenOrientation";
105 } 101 }
106 102
107 Document* ScreenOrientation::document() const 103 Document* ScreenOrientation::document() const
108 { 104 {
109 if (!m_associatedDOMWindow || !m_associatedDOMWindow->isCurrentlyDisplayedIn Frame()) 105 if (!m_associatedDOMWindow || !m_associatedDOMWindow->isCurrentlyDisplayedIn Frame())
(...skipping 14 matching lines...) Expand all
124 120
125 ScreenOrientation::~ScreenOrientation() 121 ScreenOrientation::~ScreenOrientation()
126 { 122 {
127 } 123 }
128 124
129 const AtomicString& ScreenOrientation::orientation(Screen& screen) 125 const AtomicString& ScreenOrientation::orientation(Screen& screen)
130 { 126 {
131 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); 127 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen);
132 if (!screenOrientation.document()) { 128 if (!screenOrientation.document()) {
133 // FIXME: we should try to return a better guess, like the latest known value. 129 // FIXME: we should try to return a better guess, like the latest known value.
134 return orientationToString(blink::WebScreenOrientationPortraitPrimary); 130 return orientationTypeToString(blink::WebScreenOrientationPortraitPrimar y);
135 } 131 }
136 ScreenOrientationController& controller = ScreenOrientationController::from( *screenOrientation.document()); 132 ScreenOrientationController& controller = ScreenOrientationController::from( *screenOrientation.document());
137 return orientationToString(controller.orientation()); 133 return orientationTypeToString(controller.orientation());
138 } 134 }
139 135
140 bool ScreenOrientation::lockOrientation(Screen& screen, const AtomicString& orie ntationString) 136 bool ScreenOrientation::lockOrientation(Screen& screen, const AtomicString& lock String)
141 { 137 {
142 blink::WebScreenOrientations orientations = stringToOrientations(orientation String); 138 ScreenOrientation::from(screen).lockOrientationAsync(stringToOrientationLock (lockString));
143 if (!orientations)
144 return false;
145 ScreenOrientation::from(screen).lockOrientationAsync(orientations);
146 return true; 139 return true;
147 } 140 }
148 141
149 void ScreenOrientation::unlockOrientation(Screen& screen) 142 void ScreenOrientation::unlockOrientation(Screen& screen)
150 { 143 {
151 ScreenOrientation::from(screen).lockOrientationAsync(WebScreenOrientationDef ault); 144 ScreenOrientation::from(screen).lockOrientationAsync(blink::WebScreenOrienta tionLockDefault);
152 } 145 }
153 146
154 } // namespace WebCore 147 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698