Bug 1806483 - Enable TSAN cppunittests by default. r=jmaher
[gecko.git] / dom / geolocation / MLSFallback.cpp
blob82398a4436f497151092e37a72a9390c565efdb8
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) {
21 return NS_OK;
24 mUpdateWatcher = aWatcher;
26 // No need to schedule a timer callback if there is no startup delay.
27 if (mDelayMs == 0) {
28 return CreateMLSFallbackProvider();
31 return NS_NewTimerWithCallback(getter_AddRefs(mHandoffTimer), this, mDelayMs,
32 nsITimer::TYPE_ONE_SHOT);
35 nsresult MLSFallback::Shutdown() {
36 mUpdateWatcher = nullptr;
38 if (mHandoffTimer) {
39 mHandoffTimer->Cancel();
40 mHandoffTimer = nullptr;
43 nsresult rv = NS_OK;
44 if (mMLSFallbackProvider) {
45 rv = mMLSFallbackProvider->Shutdown();
46 mMLSFallbackProvider = nullptr;
48 return rv;
51 NS_IMETHODIMP
52 MLSFallback::Notify(nsITimer* aTimer) { return CreateMLSFallbackProvider(); }
54 NS_IMETHODIMP
55 MLSFallback::GetName(nsACString& aName) {
56 aName.AssignLiteral("MLSFallback");
57 return NS_OK;
60 nsresult MLSFallback::CreateMLSFallbackProvider() {
61 if (mMLSFallbackProvider || !mUpdateWatcher) {
62 return NS_OK;
65 nsresult rv;
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;
77 return rv;