summaryrefslogtreecommitdiff
path: root/chromium/content/browser/sms/sms_provider.h
blob: 63c69a83f7902250323979269326d55659115e18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_SMS_SMS_PROVIDER_H_
#define CONTENT_BROWSER_SMS_SMS_PROVIDER_H_

#include <memory>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "content/browser/sms/sms_parser.h"
#include "content/common/content_export.h"

namespace url {
class Origin;
}

namespace content {

class RenderFrameHost;

// This class wraps the platform-specific functions and allows tests to
// inject custom providers.
class CONTENT_EXPORT SmsProvider {
 public:
  class Observer : public base::CheckedObserver {
   public:
    // Receive an |one_time_code| from an origin. Return true if the message is
    // handled, which stops its propagation to other observers.
    virtual bool OnReceive(const url::Origin&,
                           const std::string& one_time_code) = 0;
  };

  SmsProvider();
  virtual ~SmsProvider();

  // Listen to the next incoming SMS and notify observers (exactly once) when
  // it is received or (exclusively) when it timeouts.
  virtual void Retrieve() = 0;

  static std::unique_ptr<SmsProvider> Create(
      base::WeakPtr<RenderFrameHost> rfh);

  void AddObserver(Observer*);
  void RemoveObserver(const Observer*);
  void NotifyReceive(const url::Origin&, const std::string& one_time_code);
  void NotifyReceive(const std::string& sms);
  bool HasObservers();

 private:
  base::ObserverList<Observer> observers_;
  DISALLOW_COPY_AND_ASSIGN(SmsProvider);
};

}  // namespace content

#endif  // CONTENT_BROWSER_SMS_SMS_PROVIDER_H_