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.

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

mercurial