Skip to content

Commit

Permalink
CB-5059 Adds CookieManager abstraction for pluggable webviews (close a…
Browse files Browse the repository at this point in the history
…pache#151)

Crosswalk and GeckoView implementations of CordovaWebView can provide
their own ICordovaCookieManager implementation for plugins to use.
  • Loading branch information
dpogue authored and agrieve committed Jan 28, 2015
1 parent b59705b commit 8cf8da5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 5 deletions.
60 changes: 60 additions & 0 deletions framework/src/org/apache/cordova/AndroidCookieManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software 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.
*/

package org.apache.cordova;

import android.os.Build;
import android.webkit.CookieManager;
import android.webkit.WebView;

class AndroidCookieManager implements ICordovaCookieManager {

protected WebView webView;

public AndroidCookieManager(WebView webview) {
webView = webview;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(webView, true);
}
}

public void setCookiesEnabled(boolean accept) {
CookieManager.getInstance().setAcceptCookie(accept);
}

public void setCookie(final String url, final String value) {
CookieManager.getInstance().setCookie(url, value);
}

public String getCookie(final String url) {
return CookieManager.getInstance().getCookie(url);
}

public void clearCookies() {
CookieManager.getInstance().removeAllCookie();
}

public void flush() {
CookieManager.getInstance().flush();
}
};

12 changes: 7 additions & 5 deletions framework/src/org/apache/cordova/AndroidWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class AndroidWebView extends WebView implements CordovaWebView {
private HashSet<Integer> boundKeyCodes = new HashSet<Integer>();

PluginManager pluginManager;
AndroidCookieManager cookieManager;

private BroadcastReceiver receiver;

Expand Down Expand Up @@ -124,6 +125,7 @@ public void init(CordovaInterface cordova, List<PluginEntry> pluginEntries,
this.preferences = preferences;

pluginManager = new PluginManager(this, this.cordova, pluginEntries);
cookieManager = new AndroidCookieManager(this);
resourceApi = new CordovaResourceApi(this.getContext(), pluginManager);
bridge = new CordovaBridge(pluginManager, new NativeToJsMessageQueue(this, cordova), this.cordova.getActivity().getPackageName());
initWebViewSettings();
Expand All @@ -136,11 +138,6 @@ public void init(CordovaInterface cordova, List<PluginEntry> pluginEntries,
new IceCreamCordovaWebViewClient(cordova, this));
}

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(this, true);
}

if (this.chromeClient == null) {
setWebChromeClient(new AndroidChromeClient(cordova, this));
Expand Down Expand Up @@ -800,6 +797,11 @@ public CordovaPreferences getPreferences() {
return preferences;
}

@Override
public ICordovaCookieManager getCookieManager() {
return cookieManager;
}

@Override
public Object postMessage(String id, Object data) {
return pluginManager.postMessage(id, data);
Expand Down
1 change: 1 addition & 0 deletions framework/src/org/apache/cordova/CordovaWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ void init(CordovaInterface cordova, List<PluginEntry> pluginEntries,
Whitelist getWhitelist();
Whitelist getExternalWhitelist();
CordovaPreferences getPreferences();
ICordovaCookieManager getCookieManager();

void setNetworkAvailable(boolean online);

Expand Down
33 changes: 33 additions & 0 deletions framework/src/org/apache/cordova/ICordovaCookieManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software 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.
*/

package org.apache.cordova;

public interface ICordovaCookieManager {

public void setCookiesEnabled(boolean accept);

public void setCookie(final String url, final String value);

public String getCookie(final String url);

public void clearCookies();

public void flush();
};

0 comments on commit 8cf8da5

Please sign in to comment.