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/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
11 #include "core/dom/Document.h" | 11 #include "core/dom/Document.h" |
12 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
13 #include "core/frame/LocalDOMWindow.h" | |
14 #include "core/frame/LocalFrame.h" | 13 #include "core/frame/LocalFrame.h" |
15 #include "core/frame/Screen.h" | 14 #include "modules/EventTargetModules.h" |
16 #include "modules/screen_orientation/LockOrientationCallback.h" | 15 #include "modules/screen_orientation/LockOrientationCallback.h" |
17 #include "modules/screen_orientation/ScreenOrientationController.h" | 16 #include "modules/screen_orientation/ScreenOrientationController.h" |
18 #include "public/platform/WebScreenOrientationType.h" | 17 #include "public/platform/WebScreenOrientationType.h" |
19 | 18 |
20 // This code assumes that WebScreenOrientationType values are included in WebScr eenOrientationLockType. | 19 // This code assumes that WebScreenOrientationType values are included in WebScr eenOrientationLockType. |
21 #define COMPILE_ASSERT_MATCHING_ENUM(enum1, enum2) \ | 20 #define COMPILE_ASSERT_MATCHING_ENUM(enum1, enum2) \ |
22 COMPILE_ASSERT(static_cast<unsigned>(blink::enum1) == static_cast<unsigned>( blink::enum2), mismatching_types) | 21 COMPILE_ASSERT(static_cast<unsigned>(blink::enum1) == static_cast<unsigned>( blink::enum2), mismatching_types) |
23 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationPortraitPrimary, WebScreenOrien tationLockPortraitPrimary); | 22 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationPortraitPrimary, WebScreenOrien tationLockPortraitPrimary); |
24 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationPortraitSecondary, WebScreenOri entationLockPortraitSecondary); | 23 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationPortraitSecondary, WebScreenOri entationLockPortraitSecondary); |
25 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationLandscapePrimary, WebScreenOrie ntationLockLandscapePrimary); | 24 COMPILE_ASSERT_MATCHING_ENUM(WebScreenOrientationLandscapePrimary, WebScreenOrie ntationLockLandscapePrimary); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 ScreenOrientationInfo* orientationMap = orientationsMap(length); | 76 ScreenOrientationInfo* orientationMap = orientationsMap(length); |
78 for (unsigned i = 0; i < length; ++i) { | 77 for (unsigned i = 0; i < length; ++i) { |
79 if (orientationMap[i].name == orientationLockString) | 78 if (orientationMap[i].name == orientationLockString) |
80 return static_cast<blink::WebScreenOrientationLockType>(orientationM ap[i].orientation); | 79 return static_cast<blink::WebScreenOrientationLockType>(orientationM ap[i].orientation); |
81 } | 80 } |
82 | 81 |
83 ASSERT_NOT_REACHED(); | 82 ASSERT_NOT_REACHED(); |
84 return blink::WebScreenOrientationLockDefault; | 83 return blink::WebScreenOrientationLockDefault; |
85 } | 84 } |
86 | 85 |
87 ScreenOrientation::ScreenOrientation(Screen& screen) | 86 // static |
88 : DOMWindowProperty(screen.frame()) | 87 ScreenOrientation* ScreenOrientation::create(ScreenOrientationController* contro ller, ExecutionContext* executionContext) |
89 { | 88 { |
89 ScreenOrientation* orientation = adoptRefCountedGarbageCollectedWillBeNoop(n ew ScreenOrientation(controller, executionContext)); | |
90 orientation->suspendIfNeeded(); | |
91 controller->setOrientation(orientation); | |
92 | |
93 return orientation; | |
90 } | 94 } |
91 | 95 |
92 const char* ScreenOrientation::supplementName() | 96 ScreenOrientation::ScreenOrientation(ScreenOrientationController* controller, Ex ecutionContext* executionContext) |
97 : ActiveDOMObject(executionContext) | |
98 , m_controller(controller) | |
99 , m_type(blink::WebScreenOrientationUndefined) | |
100 , m_angle(0) | |
93 { | 101 { |
94 return "ScreenOrientation"; | 102 ScriptWrappable::init(this); |
95 } | |
96 | |
97 Document* ScreenOrientation::document() const | |
98 { | |
99 if (!m_associatedDOMWindow || !m_associatedDOMWindow->isCurrentlyDisplayedIn Frame()) | |
100 return 0; | |
101 ASSERT(m_associatedDOMWindow->document()); | |
102 return m_associatedDOMWindow->document(); | |
103 } | |
104 | |
105 ScreenOrientation& ScreenOrientation::from(Screen& screen) | |
106 { | |
107 ScreenOrientation* supplement = static_cast<ScreenOrientation*>(WillBeHeapSu pplement<Screen>::from(screen, supplementName())); | |
108 if (!supplement) { | |
109 supplement = new ScreenOrientation(screen); | |
110 provideTo(screen, supplementName(), adoptPtrWillBeNoop(supplement)); | |
111 } | |
112 return *supplement; | |
113 } | 103 } |
114 | 104 |
115 ScreenOrientation::~ScreenOrientation() | 105 ScreenOrientation::~ScreenOrientation() |
116 { | 106 { |
107 m_controller->setOrientation(0); | |
117 } | 108 } |
118 | 109 |
119 const AtomicString& ScreenOrientation::orientation(Screen& screen) | 110 const WTF::AtomicString& ScreenOrientation::interfaceName() const |
120 { | 111 { |
121 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); | 112 return EventTargetNames::ScreenOrientation; |
122 if (!screenOrientation.frame()) { | |
123 // FIXME: we should try to return a better guess, like the latest known value. | |
124 return orientationTypeToString(blink::WebScreenOrientationPortraitPrimar y); | |
125 } | |
126 ScreenOrientationController& controller = ScreenOrientationController::from( *screenOrientation.frame()); | |
127 return orientationTypeToString(controller.orientation()); | |
128 } | 113 } |
129 | 114 |
130 ScriptPromise ScreenOrientation::lockOrientation(ScriptState* state, Screen& scr een, const AtomicString& lockString) | 115 ExecutionContext* ScreenOrientation::executionContext() const |
116 { | |
117 return ActiveDOMObject::executionContext(); | |
118 } | |
119 | |
120 String ScreenOrientation::type() const | |
121 { | |
122 return orientationTypeToString(m_type); | |
123 } | |
124 | |
125 unsigned short ScreenOrientation::angle() const | |
126 { | |
127 return m_angle; | |
128 } | |
129 | |
130 void ScreenOrientation::setType(blink::WebScreenOrientationType type) | |
131 { | |
132 m_type = type; | |
133 } | |
134 | |
135 void ScreenOrientation::setAngle(unsigned short angle) | |
136 { | |
137 m_angle = angle; | |
138 } | |
139 | |
140 ScriptPromise ScreenOrientation::lock(ScriptState* state, const AtomicString& lo ckString) | |
131 { | 141 { |
132 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(state ); | 142 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(state ); |
133 ScriptPromise promise = resolver->promise(); | 143 ScriptPromise promise = resolver->promise(); |
134 | 144 |
135 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); | 145 Document* document = m_controller->frame().document(); |
136 Document* document = screenOrientation.document(); | |
137 | 146 |
138 if (!document || !screenOrientation.frame()) { | 147 if (!document) { |
139 RefPtrWillBeRawPtr<DOMException> exception = DOMException::create(Invali dStateError, "The object is no longer associated to a document."); | 148 RefPtrWillBeRawPtr<DOMException> exception = DOMException::create(Invali dStateError, "The object is no longer associated to a document."); |
140 resolver->reject(exception); | 149 resolver->reject(exception); |
141 return promise; | 150 return promise; |
142 } | 151 } |
143 | 152 |
144 if (document->isSandboxed(SandboxOrientationLock)) { | 153 if (document->isSandboxed(SandboxOrientationLock)) { |
145 RefPtrWillBeRawPtr<DOMException> exception = DOMException::create(Securi tyError, "The document is sandboxed and lacks the 'allow-orientation-lock' flag. "); | 154 RefPtrWillBeRawPtr<DOMException> exception = DOMException::create(Securi tyError, "The document is sandboxed and lacks the 'allow-orientation-lock' flag. "); |
146 resolver->reject(exception); | 155 resolver->reject(exception); |
147 return promise; | 156 return promise; |
148 } | 157 } |
149 | 158 |
150 ScreenOrientationController::from(*screenOrientation.frame()).lockOrientatio n(stringToOrientationLock(lockString), new LockOrientationCallback(resolver)); | 159 m_controller->lock(stringToOrientationLock(lockString), new LockOrientationC allback(resolver)); |
151 return promise; | 160 return promise; |
152 } | 161 } |
153 | 162 |
154 void ScreenOrientation::unlockOrientation(Screen& screen) | 163 void ScreenOrientation::unlock() |
155 { | 164 { |
156 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); | 165 m_controller->unlock(); |
157 if (!screenOrientation.frame()) | 166 } |
158 return; | |
159 | 167 |
160 ScreenOrientationController::from(*screenOrientation.frame()).unlockOrientat ion(); | 168 bool ScreenOrientation::hasPendingActivity() const |
169 { | |
170 return hasEventListeners(); | |
171 } | |
abarth-chromium
2014/07/07 17:29:33
You don't need this code because this object is ke
mlamouri (slow - plz ping)
2014/07/08 14:07:59
Done.
| |
172 | |
173 void ScreenOrientation::trace(Visitor* visitor) | |
174 { | |
175 EventTargetWithInlineData::trace(visitor); | |
161 } | 176 } |
162 | 177 |
163 } // namespace WebCore | 178 } // namespace WebCore |
OLD | NEW |