1

I have problem while using crosswalk in shared mode with setting User-agent and handling cookies

1.While using crosswalk in normal mode there is an api org.xwalk.core.internal.XWalkViewBridge with which I set the user agent for the webview java.lang.reflect.Method ___getBridge=XWalkView.class.getDeclaredMethod("getBridge"); ___getBridge.setAccessible(true);

XWalkViewBridge xWalkViewBridge = null;
xWalkViewBridge = (XWalkViewBridge)___getBridge.invoke(webView);
XWalkSettings xWalkSettings = xWalkViewBridge.getSettings();
xWalkSettings.setUserAgentString(userAgent);

2.And there is another api known as org.xwalk.core.internal.XWalkCookieManager which is used to handle Cookies on webview.

Both of these apis are not accessible in shared mode, Kindly suggest how to set the user agent and handle cookies while using crosswalk in shared mode.

1
  • can anyone answer my question?
    – hariszaman
    Jul 29, 2015 at 8:32

2 Answers 2

0

You can use the following method of XWalkView public API to set User-Agent:

setUserAgentString(java.lang.String userAgent)

See Javadoc for more details.

But regarding XWalkCookieManager I can't suggest anything. Let us know if you find something useful.

0

I used the following method to setUserAgent as told by @comrade

mXWalkView.setUserAgentString("MyCustomUserAgent")

For settings cookies I adopted the following approach. First I get all the cookies that are store in cookieStore of android covert them to String and make a list of strings.

public List<String> getAllCookies(){
        List<Cookie> cookies=instance.cookieStore.getCookies();
        List<String> cookiesList = new ArrayList<String>();
        for(Cookie cookie: cookies)
        {
            String cookieString = cookie.getName() + "=" + cookie.getValue() + "; Path=" + cookie.getPath();// + "; Domain=" + cookie.getDomain()

            String expires = null;
            if (cookie.getExpiryDate() != null) {
                expires = android.text.format.DateFormat.format("EEE, dd-MMM-yyyy hh:mm:ss z", cookie.getExpiryDate()).toString();
                cookieString = cookieString + "; Expires=" + expires;
            }
            cookiesList.add(cookieString);

        }
        return cookiesList;
    }

Once this is done in my CustomXWalkActivity onPageLoadFinished I get list of those cookie convert them to JSONObject and then call a javascript function syncCookies

 private boolean isLoadingFirstTime=true;
 private void updateCookies() {
     List<String> cookies = getAllCookies();
     JSONObject cookiesObj = new JSONObject();

     for(String cookie : cookies){
         Log.d(TAG, "cookie:" + cookie);
         int iend = cookie.indexOf("=");
         try {
            cookiesObj.put(cookie.substring(0, iend), cookie);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

     }
     Log.d(TAG, "All the cookies:" + cookiesObj);

     StringBuilder buf=new StringBuilder("javascript:syncCookies('"+cookiesObj+"')");
     mXWalkView.load(buf.toString(), null);

}
    @Override
    public void onLoadFinished(XWalkView view, String url) {
            Log.d(TAG, "load finished:"+url);
            if(isLoadingFirstTime){
                updateCookies();
                isLoadingFirstTime=false;
            }
            mXWalkView.clearCache(true);

            super.onLoadFinished(view, url);
        }

And then on Javascript side I have the following code to set cookies on the document

function syncCookies(cookies){
    console.log("sync cookies called",cookies);
    var allCookies=JSON.parse(cookies);
    for (var key in allCookies) {
        console.log("cookie to be set", allCookies[key]);
        document.cookie=testCookies[key];
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.