CB-5059 Adds CookieManager abstraction for pluggable webviews (close #151)

Crosswalk and GeckoView implementations of CordovaWebView can provide
their own ICordovaCookieManager implementation for plugins to use.
This commit is contained in:
Darryl Pogue 2015-01-26 20:45:25 -08:00 committed by Andrew Grieve
parent b59705bed4
commit 8cf8da5776
4 changed files with 101 additions and 5 deletions

View File

@ -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();
}
};

View File

@ -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;
@ -124,6 +125,7 @@ public class AndroidWebView extends WebView implements CordovaWebView {
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();
@ -136,11 +138,6 @@ public class AndroidWebView extends WebView implements CordovaWebView {
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));
@ -800,6 +797,11 @@ public class AndroidWebView extends WebView implements CordovaWebView {
return preferences;
}
@Override
public ICordovaCookieManager getCookieManager() {
return cookieManager;
}
@Override
public Object postMessage(String id, Object data) {
return pluginManager.postMessage(id, data);

View File

@ -84,6 +84,7 @@ public interface CordovaWebView {
Whitelist getWhitelist();
Whitelist getExternalWhitelist();
CordovaPreferences getPreferences();
ICordovaCookieManager getCookieManager();
void setNetworkAvailable(boolean online);

View File

@ -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();
};