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 26#include "config.h" 27#include "JSGeolocation.h" 28 29#if ENABLE(GEOLOCATION) 30 31#include "CallbackFunction.h" 32#include "DOMWindow.h" 33#include "Geolocation.h" 34#include "JSDOMWindow.h" 35#include "JSDictionary.h" 36#include "JSPositionCallback.h" 37#include "JSPositionErrorCallback.h" 38#include "PositionOptions.h" 39 40using namespace JSC; 41 42namespace WebCore { 43 44// JSDictionary helper functions 45 46static void setEnableHighAccuracy(PositionOptions* options, const bool& enableHighAccuracy) 47{ 48 options->setEnableHighAccuracy(enableHighAccuracy); 49} 50 51static void setTimeout(PositionOptions* options, const double& timeout) 52{ 53 // If the value is positive infinity, there's nothing to do. 54 if (!(std::isinf(timeout) && (timeout > 0))) { 55 // Wrap to int32 and force non-negative to match behavior of window.setTimeout. 56 options->setTimeout(std::max<int>(0, timeout)); 57 } 58} 59 60static void setMaximumAge(PositionOptions* options, const double& maximumAge) 61{ 62 if (std::isinf(maximumAge) && (maximumAge > 0)) { 63 // If the value is positive infinity, clear maximumAge. 64 options->clearMaximumAge(); 65 } else { 66 // Wrap to int32 and force non-negative to match behavior of window.setTimeout. 67 options->setMaximumAge(std::max<int>(0, maximumAge)); 68 } 69} 70 71 72static PassRefPtr<PositionOptions> createPositionOptions(ExecState* exec, JSValue value) 73{ 74 // Create default options. 75 RefPtr<PositionOptions> options = PositionOptions::create(); 76 77 // Argument is optional (hence undefined is allowed), and null is allowed. 78 if (value.isUndefinedOrNull()) { 79 // Use default options. 80 return options.release(); 81 } 82 83 // Given the above test, this will always yield an object. 84 JSObject* object = value.toObject(exec); 85 86 // Create the dictionary wrapper from the initializer object. 87 JSDictionary dictionary(exec, object); 88 89 if (!dictionary.tryGetProperty("enableHighAccuracy", options.get(), setEnableHighAccuracy)) 90 return 0; 91 if (!dictionary.tryGetProperty("timeout", options.get(), setTimeout)) 92 return 0; 93 if (!dictionary.tryGetProperty("maximumAge", options.get(), setMaximumAge)) 94 return 0; 95 96 return options.release(); 97} 98 99JSValue JSGeolocation::getCurrentPosition(ExecState* exec) 100{ 101 // Arguments: PositionCallback, (optional)PositionErrorCallback, (optional)PositionOptions 102 103 RefPtr<PositionCallback> positionCallback = createFunctionOnlyCallback<JSPositionCallback>(exec, globalObject(), exec->argument(0)); 104 if (exec->hadException()) 105 return jsUndefined(); 106 ASSERT(positionCallback); 107 108 RefPtr<PositionErrorCallback> positionErrorCallback = createFunctionOnlyCallback<JSPositionErrorCallback>(exec, globalObject(), exec->argument(1), CallbackAllowUndefined | CallbackAllowNull); 109 if (exec->hadException()) 110 return jsUndefined(); 111 112 RefPtr<PositionOptions> positionOptions = createPositionOptions(exec, exec->argument(2)); 113 if (exec->hadException()) 114 return jsUndefined(); 115 ASSERT(positionOptions); 116 117 m_impl->getCurrentPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release()); 118 return jsUndefined(); 119} 120 121JSValue JSGeolocation::watchPosition(ExecState* exec) 122{ 123 // Arguments: PositionCallback, (optional)PositionErrorCallback, (optional)PositionOptions 124 125 RefPtr<PositionCallback> positionCallback = createFunctionOnlyCallback<JSPositionCallback>(exec, globalObject(), exec->argument(0)); 126 if (exec->hadException()) 127 return jsUndefined(); 128 ASSERT(positionCallback); 129 130 RefPtr<PositionErrorCallback> positionErrorCallback = createFunctionOnlyCallback<JSPositionErrorCallback>(exec, globalObject(), exec->argument(1), CallbackAllowUndefined | CallbackAllowNull); 131 if (exec->hadException()) 132 return jsUndefined(); 133 134 RefPtr<PositionOptions> positionOptions = createPositionOptions(exec, exec->argument(2)); 135 if (exec->hadException()) 136 return jsUndefined(); 137 ASSERT(positionOptions); 138 139 int watchID = m_impl->watchPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release()); 140 return jsNumber(watchID); 141} 142 143} // namespace WebCore 144 145#endif // ENABLE(GEOLOCATION) 146