forked from cefsharp/CefSharp
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue cefsharp#3165
- Loading branch information
Showing
8 changed files
with
254 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 0 additions & 67 deletions
67
CefSharp.Example/DevTools/TaskMethodDevToolsMessageObserver.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// Copyright © 2020 The CefSharp Authors. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CefSharp.Callback; | ||
using CefSharp.Internals; | ||
|
||
namespace CefSharp.DevTools | ||
{ | ||
/// <summary> | ||
/// DevToolClient | ||
/// </summary> | ||
public class DevToolsClient : IDevToolsMessageObserver | ||
{ | ||
private readonly ConcurrentDictionary<int, TaskCompletionSource<DevToolsMethodResult>> queuedCommandResults = new ConcurrentDictionary<int, TaskCompletionSource<DevToolsMethodResult>>(); | ||
private int lastMessageId; | ||
private IBrowser browser; | ||
private IRegistration devToolsRegistration; | ||
private bool devToolsAttached; | ||
|
||
/// <summary> | ||
/// DevToolsEvent | ||
/// </summary> | ||
public EventHandler<DevToolsEventArgs> DevToolsEvent; | ||
|
||
/// <summary> | ||
/// DevToolsClient | ||
/// </summary> | ||
/// <param name="browser">Browser associated with this DevTools client</param> | ||
public DevToolsClient(IBrowser browser) | ||
{ | ||
this.browser = browser; | ||
|
||
lastMessageId = browser.Identifier * 100000; | ||
} | ||
|
||
public void SetDevToolsObserverRegistration(IRegistration devToolsRegistration) | ||
{ | ||
this.devToolsRegistration = devToolsRegistration; | ||
} | ||
|
||
/// <summary> | ||
/// Execute a method call over the DevTools protocol. This method can be called on any thread. | ||
/// See the DevTools protocol documentation at https://chromedevtools.github.io/devtools-protocol/ for details | ||
/// of supported methods and the expected <paramref name="parameters"/> dictionary contents. | ||
/// </summary> | ||
/// <param name="method">is the method name</param> | ||
/// <param name="parameters">are the method parameters represented as a dictionary, | ||
/// which may be empty.</param> | ||
/// <returns>return a Task that can be awaited to obtain the method result</returns> | ||
public async Task<DevToolsMethodResult> ExecuteDevToolsMethodAsync(string method, IDictionary<string, object> parameters = null) | ||
{ | ||
if (browser == null || browser.IsDisposed) | ||
{ | ||
//TODO: Queue up commands where possible | ||
return new DevToolsMethodResult { Success = false }; | ||
} | ||
|
||
var messageId = Interlocked.Increment(ref lastMessageId); | ||
|
||
var taskCompletionSource = new TaskCompletionSource<DevToolsMethodResult>(); | ||
|
||
if (!queuedCommandResults.TryAdd(messageId, taskCompletionSource)) | ||
{ | ||
return new DevToolsMethodResult { Success = false }; | ||
} | ||
|
||
var browserHost = browser.GetHost(); | ||
|
||
if (CefThread.CurrentlyOnUiThread) | ||
{ | ||
var returnedMessageId = browserHost.ExecuteDevToolsMethod(messageId, method, parameters); | ||
if (returnedMessageId == 0) | ||
{ | ||
return new DevToolsMethodResult { Success = false }; | ||
} | ||
} | ||
|
||
if (CefThread.CanExecuteOnUiThread) | ||
{ | ||
var returnedMessageId = await CefThread.ExecuteOnUiThread(() => | ||
{ | ||
return browserHost.ExecuteDevToolsMethod(messageId, method, parameters); | ||
}).ConfigureAwait(false); | ||
|
||
if (returnedMessageId == 0) | ||
{ | ||
return new DevToolsMethodResult { Success = false }; | ||
} | ||
} | ||
|
||
return await taskCompletionSource.Task; | ||
} | ||
|
||
void IDisposable.Dispose() | ||
{ | ||
devToolsRegistration?.Dispose(); | ||
devToolsRegistration = null; | ||
browser = null; | ||
} | ||
|
||
void IDevToolsMessageObserver.OnDevToolsAgentAttached(IBrowser browser) | ||
{ | ||
devToolsAttached = true; | ||
} | ||
|
||
void IDevToolsMessageObserver.OnDevToolsAgentDetached(IBrowser browser) | ||
{ | ||
devToolsAttached = false; | ||
} | ||
|
||
void IDevToolsMessageObserver.OnDevToolsEvent(IBrowser browser, string method, Stream parameters) | ||
{ | ||
//TODO: Improve this | ||
var memoryStream = new MemoryStream((int)parameters.Length); | ||
parameters.CopyTo(memoryStream); | ||
|
||
var paramsAsJsonString = Encoding.UTF8.GetString(memoryStream.ToArray()); | ||
|
||
DevToolsEvent?.Invoke(this, new DevToolsEventArgs(method, paramsAsJsonString)); | ||
} | ||
|
||
bool IDevToolsMessageObserver.OnDevToolsMessage(IBrowser browser, Stream message) | ||
{ | ||
return false; | ||
} | ||
|
||
void IDevToolsMessageObserver.OnDevToolsMethodResult(IBrowser browser, int messageId, bool success, Stream result) | ||
{ | ||
TaskCompletionSource<DevToolsMethodResult> taskCompletionSource = null; | ||
|
||
if (queuedCommandResults.TryRemove(messageId, out taskCompletionSource)) | ||
{ | ||
var methodResult = new DevToolsMethodResult | ||
{ | ||
Success = success | ||
}; | ||
|
||
if (success) | ||
{ | ||
//TODO: Improve this | ||
var memoryStream = new MemoryStream((int)result.Length); | ||
|
||
result.CopyTo(memoryStream); | ||
|
||
methodResult.ResultAsJsonString = Encoding.UTF8.GetString(memoryStream.ToArray()); | ||
} | ||
|
||
Task.Run(() => | ||
{ | ||
//Make sure continuation runs on Thread Pool | ||
taskCompletionSource.TrySetResult(methodResult); | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright © 2020 The CefSharp Authors. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. | ||
|
||
|
||
using System; | ||
|
||
namespace CefSharp.DevTools | ||
{ | ||
/// <summary> | ||
/// DevTools Event EventAargs | ||
/// </summary> | ||
public class DevToolsEventArgs : EventArgs | ||
{ | ||
/// <summary> | ||
/// Method | ||
/// </summary> | ||
public string EventName { get; private set; } | ||
|
||
/// <summary> | ||
/// Event paramaters as Json string | ||
/// </summary> | ||
public string ParametersAsJsonString { get; private set; } | ||
|
||
public DevToolsEventArgs(string eventName, string paramsAsJsonString) | ||
{ | ||
EventName = eventName; | ||
ParametersAsJsonString = paramsAsJsonString; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright © 2020 The CefSharp Authors. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. | ||
|
||
namespace CefSharp.DevTools | ||
{ | ||
/// <summary> | ||
/// DevTools Method Result | ||
/// </summary> | ||
public class DevToolsMethodResult | ||
{ | ||
/// <summary> | ||
/// MessageId | ||
/// </summary> | ||
public int MessageId { get; set; } | ||
|
||
/// <summary> | ||
/// Success | ||
/// </summary> | ||
public bool Success { get; set; } | ||
|
||
/// <summary> | ||
/// Method Result as Json string | ||
/// </summary> | ||
public string ResultAsJsonString { get; set; } | ||
|
||
} | ||
} |
Oops, something went wrong.