Merge pull request #1282 from amaitland/feature/geocallback

Add GetGeolocationAsync Implementation
This commit is contained in:
Alex Maitland
2015-09-28 07:21:37 +10:00
7 changed files with 178 additions and 0 deletions
+16
View File
@@ -22,6 +22,7 @@
#include "SchemeHandlerFactoryWrapper.h"
#include "Internals/CefTaskScheduler.h"
#include "CookieAsyncWrapper.h"
#include "Internals/CefGetGeolocationCallbackWrapper.h"
using namespace System::Collections::Generic;
using namespace System::Linq;
@@ -431,5 +432,20 @@ namespace CefSharp
{
CefEnableHighDPISupport();
}
/// <summary>
/// Request a one-time geolocation update.
/// This function bypasses any user permission checks so should only be
/// used by code that is allowed to access location information.
/// </summary>
/// <return>Returns 'best available' location info or, if the location update failed, with error info.</return>
static Task<Geoposition^>^ GetGeolocationAsync()
{
auto callback = new CefGetGeolocationCallbackWrapper();
CefGetGeolocation(callback);
return callback->GetTask();
}
};
}
+1
View File
@@ -248,6 +248,7 @@
<ItemGroup>
<ClInclude Include="BrowserSettings.h" />
<ClInclude Include="Cef.h" />
<ClInclude Include="Internals\CefGetGeolocationCallbackWrapper.h" />
<ClInclude Include="Internals\CefAuthCallbackWrapper.h" />
<ClInclude Include="Internals\CefBeforeDownloadCallbackWrapper.h" />
<ClInclude Include="Internals\CefBrowserHostWrapper.h" />
@@ -238,6 +238,12 @@
<ClInclude Include="Internals\CefPdfPrintCallbackWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Internals\CefGetGeolocationCallbackWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Internals\CefPdfPrintCallbackWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Internals\CefSharpBrowserWrapper.h">
@@ -0,0 +1,69 @@
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#pragma once
#include "Stdafx.h"
#include "include\cef_geolocation.h"
using namespace CefSharp;
using namespace System::Threading::Tasks;
namespace CefSharp
{
namespace Internals
{
private class CefGetGeolocationCallbackWrapper : public CefGetGeolocationCallback
{
private:
gcroot<TaskCompletionSource<Geoposition^>^> _taskCompletionSource;
public:
CefGetGeolocationCallbackWrapper()
{
_taskCompletionSource = gcnew TaskCompletionSource<Geoposition^>();
//NOTE: Use fully qualified name as TaskExtensions is ambiguious
CefSharp::Internals::TaskExtensions::WithTimeout<Geoposition^>(_taskCompletionSource, TimeSpan::FromMilliseconds(2000));
}
virtual void OnLocationUpdate(const CefGeoposition& position) OVERRIDE
{
auto p = gcnew Geoposition();
p->Accuracy = position.accuracy;
p->Altitude = position.altitude;
p->AltitudeAccuracy = position.altitude_accuracy;
p->ErrorCode = (CefGeoPositionErrorCode)position.error_code;
p->ErrorMessage = StringUtils::ToClr(position.error_message);
p->Heading = position.heading;
p->Latitude = position.latitude;
p->Longitude = position.longitude;
p->Speed = position.speed;
p->Timestamp = ConvertCefTimeToDateTime(position.timestamp);
_taskCompletionSource->SetResult(p);
};
DateTime ConvertCefTimeToDateTime(CefTime time)
{
auto epoch = time.GetDoubleT();
if (epoch == 0)
{
return DateTime::MinValue;
}
return DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(epoch).ToLocalTime();
}
Task<Geoposition^>^ GetTask()
{
return _taskCompletionSource->Task;
}
IMPLEMENT_REFCOUNTING(CefGetGeolocationCallbackWrapper)
};
}
}
+17
View File
@@ -0,0 +1,17 @@
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
namespace CefSharp
{
/// <summary>
/// Geoposition error codes.
/// </summary>
public enum CefGeoPositionErrorCode
{
None = 0,
PermissionDenied,
PositionUnavailable,
Timeout
}
}
+2
View File
@@ -91,9 +91,11 @@
<Compile Include="CefDirtyRect.cs" />
<Compile Include="CefExtension.cs" />
<Compile Include="CefFileDialogMode.cs" />
<Compile Include="CefGeoPositionErrorCode.cs" />
<Compile Include="CefJsDialogType.cs" />
<Compile Include="CefMenuCommand.cs" />
<Compile Include="CefPdfPrintMarginType.cs" />
<Compile Include="Geoposition.cs" />
<Compile Include="PdfPrintSettings.cs" />
<Compile Include="ICookieManager.cs" />
<Compile Include="ILoadHandler.cs" />
+67
View File
@@ -0,0 +1,67 @@
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CefSharp
{
/// <summary>
/// Structure representing geoposition information. The properties of this
/// structure correspond to those of the JavaScript Position object although
/// their types may differ.
/// </summary>
public class Geoposition
{
/// <summary>
/// Latitude in decimal degrees north (WGS84 coordinate frame).
/// </summary>
public double Latitude { get; set; }
/// <summary>
/// Longitude in decimal degrees west (WGS84 coordinate frame).
/// </summary>
public double Longitude { get; set; }
/// <summary>
/// Altitude in meters (above WGS84 datum).
/// </summary>
public double Altitude { get; set; }
//Accuracy of horizontal position in meters.
public double Accuracy { get; set; }
/// <summary>
/// Accuracy of altitude in meters.
/// </summary>
public double AltitudeAccuracy { get; set; }
/// <summary>
/// Heading in decimal degrees clockwise from true north.
/// </summary>
public double Heading { get; set; }
/// <summary>
/// Horizontal component of device velocity in meters per second.
/// </summary>
public double Speed { get; set; }
/// <summary>
/// Time of position measurement in miliseconds since Epoch in UTC time. This
/// is taken from the host computer's system clock.
/// </summary>
public DateTime Timestamp { get; set; }
/// <summary>
/// Error code, see enum above.
/// </summary>
public CefGeoPositionErrorCode ErrorCode { get; set; }
/// <summary>
/// Human-readable error message.
/// </summary>
public string ErrorMessage { get; set; }
}
}