Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "nsVolumeMountLock.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "mozilla/dom/ContentChild.h" |
michael@0 | 8 | #include "mozilla/Services.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "nsIObserverService.h" |
michael@0 | 11 | #include "nsIPowerManagerService.h" |
michael@0 | 12 | #include "nsIVolume.h" |
michael@0 | 13 | #include "nsIVolumeService.h" |
michael@0 | 14 | #include "nsString.h" |
michael@0 | 15 | #include "nsXULAppAPI.h" |
michael@0 | 16 | |
michael@0 | 17 | #define VOLUME_MANAGER_LOG_TAG "nsVolumeMountLock" |
michael@0 | 18 | #include "VolumeManagerLog.h" |
michael@0 | 19 | #include "nsServiceManagerUtils.h" |
michael@0 | 20 | #include "mozilla/dom/power/PowerManagerService.h" |
michael@0 | 21 | |
michael@0 | 22 | using namespace mozilla::dom; |
michael@0 | 23 | using namespace mozilla::services; |
michael@0 | 24 | |
michael@0 | 25 | namespace mozilla { |
michael@0 | 26 | namespace system { |
michael@0 | 27 | |
michael@0 | 28 | NS_IMPL_ISUPPORTS(nsVolumeMountLock, nsIVolumeMountLock, |
michael@0 | 29 | nsIObserver, nsISupportsWeakReference) |
michael@0 | 30 | |
michael@0 | 31 | // static |
michael@0 | 32 | already_AddRefed<nsVolumeMountLock> |
michael@0 | 33 | nsVolumeMountLock::Create(const nsAString& aVolumeName) |
michael@0 | 34 | { |
michael@0 | 35 | DBG("nsVolumeMountLock::Create called"); |
michael@0 | 36 | |
michael@0 | 37 | nsRefPtr<nsVolumeMountLock> mountLock = new nsVolumeMountLock(aVolumeName); |
michael@0 | 38 | nsresult rv = mountLock->Init(); |
michael@0 | 39 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 40 | |
michael@0 | 41 | return mountLock.forget(); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | nsVolumeMountLock::nsVolumeMountLock(const nsAString& aVolumeName) |
michael@0 | 45 | : mVolumeName(aVolumeName), |
michael@0 | 46 | mVolumeGeneration(-1), |
michael@0 | 47 | mUnlocked(false) |
michael@0 | 48 | { |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | //virtual |
michael@0 | 52 | nsVolumeMountLock::~nsVolumeMountLock() |
michael@0 | 53 | { |
michael@0 | 54 | Unlock(); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | nsresult nsVolumeMountLock::Init() |
michael@0 | 58 | { |
michael@0 | 59 | LOG("nsVolumeMountLock created for '%s'", |
michael@0 | 60 | NS_LossyConvertUTF16toASCII(mVolumeName).get()); |
michael@0 | 61 | |
michael@0 | 62 | // Add ourselves as an Observer. It's important that we use a weak |
michael@0 | 63 | // reference here. If we used a strong reference, then that reference |
michael@0 | 64 | // would prevent this object from being destructed. |
michael@0 | 65 | nsCOMPtr<nsIObserverService> obs = GetObserverService(); |
michael@0 | 66 | obs->AddObserver(this, NS_VOLUME_STATE_CHANGED, true /*weak*/); |
michael@0 | 67 | |
michael@0 | 68 | // Request the sdcard info, so we know the state/generation without having |
michael@0 | 69 | // to wait for a state change. |
michael@0 | 70 | if (XRE_GetProcessType() == GeckoProcessType_Content) { |
michael@0 | 71 | ContentChild::GetSingleton()->SendBroadcastVolume(mVolumeName); |
michael@0 | 72 | return NS_OK; |
michael@0 | 73 | } |
michael@0 | 74 | nsCOMPtr<nsIVolumeService> vs = do_GetService(NS_VOLUMESERVICE_CONTRACTID); |
michael@0 | 75 | NS_ENSURE_TRUE(vs, NS_ERROR_FAILURE); |
michael@0 | 76 | |
michael@0 | 77 | vs->BroadcastVolume(mVolumeName); |
michael@0 | 78 | |
michael@0 | 79 | return NS_OK; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | /* void unlock (); */ |
michael@0 | 83 | NS_IMETHODIMP nsVolumeMountLock::Unlock() |
michael@0 | 84 | { |
michael@0 | 85 | LOG("nsVolumeMountLock released for '%s'", |
michael@0 | 86 | NS_LossyConvertUTF16toASCII(mVolumeName).get()); |
michael@0 | 87 | |
michael@0 | 88 | mUnlocked = true; |
michael@0 | 89 | mWakeLock = nullptr; |
michael@0 | 90 | |
michael@0 | 91 | // While we don't really need to remove weak observers, we do so anyways |
michael@0 | 92 | // since it will reduce the number of times Observe gets called. |
michael@0 | 93 | nsCOMPtr<nsIObserverService> obs = GetObserverService(); |
michael@0 | 94 | obs->RemoveObserver(this, NS_VOLUME_STATE_CHANGED); |
michael@0 | 95 | return NS_OK; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | NS_IMETHODIMP nsVolumeMountLock::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData) |
michael@0 | 99 | { |
michael@0 | 100 | if (strcmp(aTopic, NS_VOLUME_STATE_CHANGED) != 0) { |
michael@0 | 101 | return NS_OK; |
michael@0 | 102 | } |
michael@0 | 103 | if (mUnlocked) { |
michael@0 | 104 | // We're not locked anymore, so we don't need to look at the notifications. |
michael@0 | 105 | return NS_OK; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | nsCOMPtr<nsIVolume> vol = do_QueryInterface(aSubject); |
michael@0 | 109 | if (!vol) { |
michael@0 | 110 | return NS_OK; |
michael@0 | 111 | } |
michael@0 | 112 | nsString volName; |
michael@0 | 113 | vol->GetName(volName); |
michael@0 | 114 | if (!volName.Equals(mVolumeName)) { |
michael@0 | 115 | return NS_OK; |
michael@0 | 116 | } |
michael@0 | 117 | int32_t state; |
michael@0 | 118 | nsresult rv = vol->GetState(&state); |
michael@0 | 119 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 120 | |
michael@0 | 121 | if (state != nsIVolume::STATE_MOUNTED) { |
michael@0 | 122 | mWakeLock = nullptr; |
michael@0 | 123 | mVolumeGeneration = -1; |
michael@0 | 124 | return NS_OK; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | int32_t mountGeneration; |
michael@0 | 128 | rv = vol->GetMountGeneration(&mountGeneration); |
michael@0 | 129 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 130 | |
michael@0 | 131 | DBG("nsVolumeMountLock::Observe mountGeneration = %d mVolumeGeneration = %d", |
michael@0 | 132 | mountGeneration, mVolumeGeneration); |
michael@0 | 133 | |
michael@0 | 134 | if (mVolumeGeneration == mountGeneration) { |
michael@0 | 135 | return NS_OK; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | // The generation changed, which means that any wakelock we may have |
michael@0 | 139 | // been holding is now invalid. Grab a new wakelock for the new generation |
michael@0 | 140 | // number. |
michael@0 | 141 | |
michael@0 | 142 | mWakeLock = nullptr; |
michael@0 | 143 | mVolumeGeneration = mountGeneration; |
michael@0 | 144 | |
michael@0 | 145 | nsRefPtr<power::PowerManagerService> pmService = |
michael@0 | 146 | power::PowerManagerService::GetInstance(); |
michael@0 | 147 | NS_ENSURE_TRUE(pmService, NS_ERROR_FAILURE); |
michael@0 | 148 | |
michael@0 | 149 | nsString mountLockName; |
michael@0 | 150 | vol->GetMountLockName(mountLockName); |
michael@0 | 151 | |
michael@0 | 152 | ErrorResult err; |
michael@0 | 153 | mWakeLock = pmService->NewWakeLock(mountLockName, nullptr, err); |
michael@0 | 154 | if (err.Failed()) { |
michael@0 | 155 | return err.ErrorCode(); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | LOG("nsVolumeMountLock acquired for '%s' gen %d", |
michael@0 | 159 | NS_LossyConvertUTF16toASCII(mVolumeName).get(), mVolumeGeneration); |
michael@0 | 160 | return NS_OK; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | } // namespace system |
michael@0 | 164 | } // namespace mozilla |