1 // Copyright 2018 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.components.browser_ui.site_settings;
6 
7 import org.chromium.components.content_settings.ContentSettingValues;
8 import org.chromium.components.embedder_support.browser_context.BrowserContextHandle;
9 
10 /**
11  * Encapsulates clearing the data of {@link Website}s.
12  * Requires native library to be initialized.
13  */
14 public class SiteDataCleaner {
15     /**
16      * Clears the data of the specified site.
17      * @param finishCallback is called when finished.
18      */
clearData( BrowserContextHandle browserContextHandle, Website site, Runnable finishCallback)19     public void clearData(
20             BrowserContextHandle browserContextHandle, Website site, Runnable finishCallback) {
21         String origin = site.getAddress().getOrigin();
22         WebsitePreferenceBridgeJni.get().clearCookieData(browserContextHandle, origin);
23         WebsitePreferenceBridgeJni.get().clearBannerData(browserContextHandle, origin);
24         WebsitePreferenceBridgeJni.get().clearMediaLicenses(browserContextHandle, origin);
25         site.clearAllStoredData(browserContextHandle, finishCallback::run);
26     }
27 
28     /**
29      * Resets the permissions of the specified site.
30      */
resetPermissions(BrowserContextHandle browserContextHandle, Website site)31     public void resetPermissions(BrowserContextHandle browserContextHandle, Website site) {
32         // Clear the permissions.
33         for (ContentSettingException exception : site.getContentSettingExceptions()) {
34             site.setContentSetting(browserContextHandle, exception.getContentSettingType(),
35                     ContentSettingValues.DEFAULT);
36         }
37         for (PermissionInfo info : site.getPermissionInfos()) {
38             site.setContentSetting(browserContextHandle, info.getContentSettingsType(),
39                     ContentSettingValues.DEFAULT);
40         }
41 
42         for (ChosenObjectInfo info : site.getChosenObjectInfo()) {
43             info.revoke(browserContextHandle);
44         }
45     }
46 }
47