Jens
Jens

Reputation: 545

share cookies between XMLHttpRequests and cordova plugin

I'm struggling with crosswalk and cordova plugin javacode to let them share the same cookies.

In javascript an authentication cookie is created and updated during several XMLHttpRequests to my webservice.

In Java, I have an IntentService which onHandleIntent gets called, for example when a location-update occures. Inside of my onHandleIntent I want to call the same webservice as the javascript part mentioned above using the same cookies. To retrieve the cookies I call new XWalkCookieManager().getCookie(url) and use it to make an HTTP-Call in Java including that cookie.

When the app is completly closed (via TaskManager), my onHandleIntent is still called. But I cannot call getCookie(url) since crosswalk is not running. getCookie(url) crashes uncatchable (ndk).

I already tried to cache the current cookie in my MainActivitys onStop, and it works on some device. But on other devices the app often gets closed without proper stop behaviour. So this is very fragile.

So my question is: Is there a way to either get crosswalks cookies when the app is closed or to intercept all requests done by javascript and cache the cookie to have the right one ready when the app is closed?

Upvotes: 0

Views: 374

Answers (1)

Jens
Jens

Reputation: 545

Since someone asks how I solved it, here is my heavy workaround, that contains 3 parts.

  1. Every Request/Response My cordovaapp is using angular and I've put an $http-interceptor that intercepts EVERY response. Whenever a response is retrieved it calls my native plugin and calls an method "updateLocalCookie". In plugins Javacode, I put the actual cookies into a SharedPreferences-String.
  2. Whenever a call from plugin/Java code is needed Get the current cookie from SharedPreferences, don't ask crosswalk to get cookie at all.
  3. Whenever a response in plugin/Java code is retrieved This part is also very important. You normally also want to write back retrieved cookies to crosswalk. But XWalkCookieManager.setCookies is also not possible when app is in killed state. So I extended MainActivity (with cordova hooks after platform add) to contain a BroadcastReciever that writes back cookies to crosswalk. The response-callback will store the new cookies in SharedPreferences AND will send out a broadcast. If the app is running, the BroadcastReciever exists and writes the cookie. If the app is in killed, the MainActivity doesn't run so this broadcast will just be ignored. The last important part is, on every appstart the cookie also must be readed from SharedPreferences and write into crosswalk, because you missed those cookies during app-downtime.

Hope that helpes, feel free to ask/comment again if something is unclear since it is a very confusion workaround.

Upvotes: 1

Related Questions