Bug 792290 - Change TimeSetting to TimeZoneSettingObserver. r=jlebar
authorSteven Lee <slee@mozilla.com>
Tue, 30 Oct 2012 11:34:49 -0400
changeset 111907 233981f907361319d66ed3f49fb03fd9b2ed2139
parent 111906 354250ed1e72526efa01c9830852152aadd1c58c
child 111908 ff4f0988d15618fbd7a54d2d09a025b77cc69ccc
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersjlebar
bugs792290
milestone19.0a1
Bug 792290 - Change TimeSetting to TimeZoneSettingObserver. r=jlebar
dom/system/gonk/SystemWorkerManager.cpp
dom/system/gonk/TimeSetting.cpp
dom/system/gonk/TimeSetting.h
--- a/dom/system/gonk/SystemWorkerManager.cpp
+++ b/dom/system/gonk/SystemWorkerManager.cpp
@@ -12,17 +12,16 @@
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 #include "SystemWorkerManager.h"
 
-#include "nsIObserverService.h"
 #include "nsIJSContextStack.h"
 #include "nsINetworkManager.h"
 #include "nsIWifi.h"
 #include "nsIWorkerHolder.h"
 #include "nsIXPConnect.h"
 
 #include "jsfriendapi.h"
 #include "mozilla/dom/workers/Workers.h"
@@ -32,17 +31,16 @@
 #include "TimeSetting.h"
 #endif
 #include "mozilla/ipc/Ril.h"
 #include "nsContentUtils.h"
 #include "nsServiceManagerUtils.h"
 #include "nsThreadUtils.h"
 #include "nsRadioInterfaceLayer.h"
 #include "WifiWorker.h"
-#include "mozilla/StaticPtr.h"
 
 USING_WORKERS_NAMESPACE
 
 using namespace mozilla::dom::gonk;
 using namespace mozilla::ipc;
 #ifdef MOZ_WIDGET_GONK
 using namespace mozilla::system;
 #endif
@@ -344,20 +342,16 @@ SystemWorkerManager::SystemWorkerManager
 SystemWorkerManager::~SystemWorkerManager()
 {
   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   NS_ASSERTION(!gInstance || gInstance == this,
                "There should only be one instance!");
   gInstance = nullptr;
 }
 
-#ifdef MOZ_WIDGET_GONK
-static mozilla::StaticRefPtr<TimeSetting> sTimeSetting;
-#endif
-
 nsresult
 SystemWorkerManager::Init()
 {
   if (XRE_GetProcessType() != GeckoProcessType_Default) {
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   NS_ASSERTION(NS_IsMainThread(), "We can only initialize on the main thread");
@@ -380,17 +374,17 @@ SystemWorkerManager::Init()
   rv = InitWifi(cx);
   if (NS_FAILED(rv)) {
     NS_WARNING("Failed to initialize WiFi Networking!");
     return rv;
   }
 
 #ifdef MOZ_WIDGET_GONK
   InitAutoMounter();
-  sTimeSetting = new TimeSetting();
+  InitializeTimeZoneSettingObserver();
   rv = InitNetd(cx);
   NS_ENSURE_SUCCESS(rv, rv);
 #endif
 
   nsCOMPtr<nsIObserverService> obs =
     do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
   if (!obs) {
     NS_WARNING("Failed to get observer service!");
@@ -410,17 +404,16 @@ void
 SystemWorkerManager::Shutdown()
 {
   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
 
   mShutdown = true;
 
 #ifdef MOZ_WIDGET_GONK
   ShutdownAutoMounter();
-  sTimeSetting = nullptr;
 #endif
 
   StopRil();
 
   mRIL = nullptr;
 
 #ifdef MOZ_WIDGET_GONK
   StopNetd();
--- a/dom/system/gonk/TimeSetting.cpp
+++ b/dom/system/gonk/TimeSetting.cpp
@@ -1,44 +1,59 @@
 /* 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"
+#include "mozilla/ClearOnShutdown.h"
 #include "mozilla/Hal.h"
 #include "mozilla/Services.h"
+#include "mozilla/StaticPtr.h"
 #include "nsCOMPtr.h"
 #include "nsDebug.h"
 #include "nsIJSContextStack.h"
+#include "nsIObserver.h"
 #include "nsIObserverService.h"
 #include "nsISettingsService.h"
 #include "nsJSUtils.h"
 #include "nsServiceManagerUtils.h"
 #include "nsString.h"
 #include "TimeSetting.h"
 #include "xpcpublic.h"
 
 #undef LOG
-#define LOG(args...)  __android_log_print(ANDROID_LOG_INFO, "Time Setting" , ## args)
-#define ERR(args...)  __android_log_print(ANDROID_LOG_ERROR, "Time Setting" , ## args)
+#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"
 
-namespace mozilla {
-namespace system {
+using namespace mozilla;
+
+namespace {
 
-class InitTimezoneCb MOZ_FINAL : public nsISettingsServiceCallback
+class TimeZoneSettingObserver : public nsIObserver
+{
+public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIOBSERVER
+
+  TimeZoneSettingObserver();
+  virtual ~TimeZoneSettingObserver();
+  static nsresult SetTimeZone(const JS::Value &aValue, JSContext *aContext);
+};
+
+class TimeZoneSettingCb MOZ_FINAL : public nsISettingsServiceCallback
 {
 public:
   NS_DECL_ISUPPORTS
 
-  InitTimezoneCb() {}
+  TimeZoneSettingCb() {}
 
   NS_IMETHOD Handle(const nsAString &aName, const JS::Value &aResult, JSContext *aContext) {
     // 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.
@@ -56,31 +71,31 @@ public:
       }
       settingsService->CreateLock(getter_AddRefs(lock));
       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()) {
-      return TimeSetting::SetTimezone(aResult, aContext);
+      return TimeZoneSettingObserver::SetTimeZone(aResult, aContext);
     }
 
     return NS_OK;
   }
 
   NS_IMETHOD HandleError(const nsAString &aName, JSContext *aContext) {
-    ERR("InitTimezoneCb::HandleError: %s\n", NS_LossyConvertUTF16toASCII(aName).get());
+    ERR("TimeZoneSettingCb::HandleError: %s\n", NS_LossyConvertUTF16toASCII(aName).get());
     return NS_OK;
   }
 };
 
-NS_IMPL_ISUPPORTS1(InitTimezoneCb, nsISettingsServiceCallback)
+NS_IMPL_ISUPPORTS1(TimeZoneSettingCb, nsISettingsServiceCallback)
 
-TimeSetting::TimeSetting()
+TimeZoneSettingObserver::TimeZoneSettingObserver()
 {
   // Setup an observer to watch changes to the setting.
   nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
   if (!observerService) {
     ERR("GetObserverService failed");
     return;
   }
   nsresult rv;
@@ -95,21 +110,21 @@ TimeSetting::TimeSetting()
   nsCOMPtr<nsISettingsServiceLock> lock;
   nsCOMPtr<nsISettingsService> settingsService =
     do_GetService("@mozilla.org/settingsService;1");
   if (!settingsService) {
     ERR("Failed to get settingsLock service!");
     return;
   }
   settingsService->CreateLock(getter_AddRefs(lock));
-  nsCOMPtr<nsISettingsServiceCallback> callback = new InitTimezoneCb();
+  nsCOMPtr<nsISettingsServiceCallback> callback = new TimeZoneSettingCb();
   lock->Get(TIME_TIMEZONE, callback);
 }
 
-nsresult TimeSetting::SetTimezone(const JS::Value &aValue, JSContext *aContext)
+nsresult TimeZoneSettingObserver::SetTimeZone(const JS::Value &aValue, JSContext *aContext)
 {
   // 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;
   }
   nsCString newTimezone = NS_ConvertUTF16toUTF8(valueStr);
@@ -118,28 +133,28 @@ nsresult TimeSetting::SetTimezone(const 
   nsCString curTimezone = hal::GetTimezone();
   if (!curTimezone.Equals(newTimezone)) {
     hal::SetTimezone(newTimezone);
   }
 
   return NS_OK;
 }
 
-TimeSetting::~TimeSetting()
+TimeZoneSettingObserver::~TimeZoneSettingObserver()
 {
   nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
   if (observerService) {
     observerService->RemoveObserver(this, MOZSETTINGS_CHANGED);
   }
 }
 
-NS_IMPL_ISUPPORTS1(TimeSetting, nsIObserver)
+NS_IMPL_ISUPPORTS1(TimeZoneSettingObserver, nsIObserver)
 
 NS_IMETHODIMP
-TimeSetting::Observe(nsISupports *aSubject,
+TimeZoneSettingObserver::Observe(nsISupports *aSubject,
                      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,
@@ -185,13 +200,25 @@ TimeSetting::Observe(nsISupports *aSubje
   // Get the value, which should be a JS string like "America/Chicago".
   JS::Value value;
   if (!JS_GetProperty(cx, &obj, "value", &value) ||
       !value.isString()) {
     return NS_OK;
   }
 
   // Set the system timezone.
-  return SetTimezone(value, cx);
+  return SetTimeZone(value, cx);
+}
+
+} // anonymous namespace
+
+static mozilla::StaticRefPtr<TimeZoneSettingObserver> sTimeZoneSettingObserver;
+namespace mozilla {
+namespace system {
+void
+InitializeTimeZoneSettingObserver()
+{
+  sTimeZoneSettingObserver = new TimeZoneSettingObserver();
+  ClearOnShutdown(&sTimeZoneSettingObserver);
 }
 
 } // namespace system
 } // namespace mozilla
--- a/dom/system/gonk/TimeSetting.h
+++ b/dom/system/gonk/TimeSetting.h
@@ -1,31 +1,20 @@
 /* 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/. */
 
 #ifndef mozilla_system_timesetting_h__
 #define mozilla_system_timesetting_h__
 
-#include "jspubtd.h"
-#include "nsIObserver.h"
-
 namespace mozilla {
 namespace system {
 
-class ResultListener;
-
-class TimeSetting : public nsIObserver
-{
-public:
-  NS_DECL_ISUPPORTS
-  NS_DECL_NSIOBSERVER
-
-  TimeSetting();
-  virtual ~TimeSetting();
-  static nsresult SetTimezone(const JS::Value &aValue, JSContext *aContext);
-};
+// Initialize TimeZoneSettingObserver which observes the time zone change
+// event from settings service. When receiving the event, it modifies the
+// system time zone.
+void InitializeTimeZoneSettingObserver();
 
 } // namespace system
 } // namespace mozilla
 
 #endif // mozilla_system_timesetting_h__