Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "Voicemail.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/dom/MozVoicemailBinding.h" |
michael@0 | 10 | #include "nsIDOMMozVoicemailStatus.h" |
michael@0 | 11 | #include "nsIDOMMozVoicemailEvent.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "mozilla/Preferences.h" |
michael@0 | 14 | #include "mozilla/Services.h" |
michael@0 | 15 | #include "nsDOMClassInfo.h" |
michael@0 | 16 | #include "nsServiceManagerUtils.h" |
michael@0 | 17 | #include "GeneratedEvents.h" |
michael@0 | 18 | |
michael@0 | 19 | #define NS_RILCONTENTHELPER_CONTRACTID "@mozilla.org/ril/content-helper;1" |
michael@0 | 20 | const char* kPrefRilNumRadioInterfaces = "ril.numRadioInterfaces"; |
michael@0 | 21 | |
michael@0 | 22 | using namespace mozilla::dom; |
michael@0 | 23 | |
michael@0 | 24 | class Voicemail::Listener : public nsIVoicemailListener |
michael@0 | 25 | { |
michael@0 | 26 | Voicemail* mVoicemail; |
michael@0 | 27 | |
michael@0 | 28 | public: |
michael@0 | 29 | NS_DECL_ISUPPORTS |
michael@0 | 30 | NS_FORWARD_SAFE_NSIVOICEMAILLISTENER(mVoicemail) |
michael@0 | 31 | |
michael@0 | 32 | Listener(Voicemail* aVoicemail) |
michael@0 | 33 | : mVoicemail(aVoicemail) |
michael@0 | 34 | { |
michael@0 | 35 | MOZ_ASSERT(mVoicemail); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | void Disconnect() |
michael@0 | 39 | { |
michael@0 | 40 | MOZ_ASSERT(mVoicemail); |
michael@0 | 41 | mVoicemail = nullptr; |
michael@0 | 42 | } |
michael@0 | 43 | }; |
michael@0 | 44 | |
michael@0 | 45 | NS_IMPL_ISUPPORTS(Voicemail::Listener, nsIVoicemailListener) |
michael@0 | 46 | |
michael@0 | 47 | Voicemail::Voicemail(nsPIDOMWindow* aWindow, |
michael@0 | 48 | nsIVoicemailProvider* aProvider) |
michael@0 | 49 | : DOMEventTargetHelper(aWindow) |
michael@0 | 50 | , mProvider(aProvider) |
michael@0 | 51 | { |
michael@0 | 52 | mListener = new Listener(this); |
michael@0 | 53 | DebugOnly<nsresult> rv = mProvider->RegisterVoicemailMsg(mListener); |
michael@0 | 54 | NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), |
michael@0 | 55 | "Failed registering voicemail messages with provider"); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | Voicemail::~Voicemail() |
michael@0 | 59 | { |
michael@0 | 60 | MOZ_ASSERT(mProvider && mListener); |
michael@0 | 61 | |
michael@0 | 62 | mListener->Disconnect(); |
michael@0 | 63 | mProvider->UnregisterVoicemailMsg(mListener); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | JSObject* |
michael@0 | 67 | Voicemail::WrapObject(JSContext* aCx) |
michael@0 | 68 | { |
michael@0 | 69 | return MozVoicemailBinding::Wrap(aCx, this); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | bool |
michael@0 | 73 | Voicemail::IsValidServiceId(uint32_t aServiceId) const |
michael@0 | 74 | { |
michael@0 | 75 | uint32_t numClients = mozilla::Preferences::GetUint(kPrefRilNumRadioInterfaces, 1); |
michael@0 | 76 | |
michael@0 | 77 | return aServiceId < numClients; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | bool |
michael@0 | 81 | Voicemail::PassedOrDefaultServiceId(const Optional<uint32_t>& aServiceId, |
michael@0 | 82 | uint32_t& aResult) const |
michael@0 | 83 | { |
michael@0 | 84 | if (aServiceId.WasPassed()) { |
michael@0 | 85 | if (!IsValidServiceId(aServiceId.Value())) { |
michael@0 | 86 | return false; |
michael@0 | 87 | } |
michael@0 | 88 | aResult = aServiceId.Value(); |
michael@0 | 89 | } else { |
michael@0 | 90 | mProvider->GetVoicemailDefaultServiceId(&aResult); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | return true; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | // MozVoicemail WebIDL |
michael@0 | 97 | |
michael@0 | 98 | already_AddRefed<nsIDOMMozVoicemailStatus> |
michael@0 | 99 | Voicemail::GetStatus(const Optional<uint32_t>& aServiceId, |
michael@0 | 100 | ErrorResult& aRv) const |
michael@0 | 101 | { |
michael@0 | 102 | if (!mProvider) { |
michael@0 | 103 | aRv.Throw(NS_ERROR_UNEXPECTED); |
michael@0 | 104 | return nullptr; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | uint32_t id = 0; |
michael@0 | 108 | if (!PassedOrDefaultServiceId(aServiceId, id)) { |
michael@0 | 109 | aRv.Throw(NS_ERROR_INVALID_ARG); |
michael@0 | 110 | return nullptr; |
michael@0 | 111 | } |
michael@0 | 112 | nsCOMPtr<nsIDOMMozVoicemailStatus> status; |
michael@0 | 113 | nsresult rv = mProvider->GetVoicemailStatus(id, getter_AddRefs(status)); |
michael@0 | 114 | if (NS_FAILED(rv)) { |
michael@0 | 115 | aRv.Throw(rv); |
michael@0 | 116 | return nullptr; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | return status.forget(); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | void |
michael@0 | 123 | Voicemail::GetNumber(const Optional<uint32_t>& aServiceId, nsString& aNumber, |
michael@0 | 124 | ErrorResult& aRv) const |
michael@0 | 125 | { |
michael@0 | 126 | aNumber.SetIsVoid(true); |
michael@0 | 127 | |
michael@0 | 128 | if (!mProvider) { |
michael@0 | 129 | aRv.Throw(NS_ERROR_UNEXPECTED); |
michael@0 | 130 | return; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | uint32_t id = 0; |
michael@0 | 134 | if (!PassedOrDefaultServiceId(aServiceId, id)) { |
michael@0 | 135 | aRv.Throw(NS_ERROR_INVALID_ARG); |
michael@0 | 136 | return; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | aRv = mProvider->GetVoicemailNumber(id, aNumber); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | void |
michael@0 | 143 | Voicemail::GetDisplayName(const Optional<uint32_t>& aServiceId, nsString& aDisplayName, |
michael@0 | 144 | ErrorResult& aRv) const |
michael@0 | 145 | { |
michael@0 | 146 | aDisplayName.SetIsVoid(true); |
michael@0 | 147 | |
michael@0 | 148 | if (!mProvider) { |
michael@0 | 149 | aRv.Throw(NS_ERROR_UNEXPECTED); |
michael@0 | 150 | return; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | uint32_t id = 0; |
michael@0 | 154 | if (!PassedOrDefaultServiceId(aServiceId, id)) { |
michael@0 | 155 | aRv.Throw(NS_ERROR_INVALID_ARG); |
michael@0 | 156 | return; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | aRv = mProvider->GetVoicemailDisplayName(id, aDisplayName); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | // nsIVoicemailListener |
michael@0 | 163 | |
michael@0 | 164 | NS_IMETHODIMP |
michael@0 | 165 | Voicemail::NotifyStatusChanged(nsIDOMMozVoicemailStatus* aStatus) |
michael@0 | 166 | { |
michael@0 | 167 | nsCOMPtr<nsIDOMEvent> event; |
michael@0 | 168 | NS_NewDOMMozVoicemailEvent(getter_AddRefs(event), this, nullptr, nullptr); |
michael@0 | 169 | |
michael@0 | 170 | nsCOMPtr<nsIDOMMozVoicemailEvent> ce = do_QueryInterface(event); |
michael@0 | 171 | nsresult rv = ce->InitMozVoicemailEvent(NS_LITERAL_STRING("statuschanged"), |
michael@0 | 172 | false, false, aStatus); |
michael@0 | 173 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 174 | |
michael@0 | 175 | return DispatchTrustedEvent(ce); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | nsresult |
michael@0 | 179 | NS_NewVoicemail(nsPIDOMWindow* aWindow, Voicemail** aVoicemail) |
michael@0 | 180 | { |
michael@0 | 181 | nsPIDOMWindow* innerWindow = aWindow->IsInnerWindow() ? |
michael@0 | 182 | aWindow : |
michael@0 | 183 | aWindow->GetCurrentInnerWindow(); |
michael@0 | 184 | |
michael@0 | 185 | nsCOMPtr<nsIVoicemailProvider> provider = |
michael@0 | 186 | do_GetService(NS_RILCONTENTHELPER_CONTRACTID); |
michael@0 | 187 | NS_ENSURE_STATE(provider); |
michael@0 | 188 | |
michael@0 | 189 | nsRefPtr<Voicemail> voicemail = new Voicemail(innerWindow, provider); |
michael@0 | 190 | voicemail.forget(aVoicemail); |
michael@0 | 191 | return NS_OK; |
michael@0 | 192 | } |