Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TaskSetCookieCallback.cs 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright © 2015 The CefSharp Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
  4. using System;
  5. using System.Threading.Tasks;
  6. using CefSharp.Internals;
  7. namespace CefSharp
  8. {
  9. /// <summary>
  10. /// Provides a callback implementation of <see cref="ISetCookieCallback"/>.
  11. /// </summary>
  12. public sealed class TaskSetCookieCallback : ISetCookieCallback
  13. {
  14. private readonly TaskCompletionSource<bool> taskCompletionSource;
  15. private volatile bool isDisposed;
  16. private bool onComplete; //Only ever accessed on the same CEF thread, so no need for thread safety
  17. /// <summary>
  18. /// Initializes a new instance of the TaskSetCookieCallback class.
  19. /// </summary>
  20. public TaskSetCookieCallback()
  21. {
  22. taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
  23. }
  24. void ISetCookieCallback.OnComplete(bool success)
  25. {
  26. onComplete = true;
  27. taskCompletionSource.TrySetResult(success);
  28. }
  29. /// <summary>
  30. /// Task used to await this callback
  31. /// </summary>
  32. public Task<bool> Task
  33. {
  34. get { return taskCompletionSource.Task; }
  35. }
  36. bool ISetCookieCallback.IsDisposed
  37. {
  38. get { return isDisposed; }
  39. }
  40. void IDisposable.Dispose()
  41. {
  42. var task = taskCompletionSource.Task;
  43. //If onComplete is false then ISetCookieCallback.OnComplete was never called,
  44. //so we'll set the result to false.
  45. if (onComplete == false && task.IsCompleted == false)
  46. {
  47. taskCompletionSource.TrySetResult(false);
  48. }
  49. isDisposed = true;
  50. }
  51. }
  52. }