|
2 | 2 | // |
3 | 3 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using CefSharp.Internals; |
5 | 8 | using CefSharp.Web; |
6 | 9 |
|
7 | 10 | namespace CefSharp |
@@ -33,5 +36,72 @@ public static int ExecuteDevToolsMethod(this IBrowserHost browserHost, int messa |
33 | 36 |
|
34 | 37 | return browserHost.ExecuteDevToolsMethod(messageId, method, json); |
35 | 38 | } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Execute a method call over the DevTools protocol. This is a more structured |
| 42 | + /// version of SendDevToolsMessage. <see cref="ExecuteDevToolsMethod"/> can only be called on the |
| 43 | + /// CEF UI Thread, this method can be called on any thread. |
| 44 | + /// See the DevTools protocol documentation at https://chromedevtools.github.io/devtools-protocol/ for details |
| 45 | + /// of supported methods and the expected <paramref name="parameters"/> dictionary contents. |
| 46 | + /// See the SendDevToolsMessage documentation for additional usage information. |
| 47 | + /// </summary> |
| 48 | + /// <param name="browser">the browser instance</param> |
| 49 | + /// <param name="messageId">is an incremental number that uniquely identifies the message (pass 0 to have the next number assigned |
| 50 | + /// automatically based on previous values)</param> |
| 51 | + /// <param name="method">is the method name</param> |
| 52 | + /// <param name="parameters">are the method parameters represented as a dictionary, |
| 53 | + /// which may be empty.</param> |
| 54 | + /// <returns>return a Task that can be awaited to obtain the assigned message Id. If the message was |
| 55 | + /// unsuccessfully submitted for validation, this value will be 0.</returns> |
| 56 | + public static Task<int> ExecuteDevToolsMethodAsync(this IBrowser browser, int messageId, string method, IDictionary<string, object> parameters = null) |
| 57 | + { |
| 58 | + WebBrowserExtensions.ThrowExceptionIfBrowserNull(browser); |
| 59 | + |
| 60 | + var browserHost = browser.GetHost(); |
| 61 | + |
| 62 | + WebBrowserExtensions.ThrowExceptionIfBrowserHostNull(browserHost); |
| 63 | + |
| 64 | + if (CefThread.CurrentlyOnUiThread) |
| 65 | + { |
| 66 | + return Task.FromResult(browserHost.ExecuteDevToolsMethod(messageId, method, parameters)); |
| 67 | + } |
| 68 | + |
| 69 | + if (CefThread.CanExecuteOnUiThread) |
| 70 | + { |
| 71 | + return CefThread.ExecuteOnUiThread(() => |
| 72 | + { |
| 73 | + return browserHost.ExecuteDevToolsMethod(messageId, method, parameters); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + //CEF returns 0 to signify failure, we'll do the same. |
| 78 | + return Task.FromResult(0); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Execute a method call over the DevTools protocol. This is a more structured |
| 83 | + /// version of SendDevToolsMessage. <see cref="ExecuteDevToolsMethod"/> can only be called on the |
| 84 | + /// CEF UI Thread, this method can be called on any thread. |
| 85 | + /// See the DevTools protocol documentation at https://chromedevtools.github.io/devtools-protocol/ for details |
| 86 | + /// of supported methods and the expected <paramref name="parameters"/> dictionary contents. |
| 87 | + /// See the SendDevToolsMessage documentation for additional usage information. |
| 88 | + /// </summary> |
| 89 | + /// <param name="chromiumWebBrowser">the ChromiumWebBrowser instance</param> |
| 90 | + /// <param name="messageId">is an incremental number that uniquely identifies the message (pass 0 to have the next number assigned |
| 91 | + /// automatically based on previous values)</param> |
| 92 | + /// <param name="method">is the method name</param> |
| 93 | + /// <param name="parameters">are the method parameters represented as a dictionary, |
| 94 | + /// which may be empty.</param> |
| 95 | + /// <returns>return a Task that can be awaited to obtain the assigned message Id. If the message was |
| 96 | + /// unsuccessfully submitted for validation, this value will be 0.</returns> |
| 97 | + public static Task<int> ExecuteDevToolsMethodAsync(this IWebBrowser chromiumWebBrowser, int messageId, string method, IDictionary<string, object> parameters = null) |
| 98 | + { |
| 99 | + chromiumWebBrowser.ThrowExceptionIfDisposed(); |
| 100 | + chromiumWebBrowser.ThrowExceptionIfBrowserNotInitialized(); |
| 101 | + |
| 102 | + var browser = chromiumWebBrowser.GetBrowser(); |
| 103 | + |
| 104 | + return browser.ExecuteDevToolsMethodAsync(messageId, method, parameters); |
| 105 | + } |
36 | 106 | } |
37 | 107 | } |
0 commit comments