|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "CellBroadcast.h" |
|
7 #include "mozilla/dom/MozCellBroadcastBinding.h" |
|
8 #include "nsIDOMMozCellBroadcastEvent.h" |
|
9 #include "nsIDOMMozCellBroadcastMessage.h" |
|
10 #include "nsServiceManagerUtils.h" |
|
11 #include "GeneratedEvents.h" |
|
12 |
|
13 #define NS_RILCONTENTHELPER_CONTRACTID "@mozilla.org/ril/content-helper;1" |
|
14 |
|
15 using namespace mozilla::dom; |
|
16 |
|
17 /** |
|
18 * CellBroadcast::Listener Implementation. |
|
19 */ |
|
20 |
|
21 class CellBroadcast::Listener : public nsICellBroadcastListener |
|
22 { |
|
23 private: |
|
24 CellBroadcast* mCellBroadcast; |
|
25 |
|
26 public: |
|
27 NS_DECL_ISUPPORTS |
|
28 NS_FORWARD_SAFE_NSICELLBROADCASTLISTENER(mCellBroadcast) |
|
29 |
|
30 Listener(CellBroadcast* aCellBroadcast) |
|
31 : mCellBroadcast(aCellBroadcast) |
|
32 { |
|
33 MOZ_ASSERT(mCellBroadcast); |
|
34 } |
|
35 |
|
36 void Disconnect() |
|
37 { |
|
38 MOZ_ASSERT(mCellBroadcast); |
|
39 mCellBroadcast = nullptr; |
|
40 } |
|
41 }; |
|
42 |
|
43 NS_IMPL_ISUPPORTS(CellBroadcast::Listener, nsICellBroadcastListener) |
|
44 |
|
45 /** |
|
46 * CellBroadcast Implementation. |
|
47 */ |
|
48 |
|
49 // static |
|
50 already_AddRefed<CellBroadcast> |
|
51 CellBroadcast::Create(nsPIDOMWindow* aWindow, ErrorResult& aRv) |
|
52 { |
|
53 MOZ_ASSERT(aWindow); |
|
54 MOZ_ASSERT(aWindow->IsInnerWindow()); |
|
55 |
|
56 nsCOMPtr<nsICellBroadcastProvider> provider = |
|
57 do_GetService(NS_RILCONTENTHELPER_CONTRACTID); |
|
58 if (!provider) { |
|
59 aRv.Throw(NS_ERROR_UNEXPECTED); |
|
60 return nullptr; |
|
61 } |
|
62 |
|
63 nsRefPtr<CellBroadcast> cb = new CellBroadcast(aWindow, provider); |
|
64 return cb.forget(); |
|
65 } |
|
66 |
|
67 CellBroadcast::CellBroadcast(nsPIDOMWindow *aWindow, |
|
68 nsICellBroadcastProvider *aProvider) |
|
69 : DOMEventTargetHelper(aWindow) |
|
70 , mProvider(aProvider) |
|
71 { |
|
72 mListener = new Listener(this); |
|
73 DebugOnly<nsresult> rv = mProvider->RegisterCellBroadcastMsg(mListener); |
|
74 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), |
|
75 "Failed registering Cell Broadcast callback with provider"); |
|
76 } |
|
77 |
|
78 CellBroadcast::~CellBroadcast() |
|
79 { |
|
80 MOZ_ASSERT(mProvider && mListener); |
|
81 |
|
82 mListener->Disconnect(); |
|
83 mProvider->UnregisterCellBroadcastMsg(mListener); |
|
84 } |
|
85 |
|
86 JSObject* |
|
87 CellBroadcast::WrapObject(JSContext* aCx) |
|
88 { |
|
89 return MozCellBroadcastBinding::Wrap(aCx, this); |
|
90 } |
|
91 |
|
92 // Forwarded nsICellBroadcastListener methods |
|
93 |
|
94 NS_IMETHODIMP |
|
95 CellBroadcast::NotifyMessageReceived(nsIDOMMozCellBroadcastMessage* aMessage) |
|
96 { |
|
97 nsCOMPtr<nsIDOMEvent> event; |
|
98 NS_NewDOMMozCellBroadcastEvent(getter_AddRefs(event), this, nullptr, nullptr); |
|
99 |
|
100 nsCOMPtr<nsIDOMMozCellBroadcastEvent> ce = do_QueryInterface(event); |
|
101 nsresult rv = ce->InitMozCellBroadcastEvent(NS_LITERAL_STRING("received"), |
|
102 true, false, aMessage); |
|
103 NS_ENSURE_SUCCESS(rv, rv); |
|
104 |
|
105 return DispatchTrustedEvent(ce); |
|
106 } |