Bug 938015 - Do not turn off FM radio when enabling airplane mode. r=khuey
authorPin Zhang <pzhang@mozilla.com>
Thu, 09 Jan 2014 10:22:19 +0800
changeset 163387 e60f0de566ecdebbee61b6f4f8e6563df34f6633
parent 163386 ce84a811c9699ffdc49a18e38d0deda0bab42499
child 163388 bb7f35d963edd00c6deb7de2763f9bca14290d29
push id38465
push userkwierso@gmail.com
push dateWed, 15 Jan 2014 00:14:00 +0000
treeherdermozilla-inbound@43832758b909 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskhuey
bugs938015
milestone29.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 938015 - Do not turn off FM radio when enabling airplane mode. r=khuey
dom/fmradio/FMRadioService.cpp
dom/fmradio/FMRadioService.h
dom/fmradio/test/marionette/manifest.ini
dom/fmradio/test/marionette/test_bug876597.js
--- a/dom/fmradio/FMRadioService.cpp
+++ b/dom/fmradio/FMRadioService.cpp
@@ -19,19 +19,16 @@
 #define BAND_87500_108000_kHz 1
 #define BAND_76000_108000_kHz 2
 #define BAND_76000_90000_kHz  3
 
 #define CHANNEL_WIDTH_200KHZ 200
 #define CHANNEL_WIDTH_100KHZ 100
 #define CHANNEL_WIDTH_50KHZ  50
 
-#define MOZSETTINGS_CHANGED_ID "mozsettings-changed"
-#define SETTING_KEY_RIL_RADIO_DISABLED "ril.radio.disabled"
-
 using namespace mozilla::hal;
 using mozilla::Preferences;
 
 BEGIN_FMRADIO_NAMESPACE
 
 // static
 IFMRadioService*
 IFMRadioService::Singleton()
@@ -88,22 +85,16 @@ FMRadioService::FMRadioService()
     case CHANNEL_WIDTH_100KHZ:
     default:
       mChannelWidthInKHz = 100;
       break;
   }
 
   nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
 
-  if (NS_FAILED(obs->AddObserver(this,
-                                 MOZSETTINGS_CHANGED_ID,
-                                 /* useWeak */ false))) {
-    NS_WARNING("Failed to add settings change observer!");
-  }
-
   RegisterFMRadioObserver(this);
 }
 
 FMRadioService::~FMRadioService()
 {
   UnregisterFMRadioObserver(this);
 }
 
@@ -441,52 +432,45 @@ FMRadioService::Enable(double aFrequency
       TransitionState(ErrorResponse(
         NS_LITERAL_STRING("Can't create settings lock")), Disabled);
       return;
     }
 
     nsRefPtr<ReadRilSettingTask> callback =
       new ReadRilSettingTask(mPendingRequest);
 
-    rv = settingsLock->Get(SETTING_KEY_RIL_RADIO_DISABLED, callback);
+    rv = settingsLock->Get("ril.radio.disabled", callback);
     if (NS_FAILED(rv)) {
       TransitionState(ErrorResponse(
         NS_LITERAL_STRING("Can't get settings lock")), Disabled);
     }
 
     return;
   }
 
   NS_DispatchToMainThread(new EnableRunnable(mUpperBoundInKHz,
                                              mLowerBoundInKHz,
                                              mChannelWidthInKHz));
 }
 
 void
 FMRadioService::Disable(FMRadioReplyRunnable* aReplyRunnable)
 {
-  // When airplane-mode is enabled, we will call this function from
-  // FMRadioService::Observe without passing a FMRadioReplyRunnable,
-  // so we have to check if |aReplyRunnable| is null before we dispatch it.
   MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
 
   switch (mState) {
     case Disabling:
-      if (aReplyRunnable) {
-        aReplyRunnable->SetReply(
-          ErrorResponse(NS_LITERAL_STRING("FM radio currently disabling")));
-        NS_DispatchToMainThread(aReplyRunnable);
-      }
+      aReplyRunnable->SetReply(
+        ErrorResponse(NS_LITERAL_STRING("FM radio currently disabling")));
+      NS_DispatchToMainThread(aReplyRunnable);
       return;
     case Disabled:
-      if (aReplyRunnable) {
-        aReplyRunnable->SetReply(
-          ErrorResponse(NS_LITERAL_STRING("FM radio currently disabled")));
-        NS_DispatchToMainThread(aReplyRunnable);
-      }
+      aReplyRunnable->SetReply(
+        ErrorResponse(NS_LITERAL_STRING("FM radio currently disabled")));
+      NS_DispatchToMainThread(aReplyRunnable);
       return;
     case Enabled:
     case Enabling:
     case Seeking:
       break;
   }
 
   nsRefPtr<FMRadioReplyRunnable> enablingRequest = mPendingRequest;
@@ -510,20 +494,18 @@ FMRadioService::Disable(FMRadioReplyRunn
       ErrorResponse(NS_LITERAL_STRING("Enable action is cancelled")));
     NS_DispatchToMainThread(enablingRequest);
 
     // If we haven't read the ril settings yet we won't enable the FM radio HW,
     // so fail the disable request immediately.
     if (!mHasReadRilSetting) {
       SetState(Disabled);
 
-      if (aReplyRunnable) {
-        aReplyRunnable->SetReply(SuccessResponse());
-        NS_DispatchToMainThread(aReplyRunnable);
-      }
+      aReplyRunnable->SetReply(SuccessResponse());
+      NS_DispatchToMainThread(aReplyRunnable);
     }
 
     return;
   }
 
   DoDisable();
 }
 
@@ -644,75 +626,16 @@ FMRadioService::CancelSeek(FMRadioReplyR
 
   TransitionState(
     ErrorResponse(NS_LITERAL_STRING("Seek action is cancelled")), Enabled);
 
   aReplyRunnable->SetReply(SuccessResponse());
   NS_DispatchToMainThread(aReplyRunnable);
 }
 
-NS_IMETHODIMP
-FMRadioService::Observe(nsISupports * aSubject,
-                        const char * aTopic,
-                        const char16_t * aData)
-{
-  MOZ_ASSERT(NS_IsMainThread());
-  MOZ_ASSERT(sFMRadioService);
-
-  if (strcmp(aTopic, MOZSETTINGS_CHANGED_ID) != 0) {
-    return NS_OK;
-  }
-
-  // The string that we're interested in will be a JSON string looks like:
-  //  {"key":"ril.radio.disabled","value":true}
-  AutoSafeJSContext cx;
-  const nsDependentString dataStr(aData);
-  JS::Rooted<JS::Value> val(cx);
-  if (!JS_ParseJSON(cx, dataStr.get(), dataStr.Length(), &val) ||
-      !val.isObject()) {
-    return NS_OK;
-  }
-
-  JSObject& obj(val.toObject());
-  JS::Rooted<JS::Value> key(cx);
-  if (!JS_GetProperty(cx, &obj, "key", &key) ||
-      !key.isString()) {
-    return NS_OK;
-  }
-
-  JS::Rooted<JSString*> jsKey(cx, key.toString());
-  nsDependentJSString keyStr;
-  if (!keyStr.init(cx, jsKey)) {
-    return NS_OK;
-  }
-
-  JS::Rooted<JS::Value> value(cx);
-  if (!JS_GetProperty(cx, &obj, "value", &value)) {
-    return NS_OK;
-  }
-
-  if (keyStr.EqualsLiteral(SETTING_KEY_RIL_RADIO_DISABLED)) {
-    if (!value.isBoolean()) {
-      return NS_OK;
-    }
-
-    mRilDisabled = value.toBoolean();
-    mHasReadRilSetting = true;
-
-    // Disable the FM radio HW if Airplane mode is enabled.
-    if (mRilDisabled) {
-      Disable(nullptr);
-    }
-
-    return NS_OK;
-  }
-
-  return NS_OK;
-}
-
 void
 FMRadioService::NotifyFMRadioEvent(FMRadioEventType aType)
 {
   mObserverList.Broadcast(aType);
 }
 
 void
 FMRadioService::Notify(const FMRadioOperationInformation& aInfo)
@@ -804,12 +727,12 @@ FMRadioService::Singleton()
 
   if (!sFMRadioService) {
     sFMRadioService = new FMRadioService();
   }
 
   return sFMRadioService;
 }
 
-NS_IMPL_ISUPPORTS1(FMRadioService, nsIObserver)
+NS_IMPL_ISUPPORTS0(FMRadioService)
 
 END_FMRADIO_NAMESPACE
 
--- a/dom/fmradio/FMRadioService.h
+++ b/dom/fmradio/FMRadioService.h
@@ -8,17 +8,16 @@
 #define mozilla_dom_fmradioservice_h__
 
 #include "mozilla/dom/PFMRadioRequest.h"
 #include "FMRadioCommon.h"
 #include "mozilla/Hal.h"
 #include "mozilla/StaticPtr.h"
 #include "mozilla/Services.h"
 #include "nsThreadUtils.h"
-#include "nsIObserver.h"
 #include "nsXULAppAPI.h"
 
 BEGIN_FMRADIO_NAMESPACE
 
 class FMRadioReplyRunnable : public nsRunnable
 {
 public:
   FMRadioReplyRunnable() : mResponseType(SuccessResponse()) {}
@@ -132,18 +131,18 @@ enum FMRadioState
   Disabled,
   Disabling,
   Enabling,
   Enabled,
   Seeking
 };
 
 class FMRadioService MOZ_FINAL : public IFMRadioService
+                               , public nsISupports
                                , public hal::FMRadioObserver
-                               , public nsIObserver
 {
   friend class ReadRilSettingTask;
   friend class SetFrequencyRunnable;
 
 public:
   static FMRadioService* Singleton();
   virtual ~FMRadioService();
 
@@ -167,18 +166,16 @@ public:
   virtual void AddObserver(FMRadioEventObserver* aObserver) MOZ_OVERRIDE;
   virtual void RemoveObserver(FMRadioEventObserver* aObserver) MOZ_OVERRIDE;
 
   virtual void EnableAudio(bool aAudioEnabled) MOZ_OVERRIDE;
 
   /* FMRadioObserver */
   void Notify(const hal::FMRadioOperationInformation& aInfo) MOZ_OVERRIDE;
 
-  NS_DECL_NSIOBSERVER
-
 protected:
   FMRadioService();
 
 private:
   int32_t RoundFrequency(double aFrequencyInMHz);
 
   void NotifyFMRadioEvent(FMRadioEventType aType);
   void DoDisable();
--- a/dom/fmradio/test/marionette/manifest.ini
+++ b/dom/fmradio/test/marionette/manifest.ini
@@ -5,10 +5,9 @@ browser = false
 qemu = false
 
 [test_enable_disable.js]
 [test_set_frequency.js]
 [test_cancel_seek.js]
 [test_one_seek_at_once.js]
 [test_seek_up_and_down.js]
 [test_bug862672.js]
-[test_bug876597.js]
 
deleted file mode 100644
--- a/dom/fmradio/test/marionette/test_bug876597.js
+++ /dev/null
@@ -1,90 +0,0 @@
-/* Any copyright is dedicated to the Public Domain.
- * http://creativecommons.org/publicdomain/zero/1.0/ */
-
-MARIONETTE_TIMEOUT = 10000;
-
-SpecialPowers.addPermission("fmradio", true, document);
-SpecialPowers.addPermission("settings-read", true, document);
-SpecialPowers.addPermission("settings-write", true, document);
-
-let FMRadio = window.navigator.mozFMRadio;
-let mozSettings = window.navigator.mozSettings;
-let KEY = "ril.radio.disabled";
-
-function verifyInitialState() {
-  log("Verifying initial state.");
-  ok(FMRadio);
-  is(FMRadio.enabled, false);
-  ok(mozSettings);
-
-  checkRilSettings();
-}
-
-function checkRilSettings() {
-  log("Checking airplane mode settings");
-  let req = mozSettings.createLock().get(KEY);
-  req.onsuccess = function(event) {
-    ok(!req.result[KEY], "Airplane mode is disabled.");
-    enableFMRadio();
-  };
-
-  req.onerror = function() {
-    ok(false, "Error occurs when reading settings value.");
-    finish();
-  };
-}
-
-function enableFMRadio() {
-  log("Enable FM radio");
-  let frequency = FMRadio.frequencyLowerBound + FMRadio.channelWidth;
-  let req = FMRadio.enable(frequency);
-
-  req.onsuccess = function() {
-    enableAirplaneMode();
-  };
-
-  req.onerror = function() {
-    ok(false, "Failed to enable FM radio.");
-  };
-}
-
-function enableAirplaneMode() {
-  log("Enable airplane mode");
-  FMRadio.ondisabled = function() {
-    FMRadio.ondisabled = null;
-    enableFMRadioWithAirplaneModeEnabled();
-  };
-
-  let settings = {};
-  settings[KEY] = true;
-  mozSettings.createLock().set(settings);
-}
-
-function enableFMRadioWithAirplaneModeEnabled() {
-  log("Enable FM radio with airplane mode enabled");
-  let frequency = FMRadio.frequencyLowerBound + FMRadio.channelWidth;
-  let req = FMRadio.enable(frequency);
-  req.onerror = cleanUp();
-
-  req.onsuccess = function() {
-    ok(false, "FMRadio could be enabled when airplane mode is enabled.");
-  };
-}
-
-function cleanUp() {
-  let settings = {};
-  settings[KEY] = false;
-  let req = mozSettings.createLock().set(settings);
-
-  req.onsuccess = function() {
-    ok(!FMRadio.enabled);
-    finish();
-  };
-
-  req.onerror = function() {
-    ok(false, "Error occurs when setting value");
-  };
-}
-
-verifyInitialState();
-