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.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "SmsMessage.h" |
michael@0 | 7 | #include "SmsService.h" |
michael@0 | 8 | #include "SmsSegmentInfo.h" |
michael@0 | 9 | #include "mozilla/Preferences.h" |
michael@0 | 10 | #include "nsServiceManagerUtils.h" |
michael@0 | 11 | |
michael@0 | 12 | namespace { |
michael@0 | 13 | |
michael@0 | 14 | const char* kPrefRilNumRadioInterfaces = "ril.numRadioInterfaces"; |
michael@0 | 15 | #define kPrefDefaultServiceId "dom.sms.defaultServiceId" |
michael@0 | 16 | const char* kObservedPrefs[] = { |
michael@0 | 17 | kPrefDefaultServiceId, |
michael@0 | 18 | nullptr |
michael@0 | 19 | }; |
michael@0 | 20 | |
michael@0 | 21 | uint32_t |
michael@0 | 22 | getDefaultServiceId() |
michael@0 | 23 | { |
michael@0 | 24 | int32_t id = mozilla::Preferences::GetInt(kPrefDefaultServiceId, 0); |
michael@0 | 25 | int32_t numRil = mozilla::Preferences::GetInt(kPrefRilNumRadioInterfaces, 1); |
michael@0 | 26 | |
michael@0 | 27 | if (id >= numRil || id < 0) { |
michael@0 | 28 | id = 0; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | return id; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | } // Anonymous namespace |
michael@0 | 35 | |
michael@0 | 36 | namespace mozilla { |
michael@0 | 37 | namespace dom { |
michael@0 | 38 | namespace mobilemessage { |
michael@0 | 39 | |
michael@0 | 40 | NS_IMPL_ISUPPORTS(SmsService, |
michael@0 | 41 | nsISmsService, |
michael@0 | 42 | nsIObserver) |
michael@0 | 43 | |
michael@0 | 44 | SmsService::SmsService() |
michael@0 | 45 | { |
michael@0 | 46 | mRil = do_GetService("@mozilla.org/ril;1"); |
michael@0 | 47 | NS_WARN_IF_FALSE(mRil, "This shouldn't fail!"); |
michael@0 | 48 | |
michael@0 | 49 | // Initialize observer. |
michael@0 | 50 | Preferences::AddStrongObservers(this, kObservedPrefs); |
michael@0 | 51 | mDefaultServiceId = getDefaultServiceId(); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | /* |
michael@0 | 55 | * Implementation of nsIObserver. |
michael@0 | 56 | */ |
michael@0 | 57 | |
michael@0 | 58 | NS_IMETHODIMP |
michael@0 | 59 | SmsService::Observe(nsISupports* aSubject, |
michael@0 | 60 | const char* aTopic, |
michael@0 | 61 | const char16_t* aData) |
michael@0 | 62 | { |
michael@0 | 63 | if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) { |
michael@0 | 64 | nsDependentString data(aData); |
michael@0 | 65 | if (data.EqualsLiteral(kPrefDefaultServiceId)) { |
michael@0 | 66 | mDefaultServiceId = getDefaultServiceId(); |
michael@0 | 67 | } |
michael@0 | 68 | return NS_OK; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | MOZ_ASSERT(false, "SmsService got unexpected topic!"); |
michael@0 | 72 | return NS_ERROR_UNEXPECTED; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | /* |
michael@0 | 76 | * Implementation of nsISmsService. |
michael@0 | 77 | */ |
michael@0 | 78 | |
michael@0 | 79 | NS_IMETHODIMP |
michael@0 | 80 | SmsService::GetSmsDefaultServiceId(uint32_t* aServiceId) |
michael@0 | 81 | { |
michael@0 | 82 | *aServiceId = mDefaultServiceId; |
michael@0 | 83 | return NS_OK; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | NS_IMETHODIMP |
michael@0 | 87 | SmsService::GetSegmentInfoForText(const nsAString& aText, |
michael@0 | 88 | nsIMobileMessageCallback* aRequest) |
michael@0 | 89 | { |
michael@0 | 90 | nsCOMPtr<nsIRadioInterface> radioInterface; |
michael@0 | 91 | if (mRil) { |
michael@0 | 92 | mRil->GetRadioInterface(0, getter_AddRefs(radioInterface)); |
michael@0 | 93 | } |
michael@0 | 94 | NS_ENSURE_TRUE(radioInterface, NS_ERROR_FAILURE); |
michael@0 | 95 | |
michael@0 | 96 | return radioInterface->GetSegmentInfoForText(aText, aRequest); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | NS_IMETHODIMP |
michael@0 | 100 | SmsService::Send(uint32_t aServiceId, |
michael@0 | 101 | const nsAString& aNumber, |
michael@0 | 102 | const nsAString& aMessage, |
michael@0 | 103 | bool aSilent, |
michael@0 | 104 | nsIMobileMessageCallback* aRequest) |
michael@0 | 105 | { |
michael@0 | 106 | nsCOMPtr<nsIRadioInterface> radioInterface; |
michael@0 | 107 | if (mRil) { |
michael@0 | 108 | mRil->GetRadioInterface(aServiceId, getter_AddRefs(radioInterface)); |
michael@0 | 109 | } |
michael@0 | 110 | NS_ENSURE_TRUE(radioInterface, NS_ERROR_FAILURE); |
michael@0 | 111 | |
michael@0 | 112 | return radioInterface->SendSMS(aNumber, aMessage, aSilent, aRequest); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | NS_IMETHODIMP |
michael@0 | 116 | SmsService::IsSilentNumber(const nsAString& aNumber, |
michael@0 | 117 | bool* aIsSilent) |
michael@0 | 118 | { |
michael@0 | 119 | *aIsSilent = mSilentNumbers.Contains(aNumber); |
michael@0 | 120 | return NS_OK; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | NS_IMETHODIMP |
michael@0 | 124 | SmsService::AddSilentNumber(const nsAString& aNumber) |
michael@0 | 125 | { |
michael@0 | 126 | if (mSilentNumbers.Contains(aNumber)) { |
michael@0 | 127 | return NS_ERROR_UNEXPECTED; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | NS_ENSURE_TRUE(mSilentNumbers.AppendElement(aNumber), NS_ERROR_FAILURE); |
michael@0 | 131 | return NS_OK; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | NS_IMETHODIMP |
michael@0 | 135 | SmsService::RemoveSilentNumber(const nsAString& aNumber) |
michael@0 | 136 | { |
michael@0 | 137 | if (!mSilentNumbers.Contains(aNumber)) { |
michael@0 | 138 | return NS_ERROR_INVALID_ARG; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | NS_ENSURE_TRUE(mSilentNumbers.RemoveElement(aNumber), NS_ERROR_FAILURE); |
michael@0 | 142 | return NS_OK; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | NS_IMETHODIMP |
michael@0 | 146 | SmsService::GetSmscAddress(uint32_t aServiceId, |
michael@0 | 147 | nsIMobileMessageCallback *aRequest) |
michael@0 | 148 | { |
michael@0 | 149 | nsCOMPtr<nsIRadioInterface> radioInterface; |
michael@0 | 150 | if (mRil) { |
michael@0 | 151 | mRil->GetRadioInterface(aServiceId, getter_AddRefs(radioInterface)); |
michael@0 | 152 | } |
michael@0 | 153 | NS_ENSURE_TRUE(radioInterface, NS_ERROR_FAILURE); |
michael@0 | 154 | |
michael@0 | 155 | return radioInterface->GetSmscAddress(aRequest); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | } // namespace mobilemessage |
michael@0 | 159 | } // namespace dom |
michael@0 | 160 | } // namespace mozilla |