/platform/external/webkit/WebCore/bindings/js/JSMessagePortCustom.cpp

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 116 lines · 69 code · 17 blank · 30 comment · 9 complexity · b89e0908d28349fce5f2880138f2d41a MD5 · raw file

  1. /*
  2. * Copyright (C) 2008, 2009 Apple 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
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. 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 APPLE INC. ``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 APPLE INC. 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. #include "config.h"
  26. #include "JSMessagePort.h"
  27. #include "AtomicString.h"
  28. #include "Event.h"
  29. #include "ExceptionCode.h"
  30. #include "Frame.h"
  31. #include "JSDOMGlobalObject.h"
  32. #include "JSEvent.h"
  33. #include "JSEventListener.h"
  34. #include "JSMessagePortCustom.h"
  35. #include "MessagePort.h"
  36. #include <runtime/Error.h>
  37. using namespace JSC;
  38. namespace WebCore {
  39. void JSMessagePort::markChildren(MarkStack& markStack)
  40. {
  41. Base::markChildren(markStack);
  42. // If we have a locally entangled port, we can directly mark it as reachable. Ports that are remotely entangled are marked in-use by markActiveObjectsForContext().
  43. if (MessagePort* entangledPort = m_impl->locallyEntangledPort())
  44. markDOMObjectWrapper(markStack, *Heap::heap(this)->globalData(), entangledPort);
  45. m_impl->markJSEventListeners(markStack);
  46. }
  47. JSValue JSMessagePort::addEventListener(ExecState* exec, const ArgList& args)
  48. {
  49. JSValue listener = args.at(1);
  50. if (!listener.isObject())
  51. return jsUndefined();
  52. impl()->addEventListener(args.at(0).toString(exec), JSEventListener::create(asObject(listener), this, false, currentWorld(exec)).get(), args.at(2).toBoolean(exec));
  53. return jsUndefined();
  54. }
  55. JSValue JSMessagePort::removeEventListener(ExecState* exec, const ArgList& args)
  56. {
  57. JSValue listener = args.at(1);
  58. if (!listener.isObject())
  59. return jsUndefined();
  60. impl()->removeEventListener(args.at(0).toString(exec), JSEventListener::create(asObject(listener), this, false, currentWorld(exec)).get(), args.at(2).toBoolean(exec));
  61. return jsUndefined();
  62. }
  63. JSC::JSValue JSMessagePort::postMessage(JSC::ExecState* exec, const JSC::ArgList& args)
  64. {
  65. return handlePostMessage(exec, args, impl());
  66. }
  67. void fillMessagePortArray(JSC::ExecState* exec, JSC::JSValue value, MessagePortArray& portArray)
  68. {
  69. // Convert from the passed-in JS array-like object to a MessagePortArray.
  70. // Also validates the elements per sections 4.1.13 and 4.1.15 of the WebIDL spec and section 8.3.3 of the HTML5 spec.
  71. if (value.isUndefinedOrNull()) {
  72. portArray.resize(0);
  73. return;
  74. }
  75. // Validation of sequence types, per WebIDL spec 4.1.13.
  76. unsigned length;
  77. JSObject* object = toJSSequence(exec, value, length);
  78. if (exec->hadException())
  79. return;
  80. portArray.resize(length);
  81. for (unsigned i = 0 ; i < length; ++i) {
  82. JSValue value = object->get(exec, i);
  83. if (exec->hadException())
  84. return;
  85. // Validation of non-null objects, per HTML5 spec 8.3.3.
  86. if (value.isUndefinedOrNull()) {
  87. setDOMException(exec, INVALID_STATE_ERR);
  88. return;
  89. }
  90. // Validation of Objects implementing an interface, per WebIDL spec 4.1.15.
  91. RefPtr<MessagePort> port = toMessagePort(value);
  92. if (!port) {
  93. throwError(exec, TypeError);
  94. return;
  95. }
  96. portArray[i] = port.release();
  97. }
  98. }
  99. } // namespace WebCore