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 
5 #include "services/device/geolocation/geolocation_context.h"
6 
7 #include <utility>
8 
9 #include "base/memory/ptr_util.h"
10 #include "mojo/public/cpp/bindings/self_owned_receiver.h"
11 #include "services/device/geolocation/geolocation_impl.h"
12 
13 namespace device {
14 
15 GeolocationContext::GeolocationContext() = default;
16 
17 GeolocationContext::~GeolocationContext() = default;
18 
19 // static
Create(mojo::PendingReceiver<mojom::GeolocationContext> receiver)20 void GeolocationContext::Create(
21     mojo::PendingReceiver<mojom::GeolocationContext> receiver) {
22   mojo::MakeSelfOwnedReceiver(std::make_unique<GeolocationContext>(),
23                               std::move(receiver));
24 }
25 
BindGeolocation(mojo::PendingReceiver<mojom::Geolocation> receiver,const GURL & requesting_origin)26 void GeolocationContext::BindGeolocation(
27     mojo::PendingReceiver<mojom::Geolocation> receiver,
28     const GURL& requesting_origin) {
29   GeolocationImpl* impl = new GeolocationImpl(std::move(receiver), this);
30   impls_.push_back(base::WrapUnique<GeolocationImpl>(impl));
31   if (geoposition_override_)
32     impl->SetOverride(*geoposition_override_);
33   else
34     impl->StartListeningForUpdates();
35 }
36 
OnConnectionError(GeolocationImpl * impl)37 void GeolocationContext::OnConnectionError(GeolocationImpl* impl) {
38   auto it = std::find_if(impls_.begin(), impls_.end(),
39                          [impl](const std::unique_ptr<GeolocationImpl>& gi) {
40                            return impl == gi.get();
41                          });
42   DCHECK(it != impls_.end());
43   impls_.erase(it);
44 }
45 
SetOverride(mojom::GeopositionPtr geoposition)46 void GeolocationContext::SetOverride(mojom::GeopositionPtr geoposition) {
47   geoposition_override_ = std::move(geoposition);
48   for (auto& impl : impls_) {
49     impl->SetOverride(*geoposition_override_);
50   }
51 }
52 
ClearOverride()53 void GeolocationContext::ClearOverride() {
54   geoposition_override_.reset();
55   for (auto& impl : impls_) {
56     impl->ClearOverride();
57   }
58 }
59 
60 }  // namespace device
61