2 * Copyright 2009, The Android Open Source Project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "V8Geolocation.h"
29 #include "V8PositionCallback.h"
30 #include "V8PositionErrorCallback.h"
31 #include "bindings/v8/V8Binding.h"
32 #include "bindings/v8/V8Callback.h"
33 #include "bindings/v8/V8Utilities.h"
34 #include "modules/geolocation/Geolocation.h"
41 static PassRefPtrWillBeRawPtr<PositionOptions> createPositionOptions(v8::Local<v8::Value> value, v8::Isolate* isolate, bool& succeeded)
45 // Create default options.
46 RefPtrWillBeRawPtr<PositionOptions> options = PositionOptions::create();
48 // Argument is optional (hence undefined is allowed), and null is allowed.
49 if (isUndefinedOrNull(value)) {
50 // Use default options.
51 return options.release();
54 // Given the above test, this will always yield an object.
55 v8::Local<v8::Object> object = value->ToObject();
57 // For all three properties, we apply the following ...
58 // - If the getter or the property's valueOf method throws an exception, we
59 // quit so as not to risk overwriting the exception.
60 // - If the value is absent or undefined, we don't override the default.
61 v8::Local<v8::Value> enableHighAccuracyValue = object->Get(v8AtomicString(isolate, "enableHighAccuracy"));
62 if (enableHighAccuracyValue.IsEmpty()) {
66 if (!enableHighAccuracyValue->IsUndefined()) {
67 v8::Local<v8::Boolean> enableHighAccuracyBoolean = enableHighAccuracyValue->ToBoolean();
68 if (enableHighAccuracyBoolean.IsEmpty()) {
72 options->setEnableHighAccuracy(enableHighAccuracyBoolean->Value());
75 v8::Local<v8::Value> timeoutValue = object->Get(v8AtomicString(isolate, "timeout"));
76 if (timeoutValue.IsEmpty()) {
80 if (!timeoutValue->IsUndefined()) {
81 v8::Local<v8::Number> timeoutNumber = timeoutValue->ToNumber();
82 if (timeoutNumber.IsEmpty()) {
86 double timeoutDouble = timeoutNumber->Value();
87 // If the value is positive infinity, there's nothing to do.
88 if (!(std::isinf(timeoutDouble) && timeoutDouble > 0)) {
89 v8::Local<v8::Int32> timeoutInt32 = timeoutValue->ToInt32();
90 if (timeoutInt32.IsEmpty()) {
94 // Wrap to int32 and force non-negative to match behavior of window.setTimeout.
95 options->setTimeout(max(0, timeoutInt32->Value()));
99 v8::Local<v8::Value> maximumAgeValue = object->Get(v8AtomicString(isolate, "maximumAge"));
100 if (maximumAgeValue.IsEmpty()) {
104 if (!maximumAgeValue->IsUndefined()) {
105 v8::Local<v8::Number> maximumAgeNumber = maximumAgeValue->ToNumber();
106 if (maximumAgeNumber.IsEmpty()) {
110 double maximumAgeDouble = maximumAgeNumber->Value();
111 if (std::isinf(maximumAgeDouble) && maximumAgeDouble > 0) {
112 // If the value is positive infinity, clear maximumAge.
113 options->clearMaximumAge();
115 v8::Local<v8::Int32> maximumAgeInt32 = maximumAgeValue->ToInt32();
116 if (maximumAgeInt32.IsEmpty()) {
120 // Wrap to int32 and force non-negative to match behavior of window.setTimeout.
121 options->setMaximumAge(max(0, maximumAgeInt32->Value()));
125 return options.release();
128 void V8Geolocation::getCurrentPositionMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
130 bool succeeded = false;
132 OwnPtr<PositionCallback> positionCallback = createFunctionOnlyCallback<V8PositionCallback>(info[0], succeeded, info.GetIsolate());
135 ASSERT(positionCallback);
137 // Argument is optional (hence undefined is allowed), and null is allowed.
138 OwnPtr<PositionErrorCallback> positionErrorCallback = createFunctionOnlyCallback<V8PositionErrorCallback>(info[1], succeeded, info.GetIsolate(), CallbackAllowUndefined | CallbackAllowNull);
142 RefPtrWillBeRawPtr<PositionOptions> positionOptions = createPositionOptions(info[2], info.GetIsolate(), succeeded);
145 ASSERT(positionOptions);
147 Geolocation* geolocation = V8Geolocation::toNative(info.Holder());
148 geolocation->getCurrentPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
151 void V8Geolocation::watchPositionMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
153 bool succeeded = false;
155 OwnPtr<PositionCallback> positionCallback = createFunctionOnlyCallback<V8PositionCallback>(info[0], succeeded, info.GetIsolate());
158 ASSERT(positionCallback);
160 // Argument is optional (hence undefined is allowed), and null is allowed.
161 OwnPtr<PositionErrorCallback> positionErrorCallback = createFunctionOnlyCallback<V8PositionErrorCallback>(info[1], succeeded, info.GetIsolate(), CallbackAllowUndefined | CallbackAllowNull);
165 RefPtrWillBeRawPtr<PositionOptions> positionOptions = createPositionOptions(info[2], info.GetIsolate(), succeeded);
168 ASSERT(positionOptions);
170 Geolocation* geolocation = V8Geolocation::toNative(info.Holder());
171 int watchId = geolocation->watchPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
172 v8SetReturnValue(info, watchId);
175 } // namespace WebCore