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