|
1 /* |
|
2 * Copyright 2010, The Android Open Source Project |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions |
|
6 * are met: |
|
7 * * Redistributions of source code must retain the above copyright |
|
8 * notice, this list of conditions and the following disclaimer. |
|
9 * * Redistributions in binary form must reproduce the above copyright |
|
10 * notice, this list of conditions and the following disclaimer in the |
|
11 * documentation and/or other materials provided with the distribution. |
|
12 * |
|
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
|
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
24 */ |
|
25 |
|
26 #include "config.h" |
|
27 #include "DeviceOrientationController.h" |
|
28 |
|
29 #if ENABLE(DEVICE_ORIENTATION) |
|
30 |
|
31 #include "DeviceOrientation.h" |
|
32 #include "DeviceOrientationClient.h" |
|
33 #include "DeviceOrientationEvent.h" |
|
34 |
|
35 namespace WebCore { |
|
36 |
|
37 DeviceOrientationController::DeviceOrientationController(Page* page, DeviceOrientationClient* client) |
|
38 : m_page(page) |
|
39 , m_client(client) |
|
40 { |
|
41 } |
|
42 |
|
43 void DeviceOrientationController::addListener(DOMWindow* window) |
|
44 { |
|
45 // If no client is present, signal that no orientation data is available. |
|
46 // If the client already has an orientation, call back to this new listener |
|
47 // immediately. |
|
48 if (!m_client) { |
|
49 RefPtr<DeviceOrientation> emptyOrientation = DeviceOrientation::create(); |
|
50 window->dispatchEvent(DeviceOrientationEvent::create(eventNames().deviceorientationEvent, emptyOrientation.get())); |
|
51 } else if (m_client && m_client->lastOrientation()) |
|
52 window->dispatchEvent(DeviceOrientationEvent::create(eventNames().deviceorientationEvent, m_client->lastOrientation())); |
|
53 |
|
54 // The client may call back synchronously. |
|
55 bool wasEmpty = m_listeners.isEmpty(); |
|
56 m_listeners.add(window); |
|
57 if (wasEmpty && m_client) |
|
58 m_client->startUpdating(); |
|
59 } |
|
60 |
|
61 void DeviceOrientationController::removeListener(DOMWindow* window) |
|
62 { |
|
63 m_listeners.remove(window); |
|
64 if (m_listeners.isEmpty() && m_client) |
|
65 m_client->stopUpdating(); |
|
66 } |
|
67 |
|
68 void DeviceOrientationController::removeAllListeners(DOMWindow* window) |
|
69 { |
|
70 // May be called with a DOMWindow that's not a listener. |
|
71 if (!m_listeners.contains(window)) |
|
72 return; |
|
73 |
|
74 m_listeners.removeAll(window); |
|
75 if (m_listeners.isEmpty() && m_client) |
|
76 m_client->stopUpdating(); |
|
77 } |
|
78 |
|
79 void DeviceOrientationController::didChangeDeviceOrientation(DeviceOrientation* orientation) |
|
80 { |
|
81 RefPtr<DeviceOrientationEvent> event = DeviceOrientationEvent::create(eventNames().deviceorientationEvent, orientation); |
|
82 Vector<DOMWindow*> listenersVector; |
|
83 copyToVector(m_listeners, listenersVector); |
|
84 for (size_t i = 0; i < listenersVector.size(); ++i) |
|
85 listenersVector[i]->dispatchEvent(event); |
|
86 } |
|
87 |
|
88 } // namespace WebCore |
|
89 |
|
90 #endif // ENABLE(DEVICE_ORIENTATION) |