OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.ntp; |
| 6 |
| 7 import android.app.Activity; |
| 8 import android.net.Uri; |
| 9 |
| 10 import org.chromium.base.VisibleForTesting; |
| 11 import org.chromium.chrome.browser.NativePage; |
| 12 import org.chromium.chrome.browser.Tab; |
| 13 import org.chromium.chrome.browser.UrlConstants; |
| 14 import org.chromium.chrome.browser.enhancedbookmarks.EnhancedBookmarkPage; |
| 15 import org.chromium.chrome.browser.enhancedbookmarks.EnhancedBookmarkUtils; |
| 16 import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
| 17 import org.chromium.chrome.browser.util.FeatureUtilities; |
| 18 import org.chromium.ui.base.DeviceFormFactor; |
| 19 |
| 20 /** |
| 21 * Creates NativePage objects to show chrome-native:// URLs using the native And
roid view system. |
| 22 */ |
| 23 public class NativePageFactory { |
| 24 |
| 25 public static final String CHROME_NATIVE_SCHEME = "chrome-native"; |
| 26 |
| 27 private static NativePageBuilder sNativePageBuilder = new NativePageBuilder(
); |
| 28 |
| 29 @VisibleForTesting |
| 30 static class NativePageBuilder { |
| 31 protected NativePage buildNewTabPage(Activity activity, Tab tab, |
| 32 TabModelSelector tabModelSelector) { |
| 33 if (tab.isIncognito()) { |
| 34 return new IncognitoNewTabPage(activity, tab); |
| 35 } else if (FeatureUtilities.isDocumentMode(activity)) { |
| 36 return new DocumentNewTabPage(activity, tab, tabModelSelector); |
| 37 } else { |
| 38 return new NewTabPage(activity, tab, tabModelSelector); |
| 39 } |
| 40 } |
| 41 |
| 42 protected NativePage buildBookmarksPage(Activity activity, Tab tab, |
| 43 TabModelSelector tabModelSelector) { |
| 44 if (EnhancedBookmarkUtils.isEnhancedBookmarkEnabled( |
| 45 tab.getProfile().getOriginalProfile()) && DeviceFormFactor.i
sTablet(activity)) { |
| 46 return EnhancedBookmarkPage.buildPage(activity, tab); |
| 47 } else { |
| 48 return BookmarksPage.buildPage(activity, tab, tabModelSelector); |
| 49 } |
| 50 } |
| 51 |
| 52 protected NativePage buildRecentTabsPage(Activity activity, Tab tab) { |
| 53 RecentTabsManager recentTabsManager = FeatureUtilities.isDocumentMod
e(activity) |
| 54 ? new DocumentRecentTabsManager(tab, activity, false) |
| 55 : new RecentTabsManager(tab, tab.getProfile(), activity); |
| 56 return new RecentTabsPage(activity, recentTabsManager); |
| 57 } |
| 58 } |
| 59 |
| 60 enum NativePageType { |
| 61 NONE, CANDIDATE, NTP, BOOKMARKS, RECENT_TABS |
| 62 } |
| 63 |
| 64 private static NativePageType nativePageType(String url, NativePage candidat
ePage, |
| 65 boolean isIncognito) { |
| 66 if (url == null) return NativePageType.NONE; |
| 67 |
| 68 Uri uri = Uri.parse(url); |
| 69 if (!CHROME_NATIVE_SCHEME.equals(uri.getScheme())) { |
| 70 return NativePageType.NONE; |
| 71 } |
| 72 |
| 73 String host = uri.getHost(); |
| 74 if (candidatePage != null && candidatePage.getHost().equals(host)) { |
| 75 return NativePageType.CANDIDATE; |
| 76 } |
| 77 |
| 78 if (UrlConstants.NTP_HOST.equals(host)) { |
| 79 return NativePageType.NTP; |
| 80 } else if (UrlConstants.BOOKMARKS_HOST.equals(host)) { |
| 81 return NativePageType.BOOKMARKS; |
| 82 } else if (UrlConstants.RECENT_TABS_HOST.equals(host) && !isIncognito) { |
| 83 return NativePageType.RECENT_TABS; |
| 84 } else { |
| 85 return NativePageType.NONE; |
| 86 } |
| 87 } |
| 88 |
| 89 /** |
| 90 * Returns a NativePage for displaying the given URL if the URL is a valid c
hrome-native URL, |
| 91 * or null otherwise. If candidatePage is non-null and corresponds to the UR
L, it will be |
| 92 * returned. Otherwise, a new NativePage will be constructed. |
| 93 * |
| 94 * @param url The URL to be handled. |
| 95 * @param candidatePage A NativePage to be reused if it matches the url, or
null. |
| 96 * @param tab The Tab that will show the page. |
| 97 * @param tabModelSelector The TabModelSelector containing the tab. |
| 98 * @param activity The activity used to create the views for the page. |
| 99 * @return A NativePage showing the specified url or null. |
| 100 */ |
| 101 public static NativePage createNativePageForURL(String url, NativePage candi
datePage, |
| 102 Tab tab, TabModelSelector tabModelSelector, Activity activity) { |
| 103 return createNativePageForURL(url, candidatePage, tab, tabModelSelector,
activity, |
| 104 tab.isIncognito()); |
| 105 } |
| 106 |
| 107 @VisibleForTesting |
| 108 static NativePage createNativePageForURL(String url, NativePage candidatePag
e, |
| 109 Tab tab, TabModelSelector tabModelSelector, Activity activity, |
| 110 boolean isIncognito) { |
| 111 NativePage page; |
| 112 |
| 113 switch (nativePageType(url, candidatePage, isIncognito)) { |
| 114 case NONE: |
| 115 return null; |
| 116 case CANDIDATE: |
| 117 page = candidatePage; |
| 118 break; |
| 119 case NTP: |
| 120 page = sNativePageBuilder.buildNewTabPage(activity, tab, tabMode
lSelector); |
| 121 break; |
| 122 case BOOKMARKS: |
| 123 page = sNativePageBuilder.buildBookmarksPage(activity, tab, tabM
odelSelector); |
| 124 if (page == null) return null; // Enhanced Bookmarks page is no
t shown on phone |
| 125 break; |
| 126 case RECENT_TABS: |
| 127 page = sNativePageBuilder.buildRecentTabsPage(activity, tab); |
| 128 break; |
| 129 default: |
| 130 assert false; |
| 131 return null; |
| 132 } |
| 133 page.updateForUrl(url); |
| 134 return page; |
| 135 } |
| 136 |
| 137 /** |
| 138 * Returns whether the URL would navigate to a native page. |
| 139 * |
| 140 * @param url The URL to be checked. |
| 141 * @param isIncognito Whether the page will be displayed in incognito mode. |
| 142 * @return Whether the host and the scheme of the passed in URL matches one
of the supported |
| 143 * native pages. |
| 144 */ |
| 145 public static boolean isNativePageUrl(String url, boolean isIncognito) { |
| 146 return nativePageType(url, null, isIncognito) != NativePageType.NONE; |
| 147 } |
| 148 |
| 149 @VisibleForTesting |
| 150 static void setNativePageBuilderForTesting(NativePageBuilder builder) { |
| 151 sNativePageBuilder = builder; |
| 152 } |
| 153 } |
OLD | NEW |