2012-09-04 07:56:30 +00: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 "base/message_loop.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
2012-10-30 15:34:49 +00:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2012-09-04 07:56:30 +00:00
|
|
|
#include "mozilla/Hal.h"
|
|
|
|
#include "mozilla/Services.h"
|
2012-10-30 15:34:49 +00:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2012-09-04 07:56:30 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDebug.h"
|
2012-10-30 15:34:49 +00:00
|
|
|
#include "nsIObserver.h"
|
2012-09-04 07:56:30 +00:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsISettingsService.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsString.h"
|
2012-10-30 15:34:50 +00:00
|
|
|
#include "TimeZoneSettingObserver.h"
|
2012-09-04 07:56:30 +00:00
|
|
|
#include "xpcpublic.h"
|
2012-11-14 21:46:38 +00:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 16:05:26 +00:00
|
|
|
#include "nsCxPusher.h"
|
2012-09-04 07:56:30 +00:00
|
|
|
|
|
|
|
#undef LOG
|
2012-10-30 15:34:49 +00:00
|
|
|
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Time Zone Setting" , ## args)
|
|
|
|
#define ERR(args...) __android_log_print(ANDROID_LOG_ERROR, "Time Zone Setting" , ## args)
|
2012-09-04 07:56:30 +00:00
|
|
|
|
|
|
|
#define TIME_TIMEZONE "time.timezone"
|
|
|
|
#define MOZSETTINGS_CHANGED "mozsettings-changed"
|
|
|
|
|
2012-10-30 15:34:49 +00:00
|
|
|
using namespace mozilla;
|
2012-09-04 07:56:30 +00:00
|
|
|
|
2012-10-30 15:34:49 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TimeZoneSettingObserver : public nsIObserver
|
2012-09-04 07:56:30 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2012-10-30 15:34:49 +00:00
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
TimeZoneSettingObserver();
|
|
|
|
virtual ~TimeZoneSettingObserver();
|
|
|
|
static nsresult SetTimeZone(const JS::Value &aValue, JSContext *aContext);
|
|
|
|
};
|
2012-09-04 07:56:30 +00:00
|
|
|
|
2012-10-30 15:34:49 +00:00
|
|
|
class TimeZoneSettingCb MOZ_FINAL : public nsISettingsServiceCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
TimeZoneSettingCb() {}
|
2012-09-04 07:56:30 +00:00
|
|
|
|
2012-11-14 21:46:38 +00:00
|
|
|
NS_IMETHOD Handle(const nsAString &aName, const JS::Value &aResult) {
|
|
|
|
|
2012-11-14 21:46:48 +00:00
|
|
|
JSContext *cx = nsContentUtils::GetCurrentJSContext();
|
2012-11-14 21:46:38 +00:00
|
|
|
NS_ENSURE_TRUE(cx, NS_OK);
|
|
|
|
|
2012-09-04 07:56:30 +00:00
|
|
|
// If we don't have time.timezone value in the settings, we need
|
|
|
|
// to initialize the settings based on the current system timezone
|
|
|
|
// to make settings consistent with system. This usually happens
|
|
|
|
// at the very first boot. After that, settings must have a value.
|
|
|
|
if (aResult.isNull()) {
|
|
|
|
// Get the current system timezone and convert it to a JS string.
|
|
|
|
nsCString curTimezone = hal::GetTimezone();
|
|
|
|
NS_ConvertUTF8toUTF16 utf16Str(curTimezone);
|
2012-11-14 21:46:38 +00:00
|
|
|
JSString *jsStr = JS_NewUCStringCopyN(cx, utf16Str.get(), utf16Str.Length());
|
2012-09-04 07:56:30 +00:00
|
|
|
|
|
|
|
// Set the settings based on the current system timezone.
|
|
|
|
nsCOMPtr<nsISettingsServiceLock> lock;
|
|
|
|
nsCOMPtr<nsISettingsService> settingsService =
|
|
|
|
do_GetService("@mozilla.org/settingsService;1");
|
|
|
|
if (!settingsService) {
|
|
|
|
ERR("Failed to get settingsLock service!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-09-06 00:33:03 +00:00
|
|
|
settingsService->CreateLock(getter_AddRefs(lock));
|
2012-09-04 07:56:30 +00:00
|
|
|
lock->Set(TIME_TIMEZONE, STRING_TO_JSVAL(jsStr), nullptr, nullptr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the system timezone based on the current settings.
|
|
|
|
if (aResult.isString()) {
|
2012-11-14 21:46:38 +00:00
|
|
|
return TimeZoneSettingObserver::SetTimeZone(aResult, cx);
|
2012-09-04 07:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 21:46:38 +00:00
|
|
|
NS_IMETHOD HandleError(const nsAString &aName) {
|
2012-10-30 15:34:49 +00:00
|
|
|
ERR("TimeZoneSettingCb::HandleError: %s\n", NS_LossyConvertUTF16toASCII(aName).get());
|
2012-09-04 07:56:30 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-30 15:34:49 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(TimeZoneSettingCb, nsISettingsServiceCallback)
|
2012-09-04 07:56:30 +00:00
|
|
|
|
2012-10-30 15:34:49 +00:00
|
|
|
TimeZoneSettingObserver::TimeZoneSettingObserver()
|
2012-09-04 07:56:30 +00:00
|
|
|
{
|
|
|
|
// Setup an observer to watch changes to the setting.
|
|
|
|
nsCOMPtr<nsIObserverService> observerService = 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the 'time.timezone' setting in order to start with a known
|
|
|
|
// value at boot time. The handle() will be called after reading.
|
|
|
|
nsCOMPtr<nsISettingsServiceLock> lock;
|
|
|
|
nsCOMPtr<nsISettingsService> settingsService =
|
|
|
|
do_GetService("@mozilla.org/settingsService;1");
|
|
|
|
if (!settingsService) {
|
|
|
|
ERR("Failed to get settingsLock service!");
|
|
|
|
return;
|
|
|
|
}
|
2012-09-06 00:33:03 +00:00
|
|
|
settingsService->CreateLock(getter_AddRefs(lock));
|
2012-10-30 15:34:49 +00:00
|
|
|
nsCOMPtr<nsISettingsServiceCallback> callback = new TimeZoneSettingCb();
|
2012-09-04 07:56:30 +00:00
|
|
|
lock->Get(TIME_TIMEZONE, callback);
|
|
|
|
}
|
|
|
|
|
2012-10-30 15:34:49 +00:00
|
|
|
nsresult TimeZoneSettingObserver::SetTimeZone(const JS::Value &aValue, JSContext *aContext)
|
2012-09-04 07:56:30 +00:00
|
|
|
{
|
|
|
|
// Convert the JS value to a nsCString type.
|
|
|
|
nsDependentJSString valueStr;
|
|
|
|
if (!valueStr.init(aContext, aValue.toString())) {
|
|
|
|
ERR("Failed to convert JS value to nsCString");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-06-03 08:41:27 +00:00
|
|
|
NS_ConvertUTF16toUTF8 newTimezone(valueStr);
|
2012-09-04 07:56:30 +00:00
|
|
|
|
|
|
|
// Set the timezone only when the system timezone is not identical.
|
|
|
|
nsCString curTimezone = hal::GetTimezone();
|
|
|
|
if (!curTimezone.Equals(newTimezone)) {
|
|
|
|
hal::SetTimezone(newTimezone);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-30 15:34:49 +00:00
|
|
|
TimeZoneSettingObserver::~TimeZoneSettingObserver()
|
2012-09-04 07:56:30 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->RemoveObserver(this, MOZSETTINGS_CHANGED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-30 15:34:49 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(TimeZoneSettingObserver, nsIObserver)
|
2012-09-04 07:56:30 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-10-30 15:34:49 +00:00
|
|
|
TimeZoneSettingObserver::Observe(nsISupports *aSubject,
|
2012-09-04 07:56:30 +00:00
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
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":"time.timezone","value":"America/Chicago"}
|
|
|
|
|
2013-05-06 13:04:17 +00:00
|
|
|
AutoSafeJSContext cx;
|
2012-09-04 07:56:30 +00:00
|
|
|
|
|
|
|
// Parse the JSON value.
|
|
|
|
nsDependentString dataStr(aData);
|
2013-05-29 08:29:39 +00:00
|
|
|
JS::Rooted<JS::Value> val(cx);
|
2012-09-04 07:56:30 +00:00
|
|
|
if (!JS_ParseJSON(cx, dataStr.get(), dataStr.Length(), &val) ||
|
|
|
|
!val.isObject()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the key, which should be the JS string "time.timezone".
|
|
|
|
JSObject &obj(val.toObject());
|
2013-07-26 10:34:25 +00:00
|
|
|
JS::Value key;
|
2012-09-04 07:56:30 +00:00
|
|
|
if (!JS_GetProperty(cx, &obj, "key", &key) ||
|
|
|
|
!key.isString()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
JSBool match;
|
|
|
|
if (!JS_StringEqualsAscii(cx, key.toString(), TIME_TIMEZONE, &match) ||
|
|
|
|
match != JS_TRUE) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the value, which should be a JS string like "America/Chicago".
|
2013-07-26 10:34:25 +00:00
|
|
|
JS::Value value;
|
2012-09-04 07:56:30 +00:00
|
|
|
if (!JS_GetProperty(cx, &obj, "value", &value) ||
|
|
|
|
!value.isString()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the system timezone.
|
2012-10-30 15:34:49 +00:00
|
|
|
return SetTimeZone(value, cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
static mozilla::StaticRefPtr<TimeZoneSettingObserver> sTimeZoneSettingObserver;
|
|
|
|
namespace mozilla {
|
|
|
|
namespace system {
|
|
|
|
void
|
|
|
|
InitializeTimeZoneSettingObserver()
|
|
|
|
{
|
|
|
|
sTimeZoneSettingObserver = new TimeZoneSettingObserver();
|
|
|
|
ClearOnShutdown(&sTimeZoneSettingObserver);
|
2012-09-04 07:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace system
|
|
|
|
} // namespace mozilla
|