Skip to content

Commit f9086e8

Browse files
Modernize Geolocation code
https://bugs.webkit.org/show_bug.cgi?id=178148 Reviewed by Ryosuke Niwa. Source/WebCore: Modernize Geolocation code: - Use std::optional<> instead of separate boolean members - Make GeolocationPosition a simple struct that can be passed via IPC - Replace WebGeolocationPosition::Data with GeolocationPosition - Move logic to construct a GeolocationPosition from a CLLocation on iOS in one place to avoid code duplication. * Modules/geolocation/Coordinates.cpp: (WebCore::Coordinates::Coordinates): * Modules/geolocation/Coordinates.h: (WebCore::Coordinates::create): (WebCore::Coordinates::isolatedCopy const): (WebCore::Coordinates::latitude const): (WebCore::Coordinates::longitude const): (WebCore::Coordinates::altitude const): (WebCore::Coordinates::accuracy const): (WebCore::Coordinates::altitudeAccuracy const): (WebCore::Coordinates::heading const): (WebCore::Coordinates::speed const): * Modules/geolocation/Geolocation.cpp: (WebCore::createGeoposition): (WebCore::Geolocation::lastPosition): * Modules/geolocation/GeolocationClient.h: * Modules/geolocation/GeolocationController.cpp: (WebCore::GeolocationController::positionChanged): (WebCore::GeolocationController::lastPosition): * Modules/geolocation/GeolocationController.h: * Modules/geolocation/GeolocationPosition.h: (WebCore::GeolocationPosition::GeolocationPosition): The default constructor is only needed by our IPC decoding code. (WebCore::GeolocationPosition::encode const): (WebCore::GeolocationPosition::decode): * Modules/geolocation/ios/GeolocationPositionIOS.mm: Copied from Source/WebCore/Modules/geolocation/Coordinates.cpp. (WebCore::GeolocationPosition::GeolocationPosition): * WebCore.xcodeproj/project.pbxproj: * platform/mock/GeolocationClientMock.cpp: (WebCore::GeolocationClientMock::lastPosition): (WebCore::GeolocationClientMock::controllerTimerFired): * platform/mock/GeolocationClientMock.h: Source/WebKit: * Shared/WebGeolocationPosition.cpp: (WebKit::WebGeolocationPosition::create): (WebKit::WebGeolocationPosition::~WebGeolocationPosition): * Shared/WebGeolocationPosition.h: (WebKit::WebGeolocationPosition::timestamp const): (WebKit::WebGeolocationPosition::latitude const): (WebKit::WebGeolocationPosition::longitude const): (WebKit::WebGeolocationPosition::accuracy const): (WebKit::WebGeolocationPosition::altitude const): (WebKit::WebGeolocationPosition::altitudeAccuracy const): (WebKit::WebGeolocationPosition::heading const): (WebKit::WebGeolocationPosition::speed const): (WebKit::WebGeolocationPosition::corePosition const): (WebKit::WebGeolocationPosition::WebGeolocationPosition): * UIProcess/API/C/WKGeolocationPosition.cpp: (WKGeolocationPositionCreate_b): * UIProcess/API/Cocoa/_WKGeolocationPosition.mm: (WebKit::wrapper): * UIProcess/API/glib/WebKitGeolocationProvider.cpp: (WebKit::WebKitGeolocationProvider::notifyPositionChanged): * UIProcess/WebGeolocationManagerProxy.cpp: (WebKit::WebGeolocationManagerProxy::providerDidChangePosition): * UIProcess/ios/WKGeolocationProviderIOS.mm: (-[WKLegacyCoreLocationProvider positionChanged:]): * WebProcess/Geolocation/WebGeolocationManager.cpp: (WebKit::WebGeolocationManager::didChangePosition): * WebProcess/Geolocation/WebGeolocationManager.h: * WebProcess/Geolocation/WebGeolocationManager.messages.in: * WebProcess/WebCoreSupport/WebGeolocationClient.cpp: (WebKit::WebGeolocationClient::lastPosition): * WebProcess/WebCoreSupport/WebGeolocationClient.h: Source/WebKitLegacy/ios: * Misc/WebGeolocationCoreLocationProvider.h: * Misc/WebGeolocationCoreLocationProvider.mm: (-[WebGeolocationCoreLocationProvider sendLocation:]): * Misc/WebGeolocationProviderIOS.mm: (-[_WebCoreLocationUpdateThreadingProxy positionChanged:]): Source/WebKitLegacy/mac: * WebCoreSupport/WebGeolocationClient.h: * WebCoreSupport/WebGeolocationClient.mm: (WebGeolocationClient::lastPosition): * WebView/WebGeolocationPosition.mm: (-[WebGeolocationPositionInternal initWithCoreGeolocationPosition:]): (core): (-[WebGeolocationPosition initWithTimestamp:latitude:longitude:accuracy:]): (-[WebGeolocationPosition initWithGeolocationPosition:]): * WebView/WebGeolocationPositionInternal.h: Source/WebKitLegacy/win: * WebCoreSupport/WebGeolocationClient.cpp: (WebGeolocationClient::lastPosition): * WebCoreSupport/WebGeolocationClient.h: * WebGeolocationPosition.cpp: (WebGeolocationPosition::initWithTimestamp): (core): * WebGeolocationPosition.h: (WebGeolocationPosition::impl const): Tools: * DumpRenderTree/mac/TestRunnerMac.mm: (TestRunner::setMockGeolocationPosition): git-svn-id: http://svn.webkit.org/repository/webkit/trunk@223192 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 6fd8714 commit f9086e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+413
-317
lines changed

Source/WebCore/ChangeLog

+48
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,51 @@
1+
2017-10-11 Chris Dumez <[email protected]>
2+
3+
Modernize Geolocation code
4+
https://bugs.webkit.org/show_bug.cgi?id=178148
5+
6+
Reviewed by Ryosuke Niwa.
7+
8+
Modernize Geolocation code:
9+
- Use std::optional<> instead of separate boolean members
10+
- Make GeolocationPosition a simple struct that can be passed via IPC
11+
- Replace WebGeolocationPosition::Data with GeolocationPosition
12+
- Move logic to construct a GeolocationPosition from a CLLocation on iOS
13+
in one place to avoid code duplication.
14+
15+
* Modules/geolocation/Coordinates.cpp:
16+
(WebCore::Coordinates::Coordinates):
17+
* Modules/geolocation/Coordinates.h:
18+
(WebCore::Coordinates::create):
19+
(WebCore::Coordinates::isolatedCopy const):
20+
(WebCore::Coordinates::latitude const):
21+
(WebCore::Coordinates::longitude const):
22+
(WebCore::Coordinates::altitude const):
23+
(WebCore::Coordinates::accuracy const):
24+
(WebCore::Coordinates::altitudeAccuracy const):
25+
(WebCore::Coordinates::heading const):
26+
(WebCore::Coordinates::speed const):
27+
* Modules/geolocation/Geolocation.cpp:
28+
(WebCore::createGeoposition):
29+
(WebCore::Geolocation::lastPosition):
30+
* Modules/geolocation/GeolocationClient.h:
31+
* Modules/geolocation/GeolocationController.cpp:
32+
(WebCore::GeolocationController::positionChanged):
33+
(WebCore::GeolocationController::lastPosition):
34+
* Modules/geolocation/GeolocationController.h:
35+
* Modules/geolocation/GeolocationPosition.h:
36+
(WebCore::GeolocationPosition::GeolocationPosition):
37+
The default constructor is only needed by our IPC decoding code.
38+
39+
(WebCore::GeolocationPosition::encode const):
40+
(WebCore::GeolocationPosition::decode):
41+
* Modules/geolocation/ios/GeolocationPositionIOS.mm: Copied from Source/WebCore/Modules/geolocation/Coordinates.cpp.
42+
(WebCore::GeolocationPosition::GeolocationPosition):
43+
* WebCore.xcodeproj/project.pbxproj:
44+
* platform/mock/GeolocationClientMock.cpp:
45+
(WebCore::GeolocationClientMock::lastPosition):
46+
(WebCore::GeolocationClientMock::controllerTimerFired):
47+
* platform/mock/GeolocationClientMock.h:
48+
149
2017-10-11 Brady Eidson <[email protected]>
250

351
Add a SW context process (where SW scripts will actually execute).

Source/WebCore/Modules/geolocation/Coordinates.cpp

+3-25
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,10 @@
2828

2929
namespace WebCore {
3030

31-
std::optional<double> Coordinates::altitude() const
31+
Coordinates::Coordinates(GeolocationPosition&& position)
32+
: m_position(WTFMove(position))
3233
{
33-
if (!m_canProvideAltitude)
34-
return std::nullopt;
35-
return m_altitude;
34+
ASSERT(m_position.isValid());
3635
}
3736

38-
std::optional<double> Coordinates::altitudeAccuracy() const
39-
{
40-
if (!m_canProvideAltitudeAccuracy)
41-
return std::nullopt;
42-
return m_altitudeAccuracy;
43-
}
44-
45-
std::optional<double> Coordinates::heading() const
46-
{
47-
if (!m_canProvideHeading)
48-
return std::nullopt;
49-
return m_heading;
50-
}
51-
52-
std::optional<double> Coordinates::speed() const
53-
{
54-
if (!m_canProvideSpeed)
55-
return std::nullopt;
56-
return m_speed;
57-
}
58-
5937
} // namespace WebCore
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2009 Apple Inc. All Rights Reserved.
2+
* Copyright (C) 2009-2017 Apple Inc. All Rights Reserved.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -25,6 +25,7 @@
2525

2626
#pragma once
2727

28+
#include "GeolocationPosition.h"
2829
#include <wtf/Optional.h>
2930
#include <wtf/Ref.h>
3031
#include <wtf/RefCounted.h>
@@ -33,49 +34,28 @@ namespace WebCore {
3334

3435
class Coordinates : public RefCounted<Coordinates> {
3536
public:
36-
static Ref<Coordinates> create(double latitude, double longitude, bool providesAltitude, double altitude, double accuracy, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed) { return adoptRef(*new Coordinates(latitude, longitude, providesAltitude, altitude, accuracy, providesAltitudeAccuracy, altitudeAccuracy, providesHeading, heading, providesSpeed, speed)); }
37+
static Ref<Coordinates> create(GeolocationPosition&& position)
38+
{
39+
return adoptRef(*new Coordinates(WTFMove(position)));
40+
}
3741

3842
Ref<Coordinates> isolatedCopy() const
3943
{
40-
return Coordinates::create(m_latitude, m_longitude, m_canProvideAltitude, m_altitude, m_accuracy, m_canProvideAltitudeAccuracy, m_altitudeAccuracy, m_canProvideHeading, m_heading, m_canProvideSpeed, m_speed);
44+
return Coordinates::create( GeolocationPosition { m_position });
4145
}
4246

43-
double latitude() const { return m_latitude; }
44-
double longitude() const { return m_longitude; }
45-
std::optional<double> altitude() const;
46-
double accuracy() const { return m_accuracy; }
47-
std::optional<double> altitudeAccuracy() const;
48-
std::optional<double> heading() const;
49-
std::optional<double> speed() const;
47+
double latitude() const { return m_position.latitude; }
48+
double longitude() const { return m_position.longitude; }
49+
std::optional<double> altitude() const { return m_position.altitude; }
50+
double accuracy() const { return m_position.accuracy; }
51+
std::optional<double> altitudeAccuracy() const { return m_position.altitudeAccuracy; }
52+
std::optional<double> heading() const { return m_position.heading; }
53+
std::optional<double> speed() const { return m_position.speed; }
5054

5155
private:
52-
Coordinates(double latitude, double longitude, bool providesAltitude, double altitude, double accuracy, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed)
53-
: m_latitude(latitude)
54-
, m_longitude(longitude)
55-
, m_altitude(altitude)
56-
, m_accuracy(accuracy)
57-
, m_altitudeAccuracy(altitudeAccuracy)
58-
, m_heading(heading)
59-
, m_speed(speed)
60-
, m_canProvideAltitude(providesAltitude)
61-
, m_canProvideAltitudeAccuracy(providesAltitudeAccuracy)
62-
, m_canProvideHeading(providesHeading)
63-
, m_canProvideSpeed(providesSpeed)
64-
{
65-
}
56+
explicit Coordinates(GeolocationPosition&&);
6657

67-
double m_latitude;
68-
double m_longitude;
69-
double m_altitude;
70-
double m_accuracy;
71-
double m_altitudeAccuracy;
72-
double m_heading;
73-
double m_speed;
74-
75-
bool m_canProvideAltitude;
76-
bool m_canProvideAltitudeAccuracy;
77-
bool m_canProvideHeading;
78-
bool m_canProvideSpeed;
58+
GeolocationPosition m_position;
7959
};
8060

8161
} // namespace WebCore

Source/WebCore/Modules/geolocation/Geolocation.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,13 @@ static const char failedToStartServiceErrorMessage[] = "Failed to start Geolocat
5353
static const char framelessDocumentErrorMessage[] = "Geolocation cannot be used in frameless documents";
5454
static const char originCannotRequestGeolocationErrorMessage[] = "Origin does not have permission to use Geolocation service";
5555

56-
static RefPtr<Geoposition> createGeoposition(GeolocationPosition* position)
56+
static RefPtr<Geoposition> createGeoposition(std::optional<GeolocationPosition>&& position)
5757
{
5858
if (!position)
5959
return nullptr;
6060

61-
auto coordinates = Coordinates::create(position->latitude(), position->longitude(), position->canProvideAltitude(), position->altitude(),
62-
position->accuracy(), position->canProvideAltitudeAccuracy(), position->altitudeAccuracy(),
63-
position->canProvideHeading(), position->heading(), position->canProvideSpeed(), position->speed());
64-
65-
return Geoposition::create(WTFMove(coordinates), convertSecondsToDOMTimeStamp(position->timestamp()));
61+
DOMTimeStamp timestamp = convertSecondsToDOMTimeStamp(position->timestamp);
62+
return Geoposition::create(Coordinates::create(WTFMove(position.value())), timestamp);
6663
}
6764

6865
static Ref<PositionError> createPositionError(GeolocationError& error)
@@ -296,7 +293,7 @@ Geoposition* Geolocation::lastPosition()
296293
{
297294
Page* page = this->page();
298295
if (!page)
299-
return 0;
296+
return nullptr;
300297

301298
m_lastPosition = createGeoposition(GeolocationController::from(page)->lastPosition());
302299

Source/WebCore/Modules/geolocation/GeolocationClient.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#pragma once
2727

28+
#include <wtf/Optional.h>
29+
2830
namespace WebCore {
2931

3032
class Geolocation;
@@ -42,7 +44,7 @@ class GeolocationClient {
4244
// We should update WebKit to reflect this if and when the V2 specification
4345
// is published.
4446
virtual void setEnableHighAccuracy(bool) = 0;
45-
virtual GeolocationPosition* lastPosition() = 0;
47+
virtual std::optional<GeolocationPosition> lastPosition() = 0;
4648

4749
virtual void requestPermission(Geolocation&) = 0;
4850
virtual void cancelPermissionRequest(Geolocation&) = 0;

Source/WebCore/Modules/geolocation/GeolocationController.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void GeolocationController::cancelPermissionRequest(Geolocation& geolocation)
9999
m_client.cancelPermissionRequest(geolocation);
100100
}
101101

102-
void GeolocationController::positionChanged(GeolocationPosition* position)
102+
void GeolocationController::positionChanged(const std::optional<GeolocationPosition>& position)
103103
{
104104
m_lastPosition = position;
105105
Vector<Ref<Geolocation>> observersVector;
@@ -120,10 +120,10 @@ void GeolocationController::errorOccurred(GeolocationError& error)
120120
observer->setError(error);
121121
}
122122

123-
GeolocationPosition* GeolocationController::lastPosition()
123+
std::optional<GeolocationPosition> GeolocationController::lastPosition()
124124
{
125-
if (m_lastPosition.get())
126-
return m_lastPosition.get();
125+
if (m_lastPosition)
126+
return m_lastPosition.value();
127127

128128
return m_client.lastPosition();
129129
}

Source/WebCore/Modules/geolocation/GeolocationController.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ class GeolocationController : public Supplement<Page>, private ActivityStateChan
5353
void requestPermission(Geolocation&);
5454
void cancelPermissionRequest(Geolocation&);
5555

56-
WEBCORE_EXPORT void positionChanged(GeolocationPosition*);
56+
WEBCORE_EXPORT void positionChanged(const std::optional<GeolocationPosition>&);
5757
WEBCORE_EXPORT void errorOccurred(GeolocationError&);
5858

59-
GeolocationPosition* lastPosition();
59+
std::optional<GeolocationPosition> lastPosition();
6060

6161
GeolocationClient& client() { return m_client; }
6262

@@ -69,7 +69,7 @@ class GeolocationController : public Supplement<Page>, private ActivityStateChan
6969

7070
void activityStateDidChange(ActivityState::Flags oldActivityState, ActivityState::Flags newActivityState) override;
7171

72-
RefPtr<GeolocationPosition> m_lastPosition;
72+
std::optional<GeolocationPosition> m_lastPosition;
7373

7474
typedef HashSet<Ref<Geolocation>> ObserversSet;
7575
// All observers; both those requesting high accuracy and those not.

0 commit comments

Comments
 (0)