lazy_event_dispatcher.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2017 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_LAZY_EVENT_DISPATCHER_H_
  5. #define EXTENSIONS_BROWSER_EVENTS_LAZY_EVENT_DISPATCHER_H_
  6. #include <set>
  7. #include <utility>
  8. #include "base/callback.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "extensions/browser/lazy_context_id.h"
  11. #include "extensions/browser/lazy_context_task_queue.h"
  12. #include "extensions/common/extension_id.h"
  13. namespace base {
  14. class DictionaryValue;
  15. }
  16. namespace content {
  17. class BrowserContext;
  18. }
  19. namespace extensions {
  20. class Extension;
  21. struct Event;
  22. // Helper class for EventRouter to dispatch lazy events to lazy contexts.
  23. //
  24. // Manages waking up lazy contexts if they are stopped.
  25. class LazyEventDispatcher {
  26. public:
  27. using DispatchFunction = base::RepeatingCallback<void(
  28. std::unique_ptr<Event>,
  29. std::unique_ptr<LazyContextTaskQueue::ContextInfo>)>;
  30. LazyEventDispatcher(content::BrowserContext* browser_context,
  31. DispatchFunction dispatch_function);
  32. LazyEventDispatcher(const LazyEventDispatcher&) = delete;
  33. LazyEventDispatcher& operator=(const LazyEventDispatcher&) = delete;
  34. ~LazyEventDispatcher();
  35. // Dispatches the lazy |event| to |dispatch_context|.
  36. //
  37. // If [dispatch_context| is for an event page, it ensures all of the pages
  38. // interested in the event are loaded and queues the event if any pages are
  39. // not ready yet.
  40. //
  41. // If [dispatch_context| is for a service worker, it ensures the worker is
  42. // started before dispatching the event.
  43. void Dispatch(const Event& event,
  44. const LazyContextId& dispatch_context,
  45. const base::DictionaryValue* listener_filter);
  46. // Returns whether or not an event listener identical for |dispatch_context|
  47. // is already queued for dispatch.
  48. bool HasAlreadyDispatched(const LazyContextId& dispatch_context) const;
  49. private:
  50. // Possibly loads given extension's background page or extension Service
  51. // Worker in preparation to dispatch an event. Returns true if the event was
  52. // queued for subsequent dispatch, false otherwise.
  53. bool QueueEventDispatch(const Event& event,
  54. const LazyContextId& dispatch_context,
  55. const Extension* extension,
  56. const base::DictionaryValue* listener_filter);
  57. void RecordAlreadyDispatched(const LazyContextId& dispatch_context);
  58. content::BrowserContext* GetIncognitoContext(const Extension* extension);
  59. const raw_ptr<content::BrowserContext> browser_context_;
  60. DispatchFunction dispatch_function_;
  61. std::set<LazyContextId> dispatched_ids_;
  62. };
  63. } // namespace extensions
  64. #endif // EXTENSIONS_BROWSER_EVENTS_LAZY_EVENT_DISPATCHER_H_