File: sms_client.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (186 lines) | stat: -rw-r--r-- 6,379 bytes parent folder | download | duplicates (7)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/dbus/shill/sms_client.h"

#include <map>
#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/shill/fake_sms_client.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "dbus/property.h"
#include "dbus/values_util.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace ash {

namespace {

// See "enum MMSMSState" definition in ModemManager.
constexpr uint32_t kSMSStateReceived = 3;  // MM_SMS_STATE_RECEIVED

SMSClient* g_instance = nullptr;

class SMSReceiveHandler {
 public:
  SMSReceiveHandler(dbus::ObjectProxy* object_proxy,
                    SMSClient::GetAllCallback callback)
      : callback_(std::move(callback)) {
    property_set_ = std::make_unique<dbus::PropertySet>(
        object_proxy, modemmanager::kModemManager1SmsInterface,
        base::BindRepeating(&SMSReceiveHandler::OnPropertyChanged,
                            weak_ptr_factory_.GetWeakPtr()));
    property_set_->RegisterProperty(SMSClient::kSMSPropertyState, &state_);
    property_set_->ConnectSignals();
    property_set_->Get(&state_, dbus::PropertySet::GetCallback());
  }

  SMSReceiveHandler(const SMSReceiveHandler&) = delete;
  SMSReceiveHandler& operator=(const SMSReceiveHandler&) = delete;

  ~SMSReceiveHandler() = default;

 private:
  void OnPropertyChanged(const std::string& property_name) {
    // Initially, we monitor only the "State" property of the SMS object. When
    // the "State" property is updated as a result of the initial
    // PropertySet::Get() call or subsequent D-Bus signals on property changes,
    // we check if the "State" property indicates that the SMS is fully
    // received. If the SMS is fully received, we fetch all properties via
    // PropertySet::GetAll(). When the properties of interest are updated,
    // |callback_| is invoked to notify the observer of the received SMS.
    if (callback_.is_null())
      return;

    if (number_.is_valid() && text_.is_valid() && timestamp_.is_valid()) {
      base::Value::Dict sms;
      sms.Set(SMSClient::kSMSPropertyNumber, number_.value());
      sms.Set(SMSClient::kSMSPropertyText, text_.value());
      sms.Set(SMSClient::kSMSPropertyTimestamp, timestamp_.value());
      // Move |callback_| to the task to ensure that |callback_| is only called
      // once. Since |callback_| may destruct this object, schedule it to the
      // task runner to run after this method returns.
      base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
          FROM_HERE, base::BindOnce(std::move(callback_), std::move(sms)));
      return;
    }

    if (state_.is_valid() && state_.value() == kSMSStateReceived &&
        !sms_received_) {
      sms_received_ = true;
      property_set_->RegisterProperty(SMSClient::kSMSPropertyNumber, &number_);
      property_set_->RegisterProperty(SMSClient::kSMSPropertyText, &text_);
      property_set_->RegisterProperty(SMSClient::kSMSPropertyTimestamp,
                                      &timestamp_);
      property_set_->GetAll();
    }
  }

  SMSClient::GetAllCallback callback_;
  bool sms_received_ = false;

  // Property Set must be first to be destroyed after all properties.
  std::unique_ptr<dbus::PropertySet> property_set_;
  dbus::Property<uint32_t> state_;
  dbus::Property<std::string> number_;
  dbus::Property<std::string> text_;
  dbus::Property<std::string> timestamp_;
  base::WeakPtrFactory<SMSReceiveHandler> weak_ptr_factory_{this};
};

// SMSClient is used to communicate with the
// org.freedesktop.ModemManager1.SMS service.  All methods should be
// called from the origin thread (UI thread) which initializes the
// DBusThreadManager instance.
class SMSClientImpl : public SMSClient {
 public:
  explicit SMSClientImpl(dbus::Bus* bus) : bus_(bus) {}

  SMSClientImpl(const SMSClientImpl&) = delete;
  SMSClientImpl& operator=(const SMSClientImpl&) = delete;

  ~SMSClientImpl() override = default;

  // Calls GetAll method.  |callback| is called after the method call succeeds.
  void GetAll(const std::string& service_name,
              const dbus::ObjectPath& object_path,
              GetAllCallback callback) override {
    dbus::ObjectProxy* proxy = bus_->GetObjectProxy(service_name, object_path);
    sms_receive_handlers_[object_path] = std::make_unique<SMSReceiveHandler>(
        proxy, base::BindOnce(&SMSClientImpl::OnSMSReceived,
                              weak_ptr_factory_.GetWeakPtr(), object_path,
                              std::move(callback)));
  }

 private:
  void OnSMSReceived(const dbus::ObjectPath& object_path,
                     GetAllCallback callback,
                     const base::Value::Dict& sms) {
    sms_receive_handlers_.erase(object_path);
    std::move(callback).Run(sms);
  }

  raw_ptr<dbus::Bus> bus_;

  std::map<dbus::ObjectPath, std::unique_ptr<SMSReceiveHandler>>
      sms_receive_handlers_;

  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<SMSClientImpl> weak_ptr_factory_{this};
};

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// SMSClient

// Properties of org.freedesktop.ModemManager1.Sms object:
const char SMSClient::kSMSPropertyState[] = "State";
const char SMSClient::kSMSPropertyNumber[] = "Number";
const char SMSClient::kSMSPropertyText[] = "Text";
const char SMSClient::kSMSPropertyTimestamp[] = "Timestamp";

SMSClient::SMSClient() {
  DCHECK(!g_instance);
  g_instance = this;
}

SMSClient::~SMSClient() {
  DCHECK_EQ(this, g_instance);
  g_instance = nullptr;
}

// static
void SMSClient::Initialize(dbus::Bus* bus) {
  DCHECK(bus);
  new SMSClientImpl(bus);
}

// static
void SMSClient::InitializeFake() {
  new FakeSMSClient();
}

// static
void SMSClient::Shutdown() {
  DCHECK(g_instance);
  delete g_instance;
}

// static
SMSClient* SMSClient::Get() {
  return g_instance;
}

}  // namespace ash