Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "nsCOMPtr.h" |
michael@0 | 6 | #include "nsComponentManagerUtils.h" |
michael@0 | 7 | #include "nsServiceManagerUtils.h" |
michael@0 | 8 | #include "nsThreadUtils.h" |
michael@0 | 9 | #include "nsXPCOM.h" |
michael@0 | 10 | #include "nsXPCOMCID.h" |
michael@0 | 11 | #include "nsIObserver.h" |
michael@0 | 12 | #include "nsIObserverService.h" |
michael@0 | 13 | #include "nsWifiMonitor.h" |
michael@0 | 14 | #include "nsWifiAccessPoint.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "nsServiceManagerUtils.h" |
michael@0 | 17 | #include "nsComponentManagerUtils.h" |
michael@0 | 18 | #include "mozilla/Services.h" |
michael@0 | 19 | |
michael@0 | 20 | #include "nsIInterfaceRequestor.h" |
michael@0 | 21 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 22 | |
michael@0 | 23 | using namespace mozilla; |
michael@0 | 24 | |
michael@0 | 25 | #if defined(PR_LOGGING) |
michael@0 | 26 | PRLogModuleInfo *gWifiMonitorLog; |
michael@0 | 27 | #endif |
michael@0 | 28 | |
michael@0 | 29 | NS_IMPL_ISUPPORTS(nsWifiMonitor, |
michael@0 | 30 | nsIWifiMonitor, |
michael@0 | 31 | nsIObserver, |
michael@0 | 32 | nsIWifiScanResultsReady) |
michael@0 | 33 | |
michael@0 | 34 | nsWifiMonitor::nsWifiMonitor() |
michael@0 | 35 | { |
michael@0 | 36 | #if defined(PR_LOGGING) |
michael@0 | 37 | gWifiMonitorLog = PR_NewLogModule("WifiMonitor"); |
michael@0 | 38 | #endif |
michael@0 | 39 | |
michael@0 | 40 | nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService(); |
michael@0 | 41 | if (obsSvc) { |
michael@0 | 42 | obsSvc->AddObserver(this, "xpcom-shutdown", false); |
michael@0 | 43 | } |
michael@0 | 44 | LOG(("@@@@@ wifimonitor created\n")); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | nsWifiMonitor::~nsWifiMonitor() |
michael@0 | 48 | { |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | NS_IMETHODIMP |
michael@0 | 52 | nsWifiMonitor::StartWatching(nsIWifiListener *aListener) |
michael@0 | 53 | { |
michael@0 | 54 | LOG(("@@@@@ nsWifiMonitor::StartWatching\n")); |
michael@0 | 55 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 56 | if (!aListener) { |
michael@0 | 57 | return NS_ERROR_NULL_POINTER; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | mListeners.AppendElement(nsWifiListener(new nsMainThreadPtrHolder<nsIWifiListener>(aListener))); |
michael@0 | 61 | |
michael@0 | 62 | if (!mTimer) { |
michael@0 | 63 | mTimer = do_CreateInstance("@mozilla.org/timer;1"); |
michael@0 | 64 | mTimer->Init(this, 5000, nsITimer::TYPE_REPEATING_SLACK); |
michael@0 | 65 | } |
michael@0 | 66 | return NS_OK; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | NS_IMETHODIMP |
michael@0 | 70 | nsWifiMonitor::StopWatching(nsIWifiListener *aListener) |
michael@0 | 71 | { |
michael@0 | 72 | LOG(("@@@@@ nsWifiMonitor::StopWatching\n")); |
michael@0 | 73 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 74 | if (!aListener) { |
michael@0 | 75 | return NS_ERROR_NULL_POINTER; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | for (uint32_t i = 0; i < mListeners.Length(); i++) { |
michael@0 | 79 | if (mListeners[i].mListener == aListener) { |
michael@0 | 80 | mListeners.RemoveElementAt(i); |
michael@0 | 81 | break; |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | if (mListeners.Length() == 0) { |
michael@0 | 86 | ClearTimer(); |
michael@0 | 87 | } |
michael@0 | 88 | return NS_OK; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | NS_IMETHODIMP |
michael@0 | 92 | nsWifiMonitor::Observe(nsISupports *subject, const char *topic, |
michael@0 | 93 | const char16_t *data) |
michael@0 | 94 | { |
michael@0 | 95 | if (!strcmp(topic, "timer-callback")) { |
michael@0 | 96 | LOG(("timer callback\n")); |
michael@0 | 97 | |
michael@0 | 98 | nsCOMPtr<nsIInterfaceRequestor> ir = do_GetService("@mozilla.org/telephony/system-worker-manager;1"); |
michael@0 | 99 | nsCOMPtr<nsIWifi> wifi = do_GetInterface(ir); |
michael@0 | 100 | if (!wifi) { |
michael@0 | 101 | return NS_ERROR_UNEXPECTED; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | wifi->GetWifiScanResults(this); |
michael@0 | 105 | return NS_OK; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | if (!strcmp(topic, "xpcom-shutdown")) { |
michael@0 | 109 | LOG(("Shutting down\n")); |
michael@0 | 110 | ClearTimer(); |
michael@0 | 111 | return NS_OK; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | return NS_ERROR_UNEXPECTED; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | NS_IMETHODIMP |
michael@0 | 118 | nsWifiMonitor::Onready(uint32_t count, nsIWifiScanResult **results) |
michael@0 | 119 | { |
michael@0 | 120 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 121 | LOG(("@@@@@ About to send data to the wifi listeners\n")); |
michael@0 | 122 | |
michael@0 | 123 | nsCOMArray<nsWifiAccessPoint> accessPoints; |
michael@0 | 124 | |
michael@0 | 125 | for (uint32_t i = 0; i < count; i++) { |
michael@0 | 126 | nsRefPtr<nsWifiAccessPoint> ap = new nsWifiAccessPoint(); |
michael@0 | 127 | |
michael@0 | 128 | nsString temp; |
michael@0 | 129 | results[i]->GetBssid(temp); |
michael@0 | 130 | // 00:00:00:00:00:00 --> 00-00-00-00-00-00 |
michael@0 | 131 | for (int32_t x=0; x<6; x++) { |
michael@0 | 132 | temp.ReplaceSubstring(NS_LITERAL_STRING(":"), NS_LITERAL_STRING("-")); // would it be too much to ask for a ReplaceAll()? |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | nsCString mac; |
michael@0 | 136 | mac.AssignWithConversion(temp); |
michael@0 | 137 | |
michael@0 | 138 | results[i]->GetSsid(temp); |
michael@0 | 139 | |
michael@0 | 140 | nsCString ssid; |
michael@0 | 141 | ssid.AssignWithConversion(temp); |
michael@0 | 142 | |
michael@0 | 143 | uint32_t signal; |
michael@0 | 144 | results[i]->GetSignalStrength(&signal); |
michael@0 | 145 | |
michael@0 | 146 | ap->setSignal(signal); |
michael@0 | 147 | ap->setMacRaw(mac.get()); |
michael@0 | 148 | ap->setSSIDRaw(ssid.get(), ssid.Length()); |
michael@0 | 149 | |
michael@0 | 150 | accessPoints.AppendObject(ap); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | bool accessPointsChanged = !AccessPointsEqual(accessPoints, mLastAccessPoints); |
michael@0 | 154 | ReplaceArray(mLastAccessPoints, accessPoints); |
michael@0 | 155 | |
michael@0 | 156 | nsTArray<nsIWifiAccessPoint*> ac; |
michael@0 | 157 | uint32_t resultCount = mLastAccessPoints.Count(); |
michael@0 | 158 | for (uint32_t i = 0; i < resultCount; i++) { |
michael@0 | 159 | ac.AppendElement(mLastAccessPoints[i]); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | for (uint32_t i = 0; i < mListeners.Length(); i++) { |
michael@0 | 163 | if (!mListeners[i].mHasSentData || accessPointsChanged) { |
michael@0 | 164 | mListeners[i].mHasSentData = true; |
michael@0 | 165 | mListeners[i].mListener->OnChange(ac.Elements(), ac.Length()); |
michael@0 | 166 | } |
michael@0 | 167 | } |
michael@0 | 168 | return NS_OK; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | NS_IMETHODIMP |
michael@0 | 172 | nsWifiMonitor::Onfailure() |
michael@0 | 173 | { |
michael@0 | 174 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 175 | LOG(("@@@@@ About to send error to the wifi listeners\n")); |
michael@0 | 176 | for (uint32_t i = 0; i < mListeners.Length(); i++) { |
michael@0 | 177 | mListeners[i].mListener->OnError(NS_ERROR_UNEXPECTED); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | ClearTimer(); |
michael@0 | 181 | return NS_OK; |
michael@0 | 182 | } |