//
// $Id$
//
// OOO GWT Utils - utilities for creating GWT applications
// Copyright (C) 2009-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/ooo-gwt-utils/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.gwt.util;

/**
 * Wraps the necessary JavaScript fiddling to read and write cookies on the
 * client.
 */
public class CookieUtil
{
    /**
     * Sets the specified cookie to the supplied value.
     *
     * @param expires the number of days in which the cookie should expire.
     */
    public static void set (String path, int expires, String name, String value)
    {
        String extra = "";
        if (path.length() > 0) {
            extra += "; path=" + path;
        }
        doSet(name, value, expires, extra);
    }

    /**
     * Clears out the specified cookie.
     */
    public static void clear (String path, String name)
    {
        set(path, -1, name, "");
    }

    /**
     * Looks up and returns the value for the specified cookie.
     */
    public static native String get (String name) /*-{
        var dc = $doc.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1) {
            begin = dc.indexOf(prefix);
            if (begin != 0) {
                return null;
            }
        } else {
            begin += 2;
        }
        var end = $doc.cookie.indexOf(";", begin);
        if (end == -1) {
            end = dc.length;
        }
        return unescape(dc.substring(begin + prefix.length, end));
     }-*/;

    /**
     * Handles the actual setting of the cookie.
     */
    protected static native void doSet (String name, String value, int expires, String extra) /*-{
        if (expires != 0) {
            var date = new Date();
            date.setTime(date.getTime() + (expires*24*60*60*1000));
            extra += "; expires=" + date.toGMTString();
        }
        if (location.host.indexOf("localhost") != 0) {
            var host = location.host;
            var cidx = host.indexOf(":");
            if (cidx != -1) {
                host = host.substring(0, cidx);
            }
            var didx = host.indexOf(".");
            if (didx != -1) {
                host = host.substring(didx);
            }
            extra += "; domain=" + host;
        }
        $doc.cookie = name + "=" + escape(value) + extra;
    }-*/;
}

@@ -954,16 +954,120 @@
 .util;%0A%0A
+// import com.google.common.net.InternetDomainName;%0Aimport com.google.gwt.user.client.Window.Location;%0A%0A
 /**%0A * W
@@ -1272,17 +1272,17 @@
 expires 
-t
+T
 he numbe
@@ -1326,16 +1326,71 @@
 expire.%0A
+     * @param domain The domain to set this cookie on.%0A
      */%0A
@@ -1413,24 +1413,33 @@
 c void set (
+%0A        
 String path,
@@ -1477,16 +1477,31 @@
 ng value
+, String domain
 )%0A    %7B%0A
@@ -1621,41 +1621,1270 @@
     
-doSet(name, value, expires, extra
+if (domain != null) %7B%0A            extra += %22; domain=%22 + domain;%0A        %7D%0A        doSet(name, value, expires, extra);%0A    %7D%0A%0A    /**%0A     * Sets a cross-domain cookie to the supplied value, which can be accessed by other subdomains.%0A     *%0A     * WARNING!%0A     * This doesn't work if you use it on a top level domain. InternetDomainName is the correct way%0A     * to get the parent domain, but GWT fails to compile it (bug?). For backwards compatibility,%0A     * the existing behavior of manually guessing the parent domain remains the default. If you try%0A     * to use this on a top level owned domain (foo.com) this will try to set a cookie on an invalid%0A     * domain (.com) which no browser will allow.%0A     *%0A     * Instead, call set() with an explicit domain, or null if you don't care about cross-domain%0A     * cookies.%0A     */%0A    public static void set (String path, int expires, String name, String value)%0A    %7B%0A        // String domain = %22.%22 + InternetDomainName.from(%0A        //     Location.getHostName()).topPrivateDomain().name();%0A%0A        String domain = Location.getHostName();%0A        int didx = domain.indexOf(%22.%22);%0A        if (didx != -1) %7B%0A            domain = domain.substring(didx);%0A        %7D%0A%0A        set(path, expires, name, value, domain
 );%0A 
@@ -4042,417 +4042,8 @@
   %7D%0A
-        if (location.host.indexOf(%22localhost%22) != 0) %7B%0A            var host = location.host;%0A            var cidx = host.indexOf(%22:%22);%0A            if (cidx != -1) %7B%0A                host = host.substring(0, cidx);%0A            %7D%0A            var didx = host.indexOf(%22.%22);%0A            if (didx != -1) %7B%0A                host = host.substring(didx);%0A            %7D%0A            extra += %22; domain=%22 + host;%0A        %7D%0A
     

Document a bug in CookieUtil and provide a temporary workaround.