event_ack_data.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef EXTENSIONS_BROWSER_EVENTS_EVENT_ACK_DATA_H_
  5. #define EXTENSIONS_BROWSER_EVENTS_EVENT_ACK_DATA_H_
  6. #include "base/callback_forward.h"
  7. #include "base/memory/weak_ptr.h"
  8. namespace content {
  9. class ServiceWorkerContext;
  10. }
  11. namespace extensions {
  12. // Manages inflight events for extension Service Worker.
  13. class EventAckData {
  14. public:
  15. EventAckData();
  16. EventAckData(const EventAckData&) = delete;
  17. EventAckData& operator=(const EventAckData&) = delete;
  18. ~EventAckData();
  19. // Records the fact that an event with |event_id| was dispatched to an
  20. // extension Service Worker and we expect an ack for the event from the worker
  21. // later on.
  22. void IncrementInflightEvent(content::ServiceWorkerContext* context,
  23. int render_process_id,
  24. int64_t version_id,
  25. int event_id);
  26. // Clears the record of our knowledge of an inflight event with |event_id|.
  27. //
  28. // On failure, |failure_callback| is called synchronously or asynchronously.
  29. void DecrementInflightEvent(content::ServiceWorkerContext* context,
  30. int render_process_id,
  31. int64_t version_id,
  32. int event_id,
  33. bool worker_stopped,
  34. base::OnceClosure failure_callback);
  35. private:
  36. class CoreThreadEventInfo;
  37. static void StartExternalRequestOnCoreThread(
  38. content::ServiceWorkerContext* context,
  39. int render_process_id,
  40. int64_t version_id,
  41. int event_id,
  42. scoped_refptr<EventAckData::CoreThreadEventInfo> unacked_events);
  43. static void FinishExternalRequestOnCoreThread(
  44. content::ServiceWorkerContext* context,
  45. int render_process_id,
  46. int64_t version_id,
  47. int event_id,
  48. bool worker_stopped,
  49. scoped_refptr<CoreThreadEventInfo> unacked_events,
  50. base::OnceClosure failure_callback);
  51. // Contains map of unacked event information keyed by event id.
  52. scoped_refptr<CoreThreadEventInfo> unacked_events_;
  53. base::WeakPtrFactory<EventAckData> weak_factory_{this};
  54. };
  55. } // namespace extensions
  56. #endif // EXTENSIONS_BROWSER_EVENTS_EVENT_ACK_DATA_H_