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

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

Issue 169403006: Screen Orientation API: screen.orientation & orientationchange event (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 10 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 | Annotate | Revision Log
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/events/Event.h"
8 #include "core/frame/DOMWindow.h" 9 #include "core/frame/DOMWindow.h"
9 #include "core/frame/Frame.h" 10 #include "core/frame/Frame.h"
10 #include "core/frame/Screen.h" 11 #include "core/frame/Screen.h"
12 #include "modules/screen_orientation/ScreenOrientationController.h"
11 13
12 namespace WebCore { 14 namespace WebCore {
13 15
14 ScreenOrientation::ScreenOrientation(Screen* screen) 16 ScreenOrientation::ScreenOrientation(Screen* screen)
15 : DOMWindowProperty(screen->frame()) 17 : DOMWindowProperty(screen->frame())
16 { 18 {
19 if (ScreenOrientationController* controller = ScreenOrientationController::f rom(page()))
20 controller->addObserver(this);
abarth-chromium 2014/02/18 01:34:27 Under what circumstances can |controller| be zero.
Inactive 2014/02/18 20:52:47 Done. No longer needed now that the controller is
17 } 21 }
18 22
19 const char* ScreenOrientation::supplementName() 23 const char* ScreenOrientation::supplementName()
20 { 24 {
21 return "ScreenOrientation"; 25 return "ScreenOrientation";
22 } 26 }
23 27
24 ScreenOrientation* ScreenOrientation::from(Screen* screen) 28 ScreenOrientation* ScreenOrientation::from(Screen* screen)
25 { 29 {
26 ScreenOrientation* supplement = static_cast<ScreenOrientation*>(Supplement<S creen>::from(screen, supplementName())); 30 ScreenOrientation* supplement = static_cast<ScreenOrientation*>(Supplement<S creen>::from(screen, supplementName()));
27 if (!supplement) { 31 if (!supplement) {
28 ASSERT(screen); 32 ASSERT(screen);
29 supplement = new ScreenOrientation(screen); 33 supplement = new ScreenOrientation(screen);
30 provideTo(screen, supplementName(), adoptPtr(supplement)); 34 provideTo(screen, supplementName(), adoptPtr(supplement));
31 } 35 }
32 return supplement; 36 return supplement;
33 } 37 }
34 38
35 ScreenOrientation::~ScreenOrientation() 39 ScreenOrientation::~ScreenOrientation()
36 { 40 {
41 if (ScreenOrientationController* controller = ScreenOrientationController::f rom(page()))
Inactive 2014/02/17 18:23:13 I looked at the Geolocation module and it subclass
42 controller->removeObserver(this);
abarth-chromium 2014/02/18 01:34:27 I believe the correct thing to do is to override w
37 } 43 }
38 44
39 Screen* ScreenOrientation::screen() const 45 Screen* ScreenOrientation::screen() const
40 { 46 {
41 Frame* frame = this->frame(); 47 Frame* frame = this->frame();
42 ASSERT(frame); 48 ASSERT(frame);
43 DOMWindow* window = frame->domWindow(); 49 DOMWindow* window = frame->domWindow();
44 ASSERT(window); 50 ASSERT(window);
45 return window->screen(); 51 return window->screen();
abarth-chromium 2014/02/18 01:34:27 Why not just: return m_associatedDOMWindow->scree
Inactive 2014/02/18 20:52:47 This function is no longer needed now that the con
46 } 52 }
47 53
54 struct ScreenOrientationInfo {
55 const AtomicString& name;
56 ScreenOrientationValues orientation;
57 };
58
59 static ScreenOrientationInfo* allowedOrientationsMap(unsigned& length)
abarth-chromium 2014/02/18 01:34:27 Please put free static functions at the top of the
Inactive 2014/02/18 20:52:47 Done.
60 {
61 DEFINE_STATIC_LOCAL(const AtomicString, portrait, ("portrait", AtomicString: :ConstructFromLiteral));
62 DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary" , AtomicString::ConstructFromLiteral));
63 DEFINE_STATIC_LOCAL(const AtomicString, portraitSecondary, ("portrait-second ary", AtomicString::ConstructFromLiteral));
64 DEFINE_STATIC_LOCAL(const AtomicString, landscape, ("landscape", AtomicStrin g::ConstructFromLiteral));
65 DEFINE_STATIC_LOCAL(const AtomicString, landscapePrimary, ("landscape-primar y", AtomicString::ConstructFromLiteral));
66 DEFINE_STATIC_LOCAL(const AtomicString, landscapeSecondary, ("landscape-seco ndary", AtomicString::ConstructFromLiteral));
67
68 static ScreenOrientationInfo orientationMap[] = {
69 { portrait, OrientationPortrait },
70 { portraitPrimary, OrientationPortraitPrimary },
71 { portraitSecondary, OrientationPortraitSecondary },
72 { landscape, OrientationLandscape },
73 { landscapePrimary, OrientationLandscapePrimary },
74 { landscapeSecondary, OrientationLandscapeSecondary }
75 };
76 length = WTF_ARRAY_LENGTH(orientationMap);
77 return orientationMap;
78 }
79
80 const AtomicString& ScreenOrientation::orientationToString(ScreenOrientationValu e orientation)
81 {
82 unsigned length = 0;
83 ScreenOrientationInfo* orientationMap = allowedOrientationsMap(length);
84 for (unsigned i = 0; i < length; ++i) {
85 if (orientationMap[i].orientation == orientation)
86 return orientationMap[i].name;
87 }
88 // We do no handle OrientationInvalid and OrientationAny but this is fine be cause screen.orientation
89 // should never return these and WebScreenOrientation does not define those values.
90 ASSERT_NOT_REACHED();
91 return nullAtom;
92 }
93
48 const AtomicString& ScreenOrientation::orientation(Screen* screen) 94 const AtomicString& ScreenOrientation::orientation(Screen* screen)
49 { 95 {
50 // FIXME: Implement. 96 ScreenOrientation* screenOrientation = ScreenOrientation::from(screen);
51 DEFINE_STATIC_LOCAL(const AtomicString, portraitPrimary, ("portrait-primary" , AtomicString::ConstructFromLiteral)); 97 ASSERT(screenOrientation);
52 return portraitPrimary; 98
99 ScreenOrientationController* controller = ScreenOrientationController::from( screenOrientation->page());
100 if (!controller)
101 return orientationToString(OrientationPortraitPrimary);
102
103 return orientationToString(controller->orientation());
53 } 104 }
54 105
55 bool ScreenOrientation::lockOrientation(Screen* screen, const Vector<String>& or ientations) 106 bool ScreenOrientation::lockOrientation(Screen* screen, const Vector<String>& or ientations)
56 { 107 {
57 // FIXME: Implement. 108 // FIXME: Implement.
58 return false; 109 return false;
59 } 110 }
60 111
61 bool ScreenOrientation::lockOrientation(Screen* screen, const AtomicString& orie ntation) 112 bool ScreenOrientation::lockOrientation(Screen* screen, const AtomicString& orie ntation)
62 { 113 {
63 // FIXME: Implement. 114 // FIXME: Implement.
64 return false; 115 return false;
65 } 116 }
66 117
67 void ScreenOrientation::unlockOrientation(Screen* screen) 118 void ScreenOrientation::unlockOrientation(Screen* screen)
68 { 119 {
69 // FIXME: Implement. 120 // FIXME: Implement.
70 } 121 }
71 122
123 void ScreenOrientation::orientationChanged()
124 {
125 ASSERT(screen());
126 screen()->dispatchEvent(Event::create(EventTypeNames::orientationchange));
127 }
128
129 Page* ScreenOrientation::page() const
130 {
131 Frame* frame = this->frame();
132 ASSERT(frame);
133 return frame->page();
134 }
135
72 } // namespace WebCore 136 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698