geolocation_context.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/device/geolocation/geolocation_context.h"
  5. #include <utility>
  6. #include "base/memory/ptr_util.h"
  7. #include "mojo/public/cpp/bindings/self_owned_receiver.h"
  8. #include "services/device/geolocation/geolocation_impl.h"
  9. namespace device {
  10. GeolocationContext::GeolocationContext() = default;
  11. GeolocationContext::~GeolocationContext() = default;
  12. // static
  13. void GeolocationContext::Create(
  14. mojo::PendingReceiver<mojom::GeolocationContext> receiver) {
  15. mojo::MakeSelfOwnedReceiver(std::make_unique<GeolocationContext>(),
  16. std::move(receiver));
  17. }
  18. void GeolocationContext::BindGeolocation(
  19. mojo::PendingReceiver<mojom::Geolocation> receiver,
  20. const GURL& requesting_origin) {
  21. GeolocationImpl* impl = new GeolocationImpl(std::move(receiver), this);
  22. impls_.push_back(base::WrapUnique<GeolocationImpl>(impl));
  23. if (geoposition_override_)
  24. impl->SetOverride(*geoposition_override_);
  25. else
  26. impl->StartListeningForUpdates();
  27. }
  28. void GeolocationContext::OnConnectionError(GeolocationImpl* impl) {
  29. auto it = std::find_if(impls_.begin(), impls_.end(),
  30. [impl](const std::unique_ptr<GeolocationImpl>& gi) {
  31. return impl == gi.get();
  32. });
  33. DCHECK(it != impls_.end());
  34. impls_.erase(it);
  35. }
  36. void GeolocationContext::SetOverride(mojom::GeopositionPtr geoposition) {
  37. geoposition_override_ = std::move(geoposition);
  38. for (auto& impl : impls_) {
  39. impl->SetOverride(*geoposition_override_);
  40. }
  41. }
  42. void GeolocationContext::ClearOverride() {
  43. geoposition_override_.reset();
  44. for (auto& impl : impls_) {
  45. impl->ClearOverride();
  46. }
  47. }
  48. } // namespace device