Files
palemoon27/dom/system/gonk/TimeZoneSettingObserver.cpp
T
roytam1 6bedaf2149 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1193050 - Update the copy of GentiumPlus used in font-inspector test. r=jdaggett (253fce5764)
- align tests (5b817116a0)
- Bug 1246106 - pass aStyleBorder as reference to ComputeBorderOverflow. r=mats (53ccaa1282)
- align tests (81b3943058)
- Bug 1269080 - Convert NS_ERROR to NS_WARNING when setting an invalid scheme in nsSimpleURI. r=mcmanus (cad86c963d)
- Bug 1248558 - "[Static Analysis][Unintentional integer overflow][CacheFile.cpp::PreloadChunk, CacheFile.cpp::GetChunkLocked]". r=michal (6a9e34dfbe)
- Bug 1121672 - Make CacheFile::IsDoomed() protect mHandle with lock, r=michal (9e925b903a)
- Bug 1253040 - Do not write metadata if CacheFile is killed, r=honzab (c0378088b2)
- grammar as 1268922 (c801618b1d)
- Bug 1247548 - Changed nsCookieService::EnsureReadComplete and nsCookieService::PurgeCookeis to allocate nsTArray instead of AutoTArray. r=jdm (0e2bb0465e)
- var-let (1702e664dc)
- Bug 1233813 - Fix mDNS bug in resolving services with no TXT records. r=schien (4ce7e5fca0)
- Bug 1266632 - Add a null check in nsHostResolver::SizeOfIncludingThis. r=sworkman. (035d4a7130)
- Bug 1267000 - null deref with spdy proxy r=hurley (099db40d0f)
- Bug 1240932 - figure out 'network id' on Linux. r=mcmanus (47ceb12a70)
- Bug 1240932: add Telemetry to record network id successes, r=mcmanus (6985d1a4ce)
- Bug 1205158 - Use channel->Open2() netwerk/test/ (r=sicking) (64de01cda3)
- Bug 1240932 - figure out 'network id' on OS X, r=mcmanus (7ee4875835)
- Bug 1240932 - figure out 'network id' on Windows, r=mcmanus (3227a81c14)
- Bug 1188644 - Use channel->ascynOpen2 in netwerk/test (r=mcmanus) (7097897cdc)
- Bug 1264887: |nsWifiScannerDBus|: Hold DBusMessage references in |RefPtr|, r=josh (7e68e8663f)
- Bug 1196638 - Infer the proper button for synthesized mouse clicks on all callsites. r=jmaher (838230baaa)
- Bug 1258532 - Port test_bug574596.html to mochitest-plain in order to enable it on e10s; r=mrbkap (ae5693ab54)
- Bug 1252855 - allow setting a specific list of prefs from the content process, r=mrbkap,margaret,haik (b13645826a)
- Bug 1215965 - Remove use of non-standard features from toolkit/components/social/SocialService.jsm. r=mixedpuppy (a1d79f295c)
- ship cmpiler for XP (ac60884173)
- Bug 1262814 - Warn on Wlanapi.dll load or init fail, don't fail assertion, r=mcmanus (aa1335320a)
- Bug 1137151: Marked destructors of ref-counted time-zone classes as protected, r=dhylands (5668226442)
- Bug 1225549 - Modify the data type of mThreshold of NetworkParams to long long from long. r=ettseng (7164a517ac)
- Bug 1139805 - B2G NetworkManager: move NetworkManager.js out of EXTRA_PP_COMPONENTS in moz.build. r=echen (e857f53cfc)
- Bug 1087608 - ensuring multitap gestures do not resolve to explore. r=eeejay (6c4b603738)
- bug 1259023 - make nsIAccessible.{Next,Prev}Sibling work with proxied accessibles r=yzen (84b47a82e0)
- bug 1259023 - make nsIAccessible.indexInParent work on proxied accessibles r=yzen (3531b45e35)
- bug 1250882 - implement xpcAccessible::GetState() for proxied accessibles r=davidb (5e58214441)
- bug 1250882 - implement xpcAccessible::Name() for proxied accessibles r=davidb (4b11af6ecc)
- bug 1250882 - implement xpcAccessible::GetDescription() for proxied accessibles r=davidb (d0d0e133fb)
- bug 1250882 - implement xpcAccessible::GetLanguage() for proxied accessibles r=davidb (88e98a5b24)
- bug 1250882 - implement xpcAccessible::GetValue() for proxied accessibles r=davidb (9ad4347d45)
- bug 1250882 - implement xpcAccessible::GetBounds() for proxied accessibles r=davidb (e67f7c551b)
- Bug 1249930 - menupoup shouldn't look for children in XBL anonymous content, r=davidb (14b3e39954)
- Bug 1243233 - Test ALLOW_COMPILER_WARNINGS instead of WARNINGS_AS_ERRORS, and move it to after it is set, r=glandium (216c407d20)
- Bug 1245992 - Update the Safe Browsing phishing interstitial page. r=flod,past (80afbfc6c9)
- Bug 1245260 - Ignore redundant calls to RestyleManager::IncrementAnimationGeneration; r=dbaron (e35571b613)
- Bug 1182778, r=margaret,f=bz (b379c97f8e)
- Bug 1258212 - Hook the parent up to the cild for registerContentHandler. r=gwright (039c75d4d5)
2024-10-01 10:23:59 +08:00

240 lines
7.4 KiB
C++

/* 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/dom/ScriptSettings.h"
#include "mozilla/Attributes.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Hal.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPtr.h"
#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsISettingsService.h"
#include "nsJSUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsString.h"
#include "TimeZoneSettingObserver.h"
#include "xpcpublic.h"
#include "nsContentUtils.h"
#include "nsPrintfCString.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/SettingChangeNotificationBinding.h"
#undef LOG
#undef ERR
#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)
#define TIME_TIMEZONE "time.timezone"
#define MOZSETTINGS_CHANGED "mozsettings-changed"
using namespace mozilla;
using namespace mozilla::dom;
namespace {
class TimeZoneSettingObserver : public nsIObserver
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
TimeZoneSettingObserver();
static nsresult SetTimeZone(const JS::Value &aValue, JSContext *aContext);
protected:
virtual ~TimeZoneSettingObserver();
};
class TimeZoneSettingCb final : public nsISettingsServiceCallback
{
public:
NS_DECL_ISUPPORTS
TimeZoneSettingCb() {}
NS_IMETHOD Handle(const nsAString &aName, JS::Handle<JS::Value> aResult) {
JSContext *cx = nsContentUtils::GetCurrentJSContext();
NS_ENSURE_TRUE(cx, NS_OK);
// 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 time zone offset. Note that we need to
// convert the value to a UTC representation in the format of
// "UTC{+,-}hh:mm", so that the Gaia end can know how to interpret.
// E.g., -480 is "UTC+08:00"; 630 is "UTC-10:30".
int32_t timeZoneOffset = hal::GetTimezoneOffset();
nsPrintfCString curTimeZone("UTC%+03d:%02d",
-timeZoneOffset / 60,
abs(timeZoneOffset) % 60);
// Convert it to a JS string.
NS_ConvertUTF8toUTF16 utf16Str(curTimeZone);
JS::Rooted<JSString*> jsStr(cx, JS_NewUCStringCopyN(cx,
utf16Str.get(),
utf16Str.Length()));
if (!jsStr) {
return NS_ERROR_OUT_OF_MEMORY;
}
// 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;
}
settingsService->CreateLock(nullptr, getter_AddRefs(lock));
JS::Rooted<JS::Value> value(cx, JS::StringValue(jsStr));
lock->Set(TIME_TIMEZONE, value, nullptr, nullptr);
return NS_OK;
}
// Set the system timezone based on the current settings.
if (aResult.isString()) {
return TimeZoneSettingObserver::SetTimeZone(aResult, cx);
}
return NS_OK;
}
NS_IMETHOD HandleError(const nsAString &aName) {
ERR("TimeZoneSettingCb::HandleError: %s\n", NS_LossyConvertUTF16toASCII(aName).get());
return NS_OK;
}
protected:
~TimeZoneSettingCb() {}
};
NS_IMPL_ISUPPORTS(TimeZoneSettingCb, nsISettingsServiceCallback)
TimeZoneSettingObserver::TimeZoneSettingObserver()
{
// 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;
}
settingsService->CreateLock(nullptr, getter_AddRefs(lock));
nsCOMPtr<nsISettingsServiceCallback> callback = new TimeZoneSettingCb();
lock->Get(TIME_TIMEZONE, callback);
}
nsresult TimeZoneSettingObserver::SetTimeZone(const JS::Value &aValue, JSContext *aContext)
{
// Convert the JS value to a nsCString type.
// The value should be a JS string like "America/Chicago" or "UTC-05:00".
nsAutoJSString valueStr;
if (!valueStr.init(aContext, aValue.toString())) {
ERR("Failed to convert JS value to nsCString");
return NS_ERROR_FAILURE;
}
NS_ConvertUTF16toUTF8 newTimezone(valueStr);
// Hal expects opposite sign from general notations,
// so we need to flip it.
if (newTimezone.Find(NS_LITERAL_CSTRING("UTC+")) == 0) {
if (!newTimezone.SetCharAt('-', 3)) {
return NS_ERROR_FAILURE;
}
} else if (newTimezone.Find(NS_LITERAL_CSTRING("UTC-")) == 0) {
if (!newTimezone.SetCharAt('+', 3)) {
return NS_ERROR_FAILURE;
}
}
// 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;
}
TimeZoneSettingObserver::~TimeZoneSettingObserver()
{
nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
if (observerService) {
observerService->RemoveObserver(this, MOZSETTINGS_CHANGED);
}
}
NS_IMPL_ISUPPORTS(TimeZoneSettingObserver, nsIObserver)
NS_IMETHODIMP
TimeZoneSettingObserver::Observe(nsISupports *aSubject,
const char *aTopic,
const char16_t *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"} or
// {"key":"time.timezone","value":"UTC-05:00"}
AutoSafeJSContext cx;
RootedDictionary<SettingChangeNotification> setting(cx);
if (!WrappedJSToDictionary(cx, aSubject, setting)) {
return NS_OK;
}
if (!setting.mKey.EqualsASCII(TIME_TIMEZONE)) {
return NS_OK;
}
if (!setting.mValue.isString()) {
return NS_OK;
}
// Set the system timezone.
return SetTimeZone(setting.mValue, cx);
}
} // namespace
static mozilla::StaticRefPtr<TimeZoneSettingObserver> sTimeZoneSettingObserver;
namespace mozilla {
namespace system {
void
InitializeTimeZoneSettingObserver()
{
sTimeZoneSettingObserver = new TimeZoneSettingObserver();
ClearOnShutdown(&sTimeZoneSettingObserver);
}
} // namespace system
} // namespace mozilla