1 // Copyright 2017 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 SERVICES_NETWORK_PROXY_CONFIG_SERVICE_MOJO_H_
6 #define SERVICES_NETWORK_PROXY_CONFIG_SERVICE_MOJO_H_
7 
8 #include "base/component_export.h"
9 #include "base/macros.h"
10 #include "base/observer_list.h"
11 #include "base/optional.h"
12 #include "mojo/public/cpp/bindings/pending_receiver.h"
13 #include "mojo/public/cpp/bindings/pending_remote.h"
14 #include "mojo/public/cpp/bindings/receiver.h"
15 #include "mojo/public/cpp/bindings/remote.h"
16 #include "net/proxy_resolution/proxy_config.h"
17 #include "net/proxy_resolution/proxy_config_service.h"
18 #include "net/proxy_resolution/proxy_config_with_annotation.h"
19 #include "services/network/public/mojom/proxy_config.mojom.h"
20 #include "services/network/public/mojom/proxy_config_with_annotation.mojom.h"
21 
22 namespace net {
23 class ProxyConfigWithAnnotation;
24 }
25 
26 namespace network {
27 
28 // ProxyConfigService that gets its proxy configuration over a Mojo pipe.
COMPONENT_EXPORT(NETWORK_SERVICE)29 class COMPONENT_EXPORT(NETWORK_SERVICE) ProxyConfigServiceMojo
30     : public net::ProxyConfigService,
31       public mojom::ProxyConfigClient {
32  public:
33   // |proxy_config_client_receiver| is the Mojo pipe over which new
34   // configurations are received. |initial_proxy_config| is the initial proxy
35   // configuration. If mojo::NullReceiver(), waits for the initial configuration
36   // over |proxy_config_client_receiver|. Either |proxy_config_client_receiver|
37   // or |initial_proxy_config| must be non-null. If
38   // |proxy_config_client_receiver| and |proxy_poller_client| are non-null,
39   // calls into |proxy_poller_client| whenever OnLazyPoll() is invoked.
40   explicit ProxyConfigServiceMojo(
41       mojo::PendingReceiver<mojom::ProxyConfigClient>
42           proxy_config_client_receiver,
43       base::Optional<net::ProxyConfigWithAnnotation> initial_proxy_config,
44       mojo::PendingRemote<mojom::ProxyConfigPollerClient> proxy_poller_client);
45   ~ProxyConfigServiceMojo() override;
46 
47   // net::ProxyConfigService implementation:
48   void AddObserver(Observer* observer) override;
49   void RemoveObserver(Observer* observer) override;
50   ConfigAvailability GetLatestProxyConfig(
51       net::ProxyConfigWithAnnotation* config) override;
52   void OnLazyPoll() override;
53 
54  private:
55   // mojom::ProxyConfigClient implementation:
56   void OnProxyConfigUpdated(
57       const net::ProxyConfigWithAnnotation& proxy_config) override;
58   void FlushProxyConfig(FlushProxyConfigCallback callback) override;
59 
60   mojo::Remote<mojom::ProxyConfigPollerClient> proxy_poller_client_;
61 
62   net::ProxyConfigWithAnnotation config_;
63   bool config_pending_ = true;
64 
65   mojo::Receiver<mojom::ProxyConfigClient> receiver_{this};
66 
67   base::ObserverList<Observer>::Unchecked observers_;
68 
69   DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceMojo);
70 };
71 
72 }  // namespace network
73 
74 #endif  // SERVICES_NETWORK_PROXY_CONFIG_SERVICE_MOJO_H_
75