1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "MLSFallback.h"
8 #include "GeolocationPosition.h"
9 #include "nsComponentManagerUtils.h"
10 #include "nsIGeolocationProvider.h"
11 #include "nsServiceManagerUtils.h"
13 NS_IMPL_ISUPPORTS(MLSFallback
, nsITimerCallback
, nsINamed
)
15 MLSFallback::MLSFallback(uint32_t delay
) : mDelayMs(delay
) {}
17 MLSFallback::~MLSFallback() = default;
19 nsresult
MLSFallback::Startup(nsIGeolocationUpdate
* aWatcher
) {
20 if (mHandoffTimer
|| mMLSFallbackProvider
) {
24 mUpdateWatcher
= aWatcher
;
26 // No need to schedule a timer callback if there is no startup delay.
28 return CreateMLSFallbackProvider();
31 return NS_NewTimerWithCallback(getter_AddRefs(mHandoffTimer
), this, mDelayMs
,
32 nsITimer::TYPE_ONE_SHOT
);
35 nsresult
MLSFallback::Shutdown() {
36 mUpdateWatcher
= nullptr;
39 mHandoffTimer
->Cancel();
40 mHandoffTimer
= nullptr;
44 if (mMLSFallbackProvider
) {
45 rv
= mMLSFallbackProvider
->Shutdown();
46 mMLSFallbackProvider
= nullptr;
52 MLSFallback::Notify(nsITimer
* aTimer
) { return CreateMLSFallbackProvider(); }
55 MLSFallback::GetName(nsACString
& aName
) {
56 aName
.AssignLiteral("MLSFallback");
60 nsresult
MLSFallback::CreateMLSFallbackProvider() {
61 if (mMLSFallbackProvider
|| !mUpdateWatcher
) {
66 mMLSFallbackProvider
=
67 do_CreateInstance("@mozilla.org/geolocation/mls-provider;1", &rv
);
68 NS_ENSURE_SUCCESS(rv
, rv
);
70 if (mMLSFallbackProvider
) {
71 rv
= mMLSFallbackProvider
->Startup();
72 if (NS_SUCCEEDED(rv
)) {
73 mMLSFallbackProvider
->Watch(mUpdateWatcher
);
76 mUpdateWatcher
= nullptr;