What's new?
|
Help
|
Directory
|
Sign in
gears
Improving Your Web Browser
Project Home
Wiki
Issues
Source
Checkout
|
Browse
|
Changes
|
Source Path:
svn
/
trunk
/
gears
/
geolocation
/
geolocation.cc
‹ r2433
r2489
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
// Copyright 2008, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// This file implements GearsGeolocation, the main class of the Gears
// Geolocation API.
#include "gears/geolocation/geolocation.h"
#include <math.h>
#include "gears/base/common/js_runner.h"
#include "gears/base/common/module_wrapper.h"
#include "gears/base/common/permissions_manager.h"
#include "gears/base/common/stopwatch.h" // For GetCurrentTimeMillis()
#include "gears/geolocation/location_provider_pool.h"
#include "gears/geolocation/device_data_provider.h"
#include "gears/geolocation/geolocation_db.h"
#include "third_party/googleurl/src/gurl.h"
// TODO(steveblock): Update default URL when finalized.
static const char16 *kDefaultLocationProviderUrl =
STRING16(L"http://www.google.com/");
// API options constants.
static const char16 *kEnableHighAccuracy = STRING16(L"enableHighAccuracy");
static const char16 *kRequestAddress = STRING16(L"requestAddress");
static const char16 *kAddressLanguage = STRING16(L"addressLanguage");
static const char16 *kGearsLocationProviderUrls =
STRING16(L"gearsLocationProviderUrls");
// Timing constants.
static const int64 kMinimumCallbackInterval = 1000; // 1 second.
static const int64 kMaximumPositionFixAge = 60 * 1000; // 1 minute.
// DB caching constants.
static const char16 *kLastPositionName = STRING16(L"LastPosition");
// MessageService constants.
static const char16 *kLocationAvailableObserverTopic
= STRING16(L"location available");
static const char16 *kCallbackRequiredObserverTopic
= STRING16(L"callback required");
// Data classes for use with MessageService.
class NotificationDataGeoBase : public NotificationData {
public:
NotificationDataGeoBase(GearsGeolocation *object_in)
: object(object_in) {}
virtual ~NotificationDataGeoBase() {}
friend class GearsGeolocation;
private:
// NotificationData implementation. These methods are not required.
virtual SerializableClassId GetSerializableClassId() const {
assert(false);
return SERIALIZABLE_NULL;
}
virtual bool Serialize(Serializer *out) const {
assert(false);
return false;
}
virtual bool Deserialize(Deserializer *in) {
assert(false);
return false;
}
GearsGeolocation *object;
DISALLOW_EVIL_CONSTRUCTORS(NotificationDataGeoBase);
};
class LocationAvailableNotificationData : public NotificationDataGeoBase {
public:
LocationAvailableNotificationData(GearsGeolocation *object_in,
LocationProviderBase *provider_in)
: NotificationDataGeoBase(object_in),
provider(provider_in) {}
virtual ~LocationAvailableNotificationData() {}
friend class GearsGeolocation;
private:
LocationProviderBase *provider;
DISALLOW_EVIL_CONSTRUCTORS(LocationAvailableNotificationData);
};
struct CallbackRequiredNotificationData : public NotificationDataGeoBase {
public:
CallbackRequiredNotificationData(
GearsGeolocation *object_in,
GearsGeolocation::FixRequestInfo *fix_info_in)
: NotificationDataGeoBase(object_in),
fix_info(fix_info_in) {}
virtual ~CallbackRequiredNotificationData() {}
friend class GearsGeolocation;
private:
GearsGeolocation::FixRequestInfo *fix_info;
DISALLOW_EVIL_CONSTRUCTORS(CallbackRequiredNotificationData);
};
// Helper function that checks if the caller had the required permissions
// to use this API. If the permissions are not set, it prompts the user.
// If the permissions cannot be acquired, it sets an exception and returns
// false. Else it returns true.
static bool AcquirePermissionForLocationData(ModuleImplBaseClass *geo_module,
JsCallContext *context);
// Local functions
// Gets the requested property only if it is specified. Returns true on success.
static bool GetPropertyIfSpecified(JsCallContext *context,
const JsObject &object,
const std::string16 &name,
JsScopedToken *token);
// Sets an object integer property if the input value is valid.
static bool SetObjectPropertyIfValidInt(const std::string16 &property_name,
int value,
JsObject *object);
// Sets an object string property if the input value is valid.
static bool SetObjectPropertyIfValidString(const std::string16 &property_name,
const std::string16 &value,
JsObject *object);
// Returns true if there has been movement from the old position to the new
// position.
static bool IsNewPositionMovement(const Position &old_position,
const Position &new_position);
// Returns true if the new position is more accurate than the old position.
static bool IsNewPositionMoreAccurate(const Position &old_position,
const Position &new_position);
// Returns true if the old position is out of date.
static bool IsNewPositionMoreTimely(const Position &old_position,
const Position &new_position);
DECLARE_GEARS_WRAPPER(GearsGeolocation);
template<>
void Dispatcher<GearsGeolocation>::Init() {
RegisterProperty("lastPosition", &GearsGeolocation::GetLastPosition, NULL);
RegisterMethod("getCurrentPosition", &GearsGeolocation::GetCurrentPosition);
RegisterMethod("watchPosition", &GearsGeolocation::WatchPosition);
RegisterMethod("clearWatch", &GearsGeolocation::ClearWatch);
RegisterMethod("getPermission", &GearsGeolocation::GetPermission);
}
GearsGeolocation::GearsGeolocation()
: ModuleImplBaseClass("GearsGeolocation"),
next_watch_id_(1) {
// Set up the thread message queue.
if (!ThreadMessageQueue::GetInstance()->InitThreadMessageQueue()) {
LOG(("Failed to set up thread message queue.\n"));
assert(false);
return;
}
MessageService::GetInstance()->AddObserver(this,
kLocationAvailableObserverTopic);
MessageService::GetInstance()->AddObserver(this,
kCallbackRequiredObserverTopic);
// Retrieve the cached last known position, if available.
GeolocationDB *db = GeolocationDB::GetDB();
if (db) {
db->RetrievePosition(kLastPositionName, &last_position_);
}
}
GearsGeolocation::~GearsGeolocation() {
// Cancel all pending requests by unregistering from our location providers.
for (ProviderMap::iterator iter = providers_.begin();
iter != providers_.end();
++iter) {
LocationProviderPool::GetInstance()->Unregister(iter->first, this);
}
MessageService::GetInstance()->RemoveObserver(
this,
kLocationAvailableObserverTopic);
MessageService::GetInstance()->RemoveObserver(
this,
kCallbackRequiredObserverTopic);
// Store the last known position.
if (last_position_.IsGoodFix()) {
GeolocationDB *db = GeolocationDB::GetDB();
if (db) {
db->StorePosition(kLastPositionName, last_position_);
}
}
}
// API Methods
void GearsGeolocation::GetLastPosition(JsCallContext *context) {
// Check permissions first.
if (!AcquirePermissionForLocationData(this, context)) return;
// If there's no good current position, we simply return null.
if (!last_position_.IsGoodFix()) {
return;
}
// Create the object for returning to JavaScript.
scoped_ptr<JsObject> return_object(GetJsRunner()->NewObject());
assert(return_object.get());
if (!ConvertPositionToJavaScriptObject(last_position_,
true, // Use address if present.
GetJsRunner(),
return_object.get())) {
LOG(("GearsGeolocation::GetLastPosition() : Failed to create position "
"object.\n"));
assert(false);
return;
}
context->SetReturnValue(JSPARAM_OBJECT, return_object.get());
}
void GearsGeolocation::GetCurrentPosition(JsCallContext *context) {
// Check permissions first.
if (!AcquirePermissionForLocationData(this, context)) return;
GetPositionFix(context, false);
}
void GearsGeolocation::WatchPosition(JsCallContext *context) {
// Check permissions first.
if (!AcquirePermissionForLocationData(this, context)) return;
GetPositionFix(context, true);
}
void GearsGeolocation::ClearWatch(JsCallContext *context) {
// Check permissions first.
if (!AcquirePermissionForLocationData(this, context)) return;
int id;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_INT, &id },
};
context->GetArguments(ARRAYSIZE(argv), argv);
if (context->is_exception_set()) {
return;
}
if (!CancelWatch(id)) {
context->SetException(STRING16(L"Unknown watch ID."));
}
}
void GearsGeolocation::GetPermission(JsCallContext *context) {
scoped_ptr<PermissionsDialog::CustomContent> custom_content(
PermissionsDialog::CreateCustomContent(context));
if (!custom_content.get()) { return; }
bool has_permission = GetPermissionsManager()->AcquirePermission(
PermissionsDB::PERMISSION_LOCATION_DATA,
custom_content.get());
context->SetReturnValue(JSPARAM_BOOL, &has_permission);
}
// LocationProviderBase::ListenerInterface implementation.
bool GearsGeolocation::LocationUpdateAvailable(LocationProviderBase *provider) {
assert(provider);
// We marshall this callback onto the JavaScript thread. This simplifies
// issuing new fix requests and calling back to JavaScript, which must be done
// from the JavaScript thread.
MessageService::GetInstance()->NotifyObservers(
kLocationAvailableObserverTopic,
new LocationAvailableNotificationData(this, provider));
return true;
}
// MessageObserverInterface implementation.
void GearsGeolocation::OnNotify(MessageService *service,
const char16 *topic,
const NotificationData *data) {
assert(data);
// Only respond to notifications made by this object.
const NotificationDataGeoBase *geolocation_data =
reinterpret_cast<const NotificationDataGeoBase*>(data);
if (this != geolocation_data->object) {
return;
}
if (char16_wmemcmp(kLocationAvailableObserverTopic,
topic,
char16_wcslen(topic)) == 0) {
const LocationAvailableNotificationData *location_available_data =
reinterpret_cast<const LocationAvailableNotificationData*>(data);
// Invoke the implementation.
LocationUpdateAvailableImpl(location_available_data->provider);
} else if (char16_wmemcmp(kCallbackRequiredObserverTopic,
topic,
char16_wcslen(topic)) == 0) {
const CallbackRequiredNotificationData *callback_required_data =
reinterpret_cast<const CallbackRequiredNotificationData*>(data);
// Delete this callback timer.
FixRequestInfo *fix_info = callback_required_data->fix_info;
assert(fix_info->callback_timer.get());
fix_info->callback_timer.reset();
MakeCallback(fix_info, last_position_);
}
}
// TimedCallback::ListenerInterface implementation.
void GearsGeolocation::OnTimeout(TimedCallback *caller, void *user_data) {
assert(user_data);
// Send a message to the JavaScriptThread to make the callback.
FixRequestInfo *fix_info = reinterpret_cast<FixRequestInfo*>(user_data);
MessageService::GetInstance()->NotifyObservers(
kCallbackRequiredObserverTopic,
new CallbackRequiredNotificationData(this, fix_info));
}
// Non-API methods
void GearsGeolocation::GetPositionFix(JsCallContext *context, bool repeats) {
// Get the arguments.
std::vector<std::string16> urls;
scoped_ptr<FixRequestInfo> info(new FixRequestInfo());
if (!ParseArguments(context, repeats, &urls, info.get())) {
assert(context->is_exception_set());
return;
}
// Add the providers. The lifetime of the providers is handled by the location
// provider pool, through Register and Unregister.
std::string16 host_name = EnvPageSecurityOrigin().host();
LocationProviderPool *pool = LocationProviderPool::GetInstance();
// Native providers
if (info->enable_high_accuracy) {
LocationProviderBase *gps_provider =
pool->Register(STRING16(L"GPS"),
host_name,
info->request_address,
info->address_language,
this);
if (gps_provider) {
info->providers.push_back(gps_provider);
}
}
// Network providers
for (int i = 0; i < static_cast<int>(urls.size()); ++i) {
// Check if the url is valid. If not, skip this URL. This also handles the
// case where the URL is 'GPS', which would confuse the location provider
// pool.
GURL url(urls[i]);
if (url.is_valid()) {
LocationProviderBase *network_provider =
pool->Register(urls[i],
host_name,
info->request_address,
info->address_language,
this);
if (network_provider) {
info->providers.push_back(network_provider);
}
}
}
// If this fix has no providers, throw an exception and quit.
if (info->providers.empty()) {
context->SetException(STRING16(L"Fix request has no location providers."));
return;
}
// If this is a non-repeating request, hint to all providers that new data is
// required ASAP.
if (!info->repeats) {
for (ProviderVector::iterator iter = info->providers.begin();
iter != info->providers.end();
++iter) {
(*iter)->UpdatePosition();
}
}
// Store and return the ID of this fix if it repeats.
if (info->repeats) {
context->SetReturnValue(JSPARAM_INT, &next_watch_id_);
watches_[next_watch_id_++] = info.get();
}
// Record this fix. This updates the map of providers and fix requests. The
// map takes ownership of the info structure.
MutexLock lock(&providers_mutex_);
RecordNewFixRequest(info.release());
}
bool GearsGeolocation::CancelWatch(const int &watch_id) {
FixRequestInfoMap::iterator watch_iter = watches_.find(watch_id);
if (watch_iter == watches_.end()) {
return false;
}
FixRequestInfo *info = const_cast<FixRequestInfo*>(watch_iter->second);
assert(info->repeats);
// Update our list of providers.
MutexLock providers_lock(&providers_mutex_);
RemoveFixRequest(info);
watches_.erase(watch_iter);
return true;
}
void GearsGeolocation::HandleRepeatingRequestUpdate(FixRequestInfo *fix_info) {
assert(fix_info->repeats);
// If there's already a timer active for the callback, there's nothing to do.
if (fix_info->callback_timer.get()) {
return;
}
// If the postion has changed significantly or the accuracy has improved, and
// the minimum time since our last callback to JavaScript has elapsed, we make
// a callback.
if (IsNewPositionMovement(fix_info->last_position, last_position_) ||
IsNewPositionMoreAccurate(fix_info->last_position, last_position_)) {
// The position has changed significantly. See if the minimum time interval
// since the last callback has expired.
int64 time_remaining =
kMinimumCallbackInterval -
(GetCurrentTimeMillis() - fix_info->last_callback_time);
if (time_remaining <= 0) {
if (!MakeCallback(fix_info, last_position_)) {
LOG(("GearsGeolocation::HandleRepeatingRequestUpdate() : JavaScript "
"callback failed.\n"));
assert(false);
}
} else {
// Start an asynchronous timer which will post a message back to this
// thread once the minimum time period has elapsed.
MakeFutureCallback(static_cast<int>(time_remaining), fix_info);
}
}
}
void GearsGeolocation::HandleSingleRequestUpdate(
LocationProviderBase *provider,
FixRequestInfo *fix_info,
const Position &position) {
assert(!fix_info->repeats);
assert(fix_info->last_callback_time == 0);
// Remove this provider from the this fix so that future to callbacks to this
// Geolocation object don't trigger handling for this fix.
RemoveProvider(provider, fix_info);
// We callback in two cases ...
// - This response gives a good position and we haven't yet called back
// - The fix has no remaining providers, so we'll never get a valid position
// We then cancel any pending requests and delete the fix request.
if (position.IsGoodFix() || fix_info->providers.empty()) {
if (!MakeCallback(fix_info, position)) {
LOG(("GearsGeolocation::HandleSingleRequestUpdate() : JavaScript "
"callback failed.\n"));
assert(false);
}
RemoveFixRequest(fix_info);
}
}
void GearsGeolocation::LocationUpdateAvailableImpl(
LocationProviderBase *provider) {
// Update the last known position, which is the best position estimate we
// currently have. This is shared between all repeating fix requests.
Position position;
provider->GetPosition(&position);
last_position_mutex_.Lock();
if (IsNewPositionMovement(last_position_, position) ||
IsNewPositionMoreAccurate(last_position_, position) ||
IsNewPositionMoreTimely(last_position_, position)) {
last_position_ = position;
}
last_position_mutex_.Unlock();
// Iterate over all non-repeating fix requests of which this provider is a
// part. We call back to Javascript if this is the first good position for
// that request.
MutexLock lock(&providers_mutex_);
ProviderMap::iterator provider_iter = providers_.find(provider);
// We may not have an entry in the map for this provider. This situation can
// occur when a provider calls back to this object, but the request is then
// cancelled before the call is marshalled to the JavaScript thread.
if (provider_iter != providers_.end()) {
// Take a copy of the vector of requests because the handlers below may
// remove items from the list, so ++iter will fail.
FixVector fix_requests = provider_iter->second;
for (FixVector::iterator request_iter = fix_requests.begin();
request_iter != fix_requests.end();
++request_iter) {
FixRequestInfo *fix_info = *request_iter;
// TODO(steveblock): Consider storing only non-repeating fix requests in
// the map.
if (!fix_info->repeats) {
HandleSingleRequestUpdate(provider, fix_info, position);
}
}
}
// Iterate over all repeating fix requests.
for (FixRequestInfoMap::const_iterator iter = watches_.begin();
iter != watches_.end();
iter++) {
FixRequestInfo *fix_info = const_cast<FixRequestInfo*>(iter->second);
HandleRepeatingRequestUpdate(fix_info);
}
}
bool GearsGeolocation::MakeCallback(FixRequestInfo *fix_info,
const Position &position) {
scoped_ptr<JsObject> callback_param(GetJsRunner()->NewObject());
assert(callback_param.get());
if (!ConvertPositionToJavaScriptObject(position,
fix_info->request_address,
GetJsRunner(),
callback_param.get())) {
LOG(("GearsGeolocation::MakeCallback() : Failed to create "
"position object.\n"));
assert(false);
return false;
}
JsParamToSend argv[] = { JSPARAM_OBJECT, callback_param.get() };
// InvokeCallback returns false if the callback enounters an error.
GetJsRunner()->InvokeCallback(fix_info->callback.get(),
ARRAYSIZE(argv), argv, NULL);
fix_info->last_position = position;
fix_info->last_callback_time = GetCurrentTimeMillis();
return true;
}
bool GearsGeolocation::ParseArguments(JsCallContext *context,
bool repeats,
std::vector<std::string16> *urls,
FixRequestInfo *info) {
assert(context);
assert(urls);
assert(info);
info->repeats = repeats;
// Note that GetArguments allocates a new JsRootedCallback.
JsRootedCallback *function = NULL;
JsObject options;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_FUNCTION, &function },
{ JSPARAM_OPTIONAL, JSPARAM_OBJECT, &options },
};
int num_arguments = context->GetArguments(ARRAYSIZE(argv), argv);
if (context->is_exception_set()) {
delete function;
return false;
}
assert(function);
info->callback.reset(function);
// Set default values for options.
info->enable_high_accuracy = false;
info->request_address = false;
urls->clear();
// We have to check that options is present because it's not valid to use an
// uninitialised JsObject.
if (num_arguments > 1) {
if (!ParseOptions(context, options, urls, info)) {
assert(context->is_exception_set());
return false;
}
} else {
// options is not specified, so use the default URL.
urls->push_back(kDefaultLocationProviderUrl);
}
return true;
}
bool GearsGeolocation::ParseOptions(JsCallContext *context,
const JsObject &options,
std::vector<std::string16> *urls,
FixRequestInfo *info) {
assert(context);
assert(urls);
assert(info);
JsScopedToken token;
if (GetPropertyIfSpecified(context, options, kEnableHighAccuracy, &token)) {
if (!JsTokenToBool_NoCoerce(token, context->js_context(),
&(info->enable_high_accuracy))) {
std::string16 error = STRING16(L"options.");
error += kEnableHighAccuracy;
error += STRING16(L" should be a boolean.");
context->SetException(error);
return false;
}
}
if (GetPropertyIfSpecified(context, options, kRequestAddress, &token)) {
if (!JsTokenToBool_NoCoerce(token, context->js_context(),
&(info->request_address))) {
std::string16 error = STRING16(L"options.");
error += kRequestAddress;
error += STRING16(L" should be a boolean.");
context->SetException(error);
return false;
}
}
if (GetPropertyIfSpecified(context, options, kAddressLanguage, &token)) {
if (!JsTokenToString_NoCoerce(token, context->js_context(),
&(info->address_language))) {
std::string16 error = STRING16(L"options.");
error += kAddressLanguage;
error += STRING16(L" should be a string.");
context->SetException(error);
return false;
}
}
if (GetPropertyIfSpecified(context, options, kGearsLocationProviderUrls,
&token)) {
if (!ParseLocationProviderUrls(context, token, urls)) {
std::string16 error = STRING16(L"options.");
error += kGearsLocationProviderUrls;
error += STRING16(L" should be null or an array of strings.");
context->SetException(error);
return false;
}
} else {
// gearsLocationProviderUrls is not specified, so use the default URL.
urls->push_back(kDefaultLocationProviderUrl);
}
return true;
}
bool GearsGeolocation::ParseLocationProviderUrls(
JsCallContext *context,
const JsScopedToken &token,
std::vector<std::string16> *urls) {
assert(context);
assert(urls);
if (JsTokenGetType(token, context->js_context()) == JSPARAM_ARRAY) {
// gearsLocationProviderUrls is an array.
JsArray js_array;
if (!js_array.SetArray(token, context->js_context())) {
LOG(("GearsGeolocation::ParseLocationProviderUrls() : Failed to set "
"array with gearsLocationProviderUrls."));
assert(false);
return false;
}
int length;
if (!js_array.GetLength(&length)) {
LOG(("GearsGeolocation::ParseLocationProviderUrls() : Failed to get "
"length of gearsLocationProviderUrls."));
assert(false);
return false;
}
for (int i = 0; i < length; ++i) {
JsScopedToken token;
if (!js_array.GetElement(i, &token)) {
LOG(("GearsGeolocation::ParseLocationProviderUrls() : Failed to get "
"element from gearsLocationProviderUrls."));
assert(false);
return false;
}
std::string16 url;
if (!JsTokenToString_NoCoerce(token, context->js_context(), &url)) {
return false;
}
urls->push_back(url);
}
} else if (JsTokenGetType(token, context->js_context()) != JSPARAM_NULL) {
// If gearsLocationProviderUrls is null, we do not use the default URL.
// If it's not an array and not null, this is an error.
return false;
}
return true;
}
bool GearsGeolocation::ConvertPositionToJavaScriptObject(
const Position &position,
bool use_address,
JsRunnerInterface *js_runner,
JsObject *js_object) {
assert(js_object);
assert(position.IsInitialized());
bool result = true;
if (!position.IsGoodFix()) {
// Position is not a good fix, copy only the error message.
assert(!position.error.empty());
result &= js_object->SetPropertyString(STRING16(L"errorMessage"),
position.error);
return result;
}
// latitude, longitude and date should always be valid.
result &= js_object->SetPropertyDouble(STRING16(L"latitude"),
position.latitude);
result &= js_object->SetPropertyDouble(STRING16(L"longitude"),
position.longitude);
scoped_ptr<JsObject> date_object(js_runner->NewDate(position.timestamp));
result &= NULL != date_object.get();
if (date_object.get()) {
result &= js_object->SetPropertyObject(STRING16(L"timestamp"),
date_object.get());
}
// Other properties may not be valid.
result &= SetObjectPropertyIfValidInt(STRING16(L"altitude"),
position.altitude,
js_object);
result &= SetObjectPropertyIfValidInt(STRING16(L"horizontalAccuracy"),
position.horizontal_accuracy,
js_object);
result &= SetObjectPropertyIfValidInt(STRING16(L"verticalAccuracy"),
position.vertical_accuracy,
js_object);
// Address
if (use_address) {
scoped_ptr<JsObject> address_object(js_runner->NewObject());
result &= NULL != address_object.get();
if (address_object.get()) {
result &= SetObjectPropertyIfValidString(STRING16(L"streetNumber"),
position.address.street_number,
address_object.get());
result &= SetObjectPropertyIfValidString(STRING16(L"street"),
position.address.street,
address_object.get());
result &= SetObjectPropertyIfValidString(STRING16(L"premises"),
position.address.premises,
address_object.get());
result &= SetObjectPropertyIfValidString(STRING16(L"city"),
position.address.city,
address_object.get());
result &= SetObjectPropertyIfValidString(STRING16(L"county"),
position.address.county,
address_object.get());
result &= SetObjectPropertyIfValidString(STRING16(L"region"),
position.address.region,
address_object.get());
result &= SetObjectPropertyIfValidString(STRING16(L"country"),
position.address.country,
address_object.get());
result &= SetObjectPropertyIfValidString(STRING16(L"countryCode"),
position.address.country_code,
address_object.get());
result &= SetObjectPropertyIfValidString(STRING16(L"postalCode"),
position.address.postal_code,
address_object.get());
// Only add the address object if it has some properties.
std::vector<std::string16> properties;
if (address_object.get()->GetPropertyNames(&properties) &&
!properties.empty()) {
result &= js_object->SetPropertyObject(STRING16(L"address"),
address_object.get());
}
}
}
return result;
}
void GearsGeolocation::RecordNewFixRequest(FixRequestInfo *fix_request) {
// For each location provider used by this request, update the provider's
// list of fix requests in the map.
ProviderVector *member_providers = &fix_request->providers;