1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/wifi/WifiProxyService.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,342 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "WifiProxyService.h" 1.9 +#include "nsServiceManagerUtils.h" 1.10 +#include "mozilla/ModuleUtils.h" 1.11 +#include "mozilla/ClearOnShutdown.h" 1.12 +#include "nsXULAppAPI.h" 1.13 +#include "WifiUtils.h" 1.14 +#include "nsCxPusher.h" 1.15 + 1.16 +#ifdef MOZ_TASK_TRACER 1.17 +#include "GeckoTaskTracer.h" 1.18 +using namespace mozilla::tasktracer; 1.19 +#endif 1.20 + 1.21 +#define NS_WIFIPROXYSERVICE_CID \ 1.22 + { 0xc6c9be7e, 0x744f, 0x4222, {0xb2, 0x03, 0xcd, 0x55, 0xdf, 0xc8, 0xbc, 0x12} } 1.23 + 1.24 +using namespace mozilla; 1.25 +using namespace mozilla::dom; 1.26 + 1.27 +namespace mozilla { 1.28 + 1.29 +// The singleton Wifi service, to be used on the main thread. 1.30 +static StaticRefPtr<WifiProxyService> gWifiProxyService; 1.31 + 1.32 +// The singleton supplicant class, that can be used on any thread. 1.33 +static nsAutoPtr<WpaSupplicant> gWpaSupplicant; 1.34 + 1.35 +// Runnable used dispatch the WaitForEvent result on the main thread. 1.36 +class WifiEventDispatcher : public nsRunnable 1.37 +{ 1.38 +public: 1.39 + WifiEventDispatcher(const nsAString& aEvent, const nsACString& aInterface) 1.40 + : mEvent(aEvent) 1.41 + , mInterface(aInterface) 1.42 + { 1.43 + MOZ_ASSERT(!NS_IsMainThread()); 1.44 + } 1.45 + 1.46 + NS_IMETHOD Run() 1.47 + { 1.48 + MOZ_ASSERT(NS_IsMainThread()); 1.49 + gWifiProxyService->DispatchWifiEvent(mEvent, mInterface); 1.50 + return NS_OK; 1.51 + } 1.52 + 1.53 +private: 1.54 + nsString mEvent; 1.55 + nsCString mInterface; 1.56 +}; 1.57 + 1.58 +// Runnable used to call WaitForEvent on the event thread. 1.59 +class EventRunnable : public nsRunnable 1.60 +{ 1.61 +public: 1.62 + EventRunnable(const nsACString& aInterface) 1.63 + : mInterface(aInterface) 1.64 + { 1.65 + MOZ_ASSERT(NS_IsMainThread()); 1.66 + } 1.67 + 1.68 + NS_IMETHOD Run() 1.69 + { 1.70 + MOZ_ASSERT(!NS_IsMainThread()); 1.71 + nsAutoString event; 1.72 + gWpaSupplicant->WaitForEvent(event, mInterface); 1.73 + if (!event.IsEmpty()) { 1.74 +#ifdef MOZ_TASK_TRACER 1.75 + // Make wifi initialization events to be the source events of TaskTracer, 1.76 + // and originate the rest correlation tasks from here. 1.77 + AutoSourceEvent taskTracerEvent(SourceEventType::WIFI); 1.78 + AddLabel("%s %s", mInterface.get(), NS_ConvertUTF16toUTF8(event).get()); 1.79 +#endif 1.80 + nsCOMPtr<nsIRunnable> runnable = new WifiEventDispatcher(event, mInterface); 1.81 + NS_DispatchToMainThread(runnable); 1.82 + } 1.83 + return NS_OK; 1.84 + } 1.85 + 1.86 +private: 1.87 + nsCString mInterface; 1.88 +}; 1.89 + 1.90 +// Runnable used dispatch the Command result on the main thread. 1.91 +class WifiResultDispatcher : public nsRunnable 1.92 +{ 1.93 +public: 1.94 + WifiResultDispatcher(WifiResultOptions& aResult, const nsACString& aInterface) 1.95 + : mInterface(aInterface) 1.96 + { 1.97 + MOZ_ASSERT(!NS_IsMainThread()); 1.98 + 1.99 + // XXX: is there a better way to copy webidl dictionnaries? 1.100 + // the copy constructor is private. 1.101 +#define COPY_FIELD(prop) mResult.prop = aResult.prop; 1.102 + 1.103 + COPY_FIELD(mId) 1.104 + COPY_FIELD(mStatus) 1.105 + COPY_FIELD(mReply) 1.106 + COPY_FIELD(mRoute) 1.107 + COPY_FIELD(mError) 1.108 + COPY_FIELD(mValue) 1.109 + COPY_FIELD(mIpaddr_str) 1.110 + COPY_FIELD(mGateway_str) 1.111 + COPY_FIELD(mDns1_str) 1.112 + COPY_FIELD(mDns2_str) 1.113 + COPY_FIELD(mMask_str) 1.114 + COPY_FIELD(mServer_str) 1.115 + COPY_FIELD(mVendor_str) 1.116 + COPY_FIELD(mLease) 1.117 + COPY_FIELD(mMask) 1.118 + COPY_FIELD(mIpaddr) 1.119 + COPY_FIELD(mGateway) 1.120 + COPY_FIELD(mDns1) 1.121 + COPY_FIELD(mDns2) 1.122 + COPY_FIELD(mServer) 1.123 + 1.124 +#undef COPY_FIELD 1.125 + } 1.126 + 1.127 + NS_IMETHOD Run() 1.128 + { 1.129 + MOZ_ASSERT(NS_IsMainThread()); 1.130 + gWifiProxyService->DispatchWifiResult(mResult, mInterface); 1.131 + return NS_OK; 1.132 + } 1.133 + 1.134 +private: 1.135 + WifiResultOptions mResult; 1.136 + nsCString mInterface; 1.137 +}; 1.138 + 1.139 +// Runnable used to call SendCommand on the control thread. 1.140 +class ControlRunnable : public nsRunnable 1.141 +{ 1.142 +public: 1.143 + ControlRunnable(CommandOptions aOptions, const nsACString& aInterface) 1.144 + : mOptions(aOptions) 1.145 + , mInterface(aInterface) 1.146 + { 1.147 + MOZ_ASSERT(NS_IsMainThread()); 1.148 + } 1.149 + 1.150 + NS_IMETHOD Run() 1.151 + { 1.152 + WifiResultOptions result; 1.153 + if (gWpaSupplicant->ExecuteCommand(mOptions, result, mInterface)) { 1.154 + nsCOMPtr<nsIRunnable> runnable = new WifiResultDispatcher(result, mInterface); 1.155 + NS_DispatchToMainThread(runnable); 1.156 + } 1.157 + return NS_OK; 1.158 + } 1.159 +private: 1.160 + CommandOptions mOptions; 1.161 + nsCString mInterface; 1.162 +}; 1.163 + 1.164 +NS_IMPL_ISUPPORTS(WifiProxyService, nsIWifiProxyService) 1.165 + 1.166 +WifiProxyService::WifiProxyService() 1.167 +{ 1.168 + MOZ_ASSERT(NS_IsMainThread()); 1.169 + MOZ_ASSERT(!gWifiProxyService); 1.170 +} 1.171 + 1.172 +WifiProxyService::~WifiProxyService() 1.173 +{ 1.174 + MOZ_ASSERT(!gWifiProxyService); 1.175 +} 1.176 + 1.177 +already_AddRefed<WifiProxyService> 1.178 +WifiProxyService::FactoryCreate() 1.179 +{ 1.180 + if (XRE_GetProcessType() != GeckoProcessType_Default) { 1.181 + return nullptr; 1.182 + } 1.183 + 1.184 + MOZ_ASSERT(NS_IsMainThread()); 1.185 + 1.186 + if (!gWifiProxyService) { 1.187 + gWifiProxyService = new WifiProxyService(); 1.188 + ClearOnShutdown(&gWifiProxyService); 1.189 + 1.190 + gWpaSupplicant = new WpaSupplicant(); 1.191 + ClearOnShutdown(&gWpaSupplicant); 1.192 + } 1.193 + 1.194 + nsRefPtr<WifiProxyService> service = gWifiProxyService.get(); 1.195 + return service.forget(); 1.196 +} 1.197 + 1.198 +NS_IMETHODIMP 1.199 +WifiProxyService::Start(nsIWifiEventListener* aListener, 1.200 + const char ** aInterfaces, 1.201 + uint32_t aNumOfInterfaces) 1.202 +{ 1.203 + MOZ_ASSERT(NS_IsMainThread()); 1.204 + MOZ_ASSERT(aListener); 1.205 + 1.206 + nsresult rv; 1.207 + 1.208 + // Since EventRunnable runs in the manner of blocking, we have to 1.209 + // spin a thread for each interface. 1.210 + // (See the WpaSupplicant::WaitForEvent) 1.211 + mEventThreadList.SetLength(aNumOfInterfaces); 1.212 + for (uint32_t i = 0; i < aNumOfInterfaces; i++) { 1.213 + mEventThreadList[i].mInterface = aInterfaces[i]; 1.214 + rv = NS_NewThread(getter_AddRefs(mEventThreadList[i].mThread)); 1.215 + if (NS_FAILED(rv)) { 1.216 + NS_WARNING("Can't create wifi event thread"); 1.217 + Shutdown(); 1.218 + return NS_ERROR_FAILURE; 1.219 + } 1.220 + } 1.221 + 1.222 + rv = NS_NewThread(getter_AddRefs(mControlThread)); 1.223 + if (NS_FAILED(rv)) { 1.224 + NS_WARNING("Can't create wifi control thread"); 1.225 + Shutdown(); 1.226 + return NS_ERROR_FAILURE; 1.227 + } 1.228 + 1.229 + mListener = aListener; 1.230 + 1.231 + return NS_OK; 1.232 +} 1.233 + 1.234 +NS_IMETHODIMP 1.235 +WifiProxyService::Shutdown() 1.236 +{ 1.237 + MOZ_ASSERT(NS_IsMainThread()); 1.238 + for (size_t i = 0; i < mEventThreadList.Length(); i++) { 1.239 + if (mEventThreadList[i].mThread) { 1.240 + mEventThreadList[i].mThread->Shutdown(); 1.241 + mEventThreadList[i].mThread = nullptr; 1.242 + } 1.243 + } 1.244 + mEventThreadList.Clear(); 1.245 + if (mControlThread) { 1.246 + mControlThread->Shutdown(); 1.247 + mControlThread = nullptr; 1.248 + } 1.249 + return NS_OK; 1.250 +} 1.251 + 1.252 +NS_IMETHODIMP 1.253 +WifiProxyService::SendCommand(JS::Handle<JS::Value> aOptions, 1.254 + const nsACString& aInterface, 1.255 + JSContext* aCx) 1.256 +{ 1.257 + MOZ_ASSERT(NS_IsMainThread()); 1.258 + WifiCommandOptions options; 1.259 + 1.260 + if (!options.Init(aCx, aOptions)) { 1.261 + NS_WARNING("Bad dictionary passed to WifiProxyService::SendCommand"); 1.262 + return NS_ERROR_FAILURE; 1.263 + } 1.264 + 1.265 + // Dispatch the command to the control thread. 1.266 + CommandOptions commandOptions(options); 1.267 + nsCOMPtr<nsIRunnable> runnable = new ControlRunnable(commandOptions, aInterface); 1.268 + mControlThread->Dispatch(runnable, nsIEventTarget::DISPATCH_NORMAL); 1.269 + return NS_OK; 1.270 +} 1.271 + 1.272 +NS_IMETHODIMP 1.273 +WifiProxyService::WaitForEvent(const nsACString& aInterface) 1.274 +{ 1.275 + MOZ_ASSERT(NS_IsMainThread()); 1.276 + 1.277 + // Dispatch to the event thread which has the given interface name 1.278 + for (size_t i = 0; i < mEventThreadList.Length(); i++) { 1.279 + if (mEventThreadList[i].mInterface.Equals(aInterface)) { 1.280 + nsCOMPtr<nsIRunnable> runnable = new EventRunnable(aInterface); 1.281 + mEventThreadList[i].mThread->Dispatch(runnable, nsIEventTarget::DISPATCH_NORMAL); 1.282 + return NS_OK; 1.283 + } 1.284 + } 1.285 + 1.286 + return NS_ERROR_FAILURE; 1.287 +} 1.288 + 1.289 +void 1.290 +WifiProxyService::DispatchWifiResult(const WifiResultOptions& aOptions, const nsACString& aInterface) 1.291 +{ 1.292 + MOZ_ASSERT(NS_IsMainThread()); 1.293 + 1.294 + mozilla::AutoSafeJSContext cx; 1.295 + JS::Rooted<JS::Value> val(cx); 1.296 + 1.297 + if (!aOptions.ToObject(cx, &val)) { 1.298 + return; 1.299 + } 1.300 + 1.301 + // Call the listener with a JS value. 1.302 + mListener->OnCommand(val, aInterface); 1.303 +} 1.304 + 1.305 +void 1.306 +WifiProxyService::DispatchWifiEvent(const nsAString& aEvent, const nsACString& aInterface) 1.307 +{ 1.308 + MOZ_ASSERT(NS_IsMainThread()); 1.309 + nsAutoString event; 1.310 + if (StringBeginsWith(aEvent, NS_LITERAL_STRING("IFNAME"))) { 1.311 + // Jump over IFNAME for gonk-kk. 1.312 + event = Substring(aEvent, aEvent.FindChar(' ') + 1); 1.313 + } 1.314 + else { 1.315 + event = aEvent; 1.316 + } 1.317 + // Call the listener. 1.318 + mListener->OnWaitEvent(event, aInterface); 1.319 +} 1.320 + 1.321 +NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(WifiProxyService, 1.322 + WifiProxyService::FactoryCreate) 1.323 + 1.324 +NS_DEFINE_NAMED_CID(NS_WIFIPROXYSERVICE_CID); 1.325 + 1.326 +static const mozilla::Module::CIDEntry kWifiProxyServiceCIDs[] = { 1.327 + { &kNS_WIFIPROXYSERVICE_CID, false, nullptr, WifiProxyServiceConstructor }, 1.328 + { nullptr } 1.329 +}; 1.330 + 1.331 +static const mozilla::Module::ContractIDEntry kWifiProxyServiceContracts[] = { 1.332 + { "@mozilla.org/wifi/service;1", &kNS_WIFIPROXYSERVICE_CID }, 1.333 + { nullptr } 1.334 +}; 1.335 + 1.336 +static const mozilla::Module kWifiProxyServiceModule = { 1.337 + mozilla::Module::kVersion, 1.338 + kWifiProxyServiceCIDs, 1.339 + kWifiProxyServiceContracts, 1.340 + nullptr 1.341 +}; 1.342 + 1.343 +} // namespace mozilla 1.344 + 1.345 +NSMODULE_DEFN(WifiProxyServiceModule) = &kWifiProxyServiceModule;