|
Line 0
a/Source/WebCore/html/HTMLDeviceElement.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are |
| 6 |
* met: |
| 7 |
* |
| 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 |
| 11 |
* copyright notice, this list of conditions and the following disclaimer |
| 12 |
* in the documentation and/or other materials provided with the |
| 13 |
* distribution. |
| 14 |
* * Neither the name of Google Inc. nor the names of its |
| 15 |
* contributors may be used to endorse or promote products derived from |
| 16 |
* this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "config.h" |
| 32 |
#include "HTMLDeviceElement.h" |
| 33 |
|
| 34 |
#if ENABLE(DEVICE_ELEMENT) |
| 35 |
|
| 36 |
#include "DeviceConnection.h" |
| 37 |
#include "Event.h" |
| 38 |
#include "EventHandler.h" |
| 39 |
#include "EventNames.h" |
| 40 |
#include "Frame.h" |
| 41 |
#include "HTMLNames.h" |
| 42 |
#include "MouseEvent.h" |
| 43 |
#include "Page.h" |
| 44 |
#include "RenderDevice.h" |
| 45 |
#include "Stream.h" |
| 46 |
#include <wtf/Assertions.h> |
| 47 |
|
| 48 |
namespace WebCore { |
| 49 |
|
| 50 |
using namespace HTMLNames; |
| 51 |
|
| 52 |
PassRefPtr<HTMLDeviceElement> HTMLDeviceElement::create(const QualifiedName& tagName, Document* document) |
| 53 |
{ |
| 54 |
return adoptRef(new HTMLDeviceElement(tagName, document)); |
| 55 |
} |
| 56 |
|
| 57 |
HTMLDeviceElement::HTMLDeviceElement(const QualifiedName& tagName, Document* document) |
| 58 |
: HTMLElement(tagName, document) |
| 59 |
, m_state(NotConnected) |
| 60 |
, m_controller(HTMLElement::document()->page() ? HTMLElement::document()->page()->deviceConnectionController() : 0) |
| 61 |
{ |
| 62 |
} |
| 63 |
|
| 64 |
HTMLDeviceElement::~HTMLDeviceElement() |
| 65 |
{ |
| 66 |
} |
| 67 |
|
| 68 |
bool HTMLDeviceElement::rendererIsNeeded(RenderStyle *style) |
| 69 |
{ |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
RenderObject* HTMLDeviceElement::createRenderer(RenderArena* arena, RenderStyle* style) |
| 74 |
{ |
| 75 |
return new (arena) RenderDevice(this); |
| 76 |
} |
| 77 |
|
| 78 |
void HTMLDeviceElement::parseMappedAttribute(Attribute* attribute) |
| 79 |
{ |
| 80 |
if (attribute->name() == typeAttr) |
| 81 |
m_type = DeviceType(fastGetAttribute(typeAttr)); |
| 82 |
else |
| 83 |
HTMLElement::parseMappedAttribute(attribute); |
| 84 |
} |
| 85 |
|
| 86 |
void HTMLDeviceElement::attach() |
| 87 |
{ |
| 88 |
HTMLElement::attach(); |
| 89 |
|
| 90 |
// Create a new local device connection object for the current type. |
| 91 |
m_connectionId = m_controller->registerDeviceConnection( |
| 92 |
DeviceConnection(m_type, DeviceConnectionId::Local, document()->securityOrigin()), |
| 93 |
this); |
| 94 |
|
| 95 |
// The call to updateFromElement() needs to go after the call through |
| 96 |
// to the base class's attach() because that can sometimes do a close |
| 97 |
// on the renderer. |
| 98 |
if (renderer()) |
| 99 |
renderer()->updateFromElement(); |
| 100 |
} |
| 101 |
|
| 102 |
void HTMLDeviceElement::detach() |
| 103 |
{ |
| 104 |
// Spec question: should we close any active local device connections when the device element |
| 105 |
// used to create them is detached from the DOM? There may be nothing else associated with the |
| 106 |
// connection that could ask to close it, leaving the connection open until failure. |
| 107 |
if (m_controller) { |
| 108 |
if (connection()->state() == DeviceConnection::Connected) |
| 109 |
m_controller->disconnectDevice(m_connectionId); |
| 110 |
|
| 111 |
if (!m_connectionId.isNull()) |
| 112 |
m_controller->unregisterConnectionListener(m_connectionId, this); |
| 113 |
} |
| 114 |
|
| 115 |
HTMLElement::detach(); |
| 116 |
} |
| 117 |
|
| 118 |
void HTMLDeviceElement::setType(const String& type) |
| 119 |
{ |
| 120 |
DeviceType m_previousType = m_type; |
| 121 |
m_type = DeviceType(type); |
| 122 |
|
| 123 |
if (m_type.id() != m_previousType.id()) |
| 124 |
closeAndResetConnection(); |
| 125 |
} |
| 126 |
|
| 127 |
PassRefPtr<Stream> HTMLDeviceElement::data() |
| 128 |
{ |
| 129 |
if (!m_controller) |
| 130 |
return 0; |
| 131 |
|
| 132 |
// JS binding should make this assertion always true. |
| 133 |
ASSERT(connection()->state() == DeviceConnection::Connected); |
| 134 |
return connection()->stream(); |
| 135 |
} |
| 136 |
|
| 137 |
HTMLDeviceElement::DeviceElementState HTMLDeviceElement::state() const |
| 138 |
{ |
| 139 |
return m_state; |
| 140 |
} |
| 141 |
|
| 142 |
const DeviceType& HTMLDeviceElement::deviceType() const |
| 143 |
{ |
| 144 |
return m_type; |
| 145 |
} |
| 146 |
|
| 147 |
const DeviceConnection* HTMLDeviceElement::connection() const |
| 148 |
{ |
| 149 |
if (!m_controller) |
| 150 |
return 0; |
| 151 |
|
| 152 |
ASSERT(m_controller); |
| 153 |
return &m_controller->connection(m_connectionId); |
| 154 |
} |
| 155 |
|
| 156 |
void HTMLDeviceElement::setState(DeviceElementState state) |
| 157 |
{ |
| 158 |
if (m_state != state) { |
| 159 |
m_state = state; |
| 160 |
|
| 161 |
if (renderer()) |
| 162 |
renderer()->updateFromElement(); |
| 163 |
|
| 164 |
shadowAncestorNode()->renderer()->repaint(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
void HTMLDeviceElement::closeAndResetConnection() |
| 169 |
{ |
| 170 |
if (m_controller && m_state == Connected) { |
| 171 |
// If the connection is in the Connected state then we are disconnecting. |
| 172 |
// If not then we are resetting a closed, cancelled or failed connection. |
| 173 |
if (connection()->state() == DeviceConnection::Connected) |
| 174 |
m_controller->disconnectDevice(m_connectionId); |
| 175 |
|
| 176 |
// Remove the listener to the current connection. This should destroy any non-active local device connection. |
| 177 |
// The active connections will be destroyed when the connection is updated to the closed state. |
| 178 |
if (!m_connectionId.isNull()) |
| 179 |
m_controller->unregisterConnectionListener(m_connectionId, this); |
| 180 |
|
| 181 |
// Associate the element to a new local device connection object in the NoConnection state. |
| 182 |
m_connectionId = m_controller->registerDeviceConnection( |
| 183 |
DeviceConnection(m_type, DeviceConnectionId::Local, document()->securityOrigin()), this); |
| 184 |
|
| 185 |
setState(NotConnected); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void HTMLDeviceElement::defaultEventHandler(Event* event) |
| 190 |
{ |
| 191 |
if (!m_controller) |
| 192 |
return; |
| 193 |
|
| 194 |
// Ignore events from invalid device elements. |
| 195 |
if (!m_type.isValid()) |
| 196 |
return; |
| 197 |
|
| 198 |
// For security reasons, only allow clicks directly coming from the user. |
| 199 |
if (!event->fromUserGesture()) { |
| 200 |
HTMLElement::defaultEventHandler(event); |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
if (event->type() == eventNames().clickEvent) { |
| 205 |
switch (m_state) { |
| 206 |
case NotConnected: |
| 207 |
ASSERT(connection()->state() == DeviceConnection::NotConnected); |
| 208 |
if (m_controller->connectToLocalDevice(m_connectionId)) |
| 209 |
setState(Connecting); |
| 210 |
break; |
| 211 |
|
| 212 |
case Connected: |
| 213 |
ASSERT(connection()->state() == DeviceConnection::Connected); |
| 214 |
m_controller->disconnectDevice(m_connectionId); |
| 215 |
setState(Disconnecting); |
| 216 |
break; |
| 217 |
|
| 218 |
case Connecting: |
| 219 |
case Disconnecting: |
| 220 |
// Nothing to do here, wait for some device connection update notification. |
| 221 |
break; |
| 222 |
|
| 223 |
default: |
| 224 |
// Invalid device element state value. |
| 225 |
ASSERT_NOT_REACHED(); |
| 226 |
} |
| 227 |
event->setDefaultHandled(); |
| 228 |
} |
| 229 |
|
| 230 |
if (!event->defaultHandled()) |
| 231 |
HTMLElement::defaultEventHandler(event); |
| 232 |
} |
| 233 |
|
| 234 |
void HTMLDeviceElement::deviceConnectionUpdate(const DeviceConnection& connection) |
| 235 |
{ |
| 236 |
switch (connection.state()) { |
| 237 |
case DeviceConnection::NotConnected: |
| 238 |
ASSERT_NOT_REACHED(); |
| 239 |
break; |
| 240 |
|
| 241 |
case DeviceConnection::Connected: |
| 242 |
ASSERT(m_state == Connecting); |
| 243 |
setState(Connected); |
| 244 |
|
| 245 |
// FIXME: dispatching the event only when a device is connected ignoring other situations. |
| 246 |
// Update this as the specification evolves (see failed case below). |
| 247 |
dispatchEvent(Event::create(eventNames().changeEvent, true, false)); |
| 248 |
break; |
| 249 |
|
| 250 |
case DeviceConnection::Cancelled: |
| 251 |
ASSERT(m_state == Connecting); |
| 252 |
closeAndResetConnection(); |
| 253 |
break; |
| 254 |
|
| 255 |
case DeviceConnection::Failed: |
| 256 |
ASSERT(m_state != NotConnected); |
| 257 |
// FIXME: how should we report the error to the user? Specification doesn't mention this. |
| 258 |
// Should we launch an onError event? |
| 259 |
closeAndResetConnection(); |
| 260 |
break; |
| 261 |
|
| 262 |
case DeviceConnection::Disconnected: |
| 263 |
ASSERT(m_state == Connected || m_state == Disconnecting); |
| 264 |
// FIXME: should we dispatch another onchange event? |
| 265 |
closeAndResetConnection(); |
| 266 |
break; |
| 267 |
|
| 268 |
default: |
| 269 |
// Invalid device connection state value. |
| 270 |
ASSERT_NOT_REACHED(); |
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
void HTMLDeviceElement::connectionControllerShutdown() |
| 276 |
{ |
| 277 |
m_connectionId.reset(); |
| 278 |
m_controller = 0; |
| 279 |
setState(NotConnected); |
| 280 |
} |
| 281 |
|
| 282 |
HTMLDeviceElement* toHTMLDeviceElement(Element* element) |
| 283 |
{ |
| 284 |
if (element->isHTMLElement() && element->hasTagName(HTMLNames::deviceTag)) |
| 285 |
return static_cast<HTMLDeviceElement*>(element); |
| 286 |
|
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
} // namespace WebCore |
| 291 |
|
| 292 |
#endif // ENABLE(DEVICE_ELEMENT) |