dom/fmradio/FMRadioService.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
     2 /* vim: set ts=2 et sw=2 tw=80: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef mozilla_dom_fmradioservice_h__
     8 #define mozilla_dom_fmradioservice_h__
    10 #include "mozilla/dom/PFMRadioRequest.h"
    11 #include "FMRadioCommon.h"
    12 #include "mozilla/Hal.h"
    13 #include "mozilla/StaticPtr.h"
    14 #include "mozilla/Services.h"
    15 #include "nsThreadUtils.h"
    16 #include "nsIObserver.h"
    17 #include "nsXULAppAPI.h"
    19 BEGIN_FMRADIO_NAMESPACE
    21 class FMRadioReplyRunnable : public nsRunnable
    22 {
    23 public:
    24   FMRadioReplyRunnable() : mResponseType(SuccessResponse()) {}
    25   virtual ~FMRadioReplyRunnable() {}
    27   void
    28   SetReply(const FMRadioResponseType& aResponseType)
    29   {
    30     mResponseType = aResponseType;
    31   }
    33 protected:
    34   FMRadioResponseType mResponseType;
    35 };
    37 /**
    38  * The FMRadio Service Interface for FMRadio.
    39  *
    40  * There are two concrete classes which implement this interface:
    41  *  - FMRadioService
    42  *    It's used in the main process, implements all the logics about FM Radio.
    43  *
    44  *  - FMRadioChild
    45  *    It's used in subprocess. It's a kind of proxy which just sends all
    46  *    the requests to main process through IPC channel.
    47  *
    48  * All the requests coming from the content page will be redirected to the
    49  * concrete class object.
    50  *
    51  * Consider navigator.mozFMRadio.enable(). Here is the call sequence:
    52  *  - OOP
    53  *    Child:
    54  *      (1) Call navigator.mozFMRadio.enable().
    55  *      (2) Return a DOMRequest object, and call FMRadioChild.Enable() with a
    56  *          FMRadioReplyRunnable object.
    57  *      (3) Send IPC message to main process.
    58  *    Parent:
    59  *      (4) Call FMRadioService::Enable() with a FMRadioReplyRunnable object.
    60  *      (5) Call hal::EnableFMRadio().
    61  *      (6) Notify FMRadioService object when FM radio HW is enabled.
    62  *      (7) Dispatch the FMRadioReplyRunnable object created in (4).
    63  *      (8) Send IPC message back to child process.
    64  *    Child:
    65  *      (9) Dispatch the FMRadioReplyRunnable object created in (2).
    66  *     (10) Fire success callback of the DOMRequest Object created in (2).
    67  *                     _ _ _ _ _ _ _ _ _ _ _ _ _ _
    68  *                    |            OOP            |
    69  *                    |                           |
    70  *   Page  FMRadio    |    FMRadioChild       IPC |    FMRadioService   Hal
    71  *    | (1)  |        |          |             |  |           |          |
    72  *    |----->|    (2) |          |             |  |           |          |
    73  *    |      |--------|--------->|      (3)    |  |           |          |
    74  *    |      |        |          |-----------> |  |   (4)     |          |
    75  *    |      |        |          |             |--|---------->|  (5)     |
    76  *    |      |        |          |             |  |           |--------->|
    77  *    |      |        |          |             |  |           |  (6)     |
    78  *    |      |        |          |             |  |   (7)     |<---------|
    79  *    |      |        |          |      (8)    |<-|-----------|          |
    80  *    |      |    (9) |          |<----------- |  |           |          |
    81  *    | (10) |<-------|----------|             |  |           |          |
    82  *    |<-----|        |          |             |  |           |          |
    83  *                    |                           |
    84  *                    |_ _ _ _ _ _ _ _ _ _ _ _ _ _|
    85  *  - non-OOP
    86  *    In non-OOP model, we don't need to send messages between processes, so
    87  *    the call sequences are much more simpler, it almost just follows the
    88  *    sequences presented in OOP model: (1) (2) (5) (6) (9) and (10).
    89  *
    90  */
    91 class IFMRadioService
    92 {
    93 protected:
    94   virtual ~IFMRadioService() { }
    96 public:
    97   virtual bool IsEnabled() const = 0;
    98   virtual double GetFrequency() const = 0;
    99   virtual double GetFrequencyUpperBound() const = 0;
   100   virtual double GetFrequencyLowerBound() const = 0;
   101   virtual double GetChannelWidth() const = 0;
   103   virtual void Enable(double aFrequency, FMRadioReplyRunnable* aReplyRunnable) = 0;
   104   virtual void Disable(FMRadioReplyRunnable* aReplyRunnable) = 0;
   105   virtual void SetFrequency(double aFrequency, FMRadioReplyRunnable* aReplyRunnable) = 0;
   106   virtual void Seek(mozilla::hal::FMRadioSeekDirection aDirection,
   107                     FMRadioReplyRunnable* aReplyRunnable) = 0;
   108   virtual void CancelSeek(FMRadioReplyRunnable* aReplyRunnable) = 0;
   110   /**
   111    * Register handler to receive the FM Radio events, including:
   112    *   - StateChangedEvent
   113    *   - FrequencyChangedEvent
   114    *
   115    * Called by FMRadio and FMRadioParent.
   116    */
   117   virtual void AddObserver(FMRadioEventObserver* aObserver) = 0;
   118   virtual void RemoveObserver(FMRadioEventObserver* aObserver) = 0;
   120   // Enable/Disable FMRadio
   121   virtual void EnableAudio(bool aAudioEnabled) = 0;
   123   /**
   124    * Static method to return the singleton instance. If it's in the child
   125    * process, we will get an object of FMRadioChild.
   126    */
   127   static IFMRadioService* Singleton();
   128 };
   130 enum FMRadioState
   131 {
   132   Disabled,
   133   Disabling,
   134   Enabling,
   135   Enabled,
   136   Seeking
   137 };
   139 class FMRadioService MOZ_FINAL : public IFMRadioService
   140                                , public hal::FMRadioObserver
   141                                , public nsIObserver
   142 {
   143   friend class ReadAirplaneModeSettingTask;
   144   friend class SetFrequencyRunnable;
   146 public:
   147   static FMRadioService* Singleton();
   148   virtual ~FMRadioService();
   150   NS_DECL_ISUPPORTS
   152   virtual bool IsEnabled() const MOZ_OVERRIDE;
   153   virtual double GetFrequency() const MOZ_OVERRIDE;
   154   virtual double GetFrequencyUpperBound() const MOZ_OVERRIDE;
   155   virtual double GetFrequencyLowerBound() const MOZ_OVERRIDE;
   156   virtual double GetChannelWidth() const MOZ_OVERRIDE;
   158   virtual void Enable(double aFrequency,
   159                       FMRadioReplyRunnable* aReplyRunnable) MOZ_OVERRIDE;
   160   virtual void Disable(FMRadioReplyRunnable* aReplyRunnable) MOZ_OVERRIDE;
   161   virtual void SetFrequency(double aFrequency,
   162                             FMRadioReplyRunnable* aReplyRunnable) MOZ_OVERRIDE;
   163   virtual void Seek(mozilla::hal::FMRadioSeekDirection aDirection,
   164                     FMRadioReplyRunnable* aReplyRunnable) MOZ_OVERRIDE;
   165   virtual void CancelSeek(FMRadioReplyRunnable* aReplyRunnable) MOZ_OVERRIDE;
   167   virtual void AddObserver(FMRadioEventObserver* aObserver) MOZ_OVERRIDE;
   168   virtual void RemoveObserver(FMRadioEventObserver* aObserver) MOZ_OVERRIDE;
   170   virtual void EnableAudio(bool aAudioEnabled) MOZ_OVERRIDE;
   172   /* FMRadioObserver */
   173   void Notify(const hal::FMRadioOperationInformation& aInfo) MOZ_OVERRIDE;
   175   NS_DECL_NSIOBSERVER
   177 protected:
   178   FMRadioService();
   180 private:
   181   int32_t RoundFrequency(double aFrequencyInMHz);
   183   void NotifyFMRadioEvent(FMRadioEventType aType);
   184   void DoDisable();
   185   void TransitionState(const FMRadioResponseType& aResponse, FMRadioState aState);
   186   void SetState(FMRadioState aState);
   187   void UpdatePowerState();
   188   void UpdateFrequency();
   190 private:
   191   bool mEnabled;
   193   int32_t mPendingFrequencyInKHz;
   195   FMRadioState mState;
   197   bool mHasReadAirplaneModeSetting;
   198   bool mAirplaneModeEnabled;
   200   double mUpperBoundInKHz;
   201   double mLowerBoundInKHz;
   202   double mChannelWidthInKHz;
   204   nsRefPtr<FMRadioReplyRunnable> mPendingRequest;
   206   FMRadioEventObserverList mObserverList;
   208   static StaticRefPtr<FMRadioService> sFMRadioService;
   209 };
   211 END_FMRADIO_NAMESPACE
   213 #endif // mozilla_dom_fmradioservice_h__

mercurial