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