1 // Copyright 2020 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.weblayer_private;
6 
7 import android.content.Context;
8 import android.content.Intent;
9 import android.graphics.Bitmap;
10 import android.graphics.drawable.BitmapDrawable;
11 import android.graphics.drawable.Drawable;
12 import android.webkit.ValueCallback;
13 
14 import androidx.annotation.NonNull;
15 import androidx.annotation.Nullable;
16 
17 import org.chromium.base.Callback;
18 import org.chromium.base.StrictModeContext;
19 import org.chromium.base.supplier.Supplier;
20 import org.chromium.components.browser_ui.site_settings.ContentSettingsResources;
21 import org.chromium.components.browser_ui.site_settings.SiteSettingsCategory;
22 import org.chromium.components.browser_ui.site_settings.SiteSettingsClient;
23 import org.chromium.components.content_settings.ContentSettingsType;
24 import org.chromium.components.content_settings.CookieControlsBridge;
25 import org.chromium.components.content_settings.CookieControlsObserver;
26 import org.chromium.components.embedder_support.browser_context.BrowserContextHandle;
27 import org.chromium.components.embedder_support.util.UrlConstants;
28 import org.chromium.components.page_info.PageInfoControllerDelegate;
29 import org.chromium.content_public.browser.WebContents;
30 import org.chromium.ui.modaldialog.ModalDialogManager;
31 import org.chromium.url.GURL;
32 import org.chromium.weblayer_private.interfaces.ObjectWrapper;
33 import org.chromium.weblayer_private.interfaces.SiteSettingsIntentHelper;
34 
35 /**
36  * WebLayer's customization of PageInfoControllerDelegate.
37  */
38 public class PageInfoControllerDelegateImpl extends PageInfoControllerDelegate {
39     private final Context mContext;
40     private final WebContents mWebContents;
41     private final ProfileImpl mProfile;
42 
create(WebContents webContents)43     static PageInfoControllerDelegateImpl create(WebContents webContents) {
44         TabImpl tab = TabImpl.fromWebContents(webContents);
45         assert tab != null;
46         return new PageInfoControllerDelegateImpl(tab.getBrowser().getContext(), webContents,
47                 tab.getProfile(), tab.getBrowser().getWindowAndroid()::getModalDialogManager);
48     }
49 
PageInfoControllerDelegateImpl(Context context, WebContents webContents, ProfileImpl profile, Supplier<ModalDialogManager> modalDialogManager)50     private PageInfoControllerDelegateImpl(Context context, WebContents webContents,
51             ProfileImpl profile, Supplier<ModalDialogManager> modalDialogManager) {
52         super(modalDialogManager, new AutocompleteSchemeClassifierImpl(),
53                 /** vrHandler= */ null,
54                 /** isSiteSettingsAvailable= */
55                 isHttpOrHttps(webContents.getVisibleUrl()),
56                 /** cookieControlsShown= */
57                 CookieControlsBridge.isCookieControlsEnabled(profile));
58         mContext = context;
59         mWebContents = webContents;
60         mProfile = profile;
61     }
62 
63     /**
64      * {@inheritDoc}
65      */
66     @Override
showSiteSettings(String url)67     public void showSiteSettings(String url) {
68         Intent intent = SiteSettingsIntentHelper.createIntentForSingleWebsite(
69                 mContext, mProfile.getName(), mProfile.isIncognito(), url);
70 
71         // Disabling StrictMode to avoid violations (https://crbug.com/819410).
72         launchIntent(intent);
73     }
74 
75     @Override
showCookieSettings()76     public void showCookieSettings() {
77         String category = SiteSettingsCategory.preferenceKey(SiteSettingsCategory.Type.COOKIES);
78         String title = mContext.getResources().getString(
79                 ContentSettingsResources.getTitle(ContentSettingsType.COOKIES));
80         Intent intent = SiteSettingsIntentHelper.createIntentForSingleCategory(
81                 mContext, mProfile.getName(), mProfile.isIncognito(), category, title);
82         launchIntent(intent);
83     }
84 
launchIntent(Intent intent)85     private void launchIntent(Intent intent) {
86         // Disabling StrictMode to avoid violations (https://crbug.com/819410).
87         try (StrictModeContext ignored = StrictModeContext.allowDiskReads()) {
88             mContext.startActivity(intent);
89         }
90     }
91 
92     /**
93      * {@inheritDoc}
94      */
95     @Override
96     @NonNull
createCookieControlsBridge(CookieControlsObserver observer)97     public CookieControlsBridge createCookieControlsBridge(CookieControlsObserver observer) {
98         return new CookieControlsBridge(observer, mWebContents, null);
99     }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     @NonNull
getBrowserContext()106     public BrowserContextHandle getBrowserContext() {
107         return mProfile;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     @NonNull
getSiteSettingsClient()115     public SiteSettingsClient getSiteSettingsClient() {
116         return new WebLayerSiteSettingsClient(getBrowserContext());
117     }
118 
119     @Override
getFavicon(String url, Callback<Drawable> callback)120     public void getFavicon(String url, Callback<Drawable> callback) {
121         mProfile.getCachedFaviconForPageUri(
122                 url, ObjectWrapper.wrap((ValueCallback<Bitmap>) (bitmap) -> {
123                     if (bitmap != null) {
124                         callback.onResult(new BitmapDrawable(mContext.getResources(), bitmap));
125                     } else {
126                         callback.onResult(null);
127                     }
128                 }));
129     }
130 
131     @Override
132     @Nullable
getPreviewUiIcon()133     public Drawable getPreviewUiIcon() {
134         return null;
135     }
136 
isHttpOrHttps(GURL url)137     private static boolean isHttpOrHttps(GURL url) {
138         String scheme = url.getScheme();
139         return UrlConstants.HTTP_SCHEME.equals(scheme) || UrlConstants.HTTPS_SCHEME.equals(scheme);
140     }
141 }
142