Bumping manifests a=b2g-bump
[gecko.git] / netwerk / wifi / nsWifiMonitorGonk.cpp
blob043df56bf9b75af887fc312270ef23620a958f6c
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsCOMPtr.h"
6 #include "nsComponentManagerUtils.h"
7 #include "nsServiceManagerUtils.h"
8 #include "nsThreadUtils.h"
9 #include "nsXPCOM.h"
10 #include "nsXPCOMCID.h"
11 #include "nsIObserver.h"
12 #include "nsIObserverService.h"
13 #include "nsWifiMonitor.h"
14 #include "nsWifiAccessPoint.h"
16 #include "nsServiceManagerUtils.h"
17 #include "nsComponentManagerUtils.h"
18 #include "mozilla/Services.h"
20 #include "nsIInterfaceRequestor.h"
21 #include "nsIInterfaceRequestorUtils.h"
23 using namespace mozilla;
25 #if defined(PR_LOGGING)
26 PRLogModuleInfo *gWifiMonitorLog;
27 #endif
29 NS_IMPL_ISUPPORTS(nsWifiMonitor,
30 nsIWifiMonitor,
31 nsIObserver,
32 nsIWifiScanResultsReady)
34 nsWifiMonitor::nsWifiMonitor()
36 #if defined(PR_LOGGING)
37 gWifiMonitorLog = PR_NewLogModule("WifiMonitor");
38 #endif
40 nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService();
41 if (obsSvc) {
42 obsSvc->AddObserver(this, "xpcom-shutdown", false);
44 LOG(("@@@@@ wifimonitor created\n"));
47 nsWifiMonitor::~nsWifiMonitor()
51 NS_IMETHODIMP
52 nsWifiMonitor::StartWatching(nsIWifiListener *aListener)
54 LOG(("@@@@@ nsWifiMonitor::StartWatching\n"));
55 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
56 if (!aListener) {
57 return NS_ERROR_NULL_POINTER;
60 mListeners.AppendElement(nsWifiListener(new nsMainThreadPtrHolder<nsIWifiListener>(aListener)));
62 if (!mTimer) {
63 mTimer = do_CreateInstance("@mozilla.org/timer;1");
64 mTimer->Init(this, 5000, nsITimer::TYPE_REPEATING_SLACK);
66 StartScan();
67 return NS_OK;
70 NS_IMETHODIMP
71 nsWifiMonitor::StopWatching(nsIWifiListener *aListener)
73 LOG(("@@@@@ nsWifiMonitor::StopWatching\n"));
74 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
75 if (!aListener) {
76 return NS_ERROR_NULL_POINTER;
79 for (uint32_t i = 0; i < mListeners.Length(); i++) {
80 if (mListeners[i].mListener == aListener) {
81 mListeners.RemoveElementAt(i);
82 break;
86 if (mListeners.Length() == 0) {
87 ClearTimer();
89 return NS_OK;
92 void
93 nsWifiMonitor::StartScan()
95 nsCOMPtr<nsIInterfaceRequestor> ir = do_GetService("@mozilla.org/telephony/system-worker-manager;1");
96 nsCOMPtr<nsIWifi> wifi = do_GetInterface(ir);
97 if (!wifi) {
98 return;
100 wifi->GetWifiScanResults(this);
103 NS_IMETHODIMP
104 nsWifiMonitor::Observe(nsISupports *subject, const char *topic,
105 const char16_t *data)
107 if (!strcmp(topic, "timer-callback")) {
108 LOG(("timer callback\n"));
109 StartScan();
110 return NS_OK;
113 if (!strcmp(topic, "xpcom-shutdown")) {
114 LOG(("Shutting down\n"));
115 ClearTimer();
116 return NS_OK;
119 return NS_ERROR_UNEXPECTED;
122 NS_IMETHODIMP
123 nsWifiMonitor::Onready(uint32_t count, nsIWifiScanResult **results)
125 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
126 LOG(("@@@@@ About to send data to the wifi listeners\n"));
128 nsCOMArray<nsWifiAccessPoint> accessPoints;
130 for (uint32_t i = 0; i < count; i++) {
131 nsRefPtr<nsWifiAccessPoint> ap = new nsWifiAccessPoint();
133 nsString temp;
134 results[i]->GetBssid(temp);
135 // 00:00:00:00:00:00 --> 00-00-00-00-00-00
136 for (int32_t x=0; x<6; x++) {
137 temp.ReplaceSubstring(NS_LITERAL_STRING(":"), NS_LITERAL_STRING("-")); // would it be too much to ask for a ReplaceAll()?
140 nsCString mac;
141 mac.AssignWithConversion(temp);
143 results[i]->GetSsid(temp);
145 nsCString ssid;
146 ssid.AssignWithConversion(temp);
148 uint32_t signal;
149 results[i]->GetSignalStrength(&signal);
151 ap->setSignal(signal);
152 ap->setMacRaw(mac.get());
153 ap->setSSIDRaw(ssid.get(), ssid.Length());
155 accessPoints.AppendObject(ap);
158 bool accessPointsChanged = !AccessPointsEqual(accessPoints, mLastAccessPoints);
159 ReplaceArray(mLastAccessPoints, accessPoints);
161 nsTArray<nsIWifiAccessPoint*> ac;
162 uint32_t resultCount = mLastAccessPoints.Count();
163 for (uint32_t i = 0; i < resultCount; i++) {
164 ac.AppendElement(mLastAccessPoints[i]);
167 for (uint32_t i = 0; i < mListeners.Length(); i++) {
168 if (!mListeners[i].mHasSentData || accessPointsChanged) {
169 mListeners[i].mHasSentData = true;
170 mListeners[i].mListener->OnChange(ac.Elements(), ac.Length());
173 return NS_OK;
176 NS_IMETHODIMP
177 nsWifiMonitor::Onfailure()
179 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
180 LOG(("@@@@@ About to send error to the wifi listeners\n"));
181 for (uint32_t i = 0; i < mListeners.Length(); i++) {
182 mListeners[i].mListener->OnError(NS_ERROR_UNEXPECTED);
185 ClearTimer();
186 return NS_OK;