|
| 1 | +# Background |
| 2 | +The WebView2 team has been asked for an API to allow end developers to clear the browsing data that is stored in the User Data Folder. Developers want to be able to clear data between each of their customers, clear space, and to clear data on the fly. |
| 3 | + |
| 4 | +Currently developers can delete the entire User Data Folder to clear this data. This has a few drawbacks: it removes the entire user data folder instead of specific parts which incurs performance costs later on, the WebView must be shutdown fully and then re-initialized, and deleting the entire User Data Folder is a complex process. |
| 5 | +We are creating an API that will allow developers to clear the browsing data programatically in which the developer can specify the data kind to clear. |
| 6 | + |
| 7 | +In this document we describe the updated API. We'd appreciate your feedback. |
| 8 | + |
| 9 | + |
| 10 | +# Description |
| 11 | +We expose browsing data clearing in two asynchronous APIs: |
| 12 | +```IDL |
| 13 | +HRESULT ClearBrowsingData( |
| 14 | + [in] COREWEBVIEW2_BROWSING_DATA_KINDS dataKinds, |
| 15 | + [in] ICoreWebView2ClearBrowsingDataCompletedHandler* handler); |
| 16 | +
|
| 17 | +HRESULT ClearBrowsingDataInTimeRange( |
| 18 | + [in] COREWEBVIEW2_BROWSING_DATA_KINDS dataKinds, |
| 19 | + [in] double startTime, |
| 20 | + [in] double endTime, |
| 21 | + [in] ICoreWebView2ClearBrowsingDataCompletedHandler* handler); |
| 22 | +``` |
| 23 | +The first method takes `COREWEBVIEW2_BROWSING_DATA_KINDS` which corresponds to the data type(s) to clear as well as a handler which will indicate if the proper data has been cleared successfully. This method clears the data for all time. |
| 24 | + |
| 25 | +The second method takes the same parameters for the dataKinds and handler, as well as a start and end time in which the API should clear the corresponding data between. The time parameters correspond to how many seconds have passed since the UNIX epoch. |
| 26 | + |
| 27 | +Both methods clear the data for the associated profile in which the method is called on. |
| 28 | + |
| 29 | +# Examples |
| 30 | + |
| 31 | +## Win32 C++ |
| 32 | +```cpp |
| 33 | + |
| 34 | +/// Function to clear the autofill data from the last hour |
| 35 | +void ClearAutofillData() |
| 36 | +{ |
| 37 | + wwil::com_ptr<ICoreWebView2> coreWebView2; |
| 38 | + CHECK_FAILURE(m_controller->get_CoreWebView2(&coreWebView2)); |
| 39 | + |
| 40 | + auto webview7 = coreWebView2.try_query<ICoreWebView2_7>(); |
| 41 | + if (webview7) |
| 42 | + { |
| 43 | + wil::com_ptr<ICoreWebView2Profile> profile; |
| 44 | + CHECK_FAILURE(webview7->get_Profile(&profile)); |
| 45 | + double endTime = (double)std::time(nullptr); |
| 46 | + double startTime = endTime - 3600; |
| 47 | + /// Get the current time and offset the current time by 3600 seconds to clear the data |
| 48 | + /// from the start time (one hour ago), until the end time (present time). |
| 49 | + /// This will clear the data for the last hour. |
| 50 | + COREWEBVIEW2_BROWSING_DATA_KINDS dataKinds = (COREWEBVIEW2_BROWSING_DATA_KINDS) |
| 51 | + (COREWEBVIEW2_BROWSING_DATA_KINDS_GENERAL_AUTOFILL | |
| 52 | + COREWEBVIEW2_BROWSING_DATA_KINDS_PASSWORD_AUTOSAVE)); |
| 53 | + CHECK_FAILURE(profile->ClearBrowsingDataInTimeRange( |
| 54 | + dataKinds, startTime, endTime, |
| 55 | + Callback<ICoreWebView2StagingClearBrowsingDataCompletedHandler>( |
| 56 | + [this](HRESULT error) |
| 57 | + -> HRESULT { |
| 58 | + LPCWSTR result = error == S_OK ? L"Succeeded" : L"Failed"; |
| 59 | + RunAsync([this, result]() { |
| 60 | + MessageBox(nullptr, result, L"Clear Browsing Data", MB_OK); |
| 61 | + }); |
| 62 | + return S_OK; |
| 63 | + }) |
| 64 | + .Get())); |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### .NET, WinRT |
| 70 | +```c# |
| 71 | + |
| 72 | +private void ClearAutofillData() |
| 73 | +{ |
| 74 | + CoreWebView2Profile profile; |
| 75 | + if (webView.CoreWebView2 != null) |
| 76 | + { |
| 77 | + profile = webView.CoreWebView2.Profile; |
| 78 | + double endTime = DateTimeOffset.Now.ToUnixTimeSeconds(); |
| 79 | + // Get the current time which is the time in which the browsing data will be cleared until. |
| 80 | + double startTime = endTime - 3600; |
| 81 | + // Offset the current time by 3600 seconds to clear the browsing data from the last hour. |
| 82 | + CoreWebView2BrowsingDataKinds dataKinds = (CoreWebView2BrowsingDataKinds)(CoreWebView2BrowsingDataKinds.GeneralAutofill | CoreWebView2BrowsingDataKinds.PasswordAutosave); |
| 83 | + await profile.ClearBrowsingDataAsync(dataKinds, startTime, endTime); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +``` |
| 88 | + |
| 89 | +# API Notes |
| 90 | +See [API Details](#api-details) section below for API reference. |
| 91 | + |
| 92 | + |
| 93 | +# API Details |
| 94 | + |
| 95 | +## Win32 C++ |
| 96 | + |
| 97 | +```IDL |
| 98 | +interface ICoreWebView2ClearBrowsingDataCompletedHandler; |
| 99 | +
|
| 100 | +/// Specifies the datatype for the |
| 101 | +/// `ICoreWebView2Profile::ClearBrowsingData` method. |
| 102 | +[v1_enum] |
| 103 | +typedef enum COREWEBVIEW2_BROWSING_DATA_KINDS { |
| 104 | + /// Specifies data stored by the AppCache DOM feature. |
| 105 | + COREWEBVIEW2_BROWSING_DATA_KINDS_APP_CACHE = 1 << 0, |
| 106 | +
|
| 107 | + /// Specifies file systems data. |
| 108 | + COREWEBVIEW2_BROWSING_DATA_KINDS_FILE_SYSTEMS = 1 << 1, |
| 109 | +
|
| 110 | + /// Specifies data stored by the IndexedDB DOM feature. |
| 111 | + COREWEBVIEW2_BROWSING_DATA_KINDS_INDEXED_DB = 1 << 2, |
| 112 | +
|
| 113 | + /// Specifies data stored by the localStorage DOM API. |
| 114 | + COREWEBVIEW2_BROWSING_DATA_KINDS_LOCAL_STORAGE = 1 << 3, |
| 115 | +
|
| 116 | + /// Specifies data stored by the Web SQL database DOM API. |
| 117 | + COREWEBVIEW2_BROWSING_DATA_KINDS_WEB_SQL = 1 << 4, |
| 118 | +
|
| 119 | + /// Specifies cache storage which stores the network requests |
| 120 | + /// and responses. |
| 121 | + COREWEBVIEW2_BROWSING_DATA_KINDS_CACHE_STORAGE = 1 << 5, |
| 122 | +
|
| 123 | + /// Specifies DOM storage data. This browsing data kind is inclusive |
| 124 | + /// of COREWEBVIEW2_BROWSING_DATA_KINDS_APP_CACHE, |
| 125 | + /// COREWEBVIEW2_BROWSING_DATA_KINDS_FILE_SYSTEMS, |
| 126 | + /// COREWEBVIEW2_BROWSING_DATA_KINDS_INDEXED_DB, |
| 127 | + /// COREWEBVIEW2_BROWSING_DATA_KINDS_LOCAL_STORAGE, |
| 128 | + /// COREWEBVIEW2_BROWSING_DATA_KINDS_WEB_SQL, |
| 129 | + /// COREWEBVIEW2_BROWSING_DATA_KINDS_CACHE_STORAGE. |
| 130 | + COREWEBVIEW2_BROWSING_DATA_KINDS_DOM_STORAGE = |
| 131 | + COREWEBVIEW2_BROWSING_DATA_KINDS_APP_CACHE| |
| 132 | + COREWEBVIEW2_BROWSING_DATA_KINDS_FILE_SYSTEMS | |
| 133 | + COREWEBVIEW2_BROWSING_DATA_KINDS_INDEXED_DB | |
| 134 | + COREWEBVIEW2_BROWSING_DATA_KINDS_LOCAL_STORAGE | |
| 135 | + COREWEBVIEW2_BROWSING_DATA_KINDS_WEB_SQL | |
| 136 | + COREWEBVIEW2_BROWSING_DATA_KINDS_CACHE_STORAGE, |
| 137 | +
|
| 138 | + /// Specifies HTTP cookies data. |
| 139 | + COREWEBVIEW2_BROWSING_DATA_KINDS_COOKIES = 1 << 6, |
| 140 | +
|
| 141 | + /// Specifies site data. This browsing data kind |
| 142 | + /// is inclusive of COREWEBVIEW2_BROWSING_DATA_KINDS_DOM_STORAGE and |
| 143 | + /// COREWEBVIEW2_BROWSING_DATA_KINDS_COOKIES. |
| 144 | + COREWEBVIEW2_BROWSING_DATA_KINDS_SITE = COREWEBVIEW2_BROWSING_DATA_KINDS_DOM_STORAGE | |
| 145 | + COREWEBVIEW2_BROWSING_DATA_KINDS_COOKIES, |
| 146 | +
|
| 147 | + /// Specifies content in the HTTP cache including images and other files. |
| 148 | + COREWEBVIEW2_BROWSING_DATA_KINDS_HTTP_CACHE = 1 << 7, |
| 149 | +
|
| 150 | + /// Specifies download history data. |
| 151 | + COREWEBVIEW2_BROWSING_DATA_KINDS_DOWNLOAD_HISTORY = 1 << 8, |
| 152 | +
|
| 153 | + /// Specifies general autofill form data. |
| 154 | + /// This excludes password information and includes information like: |
| 155 | + /// names, street and email addresses, phone numbers, and arbitrary input. |
| 156 | + /// This also includes payment data. |
| 157 | + COREWEBVIEW2_BROWSING_DATA_KINDS_GENERAL_AUTOFILL = 1 << 9, |
| 158 | +
|
| 159 | + /// Specifies password autosave data. |
| 160 | + COREWEBVIEW2_BROWSING_DATA_KINDS_PASSWORD_AUTOSAVE = 1 << 10, |
| 161 | +
|
| 162 | + /// Specifies browsing history data. |
| 163 | + COREWEBVIEW2_BROWSING_DATA_KINDS_BROWSING_HISTORY = 1 << 11, |
| 164 | +
|
| 165 | + /// Specifies settings data. |
| 166 | + COREWEBVIEW2_BROWSING_DATA_KINDS_SETTINGS = 1 << 12, |
| 167 | +
|
| 168 | + /// Specifies profile data that should be wiped to make it look like a new profile. |
| 169 | + /// This browsing data kind is inclusive of COREWEBVIEW2_BROWSING_DATA_KINDS_SITE, |
| 170 | + /// COREWEBVIEW2_BROWSING_DATA_KINDS_HTTP_CACHE, |
| 171 | + /// COREWEBVIEW2_BROWSING_DATA_KINDS_DOWNLOAD_HISTORY, |
| 172 | + /// COREWEBVIEW2_BROWSING_DATA_KINDS_GENERAL_AUTOFILL, |
| 173 | + /// COREWEBVIEW2_BROWSING_DATA_KINDS_PASSWORD_AUTOSAVE, |
| 174 | + /// COREWEBVIEW2_BROWSING_DATA_KINDS_BROWSING_HISTORY, and |
| 175 | + /// COREWEBVIEW2_BROWSING_DATA_KINDS_SETTINGS. |
| 176 | + COREWEBVIEW2_BROWSING_DATA_KINDS_PROFILE = |
| 177 | + COREWEBVIEW2_BROWSING_DATA_KINDS_SITE | |
| 178 | + COREWEBVIEW2_BROWSING_DATA_KINDS_HTTP_CACHE | |
| 179 | + COREWEBVIEW2_BROWSING_DATA_KINDS_DOWNLOAD_HISTORY | |
| 180 | + COREWEBVIEW2_BROWSING_DATA_KINDS_GENERAL_AUTOFILL | |
| 181 | + COREWEBVIEW2_BROWSING_DATA_KINDS_PASSWORD_AUTOSAVE | |
| 182 | + COREWEBVIEW2_BROWSING_DATA_KINDS_BROWSING_HISTORY | |
| 183 | + COREWEBVIEW2_BROWSING_DATA_KINDS_SETTINGS, |
| 184 | +} COREWEBVIEW2_BROWSING_DATA_KINDS; |
| 185 | +
|
| 186 | +[uuid(DAF8B1F9-276D-410C-B481-58CBADF85C9C), object, pointer_default(unique)] |
| 187 | +interface ICoreWebView2Profile : IUnknown { |
| 188 | +
|
| 189 | + /// Clear browsing data based on a data type. This method takes two parameters, |
| 190 | + /// the first being a mask of one or more `COREWEBVIEW2_BROWSING_DATA_KINDS`. OR operation(s) |
| 191 | + /// can be applied to multiple `COREWEBVIEW2_BROWSING_DATA_KINDS` to create a mask |
| 192 | + /// representing those data types. The browsing data kinds that are supported |
| 193 | + /// are listed below. These data kinds follow a hierarchical structure in which |
| 194 | + /// nested bullet points are included in their parent bullet point's data kind. |
| 195 | + /// Ex: DOM storage is encompassed in site data which is encompassed in the profile data. |
| 196 | + /// * Profile |
| 197 | + /// * Site Data |
| 198 | + /// * DOM Storage: App Cache, File Systems, Indexed DB, Local Storage, Web SQL, Cache |
| 199 | + /// Storage |
| 200 | + /// * Cookies |
| 201 | + /// * HTTP Cache |
| 202 | + /// * Download History |
| 203 | + /// * General Autofill |
| 204 | + /// * Password Autosave |
| 205 | + /// * Browsing History |
| 206 | + /// * Settings |
| 207 | + /// The completed handler will be invoked when the browsing data has been cleared and will |
| 208 | + /// indicate if the specified data was properly cleared. |
| 209 | + /// If the WebView object is closed before the clear browsing data operation |
| 210 | + /// has completed, the handler will be released, but not invoked. In this case |
| 211 | + /// the clear browsing data operation may or may not be completed. |
| 212 | + /// ClearBrowsingData clears the `dataKinds` for all time. |
| 213 | + HRESULT ClearBrowsingData( |
| 214 | + [in] COREWEBVIEW2_BROWSING_DATA_KINDS dataKinds, |
| 215 | + [in] ICoreWebView2ClearBrowsingDataCompletedHandler* handler); |
| 216 | + |
| 217 | + /// ClearBrowsingDataInTimeRange behaves like ClearBrowsingData except that it |
| 218 | + /// takes in two additional parameters for the start and end time for which it |
| 219 | + /// should clear the data between. The `startTime` and `endTime` |
| 220 | + /// parameters correspond to the number of seconds since the UNIX epoch. |
| 221 | + /// |
| 222 | + /// \snippet AppWindow.cpp ClearBrowsingData |
| 223 | + HRESULT ClearBrowsingDataInTimeRange( |
| 224 | + [in] COREWEBVIEW2_BROWSING_DATA_KINDS dataKinds, |
| 225 | + [in] double startTime, |
| 226 | + [in] double endTime, |
| 227 | + [in] ICoreWebView2ClearBrowsingDataCompletedHandler* handler); |
| 228 | +} |
| 229 | +
|
| 230 | +/// The caller implements this interface to receive the `ClearBrowsingData` result. |
| 231 | +[uuid(27676699-FE17-4E2B-8C1B-267395A04ED5), object, pointer_default(unique)] |
| 232 | +interface ICoreWebView2ClearBrowsingDataCompletedHandler : IUnknown { |
| 233 | + /// Provide the completion status of the corresponding asynchronous method. |
| 234 | + HRESULT Invoke([in] HRESULT errorCode); |
| 235 | +} |
| 236 | +
|
| 237 | +``` |
| 238 | +### .NET, WinRT |
| 239 | +```c# |
| 240 | +namespace Microsoft.Web.WebView2.Core |
| 241 | +{ |
| 242 | + [Flags] enum CoreWebView2BrowsingDataKinds |
| 243 | + { |
| 244 | + AppCache = 1, |
| 245 | + FileSystems = 2, |
| 246 | + IndexedDb = 4, |
| 247 | + LocalStorage = 8, |
| 248 | + WebSql = 16, |
| 249 | + CacheStorage = 32, |
| 250 | + DomStorage = 63, |
| 251 | + Cookies = 64, |
| 252 | + Site = 127, |
| 253 | + HttpCache = 128, |
| 254 | + DownloadHistory = 256, |
| 255 | + GeneralAutofill = 512, |
| 256 | + PasswordAutosave = 1024, |
| 257 | + BrowsingHistory = 2048, |
| 258 | + Settings = 4096, |
| 259 | + Profile = 8191, |
| 260 | + }; |
| 261 | + |
| 262 | + public partial class CoreWebView2Profile |
| 263 | + { |
| 264 | + public async Task<bool> ClearBrowsingDataAsync(CoreWebView2BrowsingDataKinds dataKinds); |
| 265 | + public async Task<bool> ClearBrowsingDataAsync(CoreWebView2BrowsingDataKinds dataKinds, double startTime, double endTime); |
| 266 | + } |
| 267 | +} |
| 268 | +``` |
0 commit comments