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