badge_manager.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 ELECTRON_SHELL_BROWSER_BADGING_BADGE_MANAGER_H_
  5. #define ELECTRON_SHELL_BROWSER_BADGING_BADGE_MANAGER_H_
  6. #include <memory>
  7. #include <optional>
  8. #include <string>
  9. #include "components/keyed_service/core/keyed_service.h"
  10. #include "mojo/public/cpp/bindings/receiver_set.h"
  11. #include "third_party/blink/public/mojom/badging/badging.mojom.h"
  12. #include "url/gurl.h"
  13. namespace content {
  14. class RenderFrameHost;
  15. class RenderProcessHost;
  16. } // namespace content
  17. namespace badging {
  18. // The maximum value of badge contents before saturation occurs.
  19. constexpr int kMaxBadgeContent = 99;
  20. // Maintains a record of badge contents and dispatches badge changes to a
  21. // delegate.
  22. class BadgeManager : public KeyedService, public blink::mojom::BadgeService {
  23. public:
  24. BadgeManager();
  25. ~BadgeManager() override;
  26. // disable copy
  27. BadgeManager(const BadgeManager&) = delete;
  28. BadgeManager& operator=(const BadgeManager&) = delete;
  29. static void BindFrameReceiver(
  30. content::RenderFrameHost* frame,
  31. mojo::PendingReceiver<blink::mojom::BadgeService> receiver);
  32. static void BindServiceWorkerReceiver(
  33. content::RenderProcessHost* service_worker_process_host,
  34. const GURL& service_worker_scope,
  35. mojo::PendingReceiver<blink::mojom::BadgeService> receiver);
  36. // Determines the text to put on the badge based on some badge_content.
  37. static std::string GetBadgeString(std::optional<int> badge_content);
  38. private:
  39. // The BindingContext of a mojo request. Allows mojo calls to be tied back
  40. // to the execution context they belong to without trusting the renderer for
  41. // that information. This is an abstract base class that different types of
  42. // execution contexts derive.
  43. class BindingContext {
  44. public:
  45. virtual ~BindingContext() = default;
  46. };
  47. // The BindingContext for Window execution contexts.
  48. class FrameBindingContext final : public BindingContext {
  49. public:
  50. FrameBindingContext(int process_id, int frame_id)
  51. : process_id_(process_id), frame_id_(frame_id) {}
  52. ~FrameBindingContext() override = default;
  53. int GetProcessId() { return process_id_; }
  54. int GetFrameId() { return frame_id_; }
  55. private:
  56. int process_id_;
  57. int frame_id_;
  58. };
  59. // The BindingContext for ServiceWorkerGlobalScope execution contexts.
  60. class ServiceWorkerBindingContext final : public BindingContext {
  61. public:
  62. ServiceWorkerBindingContext(int process_id, const GURL& scope)
  63. : process_id_(process_id), scope_(scope) {}
  64. ~ServiceWorkerBindingContext() override = default;
  65. int GetProcessId() { return process_id_; }
  66. GURL GetScope() { return scope_; }
  67. private:
  68. int process_id_;
  69. GURL scope_;
  70. };
  71. // blink::mojom::BadgeService:
  72. // Note: These are private to stop them being called outside of mojo as they
  73. // require a mojo binding context.
  74. void SetBadge(blink::mojom::BadgeValuePtr value) override;
  75. void ClearBadge() override;
  76. // All the mojo receivers for the BadgeManager. Keeps track of the
  77. // render_frame the binding is associated with, so as to not have to rely
  78. // on the renderer passing it in.
  79. mojo::ReceiverSet<blink::mojom::BadgeService, std::unique_ptr<BindingContext>>
  80. receivers_;
  81. // Delegate which handles actual setting and clearing of the badge.
  82. // Note: This is currently only set on Windows and MacOS.
  83. // std::unique_ptr<BadgeManagerDelegate> delegate_;
  84. };
  85. } // namespace badging
  86. #endif // ELECTRON_SHELL_BROWSER_BADGING_BADGE_MANAGER_H_