@@ -38,8 +38,8 @@ HRESULT AppWindow::CreateControllerWithOptions()
3838 }
3939
4040 wil::com_ptr<ICoreWebView2ControllerOptions> options;
41- HRESULT hr = webViewEnvironment4->CreateCoreWebView2ControllerOptions(
42- m_webviewOption.profile.c_str(), m_webviewOption.isInPrivate, options.GetAddressOf());
41+ // The validation of name occurs when setting the property.
42+ HRESULT hr = webViewEnvironment4->CreateCoreWebView2ControllerOptions( options.GetAddressOf());
4343 if (hr == E_INVALIDARG)
4444 {
4545 ShowFailure (hr, L"Unable to create WebView2 due to an invalid profile name.");
@@ -48,6 +48,11 @@ HRESULT AppWindow::CreateControllerWithOptions()
4848 }
4949 CHECK_FAILURE(hr);
5050
51+ // If call 'put_ProfileName' with an invalid profile name, the 'E_INVALIDARG' returned immediately.
52+ // ProfileName could be reused.
53+ CHECK_FAILURE (options->put_ProfileName(m_webviewOption.profile.c_str()));
54+ CHECK_FAILURE(options->put_IsInPrivateModeEnabled(m_webviewOption.isInPrivate));
55+
5156 if (m_dcompDevice || m_wincompCompositor)
5257 {
5358 CHECK_FAILURE (webViewEnvironment4->CreateCoreWebView2CompositionControllerWithOptions(
@@ -99,16 +104,13 @@ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(HRESULT result, ICore
99104 // Accesses the profile object.
100105 BOOL inPrivateModeEnabled = FALSE;
101106 CHECK_FAILURE(profile->get_IsInPrivateModeEnabled(&inPrivateModeEnabled));
102- wil::unique_cotaskmem_string profile_path;
103- CHECK_FAILURE(profile->get_ProfilePath(&profile_path));
104- std::wstring str(profile_path.get());
105- m_profileDirName = std::filesystem::path(profile_path).filename();
107+ CHECK_FAILURE(profile->get_ProfileName(&m_profileName));
106108
107- // update window title with m_profileDirName
109+ // update window title with m_profileName
108110 UpdateAppTitle();
109111
110112 // update window icon
111- SetAppIcon(inPrivate );
113+ SetAppIcon(inPrivateModeEnabled );
112114 }
113115
114116 // ...
@@ -122,14 +124,16 @@ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(HRESULT result, ICore
122124CoreWebView2Environment _webViewEnvironment;
123125public CreateWebView2ControllerWithOptions(IntPtr parentWindow, string profileName, bool isInPrivate)
124126{
125- CoreWebView2ControllerOptions options = _webViewEnvironment.CreateCoreWebView2ControllerOptions(profileName, isInPrivate);
127+ CoreWebView2ControllerOptions options = _webViewEnvironment.CreateCoreWebView2ControllerOptions();
128+ options.ProfileName = profileName;
129+ options.IsInPrivateModeEnabled = isInPrivate;
130+
126131 CoreWebView2Controller webView2Controller = await _webViewEnvironment.CreateCoreWebView2ControllerWithOptionsAsync(parentWindow, options);
127- string profilePath = webView2Controller.CoreWebView2.Profile.ProfilePath;
128- string profileDirName = Path.GetFileName(profilePath);
132+ string profileName = webView2Controller.CoreWebView2.Profile.ProfileName;
129133 bool inPrivate = webView2Controller.CoreWebView2.Profile.IsInPrivateModeEnabled;
130134
131- // update window title with profileDirName
132- UpdateAppTitle(profileDirName );
135+ // update window title with profileName
136+ UpdateAppTitle(profileName );
133137
134138 // update window icon
135139 SetAppIcon(inPrivate);
@@ -146,16 +150,20 @@ interface ICoreWebView2Environment5;
146150interface ICoreWebView2_7;
147151interface ICoreWebView2Profile;
148152
153+ /// This interface is used to manage profile options that created by 'CreateCoreWebView2ControllerOptions'.
149154[uuid(C2669A3A-03A9-45E9-97EA-03CD55E5DC03), object, pointer_default(unique)]
150155interface ICoreWebView2ControllerOptions : IUnknown {
151- /// `ProfileName` property is to specify a profile name, which is only allowed to contain
152- /// the following ASCII characters. It has a maximum length of 64 characters excluding the null terminator. It is
153- /// ASCII case insensitive.
154- /// alphabet characters: a-z and A-Z
155- /// digit characters: 0-9
156- /// and '#', '@', '$', '(', ')', '+', '-', '_', '~', '.', ' ' (space).
157- /// Note: the text must not end with a period '.' or ' ' (space). And, although upper case letters are
158- /// allowed, they're treated just as lower case couterparts because the profile name will be mapped to
156+ /// The `ProfileName` property specifies the profile's name. It has a maximum length of 64
157+ /// characters excluding the null terminator and must be a valid file name.
158+ /// See [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file)
159+ /// for more information on file names. It must contain only the following ASCII characters:
160+ ///
161+ /// * alphabet characters: a-z and A-Z
162+ /// * digit characters: 0-9
163+ /// * and '#', '@', '$', '(', ')', '+', '-', '_', '~', '.', ' ' (space).
164+ ///
165+ /// Note: the text must not end with a period '.' or ' ' (space) nor start with a ' ' (space). And, although upper case letters are
166+ /// allowed, they're treated the same as their lower case counterparts because the profile name will be mapped to
159167 /// the real profile directory path on disk and Windows file system handles path names in a case-insensitive way.
160168 [propget] HRESULT ProfileName([out, retval] LPWSTR* value);
161169 /// Sets the `ProfileName` property.
@@ -167,15 +175,23 @@ interface ICoreWebView2ControllerOptions : IUnknown {
167175 [propput] HRESULT IsInPrivateModeEnabled([in] BOOL value);
168176}
169177
178+ /// This interface is used to create 'CreateCoreWebView2ControllerOptions' object, which
179+ /// can be passed as a parameter in 'CreateCoreWebView2ControllerWithOptions' and
180+ /// 'CreateCoreWebView2CompositionControllerWithOptions' function for multiple profile support.
181+ /// The profile will be created on disk or opened when calling 'CreateCoreWebView2ControllerWithOptions' or
182+ /// 'CreateCoreWebView2CompositionControllerWithOptions' no matter InPrivate mode is enabled or not, and it will be
183+ /// released in memory when the corresponding controller is closed but still remain on disk.
184+ /// If create a WebView2Controller with {ProfileName="name", InPrivate=false} and then later create another one with
185+ /// one with {ProfileName="name", InPrivate=true}, these two controllers using the same profile would be allowed to
186+ /// run at the same time.
170187[uuid(57FD205C-39D5-4BA1-8E7B-3E53C323EA87), object, pointer_default(unique)]
171- interface ICoreWebView2Environment5 : IUnknown
172- {
188+ interface ICoreWebView2Environment5 : IUnknown {
173189 /// Create a new ICoreWebView2ControllerOptions to be passed as a parameter of
174190 /// CreateCoreWebView2ControllerWithOptions and CreateCoreWebView2CompositionControllerWithOptions.
175- /// Returns E_INVALIDARG only in case of invalid profile name.
191+ /// The 'options' is settable and in it the default value for profile name is the empty string,
192+ /// and the default value for IsInPrivateModeEnabled is false.
193+ /// Also the profile name can be reused.
176194 HRESULT CreateCoreWebView2ControllerOptions(
177- [in] LPCWSTR profileName,
178- [in] BOOL isInPrivateModeEnabled,
179195 [out, retval] ICoreWebView2ControllerOptions** options);
180196
181197 /// Create a new WebView with options.
@@ -191,9 +207,12 @@ interface ICoreWebView2Environment5 : IUnknown
191207 [in] ICoreWebView2CreateCoreWebView2CompositionControllerCompletedHandler* handler);
192208}
193209
210+ /// Used to get ICoreWebView2Profile object.
194211[uuid(6E5CE5F0-16E6-4A05-97D8-4E256B3EB609), object, pointer_default(unique)]
195212interface ICoreWebView2_7 : IUnknown {
196- /// The associated `ICoreWebView2Profile` object.
213+ /// The associated `ICoreWebView2Profile` object. If this CoreWebView2 was created with a CoreWebView2ControllerOptions, the
214+ /// CoreWebView2Profile will match those specified options. Otherwise if this CoreWebView2 was created without a
215+ /// CoreWebView2ControllerOptions, then this will be the default CoreWebView2Profile for the corresponding CoreWebView2Environment.
197216 [propget] HRESULT Profile([out, retval] ICoreWebView2Profile** value);
198217}
199218
@@ -233,8 +252,7 @@ namespace Microsoft.Web.WebView2.Core
233252 {
234253 // ...
235254
236- CoreWebView2ControllerOptions CreateCoreWebView2ControllerOptions (
237- String ProfileName , Boolean InPrivateModeEnabled );
255+ CoreWebView2ControllerOptions CreateCoreWebView2ControllerOptions ();
238256
239257 Windows .Foundation .IAsyncOperation < CoreWebView2Controller >
240258 CreateCoreWebView2ControllerWithOptionsAsync (
0 commit comments