1 /*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32
33 #include "Geolocation.h"
34 #include "V8Binding.h"
35 #include "V8CustomBinding.h"
36 #include "V8CustomPositionCallback.h"
37 #include "V8CustomPositionErrorCallback.h"
38 #include "V8Proxy.h"
39
40
41 using namespace std;
42 using namespace WTF;
43
44 namespace WebCore {
45
46 static const char* typeMismatchError = "TYPE_MISMATCH_ERR: DOM Exception 17";
47
throwTypeMismatchException()48 static void throwTypeMismatchException()
49 {
50 V8Proxy::throwError(V8Proxy::GeneralError, typeMismatchError);
51 }
52
createPositionCallback(v8::Local<v8::Value> value,bool & succeeded)53 static PassRefPtr<PositionCallback> createPositionCallback(v8::Local<v8::Value> value, bool& succeeded)
54 {
55 succeeded = true;
56
57 // The spec specifies 'FunctionOnly' for this object.
58 if (!value->IsFunction()) {
59 succeeded = false;
60 throwTypeMismatchException();
61 return 0;
62 }
63
64 Frame* frame = V8Proxy::retrieveFrameForCurrentContext();
65 return V8CustomPositionCallback::create(value, frame);
66 }
67
createPositionErrorCallback(v8::Local<v8::Value> value,bool & succeeded)68 static PassRefPtr<PositionErrorCallback> createPositionErrorCallback(v8::Local<v8::Value> value, bool& succeeded)
69 {
70 succeeded = true;
71
72 // Argument is optional (hence undefined is allowed), and null is allowed.
73 if (isUndefinedOrNull(value))
74 return 0;
75
76 // The spec specifies 'FunctionOnly' for this object.
77 if (!value->IsFunction()) {
78 succeeded = false;
79 throwTypeMismatchException();
80 return 0;
81 }
82
83 Frame* frame = V8Proxy::retrieveFrameForCurrentContext();
84 return V8CustomPositionErrorCallback::create(value, frame);
85 }
86
createPositionOptions(v8::Local<v8::Value> value,bool & succeeded)87 static PassRefPtr<PositionOptions> createPositionOptions(v8::Local<v8::Value> value, bool& succeeded)
88 {
89 succeeded = true;
90
91 // Create default options.
92 RefPtr<PositionOptions> options = PositionOptions::create();
93
94 // Argument is optional (hence undefined is allowed), and null is allowed.
95 if (isUndefinedOrNull(value)) {
96 // Use default options.
97 return options.release();
98 }
99
100 // Given the above test, this will always yield an object.
101 v8::Local<v8::Object> object = value->ToObject();
102
103 // For all three properties, we apply the following ...
104 // - If the getter or the property's valueOf method throws an exception, we
105 // quit so as not to risk overwriting the exception.
106 // - If the value is absent or undefined, we don't override the default.
107 v8::Local<v8::Value> enableHighAccuracyValue = object->Get(v8::String::New("enableHighAccuracy"));
108 if (enableHighAccuracyValue.IsEmpty()) {
109 succeeded = false;
110 return 0;
111 }
112 if (!enableHighAccuracyValue->IsUndefined()) {
113 v8::Local<v8::Boolean> enableHighAccuracyBoolean = enableHighAccuracyValue->ToBoolean();
114 if (enableHighAccuracyBoolean.IsEmpty()) {
115 succeeded = false;
116 return 0;
117 }
118 options->setEnableHighAccuracy(enableHighAccuracyBoolean->Value());
119 }
120
121 v8::Local<v8::Value> timeoutValue = object->Get(v8::String::New("timeout"));
122 if (timeoutValue.IsEmpty()) {
123 succeeded = false;
124 return 0;
125 }
126 if (!timeoutValue->IsUndefined()) {
127 v8::Local<v8::Number> timeoutNumber = timeoutValue->ToNumber();
128 if (timeoutNumber.IsEmpty()) {
129 succeeded = false;
130 return 0;
131 }
132 double timeoutDouble = timeoutNumber->Value();
133 // V8 does not export a public symbol for infinity, so we must use a
134 // platform type. On Android, it seems that V8 uses 0xf70f000000000000,
135 // which is the standard way to represent infinity in a double. However,
136 // numeric_limits<double>::infinity uses the system HUGE_VAL, which is
137 // different. Therefore we test using isinf() and check that the value
138 // is positive, which seems to handle things correctly.
139 // If the value is infinity, there's nothing to do.
140 if (!(isinf(timeoutDouble) && timeoutDouble > 0)) {
141 v8::Local<v8::Int32> timeoutInt32 = timeoutValue->ToInt32();
142 if (timeoutInt32.IsEmpty()) {
143 succeeded = false;
144 return 0;
145 }
146 // Wrap to int32 and force non-negative to match behavior of window.setTimeout.
147 options->setTimeout(max(0, timeoutInt32->Value()));
148 }
149 }
150
151 v8::Local<v8::Value> maximumAgeValue = object->Get(v8::String::New("maximumAge"));
152 if (maximumAgeValue.IsEmpty()) {
153 succeeded = false;
154 return 0;
155 }
156 if (!maximumAgeValue->IsUndefined()) {
157 v8::Local<v8::Number> maximumAgeNumber = maximumAgeValue->ToNumber();
158 if (maximumAgeNumber.IsEmpty()) {
159 succeeded = false;
160 return 0;
161 }
162 double maximumAgeDouble = maximumAgeNumber->Value();
163 if (isinf(maximumAgeDouble) && maximumAgeDouble > 0) {
164 // If the value is infinity, clear maximumAge.
165 options->clearMaximumAge();
166 } else {
167 v8::Local<v8::Int32> maximumAgeInt32 = maximumAgeValue->ToInt32();
168 if (maximumAgeInt32.IsEmpty()) {
169 succeeded = false;
170 return 0;
171 }
172 // Wrap to int32 and force non-negative to match behavior of window.setTimeout.
173 options->setMaximumAge(max(0, maximumAgeInt32->Value()));
174 }
175 }
176
177 return options.release();
178 }
179
CALLBACK_FUNC_DECL(GeolocationGetCurrentPosition)180 CALLBACK_FUNC_DECL(GeolocationGetCurrentPosition)
181 {
182 INC_STATS("DOM.Geolocation.getCurrentPosition()");
183
184 bool succeeded = false;
185
186 RefPtr<PositionCallback> positionCallback = createPositionCallback(args[0], succeeded);
187 if (!succeeded)
188 return v8::Undefined();
189 ASSERT(positionCallback);
190
191 RefPtr<PositionErrorCallback> positionErrorCallback = createPositionErrorCallback(args[1], succeeded);
192 if (!succeeded)
193 return v8::Undefined();
194
195 RefPtr<PositionOptions> positionOptions = createPositionOptions(args[2], succeeded);
196 if (!succeeded)
197 return v8::Undefined();
198 ASSERT(positionOptions);
199
200 Geolocation* geolocation = V8DOMWrapper::convertToNativeObject<Geolocation>(V8ClassIndex::GEOLOCATION, args.Holder());
201 geolocation->getCurrentPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
202 return v8::Undefined();
203 }
204
CALLBACK_FUNC_DECL(GeolocationWatchPosition)205 CALLBACK_FUNC_DECL(GeolocationWatchPosition)
206 {
207 INC_STATS("DOM.Geolocation.watchPosition()");
208
209 bool succeeded = false;
210
211 RefPtr<PositionCallback> positionCallback = createPositionCallback(args[0], succeeded);
212 if (!succeeded)
213 return v8::Undefined();
214 ASSERT(positionCallback);
215
216 RefPtr<PositionErrorCallback> positionErrorCallback = createPositionErrorCallback(args[1], succeeded);
217 if (!succeeded)
218 return v8::Undefined();
219
220 RefPtr<PositionOptions> positionOptions = createPositionOptions(args[2], succeeded);
221 if (!succeeded)
222 return v8::Undefined();
223 ASSERT(positionOptions);
224
225 Geolocation* geolocation = V8DOMWrapper::convertToNativeObject<Geolocation>(V8ClassIndex::GEOLOCATION, args.Holder());
226 int watchId = geolocation->watchPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
227 return v8::Number::New(watchId);
228 }
229
230 } // namespace WebCore
231