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

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

Issue 283423005: Use Promises for screen.lockOrientation(). (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: cdumez comments Created 6 years, 7 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/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
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
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(
130 ExecutionContext* context, Screen& screen, const AtomicString& lockString)
jochen (gone - plz use gerrit) 2014/05/22 08:34:35 should go on previous line
mlamouri (slow - plz ping) 2014/05/22 08:58:56 Done. Though, that makes the line ~120 characters
145 { 131 {
132 RefPtr<ScriptPromiseResolverWithContext> resolver = ScriptPromiseResolverWit hContext::create(ScriptState::current(toIsolate(context)));
133 ScriptPromise promise = resolver->promise();
134
146 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen); 135 ScreenOrientation& screenOrientation = ScreenOrientation::from(screen);
147 Document* document = screenOrientation.document(); 136 Document* document = screenOrientation.document();
148 if (!document) 137
149 return false; 138 if (!document) {
139 RefPtrWillBeRawPtr<DOMException> exception =
140 DOMException::create(InvalidStateError, "The object is no longer ass ociated to a document.");
jochen (gone - plz use gerrit) 2014/05/22 08:34:35 should go on previous line
mlamouri (slow - plz ping) 2014/05/22 08:58:56 Done. 140 characters long :(
141 resolver->reject(exception);
142 return promise;
143 }
144
150 if (document->isSandboxed(SandboxOrientationLock)) { 145 if (document->isSandboxed(SandboxOrientationLock)) {
151 exceptionState.throwSecurityError("The document is sandboxed and lacks t he 'allow-orientation-lock' flag."); 146 RefPtrWillBeRawPtr<DOMException> exception =
152 return false; 147 DOMException::create(SecurityError, "The document is sandboxed and l acks the 'allow-orientation-lock' flag.");
jochen (gone - plz use gerrit) 2014/05/22 08:34:35 and here
mlamouri (slow - plz ping) 2014/05/22 08:58:56 160 :'(
148 resolver->reject(exception);
149 return promise;
153 } 150 }
154 screenOrientation.lockOrientationAsync(stringToOrientationLock(lockString)); 151
155 return true; 152 blink::Platform::current()->lockOrientation(
153 stringToOrientationLock(lockString),
154 new LockOrientationCallback(resolver));
jochen (gone - plz use gerrit) 2014/05/22 08:34:35 and this, all in one line
mlamouri (slow - plz ping) 2014/05/22 08:58:56 Obviously, having vertical screen is not compatibl
155 return promise;
156 } 156 }
157 157
158 void ScreenOrientation::unlockOrientation(Screen& screen) 158 void ScreenOrientation::unlockOrientation(Screen& screen)
159 { 159 {
160 ScreenOrientation::from(screen).lockOrientationAsync(blink::WebScreenOrienta tionLockDefault); 160 blink::Platform::current()->unlockOrientation();
161 } 161 }
162 162
163 } // namespace WebCore 163 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698