2012-10-19 20:39:44 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "include/cef_geolocation.h"
|
|
|
|
#include "libcef/browser/context.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
#include "libcef/common/time_util.h"
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2012-10-19 20:39:44 +02:00
|
|
|
#include "base/logging.h"
|
2016-08-31 13:25:56 +02:00
|
|
|
#include "device/geolocation/geolocation_provider.h"
|
|
|
|
#include "device/geolocation/geoposition.h"
|
2012-10-19 20:39:44 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
class CefLocationRequest
|
|
|
|
: public base::RefCountedThreadSafe<CefLocationRequest> {
|
2013-06-04 19:41:37 +02:00
|
|
|
public:
|
|
|
|
explicit CefLocationRequest(CefRefPtr<CefGetGeolocationCallback> callback)
|
|
|
|
: callback_(callback) {
|
2014-05-29 19:15:34 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
2013-06-04 19:41:37 +02:00
|
|
|
geo_callback_ = base::Bind(&CefLocationRequest::OnLocationUpdate, this);
|
2016-08-31 13:25:56 +02:00
|
|
|
device::GeolocationProvider* provider =
|
|
|
|
device::GeolocationProvider::GetInstance();
|
2014-05-29 19:15:34 +02:00
|
|
|
subscription_ = provider->AddLocationUpdateCallback(geo_callback_, true);
|
2013-06-04 19:41:37 +02:00
|
|
|
provider->UserDidOptIntoLocationServices();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-11-12 20:25:15 +01:00
|
|
|
friend class base::RefCountedThreadSafe<CefLocationRequest>;
|
|
|
|
|
|
|
|
~CefLocationRequest() {}
|
|
|
|
|
2016-08-31 13:25:56 +02:00
|
|
|
void OnLocationUpdate(const device::Geoposition& position) {
|
2014-05-29 19:15:34 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
2014-09-27 01:48:19 +02:00
|
|
|
if (callback_.get()) {
|
2014-05-29 19:15:34 +02:00
|
|
|
CefGeoposition cef_position;
|
|
|
|
SetPosition(position, cef_position);
|
|
|
|
callback_->OnLocationUpdate(cef_position);
|
|
|
|
callback_ = NULL;
|
2013-06-04 19:41:37 +02:00
|
|
|
}
|
2014-05-29 19:15:34 +02:00
|
|
|
subscription_.reset();
|
|
|
|
geo_callback_.Reset();
|
2012-10-19 20:39:44 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 13:25:56 +02:00
|
|
|
void SetPosition(const device::Geoposition& source, CefGeoposition& target) {
|
2013-06-04 19:41:37 +02:00
|
|
|
target.latitude = source.latitude;
|
|
|
|
target.longitude = source.longitude;
|
|
|
|
target.altitude = source.altitude;
|
|
|
|
target.accuracy = source.accuracy;
|
|
|
|
target.altitude_accuracy = source.altitude_accuracy;
|
|
|
|
target.heading = source.heading;
|
|
|
|
target.speed = source.speed;
|
|
|
|
cef_time_from_basetime(source.timestamp, target.timestamp);
|
2012-10-19 20:39:44 +02:00
|
|
|
|
2013-06-04 19:41:37 +02:00
|
|
|
switch (source.error_code) {
|
2016-08-31 13:25:56 +02:00
|
|
|
case device::Geoposition::ERROR_CODE_NONE:
|
2013-06-04 19:41:37 +02:00
|
|
|
target.error_code = GEOPOSITON_ERROR_NONE;
|
|
|
|
break;
|
2016-08-31 13:25:56 +02:00
|
|
|
case device::Geoposition::ERROR_CODE_PERMISSION_DENIED:
|
2013-06-04 19:41:37 +02:00
|
|
|
target.error_code = GEOPOSITON_ERROR_PERMISSION_DENIED;
|
|
|
|
break;
|
2016-08-31 13:25:56 +02:00
|
|
|
case device::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE:
|
2013-06-04 19:41:37 +02:00
|
|
|
target.error_code = GEOPOSITON_ERROR_POSITION_UNAVAILABLE;
|
|
|
|
break;
|
2016-08-31 13:25:56 +02:00
|
|
|
case device::Geoposition::ERROR_CODE_TIMEOUT:
|
2013-06-04 19:41:37 +02:00
|
|
|
target.error_code = GEOPOSITON_ERROR_TIMEOUT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString(&target.error_message) = source.error_message;
|
2012-10-19 20:39:44 +02:00
|
|
|
}
|
2013-06-04 19:41:37 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefGetGeolocationCallback> callback_;
|
2016-08-31 13:25:56 +02:00
|
|
|
device::GeolocationProvider::LocationUpdateCallback geo_callback_;
|
|
|
|
std::unique_ptr<device::GeolocationProvider::Subscription> subscription_;
|
2013-06-04 19:41:37 +02:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefLocationRequest);
|
|
|
|
};
|
2012-10-19 20:39:44 +02:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool CefGetGeolocation(CefRefPtr<CefGetGeolocationCallback> callback) {
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED() << "context not valid";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!callback.get()) {
|
|
|
|
NOTREACHED() << "invalid parameter";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:15:34 +02:00
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
2016-08-31 13:25:56 +02:00
|
|
|
if (device::GeolocationProvider::GetInstance()) {
|
2013-06-04 19:41:37 +02:00
|
|
|
// Will be released after the callback executes.
|
|
|
|
new CefLocationRequest(callback);
|
2012-10-19 20:39:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
2014-05-29 19:15:34 +02:00
|
|
|
CEF_POST_TASK(CEF_UIT,
|
2017-05-17 11:29:28 +02:00
|
|
|
base::Bind(base::IgnoreResult(CefGetGeolocation), callback));
|
2012-10-19 20:39:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|