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 "bindings/v8/ExceptionState.h" | 8 #include "bindings/v8/ScriptPromise.h" |
| 9 #include "bindings/v8/ScriptPromiseResolverWithContext.h" |
| 10 #include "core/dom/DOMException.h" |
9 #include "core/dom/Document.h" | 11 #include "core/dom/Document.h" |
| 12 #include "core/dom/ExceptionCode.h" |
10 #include "core/frame/DOMWindow.h" | 13 #include "core/frame/DOMWindow.h" |
11 #include "core/frame/LocalFrame.h" | 14 #include "core/frame/LocalFrame.h" |
12 #include "core/frame/Screen.h" | 15 #include "core/frame/Screen.h" |
| 16 #include "modules/screen_orientation/LockOrientationCallback.h" |
13 #include "modules/screen_orientation/ScreenOrientationController.h" | 17 #include "modules/screen_orientation/ScreenOrientationController.h" |
14 #include "public/platform/Platform.h" | 18 #include "public/platform/Platform.h" |
15 #include "public/platform/WebScreenOrientationType.h" | 19 #include "public/platform/WebScreenOrientationType.h" |
16 | 20 |
17 // This code assumes that WebScreenOrientationType values are included in WebScr
eenOrientationLockType. | 21 // This code assumes that WebScreenOrientationType values are included in WebScr
eenOrientationLockType. |
18 #define COMPILE_ASSERT_MATCHING_ENUM(enum1, enum2) \ | 22 #define COMPILE_ASSERT_MATCHING_ENUM(enum1, enum2) \ |
19 COMPILE_ASSERT(static_cast<unsigned>(blink::enum1) == static_cast<unsigned>(
blink::enum2), mismatching_types) | 23 COMPILE_ASSERT(static_cast<unsigned>(blink::enum1) == static_cast<unsigned>(
blink::enum2), mismatching_types) |
20 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationPortraitPrimary, WebScreenOrien
tationLockPortraitPrimary); | 24 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationPortraitPrimary, WebScreenOrien
tationLockPortraitPrimary); |
21 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationPortraitSecondary, WebScreenOri
entationLockPortraitSecondary); | 25 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationPortraitSecondary, WebScreenOri
entationLockPortraitSecondary); |
22 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationLandscapePrimary, WebScreenOrie
ntationLockLandscapePrimary); | 26 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationLandscapePrimary, WebScreenOrie
ntationLockLandscapePrimary); |
(...skipping 23 matching lines...) Expand all Loading... |
46 { landscapeSecondary, blink::WebScreenOrientationLockLandscapeSecondary
}, | 50 { landscapeSecondary, blink::WebScreenOrientationLockLandscapeSecondary
}, |
47 { any, blink::WebScreenOrientationLockAny }, | 51 { any, blink::WebScreenOrientationLockAny }, |
48 { portrait, blink::WebScreenOrientationLockPortrait }, | 52 { portrait, blink::WebScreenOrientationLockPortrait }, |
49 { landscape, blink::WebScreenOrientationLockLandscape } | 53 { landscape, blink::WebScreenOrientationLockLandscape } |
50 }; | 54 }; |
51 length = WTF_ARRAY_LENGTH(orientationMap); | 55 length = WTF_ARRAY_LENGTH(orientationMap); |
52 | 56 |
53 return orientationMap; | 57 return orientationMap; |
54 } | 58 } |
55 | 59 |
56 static const AtomicString& orientationTypeToString(blink::WebScreenOrientationTy
pe orientation) | 60 const AtomicString& ScreenOrientation::orientationTypeToString(blink::WebScreenO
rientationType orientation) |
57 { | 61 { |
58 unsigned length = 0; | 62 unsigned length = 0; |
59 ScreenOrientationInfo* orientationMap = orientationsMap(length); | 63 ScreenOrientationInfo* orientationMap = orientationsMap(length); |
60 for (unsigned i = 0; i < length; ++i) { | 64 for (unsigned i = 0; i < length; ++i) { |
61 if (static_cast<unsigned>(orientation) == orientationMap[i].orientation) | 65 if (static_cast<unsigned>(orientation) == orientationMap[i].orientation) |
62 return orientationMap[i].name; | 66 return orientationMap[i].name; |
63 } | 67 } |
64 | 68 |
65 ASSERT_NOT_REACHED(); | 69 ASSERT_NOT_REACHED(); |
66 return nullAtom; | 70 return nullAtom; |
67 } | 71 } |
68 | 72 |
69 static blink::WebScreenOrientationLockType stringToOrientationLock(const AtomicS
tring& orientationLockString) | 73 static blink::WebScreenOrientationLockType stringToOrientationLock(const AtomicS
tring& orientationLockString) |
70 { | 74 { |
71 unsigned length = 0; | 75 unsigned length = 0; |
72 ScreenOrientationInfo* orientationMap = orientationsMap(length); | 76 ScreenOrientationInfo* orientationMap = orientationsMap(length); |
73 for (unsigned i = 0; i < length; ++i) { | 77 for (unsigned i = 0; i < length; ++i) { |
74 if (orientationMap[i].name == orientationLockString) | 78 if (orientationMap[i].name == orientationLockString) |
75 return static_cast<blink::WebScreenOrientationLockType>(orientationM
ap[i].orientation); | 79 return static_cast<blink::WebScreenOrientationLockType>(orientationM
ap[i].orientation); |
76 } | 80 } |
77 | 81 |
78 ASSERT_NOT_REACHED(); | 82 ASSERT_NOT_REACHED(); |
79 return blink::WebScreenOrientationLockDefault; | 83 return blink::WebScreenOrientationLockDefault; |
80 } | 84 } |
81 | 85 |
82 ScreenOrientation::ScreenOrientation(Screen& screen) | 86 ScreenOrientation::ScreenOrientation(Screen& screen) |
83 : DOMWindowProperty(screen.frame()) | 87 : DOMWindowProperty(screen.frame()) |
84 , m_orientationLockTimer(this, &ScreenOrientation::orientationLockTimerFired
) | |
85 , m_prospectiveLock(blink::WebScreenOrientationLockDefault) | |
86 { | 88 { |
87 } | 89 } |
88 | 90 |
89 void ScreenOrientation::lockOrientationAsync(blink::WebScreenOrientationLockType
orientation) | |
90 { | |
91 if (m_orientationLockTimer.isActive()) | |
92 m_orientationLockTimer.stop(); | |
93 | |
94 m_prospectiveLock = orientation; | |
95 m_orientationLockTimer.startOneShot(0, FROM_HERE); | |
96 } | |
97 | |
98 void ScreenOrientation::orientationLockTimerFired(Timer<ScreenOrientation>*) | |
99 { | |
100 if (m_prospectiveLock == blink::WebScreenOrientationLockDefault) | |
101 blink::Platform::current()->unlockOrientation(); | |
102 else | |
103 blink::Platform::current()->lockOrientation(m_prospectiveLock); | |
104 } | |
105 | |
106 const char* ScreenOrientation::supplementName() | 91 const char* ScreenOrientation::supplementName() |
107 { | 92 { |
108 return "ScreenOrientation"; | 93 return "ScreenOrientation"; |
109 } | 94 } |
110 | 95 |
111 Document* ScreenOrientation::document() const | 96 Document* ScreenOrientation::document() const |
112 { | 97 { |
113 if (!m_associatedDOMWindow || !m_associatedDOMWindow->isCurrentlyDisplayedIn
Frame()) | 98 if (!m_associatedDOMWindow || !m_associatedDOMWindow->isCurrentlyDisplayedIn
Frame()) |
114 return 0; | 99 return 0; |
115 ASSERT(m_associatedDOMWindow->document()); | 100 ASSERT(m_associatedDOMWindow->document()); |
(...skipping 18 matching lines...) Expand all Loading... |
134 { | 119 { |
135 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); | 120 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); |
136 if (!screenOrientation.document()) { | 121 if (!screenOrientation.document()) { |
137 // FIXME: we should try to return a better guess, like the latest known
value. | 122 // FIXME: we should try to return a better guess, like the latest known
value. |
138 return orientationTypeToString(blink::WebScreenOrientationPortraitPrimar
y); | 123 return orientationTypeToString(blink::WebScreenOrientationPortraitPrimar
y); |
139 } | 124 } |
140 ScreenOrientationController& controller = ScreenOrientationController::from(
*screenOrientation.document()); | 125 ScreenOrientationController& controller = ScreenOrientationController::from(
*screenOrientation.document()); |
141 return orientationTypeToString(controller.orientation()); | 126 return orientationTypeToString(controller.orientation()); |
142 } | 127 } |
143 | 128 |
144 bool ScreenOrientation::lockOrientation(Screen& screen, const AtomicString& lock
String, ExceptionState& exceptionState) | 129 ScriptPromise ScreenOrientation::lockOrientation(ScriptState* state, Screen& scr
een, const AtomicString& lockString) |
145 { | 130 { |
| 131 RefPtr<ScriptPromiseResolverWithContext> resolver = ScriptPromiseResolverWit
hContext::create(state); |
| 132 ScriptPromise promise = resolver->promise(); |
| 133 |
146 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); | 134 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); |
147 Document* document = screenOrientation.document(); | 135 Document* document = screenOrientation.document(); |
148 if (!document) | 136 |
149 return false; | 137 if (!document) { |
| 138 RefPtrWillBeRawPtr<DOMException> exception = DOMException::create(Invali
dStateError, "The object is no longer associated to a document."); |
| 139 resolver->reject(exception); |
| 140 return promise; |
| 141 } |
| 142 |
150 if (document->isSandboxed(SandboxOrientationLock)) { | 143 if (document->isSandboxed(SandboxOrientationLock)) { |
151 exceptionState.throwSecurityError("The document is sandboxed and lacks t
he 'allow-orientation-lock' flag."); | 144 RefPtrWillBeRawPtr<DOMException> exception = DOMException::create(Securi
tyError, "The document is sandboxed and lacks the 'allow-orientation-lock' flag.
"); |
152 return false; | 145 resolver->reject(exception); |
| 146 return promise; |
153 } | 147 } |
154 screenOrientation.lockOrientationAsync(stringToOrientationLock(lockString)); | 148 |
155 return true; | 149 blink::Platform::current()->lockOrientation(stringToOrientationLock(lockStri
ng), new LockOrientationCallback(resolver)); |
| 150 return promise; |
156 } | 151 } |
157 | 152 |
158 void ScreenOrientation::unlockOrientation(Screen& screen) | 153 void ScreenOrientation::unlockOrientation(Screen& screen) |
159 { | 154 { |
160 ScreenOrientation::from(screen).lockOrientationAsync(blink::WebScreenOrienta
tionLockDefault); | 155 blink::Platform::current()->unlockOrientation(); |
161 } | 156 } |
162 | 157 |
163 } // namespace WebCore | 158 } // namespace WebCore |
OLD | NEW |