1 // Copyright 2019 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 REMOTING_IOS_APP_NOTIFICATION_PRESENTER_H_
6 #define REMOTING_IOS_APP_NOTIFICATION_PRESENTER_H_
7 
8 #import <Foundation/Foundation.h>
9 
10 #include <memory>
11 
12 #include "base/macros.h"
13 #include "base/no_destructor.h"
14 #include "base/sequence_checker.h"
15 #include "base/threading/sequence_bound.h"
16 #include "base/timer/timer.h"
17 #include "remoting/client/notification/notification_client.h"
18 #include "remoting/client/notification/notification_message.h"
19 
20 namespace remoting {
21 
22 // Singleton class to present a notification message on the app. Message will
23 // be presented whenever the signed-in user is changed and a matching message
24 // is found, while no message has been previously presented.
25 class NotificationPresenter final {
26  public:
27   static NotificationPresenter* GetInstance();
28 
29   void Start();
30 
31  private:
32   friend class base::NoDestructor<NotificationPresenter>;
33 
34   enum class State {
35     NOT_FETCHED,
36     FETCHING,
37     FETCHED,
38   };
39 
40   NotificationPresenter();
41   ~NotificationPresenter() = delete;
42 
43   void FetchNotificationIfNecessary();
44   void OnNotificationFetched(base::Optional<NotificationMessage> notification);
45 
46   NotificationClient notification_client_;
47 
48   base::OneShotTimer fetch_notification_timer_;
49 
50   // nil if the presenter is not started.
51   id<NSObject> user_update_observer_ = nil;
52 
53   State state_ = State::NOT_FETCHED;
54 
55   SEQUENCE_CHECKER(sequence_checker_);
56 
57   DISALLOW_COPY_AND_ASSIGN(NotificationPresenter);
58 };
59 
60 }  // namespace remoting
61 
62 #endif  // REMOTING_IOS_APP_NOTIFICATION_PRESENTER_H_
63