blob: d37d9727984c8a2790e37d9b47f656aa1a1031ab [file] [log] [blame]
Garrick Evanse649d0e2021-04-27 09:57:19 +09001// Copyright 2021 The Chromium OS 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 DNS_PROXY_CHROME_FEATURES_SERVICE_CLIENT_H_
6#define DNS_PROXY_CHROME_FEATURES_SERVICE_CLIENT_H_
7
8#include <memory>
9#include <string>
10
11#include <base/callback.h>
12#include <base/macros.h>
13#include <base/memory/weak_ptr.h>
14#include <base/optional.h>
15#include <dbus/bus.h>
16#include <dbus/object_proxy.h>
17
18namespace dns_proxy {
19
20// Helper to call chrome features dbus service.
21class ChromeFeaturesServiceClient {
22 public:
23 static std::unique_ptr<ChromeFeaturesServiceClient> New(
24 scoped_refptr<dbus::Bus> bus);
25
26 explicit ChromeFeaturesServiceClient(dbus::ObjectProxy* proxy);
27 ChromeFeaturesServiceClient(const ChromeFeaturesServiceClient&) = delete;
28 ChromeFeaturesServiceClient& operator=(const ChromeFeaturesServiceClient&) =
29 delete;
Garrick Evans77e9a132021-05-13 10:51:13 +090030 virtual ~ChromeFeaturesServiceClient() = default;
Garrick Evanse649d0e2021-04-27 09:57:19 +090031
32 // Async call to check whether given feature is enabled. |enabled| is
33 // base::nullopt if there is error calling the service. Otherwise,
34 // |enable.value()| indicates whether the feature is enabled or not.
35 using IsFeatureEnabledCallback =
36 base::OnceCallback<void(base::Optional<bool> enabled)>;
37
38 // Checks the Chrome Features service to determine whether or not the
39 // dns-proxy service is enabled.
Garrick Evans77e9a132021-05-13 10:51:13 +090040 virtual void IsDNSProxyEnabled(IsFeatureEnabledCallback callback);
Garrick Evanse649d0e2021-04-27 09:57:19 +090041
42 private:
43 void OnWaitForServiceAndCallMethod(const std::string& method_name,
44 IsFeatureEnabledCallback callback,
45 bool available);
46
47 void HandleCallResponse(IsFeatureEnabledCallback callback,
48 dbus::Response* response);
49
50 dbus::ObjectProxy* proxy_ = nullptr;
51
52 base::WeakPtrFactory<ChromeFeaturesServiceClient> weak_ptr_factory_{this};
53};
54
55} // namespace dns_proxy
56
57#endif // DNS_PROXY_CHROME_FEATURES_SERVICE_CLIENT_H_