dom/mobilemessage/src/gonk/SmsService.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/mobilemessage/src/gonk/SmsService.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,160 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "SmsMessage.h"
    1.10 +#include "SmsService.h"
    1.11 +#include "SmsSegmentInfo.h"
    1.12 +#include "mozilla/Preferences.h"
    1.13 +#include "nsServiceManagerUtils.h"
    1.14 +
    1.15 +namespace {
    1.16 +
    1.17 +const char* kPrefRilNumRadioInterfaces = "ril.numRadioInterfaces";
    1.18 +#define kPrefDefaultServiceId "dom.sms.defaultServiceId"
    1.19 +const char* kObservedPrefs[] = {
    1.20 +  kPrefDefaultServiceId,
    1.21 +  nullptr
    1.22 +};
    1.23 +
    1.24 +uint32_t
    1.25 +getDefaultServiceId()
    1.26 +{
    1.27 +  int32_t id = mozilla::Preferences::GetInt(kPrefDefaultServiceId, 0);
    1.28 +  int32_t numRil = mozilla::Preferences::GetInt(kPrefRilNumRadioInterfaces, 1);
    1.29 +
    1.30 +  if (id >= numRil || id < 0) {
    1.31 +    id = 0;
    1.32 +  }
    1.33 +
    1.34 +  return id;
    1.35 +}
    1.36 +
    1.37 +} // Anonymous namespace
    1.38 +
    1.39 +namespace mozilla {
    1.40 +namespace dom {
    1.41 +namespace mobilemessage {
    1.42 +
    1.43 +NS_IMPL_ISUPPORTS(SmsService,
    1.44 +                  nsISmsService,
    1.45 +                  nsIObserver)
    1.46 +
    1.47 +SmsService::SmsService()
    1.48 +{
    1.49 +  mRil = do_GetService("@mozilla.org/ril;1");
    1.50 +  NS_WARN_IF_FALSE(mRil, "This shouldn't fail!");
    1.51 +
    1.52 +  // Initialize observer.
    1.53 +  Preferences::AddStrongObservers(this, kObservedPrefs);
    1.54 +  mDefaultServiceId = getDefaultServiceId();
    1.55 +}
    1.56 +
    1.57 +/*
    1.58 + * Implementation of nsIObserver.
    1.59 + */
    1.60 +
    1.61 +NS_IMETHODIMP
    1.62 +SmsService::Observe(nsISupports* aSubject,
    1.63 +                    const char* aTopic,
    1.64 +                    const char16_t* aData)
    1.65 +{
    1.66 +  if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
    1.67 +    nsDependentString data(aData);
    1.68 +    if (data.EqualsLiteral(kPrefDefaultServiceId)) {
    1.69 +      mDefaultServiceId = getDefaultServiceId();
    1.70 +    }
    1.71 +    return NS_OK;
    1.72 +  }
    1.73 +
    1.74 +  MOZ_ASSERT(false, "SmsService got unexpected topic!");
    1.75 +  return NS_ERROR_UNEXPECTED;
    1.76 +}
    1.77 +
    1.78 +/*
    1.79 + * Implementation of nsISmsService.
    1.80 + */
    1.81 +
    1.82 +NS_IMETHODIMP
    1.83 +SmsService::GetSmsDefaultServiceId(uint32_t* aServiceId)
    1.84 +{
    1.85 +  *aServiceId = mDefaultServiceId;
    1.86 +  return NS_OK;
    1.87 +}
    1.88 +
    1.89 +NS_IMETHODIMP
    1.90 +SmsService::GetSegmentInfoForText(const nsAString& aText,
    1.91 +                                  nsIMobileMessageCallback* aRequest)
    1.92 +{
    1.93 +  nsCOMPtr<nsIRadioInterface> radioInterface;
    1.94 +  if (mRil) {
    1.95 +    mRil->GetRadioInterface(0, getter_AddRefs(radioInterface));
    1.96 +  }
    1.97 +  NS_ENSURE_TRUE(radioInterface, NS_ERROR_FAILURE);
    1.98 +
    1.99 +  return radioInterface->GetSegmentInfoForText(aText, aRequest);
   1.100 +}
   1.101 +
   1.102 +NS_IMETHODIMP
   1.103 +SmsService::Send(uint32_t aServiceId,
   1.104 +                 const nsAString& aNumber,
   1.105 +                 const nsAString& aMessage,
   1.106 +                 bool aSilent,
   1.107 +                 nsIMobileMessageCallback* aRequest)
   1.108 +{
   1.109 +  nsCOMPtr<nsIRadioInterface> radioInterface;
   1.110 +  if (mRil) {
   1.111 +    mRil->GetRadioInterface(aServiceId, getter_AddRefs(radioInterface));
   1.112 +  }
   1.113 +  NS_ENSURE_TRUE(radioInterface, NS_ERROR_FAILURE);
   1.114 +
   1.115 +  return radioInterface->SendSMS(aNumber, aMessage, aSilent, aRequest);
   1.116 +}
   1.117 +
   1.118 +NS_IMETHODIMP
   1.119 +SmsService::IsSilentNumber(const nsAString& aNumber,
   1.120 +                           bool*            aIsSilent)
   1.121 +{
   1.122 +  *aIsSilent = mSilentNumbers.Contains(aNumber);
   1.123 +  return NS_OK;
   1.124 +}
   1.125 +
   1.126 +NS_IMETHODIMP
   1.127 +SmsService::AddSilentNumber(const nsAString& aNumber)
   1.128 +{
   1.129 +  if (mSilentNumbers.Contains(aNumber)) {
   1.130 +    return NS_ERROR_UNEXPECTED;
   1.131 +  }
   1.132 +
   1.133 +  NS_ENSURE_TRUE(mSilentNumbers.AppendElement(aNumber), NS_ERROR_FAILURE);
   1.134 +  return NS_OK;
   1.135 +}
   1.136 +
   1.137 +NS_IMETHODIMP
   1.138 +SmsService::RemoveSilentNumber(const nsAString& aNumber)
   1.139 +{
   1.140 +  if (!mSilentNumbers.Contains(aNumber)) {
   1.141 +    return NS_ERROR_INVALID_ARG;
   1.142 +  }
   1.143 +
   1.144 +  NS_ENSURE_TRUE(mSilentNumbers.RemoveElement(aNumber), NS_ERROR_FAILURE);
   1.145 +  return NS_OK;
   1.146 +}
   1.147 +
   1.148 +NS_IMETHODIMP
   1.149 +SmsService::GetSmscAddress(uint32_t aServiceId,
   1.150 +                           nsIMobileMessageCallback *aRequest)
   1.151 +{
   1.152 +  nsCOMPtr<nsIRadioInterface> radioInterface;
   1.153 +  if (mRil) {
   1.154 +    mRil->GetRadioInterface(aServiceId, getter_AddRefs(radioInterface));
   1.155 +  }
   1.156 +  NS_ENSURE_TRUE(radioInterface, NS_ERROR_FAILURE);
   1.157 +
   1.158 +  return radioInterface->GetSmscAddress(aRequest);
   1.159 +}
   1.160 +
   1.161 +} // namespace mobilemessage
   1.162 +} // namespace dom
   1.163 +} // namespace mozilla

mercurial