|
| 1 | + |
| 2 | +Title |
| 3 | +=== |
| 4 | +UserDataFolder API |
| 5 | + |
| 6 | +# Background |
| 7 | +You may have a need to cleanup or possibly add to the data being |
| 8 | +stored in the WebView2 user data directory. This directory can be either |
| 9 | +configured by the you during creation of the webview2 control or calculated |
| 10 | +at runtime by the control if it wasn't set during creation. |
| 11 | + |
| 12 | +The current WebView2 code is designed to use a the directory that the hosting |
| 13 | +application is running in as the default location. This has resulted in issues |
| 14 | +if running from a secure location (IE the program files tree). Work is in process |
| 15 | +to change that default and will be forthcoming in later changes |
| 16 | + |
| 17 | +With the current guidance it is possible for you to have built |
| 18 | +logic into your application with an assumption where the User Data is |
| 19 | +located. WebView2 changing that default can then result in a code error or |
| 20 | +possible crash. |
| 21 | + |
| 22 | +This api will provide the you a way to get the directory that is |
| 23 | +currently being used by the WebView for storage of the user data. |
| 24 | + |
| 25 | +Using this returned directory is safer for the long run so that if WebView2 |
| 26 | +changes the underlying path default logic the application will be able to adapt |
| 27 | +automatically. |
| 28 | + |
| 29 | + |
| 30 | +# Description |
| 31 | + |
| 32 | +Returns the user data folder that all CoreWebView2's created from this |
| 33 | +environment are using. |
| 34 | +This could be either the value passed in by you when creating the |
| 35 | +environment object or the calculated one for default handling. And it will |
| 36 | +always be an absolute path. |
| 37 | + |
| 38 | + |
| 39 | +# Examples |
| 40 | + |
| 41 | +```cpp |
| 42 | +HRESULT UserDataFolder() |
| 43 | +{ |
| 44 | + base::ScopedTempDir temp_dir; |
| 45 | + |
| 46 | + CreateCoreWebView2EnvironmentWithOptions( |
| 47 | + L".", temp_dir.GetPath().value().c_str(), nullptr, |
| 48 | + Microsoft::WRL::Callback< |
| 49 | + ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler> |
| 50 | + ( |
| 51 | + [&temp_dir, &environment_created] |
| 52 | + (HRESULT result, ICoreWebView2Environment* webview_environment) -> |
| 53 | + HRESULT { |
| 54 | + |
| 55 | + ScopedCoMemString environmentUserDataFolder; |
| 56 | + webview_environment->get_UserDataFolder( |
| 57 | + &environmentUserDataFolder); |
| 58 | + |
| 59 | + return S_OK; |
| 60 | + }).Get()); |
| 61 | + |
| 62 | + return S_OK;_ |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +```c# |
| 67 | + |
| 68 | + /// Webview creation and setup of completion handler are out of scope of sample |
| 69 | + void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e) |
| 70 | + { |
| 71 | + if (e.IsSuccess) |
| 72 | + { |
| 73 | + String userDataFolder = webView.CoreWebView2.Environment->UserDataFolder(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | +``` |
| 78 | + |
| 79 | +# API Details |
| 80 | +```c# (but really MIDL3) |
| 81 | +/// This interface is an extension of the ICoreWebView2Environment. An object |
| 82 | +/// implementing the ICoreWebView2Environment5 interface will also |
| 83 | +/// implement ICoreWebView2Environment. |
| 84 | +[ |
| 85 | + uuid(083CB0D7-E464-4108-807E-80AE4EAA3B28), object, |
| 86 | + pointer_default(unique) |
| 87 | +] interface ICoreWebView2Environment6 : IUnknown { |
| 88 | + /// Returns the user data folder that all CoreWebView2's created from this |
| 89 | + /// environment are using. |
| 90 | + /// This could be either the value passed in by the developer when creating the |
| 91 | + /// environment object or the calculated one for default handling. And will |
| 92 | + /// always be an absolute path. |
| 93 | + /// |
| 94 | + /// \snippet AppWindow.cpp GetUserDataFolder |
| 95 | +
|
| 96 | + [propget] HRESULT UserDataFolder([ out, retval ] LPWSTR * value); |
| 97 | +} |
| 98 | +``` |
0 commit comments