Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DYN-5208-DynamoRevit-Crash-Fix #13245

Merged
merged 6 commits into from
Aug 29, 2022
Merged

DYN-5208-DynamoRevit-Crash-Fix #13245

merged 6 commits into from
Aug 29, 2022

Conversation

RobertGlobant20
Copy link
Contributor

Purpose

Fixing crash in Dynamo for Revit when initializing WebView2.
DocumentationBrowser was crashing in Dynamo for Revit due that WebView2 was trying to create the cache folder in the path "C:\Program Files\Autodesk\Revit 2023\AddIns\DynamoForRevit" due to permissions so in this fix I'm changing the folder to use the AppData for Revit "C:\Users\roberto.tellez\AppData\Roaming\Dynamo\Dynamo Revit\2.16".
The same medicin was applied for the WebView2 used in Guided Tours, when using the ResourceUtilities.LoadWebBrowser() method it will using the cache folder passed as parameter.

Declarations

Check these if you believe they are true

  • The codebase is in a better state after this PR
  • Is documented according to the standards
  • The level of testing this PR includes is appropriate
  • User facing strings, if any, are extracted into *.resx files
  • All tests pass using the self-service CI.
  • Snapshot of UI changes, if any.
  • Changes to the API follow Semantic Versioning and are documented in the API Changes document.
  • This PR modifies some build requirements and the readme is updated

Release Notes

Fixing crash in Dynamo for Revit when initializing WebView2.

Reviewers

@QilongTang

FYIs

@avidit

commit c81f3fe
Merge: 98ff9c0 7240196
Author: t-tellro <[email protected]>
Date:   Thu Aug 25 12:46:28 2022 -0500

    Merge branch 'master' into DYN-5106-WebView2-DocumentationBrowser

commit 98ff9c0
Author: t-tellro <[email protected]>
Date:   Thu Aug 25 12:32:33 2022 -0500

    DYN-5106-WebView2-DocumentationBrowser

    DocumentationBrowser was crashing in Dynamo for Revit due that WebView2 was trying to create the cache folder in the path "C:\Program Files\Autodesk\Revit 2023\AddIns\DynamoForRevit" due to permissions so in this fix I'm changing the folder to use the AppData for Revit "C:\Users\roberto.tellez\AppData\Roaming\Dynamo\Dynamo Revit\2.16".
    The same medicin was applied for the WebView2 used in Guided Tours, when using the ResourceUtilities.LoadWebBrowser() method it will using the cache folder passed as parameter.

commit 715626f
Merge: 979f0fc 3491e96
Author: t-tellro <[email protected]>
Date:   Tue Aug 16 13:12:47 2022 -0500

    Merge branch 'master' into DYN-5106-WebView2-DocumentationBrowser

commit 979f0fc
Author: t-tellro <[email protected]>
Date:   Mon Aug 15 22:46:10 2022 -0500

    DYN-5106-WebView2-DocumentationBrowser Code Review

    Fixing CanCreateNodeDocumenationHtmlFromNodeAnnotationEventArgsWithPackageNodeWithAddtionalDocumentation test due that it was using and old format of the html <img> tab, then I updated the image to use the virtual folder and http.

commit 284eb7a
Merge: ab4326d b95ef11
Author: Roberto T <[email protected]>
Date:   Mon Aug 15 13:48:44 2022 -0500

    Merge branch 'master' into DYN-5106-WebView2-DocumentationBrowser

commit ab4326d
Merge: dc993a0 fc26f34
Author: t-tellro <[email protected]>
Date:   Mon Aug 15 13:43:15 2022 -0500

    Merge branch 'DYN-5106-WebView2-DocumentationBrowser' of https://github.com/RobertGlobant20/Dynamo into DYN-5106-WebView2-DocumentationBrowser

commit dc993a0
Author: t-tellro <[email protected]>
Date:   Mon Aug 15 13:42:26 2022 -0500

    DYN-5106-WebView2-DocumentationBrowser Code Review

    - changing the visibility for the property FallbackDirectoryName
    - renaming coreDir by docsDir

commit fc26f34
Merge: b0526ab 933f16c
Author: Aaron (Qilong) <[email protected]>
Date:   Mon Aug 15 10:00:24 2022 -0700

    Merge branch 'master' into DYN-5106-WebView2-DocumentationBrowser

commit b0526ab
Author: t-tellro <[email protected]>
Date:   Fri Aug 12 18:52:22 2022 -0500

    DYN-5106-WebView2-DocumentationBrowser Code Review

    Due that the fallback_doc directory path can change depending if Dynamo is executed as Sandbox or if is over Revit then the path can be different so I added a code that will create the virtual directory depending of the correct fallback_doc directory path.
    Also I modified the Md2Html class so now it will be inserting in the image source (<img src) the prefix "http://appassets" so it can be loaded from a virtual directory created by the DocumentationBrowser.

commit 417069a
Merge: 0c12dff bef463e
Author: t-tellro <[email protected]>
Date:   Thu Aug 11 18:11:39 2022 -0500

    Merge branch 'master' into DYN-5106-WebView2-DocumentationBrowser

commit 0c12dff
Author: t-tellro <[email protected]>
Date:   Thu Aug 11 18:01:50 2022 -0500

    DYN-5106-WebView2-DocumentationBrowser

    I replaced the WebBrowser component by WebView2 in the file DocumentationBrowserView.xaml, also I added some functions for initializing WebView2,
    The method ShouldAllowNavigation was modified due that WebView2 doesn't have the Navigating event and also the event handler receive different parameters then modified the function to implement the same behavior than the previous one.
    Also I noticed that WebView2 CORS was blocking to load the jpg images that are shown for several nodes, then I had to create a mapped virtual directory in this way the html will be able to load the images and show them in the web page.
    Finally I removed a tag in all the html that was making the web page compatible for IE based browsers (due that now we are using WebView2 a Chromium based browser).
@RobertGlobant20
Copy link
Contributor Author

This GIF is showing the behavior of Dynamo in Revit.
fix_userdatafolder_webview2

@@ -31,6 +31,11 @@ public partial class PopupWindow : Popup
//Assembly path to the Resources folder
private const string resourcesPath = "Dynamo.Wpf.Views.GuidedTour.HtmlPages.Resources";

/// <summary>
/// This property will be hold the path of the WebView2 cache folder, the value will change based in if DynamoSandbox is executed or Dynamo from Revit is executed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider this generally, it's not just DynamoRevit, what about DynamoForCivil or Dynamo Formit etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment updated: 45e845b

var docsDir = new DirectoryInfo(Path.Combine(pathManager.HostApplicationDirectory, FALLBACK_DOC_DIRECTORY_NAME));
//when running over Revit HostApplicationDirectory = C:\Program Files\Autodesk\Revit 2023\AddIns\DynamoForRevit\Revit
//Then we need to remove the Revit folder from the path so we can find the fallback_docs directory.
var hostAppDirectory = Directory.GetParent(pathManager.HostApplicationDirectory).FullName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to double check on this if there is a better way. I will get back to you

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So for Sandbox, this pathManager.HostApplicationDirectory would be null? I think this only means this is not an in process integration, for case like FormIt, where it is out of process but still delivered in programe files, this may still not work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QilongTang
For other products like FormIt or Civil not sure which is the path of the Dynamo "fallback_doc" folder, I followed the implementation that I found in PackageDocumentationManager.cs (check link below) then if the fallback_doc folder location is different for each host (Revit, Civil, Formit) then the implementation done in PackageDocumentationManager is not working or hostDynamoFallbackDocPath = null or there is another process creating the folder and moving all the md/jpg files to the HostApplicationDirectory path.

Should I install FormIt and Civil so I can test if images are shown in DocumentationBrowser?

if (!string.IsNullOrEmpty(pathManager.HostApplicationDirectory))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QilongTang now the WebView2 cache folder will be always created in AppData, see the next commit:
e101e8a

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RobertGlobant20 Is the comment above still accurate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QilongTang fixed comment about Revit in the next commit:
2729c08

Fix that will create the WebView2 cache folder always in the AppData path.
e.g.
Revit
C:\Users\roberto.tellez\AppData\Roaming\Dynamo\Dynamo Revit\2.16

Sandbox
C:\Users\roberto.tellez\AppData\Roaming\Dynamo\Dynamo Core\2.16
if(popupWindow != null)
{
popupWindow.WebBrowserUserDataFolder = userDataFolder != null ? userDataFolder.FullName : string.Empty;
}
Copy link
Contributor

@QilongTang QilongTang Aug 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this code here? Shouldn't this be set one time only at the Welcome step? Is it because all of our Welcome steps not leveraging WebView2?

Copy link
Contributor Author

@RobertGlobant20 RobertGlobant20 Aug 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QilongTang
1st question:

  • we need to set the WebBrowserUserDataFolder before calling the WebView2.EnsureCoreWebView2Async() method so WebView2 can set the cache folder in the right location, I put it here because in GuidesManager we have access to DynamoViewModel.Model.PathManager.

2nd question.

  • I did a change that will be set only for popups that use html, check the commit below.

3rd question.

  • Not all the Welcome steps use html then it will be set only for the ones using html (and by consequence WebView2).

commit:
b37dcc1

Copy link
Member

@mjkkirschner mjkkirschner Aug 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RobertGlobant20 are there any MSDN docs on how these webview2 startup options affect other instances of WebView2 in the same process?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjkkirschner MS documentation doesn't talk specifically about that but I think the startup options are specific for each WebView2 instance.
If you question is because guided tours and documentationbrowser will be using the same cache - user data folder (UDF) folder, MS Documentation says "It's possible for your host app to overlap them" but doesn't say anything about the consequences, I just opened the "User Interface" tour that opens the DocumentationBrowser at the same time than a Popup opened but I didn't see any problem (the Popup is not using html for this case), then once Notification Center is completed we will need to see what happen if we open the Notifications Center and the DocumentationBrowser at the same time.
https://docs.microsoft.com/en-us/microsoft-edge/webview2/concepts/user-data-folder?tabs=win32

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RobertGlobant20 thanks- I'm also curious because inside of Hosts like Revit, they will use WebView2 and so will the other addins running in the Host process. It's the wild west!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjkkirschner well, we asked Revit about it but for my understanding they are still in the process of adopting WebView2 then they haven't deal with this feature yet so if Revit team decide to use another cache folder in AppData for WebView2 we can evaluate a change in Dynamo to use the same than them, another thing is that we need to ask if this feature of enable/disable cache for WebView2 will be enabled in the future (since I didn't find anything about it).
Also I checked with AutoCAD team and they said they create the folder in the user profile (consider that Autocad is using C++).
Thanks

Copy link
Contributor

@QilongTang QilongTang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with some comments

Fix that will set the WebView2.UserDataFolder only when the popup is using an html page.
Copy link
Contributor

@QilongTang QilongTang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see one of my comments about updating the comments about revit, I dont think it is up to date. Otherwise, LGTM

Updating comment due that was specific to Revit.
@QilongTang QilongTang merged commit 2632111 into DynamoDS:master Aug 29, 2022
@QilongTang
Copy link
Contributor

@RobertGlobant20 Please cherry-pick this PR

QilongTang pushed a commit that referenced this pull request Aug 30, 2022
* Squashed commit of the following:

commit c81f3fe
Merge: 98ff9c0 7240196
Author: t-tellro <[email protected]>
Date:   Thu Aug 25 12:46:28 2022 -0500

    Merge branch 'master' into DYN-5106-WebView2-DocumentationBrowser

commit 98ff9c0
Author: t-tellro <[email protected]>
Date:   Thu Aug 25 12:32:33 2022 -0500

    DYN-5106-WebView2-DocumentationBrowser

    DocumentationBrowser was crashing in Dynamo for Revit due that WebView2 was trying to create the cache folder in the path "C:\Program Files\Autodesk\Revit 2023\AddIns\DynamoForRevit" due to permissions so in this fix I'm changing the folder to use the AppData for Revit "C:\Users\roberto.tellez\AppData\Roaming\Dynamo\Dynamo Revit\2.16".
    The same medicin was applied for the WebView2 used in Guided Tours, when using the ResourceUtilities.LoadWebBrowser() method it will using the cache folder passed as parameter.

commit 715626f
Merge: 979f0fc 3491e96
Author: t-tellro <[email protected]>
Date:   Tue Aug 16 13:12:47 2022 -0500

    Merge branch 'master' into DYN-5106-WebView2-DocumentationBrowser

commit 979f0fc
Author: t-tellro <[email protected]>
Date:   Mon Aug 15 22:46:10 2022 -0500

    DYN-5106-WebView2-DocumentationBrowser Code Review

    Fixing CanCreateNodeDocumenationHtmlFromNodeAnnotationEventArgsWithPackageNodeWithAddtionalDocumentation test due that it was using and old format of the html <img> tab, then I updated the image to use the virtual folder and http.

commit 284eb7a
Merge: ab4326d b95ef11
Author: Roberto T <[email protected]>
Date:   Mon Aug 15 13:48:44 2022 -0500

    Merge branch 'master' into DYN-5106-WebView2-DocumentationBrowser

commit ab4326d
Merge: dc993a0 fc26f34
Author: t-tellro <[email protected]>
Date:   Mon Aug 15 13:43:15 2022 -0500

    Merge branch 'DYN-5106-WebView2-DocumentationBrowser' of https://github.com/RobertGlobant20/Dynamo into DYN-5106-WebView2-DocumentationBrowser

commit dc993a0
Author: t-tellro <[email protected]>
Date:   Mon Aug 15 13:42:26 2022 -0500

    DYN-5106-WebView2-DocumentationBrowser Code Review

    - changing the visibility for the property FallbackDirectoryName
    - renaming coreDir by docsDir

commit fc26f34
Merge: b0526ab 933f16c
Author: Aaron (Qilong) <[email protected]>
Date:   Mon Aug 15 10:00:24 2022 -0700

    Merge branch 'master' into DYN-5106-WebView2-DocumentationBrowser

commit b0526ab
Author: t-tellro <[email protected]>
Date:   Fri Aug 12 18:52:22 2022 -0500

    DYN-5106-WebView2-DocumentationBrowser Code Review

    Due that the fallback_doc directory path can change depending if Dynamo is executed as Sandbox or if is over Revit then the path can be different so I added a code that will create the virtual directory depending of the correct fallback_doc directory path.
    Also I modified the Md2Html class so now it will be inserting in the image source (<img src) the prefix "http://appassets" so it can be loaded from a virtual directory created by the DocumentationBrowser.

commit 417069a
Merge: 0c12dff bef463e
Author: t-tellro <[email protected]>
Date:   Thu Aug 11 18:11:39 2022 -0500

    Merge branch 'master' into DYN-5106-WebView2-DocumentationBrowser

commit 0c12dff
Author: t-tellro <[email protected]>
Date:   Thu Aug 11 18:01:50 2022 -0500

    DYN-5106-WebView2-DocumentationBrowser

    I replaced the WebBrowser component by WebView2 in the file DocumentationBrowserView.xaml, also I added some functions for initializing WebView2,
    The method ShouldAllowNavigation was modified due that WebView2 doesn't have the Navigating event and also the event handler receive different parameters then modified the function to implement the same behavior than the previous one.
    Also I noticed that WebView2 CORS was blocking to load the jpg images that are shown for several nodes, then I had to create a mapped virtual directory in this way the html will be able to load the images and show them in the web page.
    Finally I removed a tag in all the html that was making the web page compatible for IE based browsers (due that now we are using WebView2 a Chromium based browser).

* DYN-5208-DynamoRevit-Crash-Fix

Removing comment.

* DYN-5208-DynamoRevit-Crash-Fix

Updating comment

* DYN-5208-DynamoRevit-Crash-Fix Code Review

Fix that will create the WebView2 cache folder always in the AppData path.
e.g.
Revit
C:\Users\roberto.tellez\AppData\Roaming\Dynamo\Dynamo Revit\2.16

Sandbox
C:\Users\roberto.tellez\AppData\Roaming\Dynamo\Dynamo Core\2.16

* DYN-5208-DynamoRevit-Crash-Fix Code Review 2

Fix that will set the WebView2.UserDataFolder only when the popup is using an html page.

* DYN-5208-DynamoRevit-Crash-Fix Code Review 2

Updating comment due that was specific to Revit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants