diff --git a/dom/geolocation/nsGeolocation.cpp b/dom/geolocation/nsGeolocation.cpp index 7326d36b73e8..32c4ea1c4b90 100644 --- a/dom/geolocation/nsGeolocation.cpp +++ b/dom/geolocation/nsGeolocation.cpp @@ -9,6 +9,7 @@ #include "mozilla/ClearOnShutdown.h" #include "mozilla/CycleCollectedJSContext.h" // for nsAutoMicroTask #include "mozilla/dom/ContentChild.h" +#include "mozilla/dom/FeaturePolicyUtils.h" #include "mozilla/dom/PermissionMessageUtils.h" #include "mozilla/dom/PositionError.h" #include "mozilla/dom/PositionErrorBinding.h" @@ -1161,6 +1162,23 @@ Geolocation::ShouldBlockInsecureRequests() const return false; } +bool +Geolocation::FeaturePolicyBlocked() const +{ + nsCOMPtr win = do_QueryReferent(mOwner); + if (!win) { + return true; + } + + nsCOMPtr doc = win->GetExtantDoc(); + if (!doc) { + return false; + } + + return FeaturePolicyUtils::IsFeatureAllowed(doc, + NS_LITERAL_STRING("geolocation")); +} + bool Geolocation::ClearPendingRequest(nsGeolocationRequest* aRequest) { @@ -1221,7 +1239,8 @@ Geolocation::GetCurrentPosition(GeoPositionCallback callback, std::move(options), static_cast(mProtocolType), target, false, EventStateManager::IsHandlingUserInput()); - if (!sGeoEnabled || ShouldBlockInsecureRequests()) { + if (!sGeoEnabled || ShouldBlockInsecureRequests() || + !FeaturePolicyBlocked()) { nsCOMPtr ev = new RequestAllowEvent(false, request); target->Dispatch(ev.forget()); return NS_OK; @@ -1304,7 +1323,8 @@ Geolocation::WatchPosition(GeoPositionCallback aCallback, static_cast(mProtocolType), target, true, EventStateManager::IsHandlingUserInput(), watchId); - if (!sGeoEnabled || ShouldBlockInsecureRequests()) { + if (!sGeoEnabled || ShouldBlockInsecureRequests() || + !FeaturePolicyBlocked()) { nsCOMPtr ev = new RequestAllowEvent(false, request); target->Dispatch(ev.forget()); return watchId; diff --git a/dom/geolocation/nsGeolocation.h b/dom/geolocation/nsGeolocation.h index 46eb1730b17e..c28e7789766c 100644 --- a/dom/geolocation/nsGeolocation.h +++ b/dom/geolocation/nsGeolocation.h @@ -207,6 +207,10 @@ private: // within a context that is not secure. bool ShouldBlockInsecureRequests() const; + // Return whather the Feature 'geolocation' is blocked by FeaturePolicy + // directive. + bool FeaturePolicyBlocked() const; + // Two callback arrays. The first |mPendingCallbacks| holds objects for only // one callback and then they are released/removed from the array. The second // |mWatchingCallbacks| holds objects until the object is explictly removed or diff --git a/dom/security/featurepolicy/FeaturePolicyUtils.cpp b/dom/security/featurepolicy/FeaturePolicyUtils.cpp index 6756513960f4..1407cb3f19c5 100644 --- a/dom/security/featurepolicy/FeaturePolicyUtils.cpp +++ b/dom/security/featurepolicy/FeaturePolicyUtils.cpp @@ -31,7 +31,6 @@ static FeatureMap sSupportedFeatures[] = { { "camera", FeatureMap::eAll }, { "encrypted-media", FeatureMap::eAll }, { "fullscreen", FeatureMap::eAll }, - // TODO: not supported yet!!! { "geolocation", FeatureMap::eAll }, // TODO: not supported yet!!! { "microphone", FeatureMap::eAll }, diff --git a/dom/tests/mochitest/geolocation/file_featurePolicy.html b/dom/tests/mochitest/geolocation/file_featurePolicy.html new file mode 100644 index 000000000000..babf7853d35c --- /dev/null +++ b/dom/tests/mochitest/geolocation/file_featurePolicy.html @@ -0,0 +1,47 @@ + + + + + + + + + + diff --git a/dom/tests/mochitest/geolocation/mochitest.ini b/dom/tests/mochitest/geolocation/mochitest.ini index 082cf9e2e518..5e7908311ab2 100644 --- a/dom/tests/mochitest/geolocation/mochitest.ini +++ b/dom/tests/mochitest/geolocation/mochitest.ini @@ -29,6 +29,8 @@ support-files = test_geolocation_is_undefined_when_pref_is_off_iframe.html [test_timerRestartWatch.html] [test_windowClose.html] [test_worseAccuracyDoesNotBlockCallback.html] +[test_featurePolicy.html] +support-files = file_featurePolicy.html # This test REQUIRES to run on HTTP (_NOT_ HTTPS). [test_geoWatchPositionBlockedInInsecureContext.html] diff --git a/dom/tests/mochitest/geolocation/test_featurePolicy.html b/dom/tests/mochitest/geolocation/test_featurePolicy.html new file mode 100644 index 000000000000..cb113c3edf90 --- /dev/null +++ b/dom/tests/mochitest/geolocation/test_featurePolicy.html @@ -0,0 +1,51 @@ + + + + Test for geolocation + featurePolicy + + + + + + +