Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDelegateFactory.java | 
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDelegateFactory.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDelegateFactory.java | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..0d213ae65af152832f5065d1c50ad269bcde3228 | 
| --- /dev/null | 
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDelegateFactory.java | 
| @@ -0,0 +1,113 @@ | 
| +// Copyright 2015 The Chromium Authors. All rights reserved. | 
| 
 
gone
2016/05/31 18:26:55
It's 2016.
 
 | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +package org.chromium.chrome.browser.webapps; | 
| + | 
| +import android.content.Intent; | 
| +import android.text.TextUtils; | 
| + | 
| +import org.chromium.base.ContextUtils; | 
| +import org.chromium.base.VisibleForTesting; | 
| +import org.chromium.chrome.browser.ShortcutHelper; | 
| +import org.chromium.chrome.browser.tab.Tab; | 
| +import org.chromium.chrome.browser.tab.TabDelegateFactory; | 
| +import org.chromium.chrome.browser.tab.TabWebContentsDelegateAndroid; | 
| +import org.chromium.chrome.browser.tab.TopControlsVisibilityDelegate; | 
| +import org.chromium.chrome.browser.util.UrlUtilities; | 
| +import org.chromium.components.security_state.ConnectionSecurityLevel; | 
| + | 
| +/** | 
| + * A {@link TabDelegateFactory} class to be used in all {@link Tab} instances owned | 
| 
 
gone
2016/05/31 18:26:55
Wrap at 100 everywhere.
 
 | 
| + * by a {@link FullScreenActivity}. | 
| + */ | 
| +public class WebappDelegateFactory extends FullScreenDelegateFactory { | 
| + private static class WebContentsDelegate extends TabWebContentsDelegateAndroid { | 
| 
 
Xi Han
2016/05/30 21:26:55
Rename WebContentsDelegate to WebappContentsDelega
 
pkotwicz
2016/05/31 02:32:01
I named the class WebContentsDelegate as a shorten
 
Xi Han
2016/05/31 14:18:27
Usually the subclass name is more specific than th
 
gone
2016/05/31 18:26:55
Naming this WebContentsDelegate goes against both
 
 | 
| + private final WebappActivity mActivity; | 
| + | 
| + public WebContentsDelegate(WebappActivity activity, Tab tab) { | 
| + super(tab); | 
| + mActivity = activity; | 
| + } | 
| + | 
| + @Override | 
| + public void activateContents() { | 
| + String startUrl = mActivity.getWebappInfo().uri().toString(); | 
| + | 
| + // Create an Intent that will be fired toward the WebappLauncherActivity, which in turn | 
| + // will fire an Intent to launch the correct WebappActivity. On L+ this could probably | 
| + // be changed to call AppTask.moveToFront(), but for backwards compatibility we relaunch | 
| + // it the hard way. | 
| + Intent intent = new Intent(); | 
| + intent.setAction(WebappLauncherActivity.ACTION_START_WEBAPP); | 
| + intent.setClassName(WebappLauncherActivity.class.getPackage().getName(), | 
| + WebappLauncherActivity.class.getName()); | 
| + mActivity.getWebappInfo().setWebappIntentExtras(intent); | 
| + | 
| + intent.putExtra( | 
| + ShortcutHelper.EXTRA_MAC, ShortcutHelper.getEncodedMac(mActivity, startUrl)); | 
| + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 
| + ContextUtils.getApplicationContext().startActivity(intent); | 
| + } | 
| + } | 
| + | 
| + @VisibleForTesting | 
| + static class TopControlsDelegate extends TopControlsVisibilityDelegate { | 
| + private final WebappActivity mActivity; | 
| + | 
| + public TopControlsDelegate(WebappActivity activity, Tab tab) { | 
| + super(tab); | 
| + mActivity = activity; | 
| + } | 
| + | 
| + @Override | 
| + public boolean isShowingTopControlsEnabled() { | 
| + if (!super.isShowingTopControlsEnabled()) return false; | 
| + | 
| + String webappStartUrl = mActivity.getWebappInfo().uri().toString(); | 
| + return shouldShowTopControls(webappStartUrl, mTab.getUrl(), mTab.getSecurityLevel()); | 
| + } | 
| + | 
| + @Override | 
| + public boolean isHidingTopControlsEnabled() { | 
| + return !isShowingTopControlsEnabled(); | 
| + } | 
| + | 
| + /** | 
| + * Returns whether the top controls should be shown when a webapp is navigated to | 
| + * {@link url}. | 
| + * @param webappStartUrl The webapp's URL when it is opened from the home screen. | 
| + * @param url | 
| 
 
gone
2016/05/31 18:26:56
Fill in these parameters.
 
 | 
| + * @param securityLevel | 
| + * @return Whether the top controls should be shown for {@link url}. | 
| + */ | 
| + public static boolean shouldShowTopControls( | 
| + String webappStartUrl, String url, int securityLevel) { | 
| + // Do not show top controls when URL is not ready yet. | 
| + boolean visible = false; | 
| + if (TextUtils.isEmpty(url)) return false; | 
| + | 
| + boolean isSameWebsite = | 
| + UrlUtilities.sameDomainOrHost(webappStartUrl, url, true); | 
| + visible = !isSameWebsite || securityLevel == ConnectionSecurityLevel.SECURITY_ERROR | 
| + || securityLevel == ConnectionSecurityLevel.SECURITY_WARNING; | 
| + return visible; | 
| + } | 
| + } | 
| + | 
| + private final WebappActivity mActivity; | 
| + | 
| + public WebappDelegateFactory(WebappActivity activity) { | 
| + mActivity = activity; | 
| + } | 
| + | 
| + @Override | 
| + public TabWebContentsDelegateAndroid createWebContentsDelegate(Tab tab) { | 
| + return new WebContentsDelegate(mActivity, tab); | 
| + } | 
| + | 
| + @Override | 
| + public TopControlsVisibilityDelegate createTopControlsVisibilityDelegate(Tab tab) { | 
| + return new TopControlsDelegate(mActivity, tab); | 
| + } | 
| +} |