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

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

Issue 374623002: [screen-orientation] Update implementation to match recent spec changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: rebase Created 6 years, 5 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 "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
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(LocalFrame* frame)
89 { 88 {
89 ASSERT(frame);
90
91 ScreenOrientation* orientation = adoptRefCountedGarbageCollectedWillBeNoop(n ew ScreenOrientation(frame));
92 ASSERT(orientation->controller());
93 // FIXME: ideally, we would like to provide the ScreenOrientationController
94 // the case where it is not defined but for the moment, it is eagerly
95 // created when the LocalFrame is created so we shouldn't be in that
96 // situtaion.
97 // In order to create the ScreenOrientationController lazily, we would need
98 // to be able to access WebFrameClient from modules/.
99
100 orientation->controller()->setOrientation(orientation);
101 return orientation;
90 } 102 }
91 103
92 const char* ScreenOrientation::supplementName() 104 ScreenOrientation::ScreenOrientation(LocalFrame* frame)
105 : DOMWindowProperty(frame)
106 , m_type(blink::WebScreenOrientationUndefined)
107 , m_angle(0)
93 { 108 {
94 return "ScreenOrientation"; 109 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 } 110 }
114 111
115 ScreenOrientation::~ScreenOrientation() 112 ScreenOrientation::~ScreenOrientation()
116 { 113 {
117 } 114 }
118 115
119 const AtomicString& ScreenOrientation::orientation(Screen& screen) 116 const WTF::AtomicString& ScreenOrientation::interfaceName() const
120 { 117 {
121 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); 118 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 } 119 }
129 120
130 ScriptPromise ScreenOrientation::lockOrientation(ScriptState* state, Screen& scr een, const AtomicString& lockString) 121 ExecutionContext* ScreenOrientation::executionContext() const
122 {
123 if (!m_frame)
124 return 0;
125 return m_frame->document();
126 }
127
128 String ScreenOrientation::type() const
129 {
130 return orientationTypeToString(m_type);
131 }
132
133 unsigned short ScreenOrientation::angle() const
134 {
135 return m_angle;
136 }
137
138 void ScreenOrientation::setType(blink::WebScreenOrientationType type)
139 {
140 m_type = type;
141 }
142
143 void ScreenOrientation::setAngle(unsigned short angle)
144 {
145 m_angle = angle;
146 }
147
148 ScriptPromise ScreenOrientation::lock(ScriptState* state, const AtomicString& lo ckString)
131 { 149 {
132 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(state ); 150 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(state );
133 ScriptPromise promise = resolver->promise(); 151 ScriptPromise promise = resolver->promise();
134 152
135 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); 153 Document* document = m_frame ? m_frame->document() : 0;
136 Document* document = screenOrientation.document();
137 154
138 if (!document || !screenOrientation.frame()) { 155 if (!document || !controller()) {
139 RefPtrWillBeRawPtr<DOMException> exception = DOMException::create(Invali dStateError, "The object is no longer associated to a document."); 156 RefPtrWillBeRawPtr<DOMException> exception = DOMException::create(Invali dStateError, "The object is no longer associated to a document.");
140 resolver->reject(exception); 157 resolver->reject(exception);
141 return promise; 158 return promise;
142 } 159 }
143 160
144 if (document->isSandboxed(SandboxOrientationLock)) { 161 if (document->isSandboxed(SandboxOrientationLock)) {
145 RefPtrWillBeRawPtr<DOMException> exception = DOMException::create(Securi tyError, "The document is sandboxed and lacks the 'allow-orientation-lock' flag. "); 162 RefPtrWillBeRawPtr<DOMException> exception = DOMException::create(Securi tyError, "The document is sandboxed and lacks the 'allow-orientation-lock' flag. ");
146 resolver->reject(exception); 163 resolver->reject(exception);
147 return promise; 164 return promise;
148 } 165 }
149 166
150 ScreenOrientationController::from(*screenOrientation.frame()).lockOrientatio n(stringToOrientationLock(lockString), new LockOrientationCallback(resolver)); 167 controller()->lock(stringToOrientationLock(lockString), new LockOrientationC allback(resolver));
151 return promise; 168 return promise;
152 } 169 }
153 170
154 void ScreenOrientation::unlockOrientation(Screen& screen) 171 void ScreenOrientation::unlock()
155 { 172 {
156 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); 173 if (!controller())
157 if (!screenOrientation.frame())
158 return; 174 return;
159 175
160 ScreenOrientationController::from(*screenOrientation.frame()).unlockOrientat ion(); 176 controller()->unlock();
177 }
178
179 ScreenOrientationController* ScreenOrientation::controller()
180 {
181 if (!m_frame)
182 return 0;
183
184 return ScreenOrientationController::from(*m_frame);
185 }
186
187 void ScreenOrientation::trace(Visitor* visitor)
188 {
189 EventTargetWithInlineData::trace(visitor);
161 } 190 }
162 191
163 } // namespace WebCore 192 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/screen_orientation/ScreenOrientation.h ('k') | Source/modules/screen_orientation/ScreenOrientation.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698