Bug 718488 - calDateTime doesn't need to implement nsIXPCScriptable; r=philipp
authorMs2ger <ms2ger@gmail.com>
Mon, 16 Jan 2012 21:32:53 +0100
changeset 10427 322e170393850091a447b93cd34f5a8603437783
parent 10426 74aaf5c6fa4840a652ce0097399756e70f168388
child 10428 a9339f69c68ab9c9404cb301893e4247fa392c73
push idunknown
push userunknown
push dateunknown
reviewersphilipp
bugs718488
Bug 718488 - calDateTime doesn't need to implement nsIXPCScriptable; r=philipp
calendar/base/public/calIDateTime.idl
calendar/base/src/calDateTime.cpp
calendar/base/src/calDateTime.h
--- a/calendar/base/public/calIDateTime.idl
+++ b/calendar/base/public/calIDateTime.idl
@@ -37,17 +37,17 @@
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsISupports.idl"
 
 interface calIDuration;
 interface calITimezone;
 [ptr] native icaltimetypeptr(struct icaltimetype);
 
-[scriptable, uuid(5678d4a3-2543-4ece-afbb-079292f2866e)]
+[scriptable, uuid(04139dff-a6f0-446d-9aec-2062df887ef2)]
 interface calIDateTime : nsISupports
 {
   /**
    * isMutable is true if this instance is modifiable.
    * If isMutable is false, any attempts to modify
    * the object will throw NS_ERROR_OBJECT_IS_IMMUTABLE.
    */
   readonly attribute boolean isMutable;
@@ -260,18 +260,17 @@ interface calIDateTime : nsISupports
   [noscript,notxpcom] void toIcalTime(in icaltimetypeptr itt);
 
   /**
    * This object as either an iCalendar DATE or DATETIME string, as
    * appropriate and sets the timezone to either UTC or floating.
    */
   attribute ACString icalString;
 
-  /* JS only:
-   * // Setting jsDate via a JavaScript Date object will set
-   * // the calIDateTime to the jsDate's time ***in UTC***.
-   * // There's no way for us to recover TZ info from a jsDate,
-   * // so we always pull it out as UTC, and force the calIDateTime's
-   * // timezone to UTC.
-   * attribute Date jsDate;
-   *
+  /**
+   * Setting jsDate via a JavaScript Date object will set
+   * the calIDateTime to the jsDate's time ***in UTC***.
+   * There's no way for us to recover TZ info from a jsDate,
+   * so we always pull it out as UTC, and force the calIDateTime's
+   * timezone to UTC.
    */
+  [implicit_jscontext] attribute jsval jsDate;
 };
--- a/calendar/base/src/calDateTime.cpp
+++ b/calendar/base/src/calDateTime.cpp
@@ -57,17 +57,17 @@ extern "C" {
 #include "ical.h"
 }
 
 #define CAL_ATTR_SET_PRE NS_ENSURE_FALSE(mImmutable, NS_ERROR_OBJECT_IS_IMMUTABLE)
 #define CAL_ATTR_SET_POST Normalize()
 #include "calAttributeHelpers.h"
 
 NS_IMPL_CLASSINFO(calDateTime, NULL, 0, CAL_DATETIME_CID)
-NS_IMPL_ISUPPORTS2_CI(calDateTime, calIDateTime, nsIXPCScriptable)
+NS_IMPL_ISUPPORTS1_CI(calDateTime, calIDateTime)
 
 calDateTime::calDateTime()
     : mImmutable(false)
 {
     Reset();
 }
 
 calDateTime::calDateTime(icaltimetype const* atimeptr, calITimezone *tz)
@@ -636,133 +636,44 @@ calDateTime::Compare(calIDateTime * aOth
         *aResult = icaltime_compare_date_only_tz(a, b, cal::getIcalTimezone(mTimezone));
     } else {
         *aResult = icaltime_compare(a, b);
     }
 
     return NS_OK;
 }
 
-/*
- * nsIXPCScriptable impl
- */
-
-#define XPC_MAP_CLASSNAME calDateTime
-#define XPC_MAP_QUOTED_CLASSNAME "calDateTime"
-#define XPC_MAP_WANT_GETPROPERTY
-#define XPC_MAP_WANT_SETPROPERTY
-#define XPC_MAP_WANT_NEWRESOLVE
-#define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
-#include "xpc_map_end.h"
-
-/* bool getProperty (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in jsid id, in JSValPtr vp); */
 NS_IMETHODIMP
-calDateTime::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
-                         JSObject * obj_, jsid id, jsval * vp, bool *_retval)
+calDateTime::GetJsDate(JSContext* aCx, JS::Value* aResult)
 {
-    NS_ENSURE_ARG_POINTER(vp);
-    NS_ENSURE_ARG_POINTER(_retval);
+    double msec = double(mNativeTime / 1000);
+    ensureTimezone();
 
-    if (JSID_IS_STRING(id)) {
-        size_t length;
-        JSString *idString = JSID_TO_STRING(id);
-        const jschar *str = JS_GetStringCharsAndLength(cx, idString, &length);
-
-        nsDependentString const val(reinterpret_cast<PRUnichar const*>(str), length);
-
-        if (val.EqualsLiteral("jsDate")) {
-            PRTime tmp, thousand;
-            jsdouble msec;
-            LL_I2L(thousand, 1000);
-            LL_DIV(tmp, mNativeTime, thousand);
-            LL_L2D(msec, tmp);
-            ensureTimezone();
-
-            JSObject *obj;
-            bool b;
-            if (NS_SUCCEEDED(mTimezone->GetIsFloating(&b)) && b) {
-                obj = JS_NewDateObject(cx, mYear, mMonth, mDay, mHour, mMinute, mSecond);
-            } else {
-                obj = JS_NewDateObjectMsec(cx, msec);
-            }
-
-            *vp = OBJECT_TO_JSVAL(obj);
-            *_retval = true;
-            return NS_SUCCESS_I_DID_SOMETHING;
-        }
+    JSObject* obj;
+    bool b;
+    if (NS_SUCCEEDED(mTimezone->GetIsFloating(&b)) && b) {
+        obj = JS_NewDateObject(aCx, mYear, mMonth, mDay, mHour, mMinute, mSecond);
+    } else {
+        obj = JS_NewDateObjectMsec(aCx, msec);
     }
 
-    *_retval = true;
+    *aResult = JS::ObjectOrNullValue(obj);
     return NS_OK;
 }
 
-
-/* bool setProperty (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in jsid id, in JSValPtr vp); */
 NS_IMETHODIMP
-calDateTime::SetProperty(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
-                         JSObject * obj, jsid id, jsval * vp, bool *_retval)
+calDateTime::SetJsDate(JSContext* aCx, const JS::Value& aDate)
 {
-    NS_ENSURE_ARG_POINTER(_retval);
-
-    if (JSID_IS_STRING(id)) {
-        size_t length;
-        JSString *idString = JSID_TO_STRING(id);
-        const jschar *str = JS_GetStringCharsAndLength(cx, idString, &length);
-
-        nsDependentString const val(reinterpret_cast<PRUnichar const*>(str), length);
+    if (!aDate.isObject()) {
+        mIsValid = false;
+        return NS_OK;
+    }
 
-        if (val.EqualsLiteral("jsDate") && vp) {
-            JSObject *dobj;
-            if (!JSVAL_IS_OBJECT(*vp) ||
-                !js_DateIsValid(cx, (dobj = JSVAL_TO_OBJECT(*vp)))) {
-                mIsValid = false;
-            } else {
-                jsdouble utcMsec = js_DateGetMsecSinceEpoch(cx, dobj);
-                PRTime utcTime, thousands;
-                LL_F2L(utcTime, utcMsec);
-                LL_I2L(thousands, 1000);
-                LL_MUL(utcTime, utcTime, thousands);
+    JSObject& dobj = aDate.toObject();
+    if (!js_DateIsValid(aCx, &dobj)) {
+        mIsValid = false;
+        return NS_OK;
+    }
 
-                nsresult rv = SetNativeTime(utcTime);
-                if (NS_SUCCEEDED(rv)) {
-                    mIsValid = true;
-                } else {
-                    mIsValid = false;
-                }
-            }
-
-            *_retval = true;
-            return NS_SUCCESS_I_DID_SOMETHING;
-        }
-    }
-    *_retval = true;
+    PRTime utcTime = PRTime(js_DateGetMsecSinceEpoch(aCx, &dobj)) * 1000;
+    mIsValid = NS_SUCCEEDED(SetNativeTime(utcTime));
     return NS_OK;
 }
-
-/* bool newResolve (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in jsid id, in PRUint32 flags, out JSObjectPtr objp); */
-NS_IMETHODIMP
-calDateTime::NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext * cx,
-                        JSObject * obj, jsid id, PRUint32 flags,
-                        JSObject * *objp, bool *_retval)
-{
-    NS_ENSURE_ARG_POINTER(objp);
-    NS_ENSURE_ARG_POINTER(_retval);
-
-    if (JSID_IS_STRING(id)) {
-        size_t length;
-        JSString *idString = JSID_TO_STRING(id);
-        const jschar *str = JS_GetStringCharsAndLength(cx, idString, &length);
-
-        nsDependentString const name(reinterpret_cast<PRUnichar const*>(str), length);
-
-        if (name.EqualsLiteral("jsDate")) {
-            *_retval = JS_DefineUCProperty(cx, obj, str,
-                                           length,
-                                           JSVAL_VOID,
-                                           nsnull, nsnull, 0);
-            *objp = obj;
-            return *_retval ? NS_OK : NS_ERROR_FAILURE;
-        }
-    }
-
-    *_retval = true;
-    return NS_OK;
-}
--- a/calendar/base/src/calDateTime.h
+++ b/calendar/base/src/calDateTime.h
@@ -33,36 +33,32 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 #if !defined(INCLUDED_CALDATETIME_H)
 #define INCLUDED_CALDATETIME_H
 
-#include "jsapi.h"
-#include "nsIXPCScriptable.h"
 #include "calIDateTime.h"
 #include "calITimezoneProvider.h"
 #include "calUtils.h"
 
 struct icaltimetype;
 typedef struct _icaltimezone icaltimezone;
 
 class calDateTime : public calIDateTime,
-                    public nsIXPCScriptable,
                     public cal::XpcomBase
 {
 public:
     calDateTime();
     calDateTime(icaltimetype const* icalt, calITimezone * tz);
 
     NS_DECL_ISUPPORTS
     NS_DECL_CALIDATETIME
-    NS_DECL_NSIXPCSCRIPTABLE
 
 protected:
     bool mImmutable;
     bool mIsValid;
     bool mIsDate;
 
     PRInt16 mYear;
     PRInt16 mMonth;