2013-08-27 13:54:51 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "WifiProxyService.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "mozilla/ModuleUtils.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2014-06-11 12:38:55 -07:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2013-08-27 13:54:51 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "WifiUtils.h"
|
|
|
|
|
2014-04-23 00:32:31 -07:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
#include "GeckoTaskTracer.h"
|
|
|
|
using namespace mozilla::tasktracer;
|
|
|
|
#endif
|
|
|
|
|
2013-08-27 13:54:51 -07:00
|
|
|
#define NS_WIFIPROXYSERVICE_CID \
|
|
|
|
{ 0xc6c9be7e, 0x744f, 0x4222, {0xb2, 0x03, 0xcd, 0x55, 0xdf, 0xc8, 0xbc, 0x12} }
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// The singleton Wifi service, to be used on the main thread.
|
2013-10-14 01:33:59 -07:00
|
|
|
static StaticRefPtr<WifiProxyService> gWifiProxyService;
|
2013-08-27 13:54:51 -07:00
|
|
|
|
|
|
|
// The singleton supplicant class, that can be used on any thread.
|
|
|
|
static nsAutoPtr<WpaSupplicant> gWpaSupplicant;
|
|
|
|
|
|
|
|
// Runnable used dispatch the WaitForEvent result on the main thread.
|
|
|
|
class WifiEventDispatcher : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-10-14 01:33:59 -07:00
|
|
|
WifiEventDispatcher(const nsAString& aEvent, const nsACString& aInterface)
|
|
|
|
: mEvent(aEvent)
|
|
|
|
, mInterface(aInterface)
|
2013-08-27 13:54:51 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-10-14 01:33:59 -07:00
|
|
|
gWifiProxyService->DispatchWifiEvent(mEvent, mInterface);
|
2013-08-27 13:54:51 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsString mEvent;
|
2013-10-14 01:33:59 -07:00
|
|
|
nsCString mInterface;
|
2013-08-27 13:54:51 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Runnable used to call WaitForEvent on the event thread.
|
|
|
|
class EventRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-10-14 01:33:59 -07:00
|
|
|
EventRunnable(const nsACString& aInterface)
|
|
|
|
: mInterface(aInterface)
|
2013-08-27 13:54:51 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
nsAutoString event;
|
2013-10-14 01:33:59 -07:00
|
|
|
gWpaSupplicant->WaitForEvent(event, mInterface);
|
2013-08-27 13:54:51 -07:00
|
|
|
if (!event.IsEmpty()) {
|
2014-04-23 00:32:31 -07:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
// Make wifi initialization events to be the source events of TaskTracer,
|
|
|
|
// and originate the rest correlation tasks from here.
|
|
|
|
AutoSourceEvent taskTracerEvent(SourceEventType::WIFI);
|
|
|
|
AddLabel("%s %s", mInterface.get(), NS_ConvertUTF16toUTF8(event).get());
|
|
|
|
#endif
|
2013-10-14 01:33:59 -07:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = new WifiEventDispatcher(event, mInterface);
|
2013-08-27 13:54:51 -07:00
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-10-14 01:33:59 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsCString mInterface;
|
2013-08-27 13:54:51 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Runnable used dispatch the Command result on the main thread.
|
|
|
|
class WifiResultDispatcher : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-10-14 01:33:59 -07:00
|
|
|
WifiResultDispatcher(WifiResultOptions& aResult, const nsACString& aInterface)
|
2014-08-17 20:02:56 -07:00
|
|
|
: mResult(aResult)
|
|
|
|
, mInterface(aInterface)
|
2013-08-27 13:54:51 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-10-14 01:33:59 -07:00
|
|
|
gWifiProxyService->DispatchWifiResult(mResult, mInterface);
|
2013-08-27 13:54:51 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WifiResultOptions mResult;
|
2013-10-14 01:33:59 -07:00
|
|
|
nsCString mInterface;
|
2013-08-27 13:54:51 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Runnable used to call SendCommand on the control thread.
|
|
|
|
class ControlRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-10-14 01:33:59 -07:00
|
|
|
ControlRunnable(CommandOptions aOptions, const nsACString& aInterface)
|
|
|
|
: mOptions(aOptions)
|
|
|
|
, mInterface(aInterface)
|
|
|
|
{
|
2013-08-28 21:02:55 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-08-27 13:54:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
WifiResultOptions result;
|
2013-10-14 01:33:59 -07:00
|
|
|
if (gWpaSupplicant->ExecuteCommand(mOptions, result, mInterface)) {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new WifiResultDispatcher(result, mInterface);
|
2013-08-27 13:54:51 -07:00
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
CommandOptions mOptions;
|
2013-10-14 01:33:59 -07:00
|
|
|
nsCString mInterface;
|
2013-08-27 13:54:51 -07:00
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(WifiProxyService, nsIWifiProxyService)
|
2013-08-27 13:54:51 -07:00
|
|
|
|
|
|
|
WifiProxyService::WifiProxyService()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!gWifiProxyService);
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiProxyService::~WifiProxyService()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!gWifiProxyService);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<WifiProxyService>
|
|
|
|
WifiProxyService::FactoryCreate()
|
|
|
|
{
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!gWifiProxyService) {
|
|
|
|
gWifiProxyService = new WifiProxyService();
|
|
|
|
ClearOnShutdown(&gWifiProxyService);
|
|
|
|
|
|
|
|
gWpaSupplicant = new WpaSupplicant();
|
|
|
|
ClearOnShutdown(&gWpaSupplicant);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<WifiProxyService> service = gWifiProxyService.get();
|
|
|
|
return service.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-10-14 01:33:59 -07:00
|
|
|
WifiProxyService::Start(nsIWifiEventListener* aListener,
|
|
|
|
const char ** aInterfaces,
|
|
|
|
uint32_t aNumOfInterfaces)
|
2013-08-27 13:54:51 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aListener);
|
|
|
|
|
2013-10-14 01:33:59 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Since EventRunnable runs in the manner of blocking, we have to
|
|
|
|
// spin a thread for each interface.
|
|
|
|
// (See the WpaSupplicant::WaitForEvent)
|
|
|
|
mEventThreadList.SetLength(aNumOfInterfaces);
|
|
|
|
for (uint32_t i = 0; i < aNumOfInterfaces; i++) {
|
|
|
|
mEventThreadList[i].mInterface = aInterfaces[i];
|
|
|
|
rv = NS_NewThread(getter_AddRefs(mEventThreadList[i].mThread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Can't create wifi event thread");
|
|
|
|
Shutdown();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-08-27 13:54:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = NS_NewThread(getter_AddRefs(mControlThread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Can't create wifi control thread");
|
2013-10-14 01:33:59 -07:00
|
|
|
Shutdown();
|
2013-08-27 13:54:51 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListener = aListener;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WifiProxyService::Shutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-10-14 01:33:59 -07:00
|
|
|
for (size_t i = 0; i < mEventThreadList.Length(); i++) {
|
|
|
|
if (mEventThreadList[i].mThread) {
|
|
|
|
mEventThreadList[i].mThread->Shutdown();
|
|
|
|
mEventThreadList[i].mThread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 19:31:18 -07:00
|
|
|
|
2013-10-14 01:33:59 -07:00
|
|
|
mEventThreadList.Clear();
|
2014-07-30 19:31:18 -07:00
|
|
|
|
2013-10-14 01:33:59 -07:00
|
|
|
if (mControlThread) {
|
|
|
|
mControlThread->Shutdown();
|
|
|
|
mControlThread = nullptr;
|
|
|
|
}
|
2014-07-30 19:31:18 -07:00
|
|
|
|
|
|
|
mListener = nullptr;
|
|
|
|
|
2013-08-27 13:54:51 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 09:39:36 -08:00
|
|
|
WifiProxyService::SendCommand(JS::Handle<JS::Value> aOptions,
|
|
|
|
const nsACString& aInterface,
|
2013-10-14 01:33:59 -07:00
|
|
|
JSContext* aCx)
|
2013-08-27 13:54:51 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
WifiCommandOptions options;
|
|
|
|
|
2014-01-09 09:39:36 -08:00
|
|
|
if (!options.Init(aCx, aOptions)) {
|
2013-08-27 13:54:51 -07:00
|
|
|
NS_WARNING("Bad dictionary passed to WifiProxyService::SendCommand");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dispatch the command to the control thread.
|
|
|
|
CommandOptions commandOptions(options);
|
2013-10-14 01:33:59 -07:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = new ControlRunnable(commandOptions, aInterface);
|
2013-08-27 13:54:51 -07:00
|
|
|
mControlThread->Dispatch(runnable, nsIEventTarget::DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-10-14 01:33:59 -07:00
|
|
|
WifiProxyService::WaitForEvent(const nsACString& aInterface)
|
2013-08-27 13:54:51 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-10-14 01:33:59 -07:00
|
|
|
|
|
|
|
// Dispatch to the event thread which has the given interface name
|
|
|
|
for (size_t i = 0; i < mEventThreadList.Length(); i++) {
|
|
|
|
if (mEventThreadList[i].mInterface.Equals(aInterface)) {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new EventRunnable(aInterface);
|
|
|
|
mEventThreadList[i].mThread->Dispatch(runnable, nsIEventTarget::DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2013-08-27 13:54:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-14 01:33:59 -07:00
|
|
|
WifiProxyService::DispatchWifiResult(const WifiResultOptions& aOptions, const nsACString& aInterface)
|
2013-08-27 13:54:51 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mozilla::AutoSafeJSContext cx;
|
2013-11-11 00:04:41 -08:00
|
|
|
JS::Rooted<JS::Value> val(cx);
|
2013-08-27 13:54:51 -07:00
|
|
|
|
2014-06-11 12:38:55 -07:00
|
|
|
if (!ToJSValue(cx, aOptions, &val)) {
|
2013-08-27 13:54:51 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the listener with a JS value.
|
2013-10-14 01:33:59 -07:00
|
|
|
mListener->OnCommand(val, aInterface);
|
2013-08-27 13:54:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-14 01:33:59 -07:00
|
|
|
WifiProxyService::DispatchWifiEvent(const nsAString& aEvent, const nsACString& aInterface)
|
2013-08-27 13:54:51 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-01-21 20:06:58 -08:00
|
|
|
nsAutoString event;
|
|
|
|
if (StringBeginsWith(aEvent, NS_LITERAL_STRING("IFNAME"))) {
|
|
|
|
// Jump over IFNAME for gonk-kk.
|
|
|
|
event = Substring(aEvent, aEvent.FindChar(' ') + 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
event = aEvent;
|
|
|
|
}
|
2013-08-27 13:54:51 -07:00
|
|
|
// Call the listener.
|
2014-01-21 20:06:58 -08:00
|
|
|
mListener->OnWaitEvent(event, aInterface);
|
2013-08-27 13:54:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(WifiProxyService,
|
|
|
|
WifiProxyService::FactoryCreate)
|
|
|
|
|
|
|
|
NS_DEFINE_NAMED_CID(NS_WIFIPROXYSERVICE_CID);
|
|
|
|
|
|
|
|
static const mozilla::Module::CIDEntry kWifiProxyServiceCIDs[] = {
|
|
|
|
{ &kNS_WIFIPROXYSERVICE_CID, false, nullptr, WifiProxyServiceConstructor },
|
|
|
|
{ nullptr }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const mozilla::Module::ContractIDEntry kWifiProxyServiceContracts[] = {
|
|
|
|
{ "@mozilla.org/wifi/service;1", &kNS_WIFIPROXYSERVICE_CID },
|
|
|
|
{ nullptr }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const mozilla::Module kWifiProxyServiceModule = {
|
|
|
|
mozilla::Module::kVersion,
|
|
|
|
kWifiProxyServiceCIDs,
|
|
|
|
kWifiProxyServiceContracts,
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
NSMODULE_DEFN(WifiProxyServiceModule) = &kWifiProxyServiceModule;
|