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

Side by Side Diff: Source/modules/device_orientation/DeviceOrientationController.cpp

Issue 315573002: Generalize and refactor DeviceSensorEvent* architecture to support multi-event type targets. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: add stopUpdating in destructor of BatteryManager. Created 6 years, 6 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 /* 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 * Copyright 2010 Apple Inc. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be
3 * Copyright (C) 2012 Samsung Electronics. All rights reserved. 3 // found in the LICENSE file.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 4
27 #include "config.h" 5 #include "config.h"
28 #include "modules/device_orientation/DeviceOrientationController.h" 6 #include "modules/device_orientation/DeviceOrientationController.h"
29 7
30 #include "core/dom/Document.h" 8 #include "core/dom/Document.h"
31 #include "core/frame/DOMWindow.h" 9 #include "modules/EventModules.h"
32 #include "core/page/Page.h"
33 #include "modules/device_orientation/DeviceOrientationData.h" 10 #include "modules/device_orientation/DeviceOrientationData.h"
34 #include "modules/device_orientation/DeviceOrientationDispatcher.h" 11 #include "modules/device_orientation/DeviceOrientationDispatcher.h"
35 #include "modules/device_orientation/DeviceOrientationEvent.h" 12 #include "modules/device_orientation/DeviceOrientationEvent.h"
36 13
37 namespace WebCore { 14 namespace WebCore {
38 15
39 DeviceOrientationController::DeviceOrientationController(Document& document) 16 DeviceOrientationController::DeviceOrientationController(Document& document)
40 : DeviceSensorEventController(document.page()) 17 : DeviceSingleWindowEventController(document)
41 , DOMWindowLifecycleObserver(document.domWindow())
42 , m_document(document)
43 { 18 {
44 } 19 }
45 20
46 DeviceOrientationController::~DeviceOrientationController() 21 DeviceOrientationController::~DeviceOrientationController()
47 { 22 {
48 stopUpdating(); 23 stopUpdating();
49 } 24 }
50 25
51 void DeviceOrientationController::didChangeDeviceOrientation(DeviceOrientationDa ta* deviceOrientationData) 26 void DeviceOrientationController::didUpdateData()
52 { 27 {
53 if (m_overrideOrientationData) 28 if (m_overrideOrientationData)
54 return; 29 return;
55 dispatchDeviceEvent(DeviceOrientationEvent::create(EventTypeNames::deviceori entation, deviceOrientationData)); 30 dispatchDeviceEvent(lastEvent());
56 } 31 }
57 32
58 const char* DeviceOrientationController::supplementName() 33 const char* DeviceOrientationController::supplementName()
59 { 34 {
60 return "DeviceOrientationController"; 35 return "DeviceOrientationController";
61 } 36 }
62 37
63 DeviceOrientationController& DeviceOrientationController::from(Document& documen t) 38 DeviceOrientationController& DeviceOrientationController::from(Document& documen t)
64 { 39 {
65 DeviceOrientationController* controller = static_cast<DeviceOrientationContr oller*>(DocumentSupplement::from(document, supplementName())); 40 DeviceOrientationController* controller = static_cast<DeviceOrientationContr oller*>(DocumentSupplement::from(document, supplementName()));
66 if (!controller) { 41 if (!controller) {
67 controller = new DeviceOrientationController(document); 42 controller = new DeviceOrientationController(document);
68 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBe Noop(controller)); 43 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBe Noop(controller));
69 } 44 }
70 return *controller; 45 return *controller;
71 } 46 }
72 47
73 DeviceOrientationData* DeviceOrientationController::lastData() 48 DeviceOrientationData* DeviceOrientationController::lastData() const
74 { 49 {
75 return m_overrideOrientationData ? m_overrideOrientationData.get() : DeviceO rientationDispatcher::instance().latestDeviceOrientationData(); 50 return m_overrideOrientationData ? m_overrideOrientationData.get() : DeviceO rientationDispatcher::instance().latestDeviceOrientationData();
76 } 51 }
77 52
78 bool DeviceOrientationController::hasLastData() 53 bool DeviceOrientationController::hasLastData()
79 { 54 {
80 return lastData(); 55 return lastData();
81 } 56 }
82 57
83 PassRefPtrWillBeRawPtr<Event> DeviceOrientationController::getLastEvent()
84 {
85 return DeviceOrientationEvent::create(EventTypeNames::deviceorientation, las tData());
86 }
87
88 void DeviceOrientationController::registerWithDispatcher() 58 void DeviceOrientationController::registerWithDispatcher()
89 { 59 {
90 DeviceOrientationDispatcher::instance().addDeviceOrientationController(this) ; 60 DeviceOrientationDispatcher::instance().addController(this);
91 } 61 }
92 62
93 void DeviceOrientationController::unregisterWithDispatcher() 63 void DeviceOrientationController::unregisterWithDispatcher()
94 { 64 {
95 DeviceOrientationDispatcher::instance().removeDeviceOrientationController(th is); 65 DeviceOrientationDispatcher::instance().removeController(this);
96 } 66 }
97 67
98 bool DeviceOrientationController::isNullEvent(Event* event) 68 PassRefPtrWillBeRawPtr<Event> DeviceOrientationController::lastEvent() const
69 {
70 return DeviceOrientationEvent::create(eventTypeName(), lastData());
71 }
72
73 bool DeviceOrientationController::isNullEvent(Event* event) const
99 { 74 {
100 DeviceOrientationEvent* orientationEvent = toDeviceOrientationEvent(event); 75 DeviceOrientationEvent* orientationEvent = toDeviceOrientationEvent(event);
101 return !orientationEvent->orientation()->canProvideEventData(); 76 return !orientationEvent->orientation()->canProvideEventData();
102 } 77 }
103 78
104 Document* DeviceOrientationController::document() 79 const AtomicString& DeviceOrientationController::eventTypeName() const
105 { 80 {
106 return &m_document; 81 return EventTypeNames::deviceorientation;
107 }
108
109 void DeviceOrientationController::didAddEventListener(DOMWindow* window, const A tomicString& eventType)
110 {
111 if (eventType != EventTypeNames::deviceorientation)
112 return;
113
114 if (page() && page()->visibilityState() == PageVisibilityStateVisible)
115 startUpdating();
116
117 m_hasEventListener = true;
118 }
119
120 void DeviceOrientationController::didRemoveEventListener(DOMWindow* window, cons t AtomicString& eventType)
121 {
122 if (eventType != EventTypeNames::deviceorientation || window->hasEventListen ers(EventTypeNames::deviceorientation))
123 return;
124
125 stopUpdating();
126 m_hasEventListener = false;
127 }
128
129 void DeviceOrientationController::didRemoveAllEventListeners(DOMWindow* window)
130 {
131 stopUpdating();
132 m_hasEventListener = false;
133 } 82 }
134 83
135 void DeviceOrientationController::setOverride(DeviceOrientationData* deviceOrien tationData) 84 void DeviceOrientationController::setOverride(DeviceOrientationData* deviceOrien tationData)
136 { 85 {
137 m_overrideOrientationData.clear(); 86 ASSERT(deviceOrientationData);
138 didChangeDeviceOrientation(deviceOrientationData);
139 m_overrideOrientationData = deviceOrientationData; 87 m_overrideOrientationData = deviceOrientationData;
88 dispatchDeviceEvent(lastEvent());
140 } 89 }
141 90
142 void DeviceOrientationController::clearOverride() 91 void DeviceOrientationController::clearOverride()
143 { 92 {
144 if (!m_overrideOrientationData) 93 if (!m_overrideOrientationData)
145 return; 94 return;
146 m_overrideOrientationData.clear(); 95 m_overrideOrientationData.clear();
147 DeviceOrientationData* orientation = lastData(); 96 if (lastData())
148 if (orientation) 97 didUpdateData();
149 didChangeDeviceOrientation(orientation);
150 } 98 }
151 99
152 void DeviceOrientationController::trace(Visitor* visitor) 100 void DeviceOrientationController::trace(Visitor* visitor)
153 { 101 {
154 visitor->trace(m_overrideOrientationData); 102 visitor->trace(m_overrideOrientationData);
155 DocumentSupplement::trace(visitor); 103 DocumentSupplement::trace(visitor);
156 } 104 }
157 105
158 } // namespace WebCore 106 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698