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 | /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* Copyright 2012 Mozilla Foundation and Mozilla contributors |
michael@0 | 4 | * |
michael@0 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 6 | * you may not use this file except in compliance with the License. |
michael@0 | 7 | * You may obtain a copy of the License at |
michael@0 | 8 | * |
michael@0 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 10 | * |
michael@0 | 11 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 14 | * See the License for the specific language governing permissions and |
michael@0 | 15 | * limitations under the License. |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | #include "SystemWorkerManager.h" |
michael@0 | 19 | |
michael@0 | 20 | #include "nsINetworkService.h" |
michael@0 | 21 | #include "nsIWifi.h" |
michael@0 | 22 | #include "nsIWorkerHolder.h" |
michael@0 | 23 | #include "nsIXPConnect.h" |
michael@0 | 24 | |
michael@0 | 25 | #include "jsfriendapi.h" |
michael@0 | 26 | #include "mozilla/dom/workers/Workers.h" |
michael@0 | 27 | #include "AutoMounter.h" |
michael@0 | 28 | #include "TimeZoneSettingObserver.h" |
michael@0 | 29 | #include "AudioManager.h" |
michael@0 | 30 | #ifdef MOZ_B2G_RIL |
michael@0 | 31 | #include "mozilla/ipc/Ril.h" |
michael@0 | 32 | #endif |
michael@0 | 33 | #ifdef MOZ_NFC |
michael@0 | 34 | #include "mozilla/ipc/Nfc.h" |
michael@0 | 35 | #endif |
michael@0 | 36 | #include "mozilla/ipc/KeyStore.h" |
michael@0 | 37 | #include "nsIObserverService.h" |
michael@0 | 38 | #include "nsCxPusher.h" |
michael@0 | 39 | #include "nsServiceManagerUtils.h" |
michael@0 | 40 | #include "nsThreadUtils.h" |
michael@0 | 41 | #include "nsRadioInterfaceLayer.h" |
michael@0 | 42 | #include "WifiWorker.h" |
michael@0 | 43 | #include "mozilla/Services.h" |
michael@0 | 44 | |
michael@0 | 45 | USING_WORKERS_NAMESPACE |
michael@0 | 46 | |
michael@0 | 47 | using namespace mozilla::dom::gonk; |
michael@0 | 48 | using namespace mozilla::ipc; |
michael@0 | 49 | using namespace mozilla::system; |
michael@0 | 50 | |
michael@0 | 51 | namespace { |
michael@0 | 52 | |
michael@0 | 53 | NS_DEFINE_CID(kWifiWorkerCID, NS_WIFIWORKER_CID); |
michael@0 | 54 | |
michael@0 | 55 | // Doesn't carry a reference, we're owned by services. |
michael@0 | 56 | SystemWorkerManager *gInstance = nullptr; |
michael@0 | 57 | |
michael@0 | 58 | } // anonymous namespace |
michael@0 | 59 | |
michael@0 | 60 | SystemWorkerManager::SystemWorkerManager() |
michael@0 | 61 | : mShutdown(false) |
michael@0 | 62 | { |
michael@0 | 63 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 64 | NS_ASSERTION(!gInstance, "There should only be one instance!"); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | SystemWorkerManager::~SystemWorkerManager() |
michael@0 | 68 | { |
michael@0 | 69 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 70 | NS_ASSERTION(!gInstance || gInstance == this, |
michael@0 | 71 | "There should only be one instance!"); |
michael@0 | 72 | gInstance = nullptr; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | nsresult |
michael@0 | 76 | SystemWorkerManager::Init() |
michael@0 | 77 | { |
michael@0 | 78 | if (XRE_GetProcessType() != GeckoProcessType_Default) { |
michael@0 | 79 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | NS_ASSERTION(NS_IsMainThread(), "We can only initialize on the main thread"); |
michael@0 | 83 | NS_ASSERTION(!mShutdown, "Already shutdown!"); |
michael@0 | 84 | |
michael@0 | 85 | mozilla::AutoSafeJSContext cx; |
michael@0 | 86 | |
michael@0 | 87 | nsresult rv = InitWifi(cx); |
michael@0 | 88 | if (NS_FAILED(rv)) { |
michael@0 | 89 | NS_WARNING("Failed to initialize WiFi Networking!"); |
michael@0 | 90 | return rv; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | InitKeyStore(cx); |
michael@0 | 94 | |
michael@0 | 95 | InitAutoMounter(); |
michael@0 | 96 | InitializeTimeZoneSettingObserver(); |
michael@0 | 97 | nsCOMPtr<nsIAudioManager> audioManager = |
michael@0 | 98 | do_GetService(NS_AUDIOMANAGER_CONTRACTID); |
michael@0 | 99 | |
michael@0 | 100 | nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); |
michael@0 | 101 | if (!obs) { |
michael@0 | 102 | NS_WARNING("Failed to get observer service!"); |
michael@0 | 103 | return NS_ERROR_FAILURE; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | rv = obs->AddObserver(this, WORKERS_SHUTDOWN_TOPIC, false); |
michael@0 | 107 | if (NS_FAILED(rv)) { |
michael@0 | 108 | NS_WARNING("Failed to initialize worker shutdown event!"); |
michael@0 | 109 | return rv; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | return NS_OK; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | void |
michael@0 | 116 | SystemWorkerManager::Shutdown() |
michael@0 | 117 | { |
michael@0 | 118 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 119 | |
michael@0 | 120 | mShutdown = true; |
michael@0 | 121 | |
michael@0 | 122 | ShutdownAutoMounter(); |
michael@0 | 123 | |
michael@0 | 124 | #ifdef MOZ_B2G_RIL |
michael@0 | 125 | RilConsumer::Shutdown(); |
michael@0 | 126 | #endif |
michael@0 | 127 | |
michael@0 | 128 | #ifdef MOZ_NFC |
michael@0 | 129 | NfcConsumer::Shutdown(); |
michael@0 | 130 | #endif |
michael@0 | 131 | |
michael@0 | 132 | nsCOMPtr<nsIWifi> wifi(do_QueryInterface(mWifiWorker)); |
michael@0 | 133 | if (wifi) { |
michael@0 | 134 | wifi->Shutdown(); |
michael@0 | 135 | wifi = nullptr; |
michael@0 | 136 | } |
michael@0 | 137 | mWifiWorker = nullptr; |
michael@0 | 138 | |
michael@0 | 139 | nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); |
michael@0 | 140 | if (obs) { |
michael@0 | 141 | obs->RemoveObserver(this, WORKERS_SHUTDOWN_TOPIC); |
michael@0 | 142 | } |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | // static |
michael@0 | 146 | already_AddRefed<SystemWorkerManager> |
michael@0 | 147 | SystemWorkerManager::FactoryCreate() |
michael@0 | 148 | { |
michael@0 | 149 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 150 | |
michael@0 | 151 | nsRefPtr<SystemWorkerManager> instance(gInstance); |
michael@0 | 152 | |
michael@0 | 153 | if (!instance) { |
michael@0 | 154 | instance = new SystemWorkerManager(); |
michael@0 | 155 | if (NS_FAILED(instance->Init())) { |
michael@0 | 156 | instance->Shutdown(); |
michael@0 | 157 | return nullptr; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | gInstance = instance; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | return instance.forget(); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | // static |
michael@0 | 167 | nsIInterfaceRequestor* |
michael@0 | 168 | SystemWorkerManager::GetInterfaceRequestor() |
michael@0 | 169 | { |
michael@0 | 170 | return gInstance; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | NS_IMETHODIMP |
michael@0 | 174 | SystemWorkerManager::GetInterface(const nsIID &aIID, void **aResult) |
michael@0 | 175 | { |
michael@0 | 176 | NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
michael@0 | 177 | |
michael@0 | 178 | if (aIID.Equals(NS_GET_IID(nsIWifi))) { |
michael@0 | 179 | return CallQueryInterface(mWifiWorker, |
michael@0 | 180 | reinterpret_cast<nsIWifi**>(aResult)); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | NS_WARNING("Got nothing for the requested IID!"); |
michael@0 | 184 | return NS_ERROR_NO_INTERFACE; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | nsresult |
michael@0 | 188 | SystemWorkerManager::RegisterRilWorker(unsigned int aClientId, |
michael@0 | 189 | JS::Handle<JS::Value> aWorker, |
michael@0 | 190 | JSContext *aCx) |
michael@0 | 191 | { |
michael@0 | 192 | #ifndef MOZ_B2G_RIL |
michael@0 | 193 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 194 | #else |
michael@0 | 195 | NS_ENSURE_TRUE(aWorker.isObject(), NS_ERROR_UNEXPECTED); |
michael@0 | 196 | |
michael@0 | 197 | JSAutoCompartment ac(aCx, &aWorker.toObject()); |
michael@0 | 198 | |
michael@0 | 199 | WorkerCrossThreadDispatcher *wctd = |
michael@0 | 200 | GetWorkerCrossThreadDispatcher(aCx, aWorker); |
michael@0 | 201 | if (!wctd) { |
michael@0 | 202 | NS_WARNING("Failed to GetWorkerCrossThreadDispatcher for ril"); |
michael@0 | 203 | return NS_ERROR_FAILURE; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | return RilConsumer::Register(aClientId, wctd); |
michael@0 | 207 | #endif // MOZ_B2G_RIL |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | nsresult |
michael@0 | 211 | SystemWorkerManager::RegisterNfcWorker(JS::Handle<JS::Value> aWorker, |
michael@0 | 212 | JSContext* aCx) |
michael@0 | 213 | { |
michael@0 | 214 | #ifndef MOZ_NFC |
michael@0 | 215 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 216 | #else |
michael@0 | 217 | NS_ENSURE_TRUE(aWorker.isObject(), NS_ERROR_UNEXPECTED); |
michael@0 | 218 | |
michael@0 | 219 | JSAutoCompartment ac(aCx, &aWorker.toObject()); |
michael@0 | 220 | |
michael@0 | 221 | WorkerCrossThreadDispatcher* wctd = |
michael@0 | 222 | GetWorkerCrossThreadDispatcher(aCx, aWorker); |
michael@0 | 223 | if (!wctd) { |
michael@0 | 224 | NS_WARNING("Failed to GetWorkerCrossThreadDispatcher for nfc"); |
michael@0 | 225 | return NS_ERROR_FAILURE; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | return NfcConsumer::Register(wctd); |
michael@0 | 229 | #endif // MOZ_NFC |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | nsresult |
michael@0 | 233 | SystemWorkerManager::InitWifi(JSContext *cx) |
michael@0 | 234 | { |
michael@0 | 235 | nsCOMPtr<nsIWorkerHolder> worker = do_CreateInstance(kWifiWorkerCID); |
michael@0 | 236 | NS_ENSURE_TRUE(worker, NS_ERROR_FAILURE); |
michael@0 | 237 | |
michael@0 | 238 | mWifiWorker = worker; |
michael@0 | 239 | return NS_OK; |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | nsresult |
michael@0 | 243 | SystemWorkerManager::InitKeyStore(JSContext *cx) |
michael@0 | 244 | { |
michael@0 | 245 | mKeyStore = new KeyStore(); |
michael@0 | 246 | return NS_OK; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | NS_IMPL_ISUPPORTS(SystemWorkerManager, |
michael@0 | 250 | nsIObserver, |
michael@0 | 251 | nsIInterfaceRequestor, |
michael@0 | 252 | nsISystemWorkerManager) |
michael@0 | 253 | |
michael@0 | 254 | NS_IMETHODIMP |
michael@0 | 255 | SystemWorkerManager::Observe(nsISupports *aSubject, const char *aTopic, |
michael@0 | 256 | const char16_t *aData) |
michael@0 | 257 | { |
michael@0 | 258 | if (!strcmp(aTopic, WORKERS_SHUTDOWN_TOPIC)) { |
michael@0 | 259 | Shutdown(); |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | return NS_OK; |
michael@0 | 263 | } |