1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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/. */
6 
7 #include "nsAndroidNetworkLinkService.h"
8 #include "nsServiceManagerUtils.h"
9 
10 #include "nsIObserverService.h"
11 #include "mozilla/StaticPrefs_network.h"
12 #include "mozilla/Services.h"
13 #include "mozilla/Logging.h"
14 
15 #include "AndroidBridge.h"
16 #include "mozilla/java/GeckoAppShellWrappers.h"
17 
18 namespace java = mozilla::java;
19 
20 static mozilla::LazyLogModule gNotifyAddrLog("nsAndroidNetworkLinkService");
21 #define LOG(args) MOZ_LOG(gNotifyAddrLog, mozilla::LogLevel::Debug, args)
22 
NS_IMPL_ISUPPORTS(nsAndroidNetworkLinkService,nsINetworkLinkService,nsIObserver)23 NS_IMPL_ISUPPORTS(nsAndroidNetworkLinkService, nsINetworkLinkService,
24                   nsIObserver)
25 
26 nsAndroidNetworkLinkService::nsAndroidNetworkLinkService()
27     : mStatusIsKnown(false) {}
28 
Init()29 nsresult nsAndroidNetworkLinkService::Init() {
30   nsCOMPtr<nsIObserverService> observerService =
31       mozilla::services::GetObserverService();
32   if (!observerService) {
33     return NS_ERROR_FAILURE;
34   }
35 
36   nsresult rv;
37   rv = observerService->AddObserver(this, "xpcom-shutdown-threads", false);
38   NS_ENSURE_SUCCESS(rv, rv);
39 
40   mNetlinkSvc = new mozilla::net::NetlinkService();
41   rv = mNetlinkSvc->Init(this);
42   if (NS_FAILED(rv)) {
43     mNetlinkSvc = nullptr;
44     LOG(("Cannot initialize NetlinkService [rv=0x%08" PRIx32 "]",
45          static_cast<uint32_t>(rv)));
46     return rv;
47   }
48   NS_ENSURE_SUCCESS(rv, rv);
49 
50   return NS_OK;
51 }
52 
Shutdown()53 nsresult nsAndroidNetworkLinkService::Shutdown() {
54   // remove xpcom shutdown observer
55   nsCOMPtr<nsIObserverService> observerService =
56       mozilla::services::GetObserverService();
57   if (observerService)
58     observerService->RemoveObserver(this, "xpcom-shutdown-threads");
59 
60   if (mNetlinkSvc) {
61     mNetlinkSvc->Shutdown();
62     mNetlinkSvc = nullptr;
63   }
64 
65   return NS_OK;
66 }
67 
68 NS_IMETHODIMP
Observe(nsISupports * subject,const char * topic,const char16_t * data)69 nsAndroidNetworkLinkService::Observe(nsISupports* subject, const char* topic,
70                                      const char16_t* data) {
71   if (!strcmp("xpcom-shutdown-threads", topic)) {
72     Shutdown();
73   }
74 
75   return NS_OK;
76 }
77 
78 NS_IMETHODIMP
GetIsLinkUp(bool * aIsUp)79 nsAndroidNetworkLinkService::GetIsLinkUp(bool* aIsUp) {
80   if (mNetlinkSvc && mStatusIsKnown) {
81     mNetlinkSvc->GetIsLinkUp(aIsUp);
82     return NS_OK;
83   }
84 
85   if (!mozilla::AndroidBridge::Bridge()) {
86     // Fail soft here and assume a connection exists
87     NS_WARNING("GetIsLinkUp is not supported without a bridge connection");
88     *aIsUp = true;
89     return NS_OK;
90   }
91 
92   *aIsUp = java::GeckoAppShell::IsNetworkLinkUp();
93   return NS_OK;
94 }
95 
96 NS_IMETHODIMP
GetLinkStatusKnown(bool * aIsKnown)97 nsAndroidNetworkLinkService::GetLinkStatusKnown(bool* aIsKnown) {
98   if (mStatusIsKnown) {
99     *aIsKnown = true;
100     return NS_OK;
101   }
102 
103   NS_ENSURE_TRUE(mozilla::AndroidBridge::Bridge(), NS_ERROR_NOT_IMPLEMENTED);
104 
105   *aIsKnown = java::GeckoAppShell::IsNetworkLinkKnown();
106   return NS_OK;
107 }
108 
109 NS_IMETHODIMP
GetLinkType(uint32_t * aLinkType)110 nsAndroidNetworkLinkService::GetLinkType(uint32_t* aLinkType) {
111   NS_ENSURE_ARG_POINTER(aLinkType);
112 
113   if (!mozilla::AndroidBridge::Bridge()) {
114     // Fail soft here and assume a connection exists
115     NS_WARNING("GetLinkType is not supported without a bridge connection");
116     *aLinkType = nsINetworkLinkService::LINK_TYPE_UNKNOWN;
117     return NS_OK;
118   }
119 
120   *aLinkType = java::GeckoAppShell::GetNetworkLinkType();
121   return NS_OK;
122 }
123 
124 NS_IMETHODIMP
GetNetworkID(nsACString & aNetworkID)125 nsAndroidNetworkLinkService::GetNetworkID(nsACString& aNetworkID) {
126   if (!mNetlinkSvc) {
127     return NS_ERROR_NOT_AVAILABLE;
128   }
129 
130   mNetlinkSvc->GetNetworkID(aNetworkID);
131   return NS_OK;
132 }
133 
134 NS_IMETHODIMP
GetDnsSuffixList(nsTArray<nsCString> & aDnsSuffixList)135 nsAndroidNetworkLinkService::GetDnsSuffixList(
136     nsTArray<nsCString>& aDnsSuffixList) {
137   aDnsSuffixList.Clear();
138   if (!mozilla::AndroidBridge::Bridge()) {
139     NS_WARNING("GetDnsSuffixList is not supported without a bridge connection");
140     return NS_ERROR_NOT_AVAILABLE;
141   }
142 
143   auto suffixList = java::GeckoAppShell::GetDNSDomains();
144   if (!suffixList) {
145     return NS_OK;
146   }
147 
148   nsAutoCString list(suffixList->ToCString());
149   for (const nsACString& suffix : list.Split(',')) {
150     aDnsSuffixList.AppendElement(suffix);
151   }
152   return NS_OK;
153 }
154 
155 NS_IMETHODIMP
GetPlatformDNSIndications(uint32_t * aPlatformDNSIndications)156 nsAndroidNetworkLinkService::GetPlatformDNSIndications(
157     uint32_t* aPlatformDNSIndications) {
158   return NS_ERROR_NOT_IMPLEMENTED;
159 }
160 
OnNetworkChanged()161 void nsAndroidNetworkLinkService::OnNetworkChanged() {
162   if (mozilla::StaticPrefs::network_notify_changed()) {
163     RefPtr<nsAndroidNetworkLinkService> self = this;
164     NS_DispatchToMainThread(NS_NewRunnableFunction(
165         "nsAndroidNetworkLinkService::OnNetworkChanged", [self]() {
166           self->NotifyObservers(NS_NETWORK_LINK_TOPIC,
167                                 NS_NETWORK_LINK_DATA_CHANGED);
168         }));
169   }
170 }
171 
OnNetworkIDChanged()172 void nsAndroidNetworkLinkService::OnNetworkIDChanged() {
173   RefPtr<nsAndroidNetworkLinkService> self = this;
174   NS_DispatchToMainThread(NS_NewRunnableFunction(
175       "nsAndroidNetworkLinkService::OnNetworkIDChanged", [self]() {
176         self->NotifyObservers(NS_NETWORK_ID_CHANGED_TOPIC, nullptr);
177       }));
178 }
179 
OnLinkUp()180 void nsAndroidNetworkLinkService::OnLinkUp() {
181   RefPtr<nsAndroidNetworkLinkService> self = this;
182   NS_DispatchToMainThread(
183       NS_NewRunnableFunction("nsAndroidNetworkLinkService::OnLinkUp", [self]() {
184         self->NotifyObservers(NS_NETWORK_LINK_TOPIC, NS_NETWORK_LINK_DATA_UP);
185       }));
186 }
187 
OnLinkDown()188 void nsAndroidNetworkLinkService::OnLinkDown() {
189   RefPtr<nsAndroidNetworkLinkService> self = this;
190   NS_DispatchToMainThread(NS_NewRunnableFunction(
191       "nsAndroidNetworkLinkService::OnLinkDown", [self]() {
192         self->NotifyObservers(NS_NETWORK_LINK_TOPIC, NS_NETWORK_LINK_DATA_DOWN);
193       }));
194 }
195 
OnLinkStatusKnown()196 void nsAndroidNetworkLinkService::OnLinkStatusKnown() { mStatusIsKnown = true; }
197 
OnDnsSuffixListUpdated()198 void nsAndroidNetworkLinkService::OnDnsSuffixListUpdated() {
199   RefPtr<nsAndroidNetworkLinkService> self = this;
200   NS_DispatchToMainThread(NS_NewRunnableFunction(
201       "nsAndroidNetworkLinkService::OnDnsSuffixListUpdated", [self]() {
202         self->NotifyObservers(NS_DNS_SUFFIX_LIST_UPDATED_TOPIC, nullptr);
203       }));
204 }
205 
206 /* Sends the given event. Assumes aTopic/aData never goes out of scope (static
207  * strings are ideal).
208  */
NotifyObservers(const char * aTopic,const char * aData)209 void nsAndroidNetworkLinkService::NotifyObservers(const char* aTopic,
210                                                   const char* aData) {
211   MOZ_ASSERT(NS_IsMainThread());
212 
213   LOG(("nsAndroidNetworkLinkService::NotifyObservers: topic:%s data:%s\n",
214        aTopic, aData ? aData : ""));
215 
216   nsCOMPtr<nsIObserverService> observerService =
217       mozilla::services::GetObserverService();
218 
219   if (observerService) {
220     observerService->NotifyObservers(
221         static_cast<nsINetworkLinkService*>(this), aTopic,
222         aData ? NS_ConvertASCIItoUTF16(aData).get() : nullptr);
223   }
224 }
225