1 // Copyright 2019 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 #ifndef SERVICES_DEVICE_GEOLOCATION_WIN_LOCATION_PROVIDER_WINRT_H_
6 #define SERVICES_DEVICE_GEOLOCATION_WIN_LOCATION_PROVIDER_WINRT_H_
7 
8 #include <windows.devices.geolocation.h>
9 #include <wrl/client.h>
10 
11 #include "base/threading/thread_checker.h"
12 #include "services/device/public/cpp/geolocation/location_provider.h"
13 #include "services/device/public/mojom/geoposition.mojom.h"
14 
15 namespace device {
16 
17 // Location provider for Windows 8/10 using the WinRT platform apis
18 class LocationProviderWinrt : public LocationProvider {
19  public:
20   LocationProviderWinrt();
21   ~LocationProviderWinrt() override;
22 
23   // LocationProvider implementation.
24   void SetUpdateCallback(
25       const LocationProviderUpdateCallback& callback) override;
26   void StartProvider(bool high_accuracy) override;
27   void StopProvider() override;
28   const mojom::Geoposition& GetPosition() override;
29   void OnPermissionGranted() override;
30 
31  protected:
32   virtual HRESULT GetGeolocator(
33       ABI::Windows::Devices::Geolocation::IGeolocator** geo_locator);
34 
35   bool permission_granted_ = false;
36   bool enable_high_accuracy_ = false;
37   base::Optional<EventRegistrationToken> position_changed_token_;
38   base::Optional<EventRegistrationToken> status_changed_token_;
39 
40  private:
41   void HandleErrorCondition(mojom::Geoposition::ErrorCode position_error_code,
42                             const std::string& position_error_message);
43   void RegisterCallbacks();
44   void UnregisterCallbacks();
45   void OnPositionChanged(
46       ABI::Windows::Devices::Geolocation::IGeolocator* geo_locator,
47       ABI::Windows::Devices::Geolocation::IPositionChangedEventArgs*
48           position_update);
49   void OnStatusChanged(
50       ABI::Windows::Devices::Geolocation::IGeolocator* geo_locator,
51       ABI::Windows::Devices::Geolocation::IStatusChangedEventArgs*
52           status_update);
53   void PopulateLocationData(
54       ABI::Windows::Devices::Geolocation::IGeoposition* geoposition,
55       mojom::Geoposition* location_data);
56 
57   mojom::Geoposition last_position_;
58   LocationProviderUpdateCallback location_update_callback_;
59   Microsoft::WRL::ComPtr<ABI::Windows::Devices::Geolocation::IGeolocator>
60       geo_locator_;
61   bool position_received_ = false;
62   ABI::Windows::Devices::Geolocation::PositionStatus position_status_;
63   base::TimeTicks position_callback_initialized_time_;
64   THREAD_CHECKER(thread_checker_);
65   base::WeakPtrFactory<LocationProviderWinrt> weak_ptr_factory_{this};
66 
67   DISALLOW_COPY_AND_ASSIGN(LocationProviderWinrt);
68 };
69 
70 }  // namespace device
71 
72 #endif  // SERVICES_DEVICE_GEOLOCATION_WIN_LOCATION_PROVIDER_WINRT_H_
73