proxy_config_service_mojo.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include "services/network/proxy_config_service_mojo.h"
  5. #include <utility>
  6. #include "base/observer_list.h"
  7. namespace network {
  8. ProxyConfigServiceMojo::ProxyConfigServiceMojo(
  9. mojo::PendingReceiver<mojom::ProxyConfigClient>
  10. proxy_config_client_receiver,
  11. absl::optional<net::ProxyConfigWithAnnotation> initial_proxy_config,
  12. mojo::PendingRemote<mojom::ProxyConfigPollerClient> proxy_poller_client) {
  13. DCHECK(initial_proxy_config || proxy_config_client_receiver.is_valid());
  14. if (initial_proxy_config)
  15. OnProxyConfigUpdated(*initial_proxy_config);
  16. if (proxy_config_client_receiver.is_valid()) {
  17. receiver_.Bind(std::move(proxy_config_client_receiver));
  18. // Only use the |proxy_poller_client| if there's a
  19. // |proxy_config_client_receiver|.
  20. if (!proxy_poller_client) {
  21. // NullRemote() could be passed in unit tests. In that case, it can't be
  22. // bound.
  23. return;
  24. }
  25. proxy_poller_client_.Bind(std::move(proxy_poller_client));
  26. }
  27. }
  28. ProxyConfigServiceMojo::~ProxyConfigServiceMojo() {}
  29. void ProxyConfigServiceMojo::OnProxyConfigUpdated(
  30. const net::ProxyConfigWithAnnotation& proxy_config) {
  31. // Do nothing if the proxy configuration is unchanged.
  32. if (!config_pending_ && config_.value().Equals(proxy_config.value()))
  33. return;
  34. config_pending_ = false;
  35. config_ = proxy_config;
  36. for (auto& observer : observers_)
  37. observer.OnProxyConfigChanged(config_, CONFIG_VALID);
  38. }
  39. void ProxyConfigServiceMojo::FlushProxyConfig(
  40. FlushProxyConfigCallback callback) {
  41. std::move(callback).Run();
  42. }
  43. void ProxyConfigServiceMojo::AddObserver(Observer* observer) {
  44. observers_.AddObserver(observer);
  45. }
  46. void ProxyConfigServiceMojo::RemoveObserver(Observer* observer) {
  47. observers_.RemoveObserver(observer);
  48. }
  49. net::ProxyConfigService::ConfigAvailability
  50. ProxyConfigServiceMojo::GetLatestProxyConfig(
  51. net::ProxyConfigWithAnnotation* config) {
  52. if (config_pending_) {
  53. *config = net::ProxyConfigWithAnnotation();
  54. return CONFIG_PENDING;
  55. }
  56. *config = config_;
  57. return CONFIG_VALID;
  58. }
  59. void ProxyConfigServiceMojo::OnLazyPoll() {
  60. // TODO(mmenke): These should either be rate limited, or the other process
  61. // should use another signal of activity.
  62. if (proxy_poller_client_)
  63. proxy_poller_client_->OnLazyProxyConfigPoll();
  64. }
  65. } // namespace network