The 'global isinf/isnan' compiler quirk required when using clang with libstdc++
[WebKit.git] / Source / WebCore / bindings / v8 / custom / V8GeolocationCustom.cpp
1 /*
2  * Copyright 2009, The Android Open Source Project
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  *  * 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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "V8Geolocation.h"
28
29 #if ENABLE(GEOLOCATION)
30
31 #include "Frame.h"
32 #include "Geolocation.h"
33 #include "V8Binding.h"
34 #include "V8Callback.h"
35 #include "V8PositionCallback.h"
36 #include "V8PositionErrorCallback.h"
37 #include "V8Utilities.h"
38
39 using namespace std;
40 using namespace WTF;
41
42 namespace WebCore {
43
44 static PassRefPtr<PositionOptions> createPositionOptions(v8::Local<v8::Value> value, bool& succeeded)
45 {
46     succeeded = true;
47
48     // Create default options.
49     RefPtr<PositionOptions> options = PositionOptions::create();
50
51     // Argument is optional (hence undefined is allowed), and null is allowed.
52     if (isUndefinedOrNull(value)) {
53         // Use default options.
54         return options.release();
55     }
56
57     // Given the above test, this will always yield an object.
58     v8::Local<v8::Object> object = value->ToObject();
59
60     // For all three properties, we apply the following ...
61     // - If the getter or the property's valueOf method throws an exception, we
62     //   quit so as not to risk overwriting the exception.
63     // - If the value is absent or undefined, we don't override the default.
64     v8::Local<v8::Value> enableHighAccuracyValue = object->Get(v8::String::NewSymbol("enableHighAccuracy"));
65     if (enableHighAccuracyValue.IsEmpty()) {
66         succeeded = false;
67         return 0;
68     }
69     if (!enableHighAccuracyValue->IsUndefined()) {
70         v8::Local<v8::Boolean> enableHighAccuracyBoolean = enableHighAccuracyValue->ToBoolean();
71         if (enableHighAccuracyBoolean.IsEmpty()) {
72             succeeded = false;
73             return 0;
74         }
75         options->setEnableHighAccuracy(enableHighAccuracyBoolean->Value());
76     }
77
78     v8::Local<v8::Value> timeoutValue = object->Get(v8::String::NewSymbol("timeout"));
79     if (timeoutValue.IsEmpty()) {
80         succeeded = false;
81         return 0;
82     }
83     if (!timeoutValue->IsUndefined()) {
84         v8::Local<v8::Number> timeoutNumber = timeoutValue->ToNumber();
85         if (timeoutNumber.IsEmpty()) {
86             succeeded = false;
87             return 0;
88         }
89         double timeoutDouble = timeoutNumber->Value();
90         // If the value is positive infinity, there's nothing to do.
91         if (!(std::isinf(timeoutDouble) && timeoutDouble > 0)) {
92             v8::Local<v8::Int32> timeoutInt32 = timeoutValue->ToInt32();
93             if (timeoutInt32.IsEmpty()) {
94                 succeeded = false;
95                 return 0;
96             }
97             // Wrap to int32 and force non-negative to match behavior of window.setTimeout.
98             options->setTimeout(max(0, timeoutInt32->Value()));
99         }
100     }
101
102     v8::Local<v8::Value> maximumAgeValue = object->Get(v8::String::NewSymbol("maximumAge"));
103     if (maximumAgeValue.IsEmpty()) {
104         succeeded = false;
105         return 0;
106     }
107     if (!maximumAgeValue->IsUndefined()) {
108         v8::Local<v8::Number> maximumAgeNumber = maximumAgeValue->ToNumber();
109         if (maximumAgeNumber.IsEmpty()) {
110             succeeded = false;
111             return 0;
112         }
113         double maximumAgeDouble = maximumAgeNumber->Value();
114         if (std::isinf(maximumAgeDouble) && maximumAgeDouble > 0) {
115             // If the value is positive infinity, clear maximumAge.
116             options->clearMaximumAge();
117         } else {
118             v8::Local<v8::Int32> maximumAgeInt32 = maximumAgeValue->ToInt32();
119             if (maximumAgeInt32.IsEmpty()) {
120                 succeeded = false;
121                 return 0;
122             }
123             // Wrap to int32 and force non-negative to match behavior of window.setTimeout.
124             options->setMaximumAge(max(0, maximumAgeInt32->Value()));
125         }
126     }
127
128     return options.release();
129 }
130
131 v8::Handle<v8::Value> V8Geolocation::getCurrentPositionCallback(const v8::Arguments& args)
132 {
133     bool succeeded = false;
134
135     RefPtr<PositionCallback> positionCallback = createFunctionOnlyCallback<V8PositionCallback>(args[0], succeeded, args.GetIsolate());
136     if (!succeeded)
137         return v8::Undefined();
138     ASSERT(positionCallback);
139
140     // Argument is optional (hence undefined is allowed), and null is allowed.
141     RefPtr<PositionErrorCallback> positionErrorCallback = createFunctionOnlyCallback<V8PositionErrorCallback>(args[1], succeeded, args.GetIsolate(), CallbackAllowUndefined | CallbackAllowNull);
142     if (!succeeded)
143         return v8::Undefined();
144
145     RefPtr<PositionOptions> positionOptions = createPositionOptions(args[2], succeeded);
146     if (!succeeded)
147         return v8::Undefined();
148     ASSERT(positionOptions);
149
150     Geolocation* geolocation = V8Geolocation::toNative(args.Holder());
151     geolocation->getCurrentPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
152     return v8::Undefined();
153 }
154
155 v8::Handle<v8::Value> V8Geolocation::watchPositionCallback(const v8::Arguments& args)
156 {
157     bool succeeded = false;
158
159     RefPtr<PositionCallback> positionCallback = createFunctionOnlyCallback<V8PositionCallback>(args[0], succeeded, args.GetIsolate());
160     if (!succeeded)
161         return v8::Undefined();
162     ASSERT(positionCallback);
163
164     // Argument is optional (hence undefined is allowed), and null is allowed.
165     RefPtr<PositionErrorCallback> positionErrorCallback = createFunctionOnlyCallback<V8PositionErrorCallback>(args[1], succeeded, args.GetIsolate(), CallbackAllowUndefined | CallbackAllowNull);
166     if (!succeeded)
167         return v8::Undefined();
168
169     RefPtr<PositionOptions> positionOptions = createPositionOptions(args[2], succeeded);
170     if (!succeeded)
171         return v8::Undefined();
172     ASSERT(positionOptions);
173
174     Geolocation* geolocation = V8Geolocation::toNative(args.Holder());
175     int watchId = geolocation->watchPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
176     return v8::Number::New(watchId);
177 }
178
179 } // namespace WebCore
180
181 #endif // ENABLE(GEOLOCATION)