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/Nullable.h"
11 #include "mozilla/dom/PFMRadioRequest.h"
12 #include "FMRadioCommon.h"
13 #include "mozilla/Hal.h"
14 #include "mozilla/Mutex.h"
15 #include "mozilla/StaticPtr.h"
16 #include "mozilla/Services.h"
17 #include "nsThreadUtils.h"
18 #include "nsIObserver.h"
19 #include "nsXULAppAPI.h"
21 BEGIN_FMRADIO_NAMESPACE
23 class FMRadioReplyRunnable
: public nsRunnable
26 FMRadioReplyRunnable() : mResponseType(SuccessResponse()) {}
27 virtual ~FMRadioReplyRunnable() {}
30 SetReply(const FMRadioResponseType
& aResponseType
)
32 mResponseType
= aResponseType
;
36 FMRadioResponseType mResponseType
;
40 * The FMRadio Service Interface for FMRadio.
42 * There are two concrete classes which implement this interface:
44 * It's used in the main process, implements all the logics about FM Radio.
47 * It's used in subprocess. It's a kind of proxy which just sends all
48 * the requests to main process through IPC channel.
50 * All the requests coming from the content page will be redirected to the
51 * concrete class object.
53 * Consider navigator.mozFMRadio.enable(). Here is the call sequence:
56 * (1) Call navigator.mozFMRadio.enable().
57 * (2) Return a DOMRequest object, and call FMRadioChild.Enable() with a
58 * FMRadioReplyRunnable object.
59 * (3) Send IPC message to main process.
61 * (4) Call FMRadioService::Enable() with a FMRadioReplyRunnable object.
62 * (5) Call hal::EnableFMRadio().
63 * (6) Notify FMRadioService object when FM radio HW is enabled.
64 * (7) Dispatch the FMRadioReplyRunnable object created in (4).
65 * (8) Send IPC message back to child process.
67 * (9) Dispatch the FMRadioReplyRunnable object created in (2).
68 * (10) Fire success callback of the DOMRequest Object created in (2).
69 * _ _ _ _ _ _ _ _ _ _ _ _ _ _
72 * Page FMRadio | FMRadioChild IPC | FMRadioService Hal
74 * |----->| (2) | | | | | |
75 * | |--------|--------->| (3) | | | |
76 * | | | |-----------> | | (4) | |
77 * | | | | |--|---------->| (5) |
78 * | | | | | | |--------->|
80 * | | | | | | (7) |<---------|
81 * | | | | (8) |<-|-----------| |
82 * | | (9) | |<----------- | | | |
83 * | (10) |<-------|----------| | | | |
84 * |<-----| | | | | | |
86 * |_ _ _ _ _ _ _ _ _ _ _ _ _ _|
88 * In non-OOP model, we don't need to send messages between processes, so
89 * the call sequences are much more simpler, it almost just follows the
90 * sequences presented in OOP model: (1) (2) (5) (6) (9) and (10).
96 virtual ~IFMRadioService() { }
99 virtual bool IsEnabled() const = 0;
100 virtual bool IsRDSEnabled() const = 0;
101 virtual double GetFrequency() const = 0;
102 virtual double GetFrequencyUpperBound() const = 0;
103 virtual double GetFrequencyLowerBound() const = 0;
104 virtual double GetChannelWidth() const = 0;
105 virtual Nullable
<unsigned short> GetPi() const = 0;
106 virtual Nullable
<uint8_t> GetPty() const = 0;
107 virtual bool GetPs(nsString
& aPsname
) = 0;
108 virtual bool GetRt(nsString
& aRadiotext
) = 0;
109 virtual bool GetRdsgroup(uint64_t& aRDSGroup
) = 0;
111 virtual void Enable(double aFrequency
, FMRadioReplyRunnable
* aReplyRunnable
) = 0;
112 virtual void Disable(FMRadioReplyRunnable
* aReplyRunnable
) = 0;
113 virtual void SetFrequency(double aFrequency
, FMRadioReplyRunnable
* aReplyRunnable
) = 0;
114 virtual void Seek(mozilla::hal::FMRadioSeekDirection aDirection
,
115 FMRadioReplyRunnable
* aReplyRunnable
) = 0;
116 virtual void CancelSeek(FMRadioReplyRunnable
* aReplyRunnable
) = 0;
117 virtual void SetRDSGroupMask(uint32_t aRDSGroupMask
) = 0;
118 virtual void EnableRDS(FMRadioReplyRunnable
* aReplyRunnable
) = 0;
119 virtual void DisableRDS(FMRadioReplyRunnable
* aReplyRunnable
) = 0;
122 * Register handler to receive the FM Radio events, including:
123 * - StateChangedEvent
124 * - FrequencyChangedEvent
126 * Called by FMRadio and FMRadioParent.
128 virtual void AddObserver(FMRadioEventObserver
* aObserver
) = 0;
129 virtual void RemoveObserver(FMRadioEventObserver
* aObserver
) = 0;
131 // Enable/Disable FMRadio
132 virtual void EnableAudio(bool aAudioEnabled
) = 0;
135 * Static method to return the singleton instance. If it's in the child
136 * process, we will get an object of FMRadioChild.
138 static IFMRadioService
* Singleton();
150 class FMRadioService MOZ_FINAL
: public IFMRadioService
151 , public hal::FMRadioObserver
152 , public hal::FMRadioRDSObserver
155 friend class ReadAirplaneModeSettingTask
;
156 friend class EnableRunnable
;
157 friend class DisableRunnable
;
158 friend class NotifyRunnable
;
161 static FMRadioService
* Singleton();
162 virtual ~FMRadioService();
166 virtual bool IsEnabled() const MOZ_OVERRIDE
;
167 virtual bool IsRDSEnabled() const MOZ_OVERRIDE
;
168 virtual double GetFrequency() const MOZ_OVERRIDE
;
169 virtual double GetFrequencyUpperBound() const MOZ_OVERRIDE
;
170 virtual double GetFrequencyLowerBound() const MOZ_OVERRIDE
;
171 virtual double GetChannelWidth() const MOZ_OVERRIDE
;
172 virtual Nullable
<unsigned short> GetPi() const MOZ_OVERRIDE
;
173 virtual Nullable
<uint8_t> GetPty() const MOZ_OVERRIDE
;
174 virtual bool GetPs(nsString
& aPsname
) MOZ_OVERRIDE
;
175 virtual bool GetRt(nsString
& aRadiotext
) MOZ_OVERRIDE
;
176 virtual bool GetRdsgroup(uint64_t& aRDSGroup
) MOZ_OVERRIDE
;
178 virtual void Enable(double aFrequency
,
179 FMRadioReplyRunnable
* aReplyRunnable
) MOZ_OVERRIDE
;
180 virtual void Disable(FMRadioReplyRunnable
* aReplyRunnable
) MOZ_OVERRIDE
;
181 virtual void SetFrequency(double aFrequency
,
182 FMRadioReplyRunnable
* aReplyRunnable
) MOZ_OVERRIDE
;
183 virtual void Seek(mozilla::hal::FMRadioSeekDirection aDirection
,
184 FMRadioReplyRunnable
* aReplyRunnable
) MOZ_OVERRIDE
;
185 virtual void CancelSeek(FMRadioReplyRunnable
* aReplyRunnable
) MOZ_OVERRIDE
;
186 virtual void SetRDSGroupMask(uint32_t aRDSGroupMask
) MOZ_OVERRIDE
;
187 virtual void EnableRDS(FMRadioReplyRunnable
* aReplyRunnable
) MOZ_OVERRIDE
;
188 virtual void DisableRDS(FMRadioReplyRunnable
* aReplyRunnable
) MOZ_OVERRIDE
;
190 virtual void AddObserver(FMRadioEventObserver
* aObserver
) MOZ_OVERRIDE
;
191 virtual void RemoveObserver(FMRadioEventObserver
* aObserver
) MOZ_OVERRIDE
;
193 virtual void EnableAudio(bool aAudioEnabled
) MOZ_OVERRIDE
;
195 /* FMRadioObserver */
196 void Notify(const hal::FMRadioOperationInformation
& aInfo
) MOZ_OVERRIDE
;
197 /* FMRadioRDSObserver */
198 void Notify(const hal::FMRadioRDSGroup
& aRDSGroup
) MOZ_OVERRIDE
;
206 int32_t RoundFrequency(double aFrequencyInMHz
);
208 void NotifyFMRadioEvent(FMRadioEventType aType
);
210 void TransitionState(const FMRadioResponseType
& aResponse
, FMRadioState aState
);
211 void SetState(FMRadioState aState
);
212 void UpdatePowerState();
213 void UpdateFrequency();
218 int32_t mPendingFrequencyInKHz
;
222 bool mHasReadAirplaneModeSetting
;
223 bool mAirplaneModeEnabled
;
226 uint32_t mUpperBoundInKHz
;
227 uint32_t mLowerBoundInKHz
;
228 uint32_t mChannelWidthInKHz
;
229 uint32_t mPreemphasis
;
231 nsCOMPtr
<nsIThread
> mTuneThread
;
232 nsRefPtr
<FMRadioReplyRunnable
> mPendingRequest
;
234 FMRadioEventObserverList mObserverList
;
236 static StaticRefPtr
<FMRadioService
> sFMRadioService
;
238 uint32_t mRDSGroupMask
;
242 Atomic
<uint32_t> mPI
;
243 Atomic
<uint32_t> mPTY
;
245 Atomic
<bool> mPTYSet
;
247 /* Protects mPSName, mRadiotext, and mRDSGroup */
250 char16_t mRadiotext
[65];
253 uint8_t mPSNameState
;
254 uint16_t mRadiotextState
;
255 uint16_t mTempPSName
[8];
256 uint16_t mTempRadiotext
[64];
263 END_FMRADIO_NAMESPACE
265 #endif // mozilla_dom_fmradioservice_h__