2012-05-25 08:03:34 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "AutoMounter.h"
|
|
|
|
#include "AutoMounterSetting.h"
|
|
|
|
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2014-12-29 13:56:00 +03:00
|
|
|
#include "nsContentUtils.h"
|
2012-05-25 08:03:34 +04:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsISettingsService.h"
|
2013-05-14 04:53:23 +04:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsPrintfCString.h"
|
2012-05-25 08:03:34 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsString.h"
|
2013-05-14 04:53:23 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2012-05-25 08:03:34 +04:00
|
|
|
#include "xpcpublic.h"
|
2014-08-15 05:47:15 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2012-06-20 07:41:56 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-09-16 21:15:16 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
|
|
|
#include "mozilla/dom/SettingChangeNotificationBinding.h"
|
2012-05-25 08:03:34 +04:00
|
|
|
|
|
|
|
#undef LOG
|
2014-10-22 04:28:12 +04:00
|
|
|
#undef ERR
|
2012-05-25 08:03:34 +04:00
|
|
|
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "AutoMounterSetting" , ## args)
|
|
|
|
#define ERR(args...) __android_log_print(ANDROID_LOG_ERROR, "AutoMounterSetting" , ## args)
|
|
|
|
|
2013-05-14 04:53:23 +04:00
|
|
|
#define UMS_MODE "ums.mode"
|
2013-10-18 02:41:46 +04:00
|
|
|
#define UMS_STATUS "ums.status"
|
2013-05-14 04:53:23 +04:00
|
|
|
#define UMS_VOLUME_ENABLED_PREFIX "ums.volume."
|
|
|
|
#define UMS_VOLUME_ENABLED_SUFFIX ".enabled"
|
|
|
|
#define MOZSETTINGS_CHANGED "mozsettings-changed"
|
2012-05-25 08:03:34 +04:00
|
|
|
|
2014-09-16 21:15:16 +04:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2012-05-25 08:03:34 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace system {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class SettingsServiceCallback final : public nsISettingsServiceCallback
|
2012-05-25 08:03:34 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-05-25 08:03:34 +04:00
|
|
|
|
|
|
|
SettingsServiceCallback() {}
|
|
|
|
|
2014-01-09 21:39:36 +04:00
|
|
|
NS_IMETHOD Handle(const nsAString& aName, JS::Handle<JS::Value> aResult)
|
2013-05-14 04:53:23 +04:00
|
|
|
{
|
2014-04-28 06:47:02 +04:00
|
|
|
if (aResult.isInt32()) {
|
2014-04-28 06:55:08 +04:00
|
|
|
int32_t mode = aResult.toInt32();
|
2012-05-25 08:03:34 +04:00
|
|
|
SetAutoMounterMode(mode);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-14 04:53:23 +04:00
|
|
|
NS_IMETHOD HandleError(const nsAString& aName)
|
|
|
|
{
|
2012-05-25 08:03:34 +04:00
|
|
|
ERR("SettingsCallback::HandleError: %s\n", NS_LossyConvertUTF16toASCII(aName).get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-10 15:44:02 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
~SettingsServiceCallback() {}
|
2012-05-25 08:03:34 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(SettingsServiceCallback, nsISettingsServiceCallback)
|
2012-05-25 08:03:34 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CheckVolumeSettingsCallback final : public nsISettingsServiceCallback
|
2013-05-14 04:53:23 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-05-14 04:53:23 +04:00
|
|
|
|
|
|
|
CheckVolumeSettingsCallback(const nsACString& aVolumeName)
|
|
|
|
: mVolumeName(aVolumeName) {}
|
|
|
|
|
2014-01-09 21:39:36 +04:00
|
|
|
NS_IMETHOD Handle(const nsAString& aName, JS::Handle<JS::Value> aResult)
|
2013-05-14 04:53:23 +04:00
|
|
|
{
|
2014-04-28 06:44:05 +04:00
|
|
|
if (aResult.isBoolean()) {
|
2014-04-28 06:34:12 +04:00
|
|
|
bool isSharingEnabled = aResult.toBoolean();
|
2013-05-14 04:53:23 +04:00
|
|
|
SetAutoMounterSharingMode(mVolumeName, isSharingEnabled);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD HandleError(const nsAString& aName)
|
|
|
|
{
|
|
|
|
ERR("CheckVolumeSettingsCallback::HandleError: %s\n", NS_LossyConvertUTF16toASCII(aName).get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-10 15:44:02 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
~CheckVolumeSettingsCallback() {}
|
|
|
|
|
2013-05-14 04:53:23 +04:00
|
|
|
private:
|
|
|
|
nsCString mVolumeName;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(CheckVolumeSettingsCallback, nsISettingsServiceCallback)
|
2013-05-14 04:53:23 +04:00
|
|
|
|
2012-05-25 08:03:34 +04:00
|
|
|
AutoMounterSetting::AutoMounterSetting()
|
2013-10-18 02:41:46 +04:00
|
|
|
: mStatus(AUTOMOUNTER_STATUS_DISABLED)
|
2012-05-25 08:03:34 +04:00
|
|
|
{
|
2013-10-18 02:41:46 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-05-25 08:03:34 +04:00
|
|
|
// Setup an observer to watch changes to the setting
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService) {
|
|
|
|
ERR("GetObserverService failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsresult rv;
|
|
|
|
rv = observerService->AddObserver(this, MOZSETTINGS_CHANGED, false);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
ERR("AddObserver failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-09 02:44:59 +04:00
|
|
|
// Force ums.mode to be 0 initially. We do this because settings are persisted.
|
|
|
|
// We don't want UMS to be enabled until such time as the phone is unlocked,
|
|
|
|
// and gaia/apps/system/js/storage.js takes care of detecting when the phone
|
|
|
|
// becomes unlocked and changes ums.mode appropriately.
|
2012-05-25 08:03:34 +04:00
|
|
|
nsCOMPtr<nsISettingsService> settingsService =
|
|
|
|
do_GetService("@mozilla.org/settingsService;1");
|
|
|
|
if (!settingsService) {
|
|
|
|
ERR("Failed to get settingsLock service!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsISettingsServiceLock> lock;
|
2014-02-07 15:19:58 +04:00
|
|
|
settingsService->CreateLock(nullptr, getter_AddRefs(lock));
|
2012-05-25 08:03:34 +04:00
|
|
|
nsCOMPtr<nsISettingsServiceCallback> callback = new SettingsServiceCallback();
|
2014-12-29 13:56:00 +03:00
|
|
|
JS::Rooted<JS::Value> value(nsContentUtils::RootingCx());
|
2014-01-09 21:39:36 +04:00
|
|
|
value.setInt32(AUTOMOUNTER_DISABLE);
|
|
|
|
lock->Set(UMS_MODE, value, callback, nullptr);
|
|
|
|
value.setInt32(mStatus);
|
|
|
|
lock->Set(UMS_STATUS, value, nullptr, nullptr);
|
2012-05-25 08:03:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoMounterSetting::~AutoMounterSetting()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->RemoveObserver(this, MOZSETTINGS_CHANGED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(AutoMounterSetting, nsIObserver)
|
2012-05-25 08:03:34 +04:00
|
|
|
|
2013-10-18 02:41:46 +04:00
|
|
|
const char *
|
|
|
|
AutoMounterSetting::StatusStr(int32_t aStatus)
|
|
|
|
{
|
|
|
|
switch (aStatus) {
|
|
|
|
case AUTOMOUNTER_STATUS_DISABLED: return "Disabled";
|
|
|
|
case AUTOMOUNTER_STATUS_ENABLED: return "Enabled";
|
|
|
|
case AUTOMOUNTER_STATUS_FILES_OPEN: return "FilesOpen";
|
|
|
|
}
|
|
|
|
return "??? Unknown ???";
|
|
|
|
}
|
|
|
|
|
2013-05-14 04:53:23 +04:00
|
|
|
class CheckVolumeSettingsRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CheckVolumeSettingsRunnable(const nsACString& aVolumeName)
|
|
|
|
: mVolumeName(aVolumeName) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsCOMPtr<nsISettingsService> settingsService =
|
|
|
|
do_GetService("@mozilla.org/settingsService;1");
|
|
|
|
NS_ENSURE_TRUE(settingsService, NS_ERROR_FAILURE);
|
|
|
|
nsCOMPtr<nsISettingsServiceLock> lock;
|
2014-02-07 15:19:58 +04:00
|
|
|
settingsService->CreateLock(nullptr, getter_AddRefs(lock));
|
2013-05-14 04:53:23 +04:00
|
|
|
nsCOMPtr<nsISettingsServiceCallback> callback =
|
|
|
|
new CheckVolumeSettingsCallback(mVolumeName);
|
|
|
|
nsPrintfCString setting(UMS_VOLUME_ENABLED_PREFIX "%s" UMS_VOLUME_ENABLED_SUFFIX,
|
|
|
|
mVolumeName.get());
|
|
|
|
lock->Get(setting.get(), callback);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCString mVolumeName;
|
|
|
|
};
|
|
|
|
|
|
|
|
//static
|
|
|
|
void
|
|
|
|
AutoMounterSetting::CheckVolumeSettings(const nsACString& aVolumeName)
|
|
|
|
{
|
|
|
|
NS_DispatchToMainThread(new CheckVolumeSettingsRunnable(aVolumeName));
|
|
|
|
}
|
|
|
|
|
2013-10-18 02:41:46 +04:00
|
|
|
class SetStatusRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SetStatusRunnable(int32_t aStatus) : mStatus(aStatus) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsCOMPtr<nsISettingsService> settingsService =
|
|
|
|
do_GetService("@mozilla.org/settingsService;1");
|
|
|
|
NS_ENSURE_TRUE(settingsService, NS_ERROR_FAILURE);
|
|
|
|
nsCOMPtr<nsISettingsServiceLock> lock;
|
2014-02-07 15:19:58 +04:00
|
|
|
settingsService->CreateLock(nullptr, getter_AddRefs(lock));
|
2013-11-08 04:14:44 +04:00
|
|
|
// lock may be null if this gets called during shutdown.
|
|
|
|
if (lock) {
|
2014-01-09 21:39:36 +04:00
|
|
|
mozilla::AutoSafeJSContext cx;
|
|
|
|
JS::Rooted<JS::Value> value(cx, JS::Int32Value(mStatus));
|
|
|
|
lock->Set(UMS_STATUS, value, nullptr, nullptr);
|
2013-11-08 04:14:44 +04:00
|
|
|
}
|
2013-10-18 02:41:46 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int32_t mStatus;
|
|
|
|
};
|
|
|
|
|
|
|
|
//static
|
|
|
|
void
|
|
|
|
AutoMounterSetting::SetStatus(int32_t aStatus)
|
|
|
|
{
|
|
|
|
if (aStatus != mStatus) {
|
|
|
|
LOG("Changing status from '%s' to '%s'",
|
|
|
|
StatusStr(mStatus), StatusStr(aStatus));
|
|
|
|
mStatus = aStatus;
|
|
|
|
NS_DispatchToMainThread(new SetStatusRunnable(aStatus));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-25 08:03:34 +04:00
|
|
|
NS_IMETHODIMP
|
2013-01-07 20:43:02 +04:00
|
|
|
AutoMounterSetting::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData)
|
2012-05-25 08:03:34 +04:00
|
|
|
{
|
|
|
|
if (strcmp(aTopic, MOZSETTINGS_CHANGED) != 0) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that this function gets called for any and all settings changes,
|
|
|
|
// so we need to carefully check if we have the one we're interested in.
|
|
|
|
//
|
|
|
|
// The string that we're interested in will be a JSON string that looks like:
|
|
|
|
// {"key":"ums.autoMount","value":true}
|
|
|
|
|
2014-12-29 13:56:00 +03:00
|
|
|
RootedDictionary<SettingChangeNotification> setting(nsContentUtils::RootingCxForThread());
|
|
|
|
if (!WrappedJSToDictionary(aSubject, setting)) {
|
2013-05-14 04:53:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for ums.mode changes
|
2014-09-16 21:15:16 +04:00
|
|
|
if (setting.mKey.EqualsASCII(UMS_MODE)) {
|
|
|
|
if (!setting.mValue.isInt32()) {
|
2013-05-14 04:53:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-09-16 21:15:16 +04:00
|
|
|
int32_t mode = setting.mValue.toInt32();
|
2013-05-14 04:53:23 +04:00
|
|
|
SetAutoMounterMode(mode);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for ums.volume.NAME.enabled
|
2014-09-16 21:15:16 +04:00
|
|
|
if (StringBeginsWith(setting.mKey, NS_LITERAL_STRING(UMS_VOLUME_ENABLED_PREFIX)) &&
|
|
|
|
StringEndsWith(setting.mKey, NS_LITERAL_STRING(UMS_VOLUME_ENABLED_SUFFIX))) {
|
|
|
|
if (!setting.mValue.isBoolean()) {
|
2013-05-14 04:53:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
const size_t prefixLen = sizeof(UMS_VOLUME_ENABLED_PREFIX) - 1;
|
|
|
|
const size_t suffixLen = sizeof(UMS_VOLUME_ENABLED_SUFFIX) - 1;
|
|
|
|
nsDependentSubstring volumeName =
|
2014-09-16 21:15:16 +04:00
|
|
|
Substring(setting.mKey, prefixLen, setting.mKey.Length() - prefixLen - suffixLen);
|
|
|
|
bool isSharingEnabled = setting.mValue.toBoolean();
|
2013-05-14 04:53:23 +04:00
|
|
|
SetAutoMounterSharingMode(NS_LossyConvertUTF16toASCII(volumeName), isSharingEnabled);
|
2012-05-25 08:03:34 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace system
|
|
|
|
} // namespace mozilla
|