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

content / browser / renderer_host / pepper / pepper_proxy_lookup_helper.h [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_PROXY_LOOKUP_HELPER_H_
#define CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_PROXY_LOOKUP_HELPER_H_

#include <memory>
#include <optional>

#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/network/public/mojom/proxy_lookup_client.mojom.h"

class GURL;

namespace net {
class ProxyInfo;
}

namespace content {

// Class that runs a single proxy resolution on the UI thread. Lives on the
// thread its created on, and uses a helper to run tasks off-thread. Can be
// destroyed at any time.
class CONTENT_EXPORT PepperProxyLookupHelper {
 public:
  // Callback to call LookUpProxyForURL. Called on the UI thread. Needed for
  // testing. Returns false if unable to make the call, for whatever reason.
  using LookUpProxyForURLCallback = base::OnceCallback<bool(
      const GURL& url,
      mojo::PendingRemote<network::mojom::ProxyLookupClient>
          proxy_lookup_client)>;

  // Callback to invoke when complete. Invoked on thread the
  // PepperProxyLookupHelper was created on.
  using LookUpCompleteCallback =
      base::OnceCallback<void(std::optional<net::ProxyInfo> proxy_info)>;

  PepperProxyLookupHelper();

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

  ~PepperProxyLookupHelper();

  // Starts a lookup for |url| on the UI thread. Invokes
  // |look_up_proxy_for_url_callback| on the UI thread to start the lookup, and
  // calls |look_up_complete_callback| on the thread Start() was called on when
  // complete. May only be invoked once.
  void Start(const GURL& url,
             LookUpProxyForURLCallback look_up_proxy_for_url_callback,
             LookUpCompleteCallback look_up_complete_callback);

 private:
  class UIThreadHelper;

  void OnProxyLookupComplete(std::optional<net::ProxyInfo> proxy_info);

  LookUpCompleteCallback look_up_complete_callback_;
  std::unique_ptr<UIThreadHelper> ui_thread_helper_;

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<PepperProxyLookupHelper> weak_factory_{this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_PROXY_LOOKUP_HELPER_H_