Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "SmsMessage.h"
7 #include "SmsService.h"
8 #include "SmsSegmentInfo.h"
9 #include "mozilla/Preferences.h"
10 #include "nsServiceManagerUtils.h"
12 namespace {
14 const char* kPrefRilNumRadioInterfaces = "ril.numRadioInterfaces";
15 #define kPrefDefaultServiceId "dom.sms.defaultServiceId"
16 const char* kObservedPrefs[] = {
17 kPrefDefaultServiceId,
18 nullptr
19 };
21 uint32_t
22 getDefaultServiceId()
23 {
24 int32_t id = mozilla::Preferences::GetInt(kPrefDefaultServiceId, 0);
25 int32_t numRil = mozilla::Preferences::GetInt(kPrefRilNumRadioInterfaces, 1);
27 if (id >= numRil || id < 0) {
28 id = 0;
29 }
31 return id;
32 }
34 } // Anonymous namespace
36 namespace mozilla {
37 namespace dom {
38 namespace mobilemessage {
40 NS_IMPL_ISUPPORTS(SmsService,
41 nsISmsService,
42 nsIObserver)
44 SmsService::SmsService()
45 {
46 mRil = do_GetService("@mozilla.org/ril;1");
47 NS_WARN_IF_FALSE(mRil, "This shouldn't fail!");
49 // Initialize observer.
50 Preferences::AddStrongObservers(this, kObservedPrefs);
51 mDefaultServiceId = getDefaultServiceId();
52 }
54 /*
55 * Implementation of nsIObserver.
56 */
58 NS_IMETHODIMP
59 SmsService::Observe(nsISupports* aSubject,
60 const char* aTopic,
61 const char16_t* aData)
62 {
63 if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
64 nsDependentString data(aData);
65 if (data.EqualsLiteral(kPrefDefaultServiceId)) {
66 mDefaultServiceId = getDefaultServiceId();
67 }
68 return NS_OK;
69 }
71 MOZ_ASSERT(false, "SmsService got unexpected topic!");
72 return NS_ERROR_UNEXPECTED;
73 }
75 /*
76 * Implementation of nsISmsService.
77 */
79 NS_IMETHODIMP
80 SmsService::GetSmsDefaultServiceId(uint32_t* aServiceId)
81 {
82 *aServiceId = mDefaultServiceId;
83 return NS_OK;
84 }
86 NS_IMETHODIMP
87 SmsService::GetSegmentInfoForText(const nsAString& aText,
88 nsIMobileMessageCallback* aRequest)
89 {
90 nsCOMPtr<nsIRadioInterface> radioInterface;
91 if (mRil) {
92 mRil->GetRadioInterface(0, getter_AddRefs(radioInterface));
93 }
94 NS_ENSURE_TRUE(radioInterface, NS_ERROR_FAILURE);
96 return radioInterface->GetSegmentInfoForText(aText, aRequest);
97 }
99 NS_IMETHODIMP
100 SmsService::Send(uint32_t aServiceId,
101 const nsAString& aNumber,
102 const nsAString& aMessage,
103 bool aSilent,
104 nsIMobileMessageCallback* aRequest)
105 {
106 nsCOMPtr<nsIRadioInterface> radioInterface;
107 if (mRil) {
108 mRil->GetRadioInterface(aServiceId, getter_AddRefs(radioInterface));
109 }
110 NS_ENSURE_TRUE(radioInterface, NS_ERROR_FAILURE);
112 return radioInterface->SendSMS(aNumber, aMessage, aSilent, aRequest);
113 }
115 NS_IMETHODIMP
116 SmsService::IsSilentNumber(const nsAString& aNumber,
117 bool* aIsSilent)
118 {
119 *aIsSilent = mSilentNumbers.Contains(aNumber);
120 return NS_OK;
121 }
123 NS_IMETHODIMP
124 SmsService::AddSilentNumber(const nsAString& aNumber)
125 {
126 if (mSilentNumbers.Contains(aNumber)) {
127 return NS_ERROR_UNEXPECTED;
128 }
130 NS_ENSURE_TRUE(mSilentNumbers.AppendElement(aNumber), NS_ERROR_FAILURE);
131 return NS_OK;
132 }
134 NS_IMETHODIMP
135 SmsService::RemoveSilentNumber(const nsAString& aNumber)
136 {
137 if (!mSilentNumbers.Contains(aNumber)) {
138 return NS_ERROR_INVALID_ARG;
139 }
141 NS_ENSURE_TRUE(mSilentNumbers.RemoveElement(aNumber), NS_ERROR_FAILURE);
142 return NS_OK;
143 }
145 NS_IMETHODIMP
146 SmsService::GetSmscAddress(uint32_t aServiceId,
147 nsIMobileMessageCallback *aRequest)
148 {
149 nsCOMPtr<nsIRadioInterface> radioInterface;
150 if (mRil) {
151 mRil->GetRadioInterface(aServiceId, getter_AddRefs(radioInterface));
152 }
153 NS_ENSURE_TRUE(radioInterface, NS_ERROR_FAILURE);
155 return radioInterface->GetSmscAddress(aRequest);
156 }
158 } // namespace mobilemessage
159 } // namespace dom
160 } // namespace mozilla