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

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 180 lines · 112 code · 27 blank · 41 comment · 26 complexity · 23c52907f58464b4a07e465d1079c991 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 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 "JSGeolocation.h"
  27. #include "DOMWindow.h"
  28. #include "ExceptionCode.h"
  29. #include "Geolocation.h"
  30. #include "GeolocationService.h"
  31. #include "JSCustomPositionCallback.h"
  32. #include "JSCustomPositionErrorCallback.h"
  33. #include "JSDOMWindow.h"
  34. #include "PositionOptions.h"
  35. #include <runtime/InternalFunction.h>
  36. using namespace JSC;
  37. using namespace std;
  38. namespace WebCore {
  39. static PassRefPtr<PositionCallback> createPositionCallback(ExecState* exec, JSDOMGlobalObject* globalObject, JSValue value)
  40. {
  41. // The spec specifies 'FunctionOnly' for this object.
  42. if (!value.inherits(&InternalFunction::info)) {
  43. setDOMException(exec, TYPE_MISMATCH_ERR);
  44. return 0;
  45. }
  46. JSObject* object = asObject(value);
  47. return JSCustomPositionCallback::create(object, globalObject);
  48. }
  49. static PassRefPtr<PositionErrorCallback> createPositionErrorCallback(ExecState* exec, JSDOMGlobalObject* globalObject, JSValue value)
  50. {
  51. // Argument is optional (hence undefined is allowed), and null is allowed.
  52. if (value.isUndefinedOrNull())
  53. return 0;
  54. // The spec specifies 'FunctionOnly' for this object.
  55. if (!value.inherits(&InternalFunction::info)) {
  56. setDOMException(exec, TYPE_MISMATCH_ERR);
  57. return 0;
  58. }
  59. JSObject* object = asObject(value);
  60. return JSCustomPositionErrorCallback::create(object, globalObject);
  61. }
  62. static PassRefPtr<PositionOptions> createPositionOptions(ExecState* exec, JSValue value)
  63. {
  64. // Create default options.
  65. RefPtr<PositionOptions> options = PositionOptions::create();
  66. // Argument is optional (hence undefined is allowed), and null is allowed.
  67. if (value.isUndefinedOrNull()) {
  68. // Use default options.
  69. return options.release();
  70. }
  71. // Given the above test, this will always yield an object.
  72. JSObject* object = value.toObject(exec);
  73. // For all three properties, we apply the following ...
  74. // - If the getter or the property's valueOf method throws an exception, we
  75. // quit so as not to risk overwriting the exception.
  76. // - If the value is absent or undefined, we don't override the default.
  77. JSValue enableHighAccuracyValue = object->get(exec, Identifier(exec, "enableHighAccuracy"));
  78. if (exec->hadException())
  79. return 0;
  80. if (!enableHighAccuracyValue.isUndefined()) {
  81. options->setEnableHighAccuracy(enableHighAccuracyValue.toBoolean(exec));
  82. if (exec->hadException())
  83. return 0;
  84. }
  85. JSValue timeoutValue = object->get(exec, Identifier(exec, "timeout"));
  86. if (exec->hadException())
  87. return 0;
  88. if (!timeoutValue.isUndefined()) {
  89. double timeoutNumber = timeoutValue.toNumber(exec);
  90. if (exec->hadException())
  91. return 0;
  92. // If the value is positive infinity, there's nothing to do.
  93. if (!(isinf(timeoutNumber) && (timeoutNumber > 0))) {
  94. // Wrap to int32 and force non-negative to match behavior of window.setTimeout.
  95. options->setTimeout(max(0, timeoutValue.toInt32(exec)));
  96. if (exec->hadException())
  97. return 0;
  98. }
  99. }
  100. JSValue maximumAgeValue = object->get(exec, Identifier(exec, "maximumAge"));
  101. if (exec->hadException())
  102. return 0;
  103. if (!maximumAgeValue.isUndefined()) {
  104. double maximumAgeNumber = maximumAgeValue.toNumber(exec);
  105. if (exec->hadException())
  106. return 0;
  107. if (isinf(maximumAgeNumber) && (maximumAgeNumber > 0)) {
  108. // If the value is positive infinity, clear maximumAge.
  109. options->clearMaximumAge();
  110. } else {
  111. // Wrap to int32 and force non-negative to match behavior of window.setTimeout.
  112. options->setMaximumAge(max(0, maximumAgeValue.toInt32(exec)));
  113. if (exec->hadException())
  114. return 0;
  115. }
  116. }
  117. return options.release();
  118. }
  119. JSValue JSGeolocation::getCurrentPosition(ExecState* exec, const ArgList& args)
  120. {
  121. // Arguments: PositionCallback, (optional)PositionErrorCallback, (optional)PositionOptions
  122. RefPtr<PositionCallback> positionCallback = createPositionCallback(exec, static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject()), args.at(0));
  123. if (exec->hadException())
  124. return jsUndefined();
  125. ASSERT(positionCallback);
  126. RefPtr<PositionErrorCallback> positionErrorCallback = createPositionErrorCallback(exec, static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject()), args.at(1));
  127. if (exec->hadException())
  128. return jsUndefined();
  129. RefPtr<PositionOptions> positionOptions = createPositionOptions(exec, args.at(2));
  130. if (exec->hadException())
  131. return jsUndefined();
  132. ASSERT(positionOptions);
  133. m_impl->getCurrentPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
  134. return jsUndefined();
  135. }
  136. JSValue JSGeolocation::watchPosition(ExecState* exec, const ArgList& args)
  137. {
  138. // Arguments: PositionCallback, (optional)PositionErrorCallback, (optional)PositionOptions
  139. RefPtr<PositionCallback> positionCallback = createPositionCallback(exec, static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject()), args.at(0));
  140. if (exec->hadException())
  141. return jsUndefined();
  142. ASSERT(positionCallback);
  143. RefPtr<PositionErrorCallback> positionErrorCallback = createPositionErrorCallback(exec, static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject()), args.at(1));
  144. if (exec->hadException())
  145. return jsUndefined();
  146. RefPtr<PositionOptions> positionOptions = createPositionOptions(exec, args.at(2));
  147. if (exec->hadException())
  148. return jsUndefined();
  149. ASSERT(positionOptions);
  150. int watchID = m_impl->watchPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
  151. return jsNumber(exec, watchID);
  152. }
  153. } // namespace WebCore