Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / 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 #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"
35
36 using namespace std;
37 using namespace WTF;
38
39 namespace WebCore {
40
41 static PassRefPtrWillBeRawPtr<PositionOptions> createPositionOptions(v8::Local<v8::Value> value, v8::Isolate* isolate, bool& succeeded)
42 {
43     succeeded = true;
44
45     // Create default options.
46     RefPtrWillBeRawPtr<PositionOptions> options = PositionOptions::create();
47
48     // Argument is optional (hence undefined is allowed), and null is allowed.
49     if (isUndefinedOrNull(value)) {
50         // Use default options.
51         return options.release();
52     }
53
54     // Given the above test, this will always yield an object.
55     v8::Local<v8::Object> object = value->ToObject();
56
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()) {
63         succeeded = false;
64         return 0;
65     }
66     if (!enableHighAccuracyValue->IsUndefined()) {
67         v8::Local<v8::Boolean> enableHighAccuracyBoolean = enableHighAccuracyValue->ToBoolean();
68         if (enableHighAccuracyBoolean.IsEmpty()) {
69             succeeded = false;
70             return 0;
71         }
72         options->setEnableHighAccuracy(enableHighAccuracyBoolean->Value());
73     }
74
75     v8::Local<v8::Value> timeoutValue = object->Get(v8AtomicString(isolate, "timeout"));
76     if (timeoutValue.IsEmpty()) {
77         succeeded = false;
78         return 0;
79     }
80     if (!timeoutValue->IsUndefined()) {
81         v8::Local<v8::Number> timeoutNumber = timeoutValue->ToNumber();
82         if (timeoutNumber.IsEmpty()) {
83             succeeded = false;
84             return 0;
85         }
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()) {
91                 succeeded = false;
92                 return 0;
93             }
94             // Wrap to int32 and force non-negative to match behavior of window.setTimeout.
95             options->setTimeout(max(0, timeoutInt32->Value()));
96         }
97     }
98
99     v8::Local<v8::Value> maximumAgeValue = object->Get(v8AtomicString(isolate, "maximumAge"));
100     if (maximumAgeValue.IsEmpty()) {
101         succeeded = false;
102         return 0;
103     }
104     if (!maximumAgeValue->IsUndefined()) {
105         v8::Local<v8::Number> maximumAgeNumber = maximumAgeValue->ToNumber();
106         if (maximumAgeNumber.IsEmpty()) {
107             succeeded = false;
108             return 0;
109         }
110         double maximumAgeDouble = maximumAgeNumber->Value();
111         if (std::isinf(maximumAgeDouble) && maximumAgeDouble > 0) {
112             // If the value is positive infinity, clear maximumAge.
113             options->clearMaximumAge();
114         } else {
115             v8::Local<v8::Int32> maximumAgeInt32 = maximumAgeValue->ToInt32();
116             if (maximumAgeInt32.IsEmpty()) {
117                 succeeded = false;
118                 return 0;
119             }
120             // Wrap to int32 and force non-negative to match behavior of window.setTimeout.
121             options->setMaximumAge(max(0, maximumAgeInt32->Value()));
122         }
123     }
124
125     return options.release();
126 }
127
128 void V8Geolocation::getCurrentPositionMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
129 {
130     bool succeeded = false;
131
132     OwnPtr<PositionCallback> positionCallback = createFunctionOnlyCallback<V8PositionCallback>(info[0], succeeded, info.GetIsolate());
133     if (!succeeded)
134         return;
135     ASSERT(positionCallback);
136
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);
139     if (!succeeded)
140         return;
141
142     RefPtrWillBeRawPtr<PositionOptions> positionOptions = createPositionOptions(info[2], info.GetIsolate(), succeeded);
143     if (!succeeded)
144         return;
145     ASSERT(positionOptions);
146
147     Geolocation* geolocation = V8Geolocation::toNative(info.Holder());
148     geolocation->getCurrentPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
149 }
150
151 void V8Geolocation::watchPositionMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
152 {
153     bool succeeded = false;
154
155     OwnPtr<PositionCallback> positionCallback = createFunctionOnlyCallback<V8PositionCallback>(info[0], succeeded, info.GetIsolate());
156     if (!succeeded)
157         return;
158     ASSERT(positionCallback);
159
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);
162     if (!succeeded)
163         return;
164
165     RefPtrWillBeRawPtr<PositionOptions> positionOptions = createPositionOptions(info[2], info.GetIsolate(), succeeded);
166     if (!succeeded)
167         return;
168     ASSERT(positionOptions);
169
170     Geolocation* geolocation = V8Geolocation::toNative(info.Holder());
171     int watchId = geolocation->watchPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
172     v8SetReturnValue(info, watchId);
173 }
174
175 } // namespace WebCore