xref: /aosp_15_r20/external/openscreen/osp/impl/service_publisher_impl.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
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 OSP_IMPL_SERVICE_PUBLISHER_IMPL_H_
6 #define OSP_IMPL_SERVICE_PUBLISHER_IMPL_H_
7 
8 #include <memory>
9 
10 #include "osp/impl/with_destruction_callback.h"
11 #include "osp/public/service_publisher.h"
12 #include "platform/base/macros.h"
13 
14 namespace openscreen {
15 namespace osp {
16 
17 class ServicePublisherImpl final : public ServicePublisher,
18                                    public WithDestructionCallback {
19  public:
20   class Delegate {
21    public:
22     Delegate();
23     virtual ~Delegate();
24 
25     void SetPublisherImpl(ServicePublisherImpl* publisher);
26 
27     virtual void StartPublisher(const ServicePublisher::Config& config) = 0;
28     virtual void StartAndSuspendPublisher(
29         const ServicePublisher::Config& config) = 0;
30     virtual void StopPublisher() = 0;
31     virtual void SuspendPublisher() = 0;
32     virtual void ResumePublisher(const ServicePublisher::Config& config) = 0;
33 
34    protected:
SetState(State state)35     void SetState(State state) { publisher_->SetState(state); }
36 
37     ServicePublisherImpl* publisher_ = nullptr;
38   };
39 
40   // |observer| is optional.  If it is provided, it will receive appropriate
41   // notifications about this ServicePublisher.  |delegate| is required and
42   // is used to implement state transitions.
43   ServicePublisherImpl(Observer* observer, std::unique_ptr<Delegate> delegate);
44   ~ServicePublisherImpl() override;
45 
46   // ServicePublisher overrides.
47   bool Start() override;
48   bool StartAndSuspend() override;
49   bool Stop() override;
50   bool Suspend() override;
51   bool Resume() override;
52 
53  private:
54   // Called by |delegate_| to transition the state machine (except kStarting and
55   // kStopping which are done automatically).
56   void SetState(State state);
57 
58   // Notifies |observer_| if the transition to |state_| is one that is watched
59   // by the observer interface.
60   void MaybeNotifyObserver();
61 
62   std::unique_ptr<Delegate> delegate_;
63 
64   OSP_DISALLOW_COPY_AND_ASSIGN(ServicePublisherImpl);
65 };
66 
67 }  // namespace osp
68 }  // namespace openscreen
69 
70 #endif  // OSP_IMPL_SERVICE_PUBLISHER_IMPL_H_
71